|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for additional information regarding |
| 4 | + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the |
| 5 | + * "License"); you may not use this file except in compliance with the License. You may obtain a |
| 6 | + * copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
| 14 | + */ |
| 15 | +package org.apache.geode.tools.pulse.controllers; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.mockito.ArgumentMatchers.anyString; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | +import static org.mockito.quality.Strictness.LENIENT; |
| 21 | +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
| 22 | +import static org.springframework.http.MediaType.parseMediaType; |
| 23 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 24 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 27 | + |
| 28 | +import java.security.Principal; |
| 29 | + |
| 30 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Rule; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.experimental.categories.Category; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.junit.MockitoJUnit; |
| 39 | +import org.mockito.junit.MockitoRule; |
| 40 | +import org.springframework.beans.factory.annotation.Autowired; |
| 41 | +import org.springframework.http.MediaType; |
| 42 | +import org.springframework.test.context.ActiveProfiles; |
| 43 | +import org.springframework.test.context.ContextConfiguration; |
| 44 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 45 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 46 | +import org.springframework.test.web.servlet.MockMvc; |
| 47 | +import org.springframework.test.web.servlet.MvcResult; |
| 48 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 49 | +import org.springframework.web.context.WebApplicationContext; |
| 50 | + |
| 51 | +import org.apache.geode.test.junit.categories.PulseTest; |
| 52 | +import org.apache.geode.tools.pulse.internal.data.Cluster; |
| 53 | +import org.apache.geode.tools.pulse.internal.data.Repository; |
| 54 | + |
| 55 | +/** |
| 56 | + * Covers the region-detail error message end to end, from the {@code /pulseUpdate} request the |
| 57 | + * Pulse UI posts through to the JSON it receives back. |
| 58 | + */ |
| 59 | +@Category({PulseTest.class}) |
| 60 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 61 | +@WebAppConfiguration |
| 62 | +@ContextConfiguration("classpath*:WEB-INF/pulse-servlet.xml") |
| 63 | +@ActiveProfiles({"pulse.controller.test"}) |
| 64 | +public class RegionDetailErrorMessageIntegrationTest { |
| 65 | + |
| 66 | + private static final String PATH_WITH_SPECIAL_CHARACTERS = "/orders<2026>&archive"; |
| 67 | + private static final String ENCODED_MESSAGE = |
| 68 | + "Region [/orders<2026>&archive] is not available"; |
| 69 | + |
| 70 | + private static final MediaType JSON_MEDIA_TYPE = parseMediaType(APPLICATION_JSON_VALUE); |
| 71 | + private static final Principal PRINCIPAL = () -> "test-user"; |
| 72 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 73 | + |
| 74 | + @Rule |
| 75 | + public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(LENIENT); |
| 76 | + |
| 77 | + @Autowired |
| 78 | + private WebApplicationContext wac; |
| 79 | + |
| 80 | + @Autowired |
| 81 | + private Repository repository; |
| 82 | + |
| 83 | + @Mock |
| 84 | + Cluster cluster; |
| 85 | + |
| 86 | + private MockMvc mockMvc; |
| 87 | + |
| 88 | + @Before |
| 89 | + public void setup() { |
| 90 | + when(repository.getCluster()).thenReturn(cluster); |
| 91 | + when(cluster.getServerName()).thenReturn("mock-cluster"); |
| 92 | + // The requested path resolves to no region, so the services take the error branch. |
| 93 | + when(cluster.getClusterRegion(anyString())).thenReturn(null); |
| 94 | + |
| 95 | + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void pulseUpdateEncodesSpecialCharactersForClusterSelectedRegion() throws Exception { |
| 100 | + MvcResult result = mockMvc |
| 101 | + .perform(post("/pulseUpdate") |
| 102 | + .with(csrf()) |
| 103 | + .param("pulseData", pulseData("ClusterSelectedRegion", PATH_WITH_SPECIAL_CHARACTERS)) |
| 104 | + .principal(PRINCIPAL) |
| 105 | + .accept(JSON_MEDIA_TYPE)) |
| 106 | + .andExpect(status().isOk()) |
| 107 | + .andExpect(jsonPath("$.ClusterSelectedRegion.selectedRegion.errorOnRegion") |
| 108 | + .value(ENCODED_MESSAGE)) |
| 109 | + .andReturn(); |
| 110 | + |
| 111 | + assertThat(result.getResponse().getContentAsString()) |
| 112 | + .contains("/orders<2026>&archive"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void pulseUpdateEncodesSpecialCharactersForClusterSelectedRegionsMember() |
| 117 | + throws Exception { |
| 118 | + MvcResult result = mockMvc |
| 119 | + .perform(post("/pulseUpdate") |
| 120 | + .with(csrf()) |
| 121 | + .param("pulseData", |
| 122 | + pulseData("ClusterSelectedRegionsMember", PATH_WITH_SPECIAL_CHARACTERS)) |
| 123 | + .principal(PRINCIPAL) |
| 124 | + .accept(JSON_MEDIA_TYPE)) |
| 125 | + .andExpect(status().isOk()) |
| 126 | + .andExpect(jsonPath("$.ClusterSelectedRegionsMember.selectedRegionsMembers.errorOnRegion") |
| 127 | + .value(ENCODED_MESSAGE)) |
| 128 | + .andReturn(); |
| 129 | + |
| 130 | + assertThat(result.getResponse().getContentAsString()) |
| 131 | + .contains("/orders<2026>&archive"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void pulseUpdateLeavesOrdinaryRegionPathUnchanged() throws Exception { |
| 136 | + mockMvc |
| 137 | + .perform(post("/pulseUpdate") |
| 138 | + .with(csrf()) |
| 139 | + .param("pulseData", pulseData("ClusterSelectedRegion", "/mock-region")) |
| 140 | + .principal(PRINCIPAL) |
| 141 | + .accept(JSON_MEDIA_TYPE)) |
| 142 | + .andExpect(status().isOk()) |
| 143 | + .andExpect(jsonPath("$.ClusterSelectedRegion.selectedRegion.errorOnRegion") |
| 144 | + .value("Region [/mock-region] is not available")); |
| 145 | + } |
| 146 | + |
| 147 | + /** Builds the {@code pulseData} body the Pulse frontend posts for the region-detail page. */ |
| 148 | + private static String pulseData(String service, String regionFullPath) { |
| 149 | + ObjectNode parameters = MAPPER.createObjectNode(); |
| 150 | + parameters.put("regionFullPath", regionFullPath); |
| 151 | + ObjectNode root = MAPPER.createObjectNode(); |
| 152 | + root.set(service, parameters); |
| 153 | + return root.toString(); |
| 154 | + } |
| 155 | +} |
0 commit comments