geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinmeil...@apache.org
Subject [geode] branch develop updated: GEODE-6574: create a LocatorWebContext to ease controller testing using Spring mock context (#3393)
Date Wed, 03 Apr 2019 21:59:29 GMT
This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 055978d  GEODE-6574: create a LocatorWebContext to ease controller testing using
Spring mock context (#3393)
055978d is described below

commit 055978dcad2cad1416bb5baf426d8bff5cb74e24
Author: jinmeiliao <jiliao@pivotal.io>
AuthorDate: Wed Apr 3 14:59:09 2019 -0700

    GEODE-6574: create a LocatorWebContext to ease controller testing using Spring mock context
(#3393)
---
 .../geode/test/dunit/rules/ClusterStartupRule.java |  3 -
 geode-web-management/build.gradle                  |  2 +
 .../internal/rest/BaseLocatorContextLoader.java    |  6 --
 .../internal/rest/LocatorWebContext.java           | 68 ++++++++++++++++++++++
 .../ClientClusterManagementServiceDUnitTest.java   | 26 +++++----
 .../client/MemberManagementServiceDUnitTest.java   | 27 +++------
 .../rest/RegionManagementIntegrationTest.java      | 18 ++----
 .../RegionManagementSecurityIntegrationTest.java   | 26 +++------
 8 files changed, 103 insertions(+), 73 deletions(-)

diff --git a/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/ClusterStartupRule.java
b/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/ClusterStartupRule.java
index 45ea912..0196e8c 100644
--- a/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/ClusterStartupRule.java
+++ b/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/ClusterStartupRule.java
@@ -192,9 +192,6 @@ public class ClusterStartupRule implements SerializableTestRule {
     DUnitLauncher.closeAndCheckForSuspects();
   }
 
-  public boolean isSkipLocalDistributedSystemCleanup() {
-    return skipLocalDistributedSystemCleanup;
-  }
 
   /**
    * In some weird situations you may not want to do local DS cleanup as that lifecyle is
deferred
diff --git a/geode-web-management/build.gradle b/geode-web-management/build.gradle
index 959ccf8..c0a1722 100644
--- a/geode-web-management/build.gradle
+++ b/geode-web-management/build.gradle
@@ -98,6 +98,8 @@ dependencies {
   }
   commonTestCompile('org.springframework:spring-test')
   commonTestCompile('org.springframework:spring-webmvc')
+  commonTestCompile('org.springframework.security:spring-security-test')
+  commonTestCompile(project(':geode-web-management'))
 
   testCompile(project(':geode-junit')) {
     exclude module: 'geode-core'
diff --git a/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/BaseLocatorContextLoader.java
b/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/BaseLocatorContextLoader.java
index fc27eec..16bfe8d 100644
--- a/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/BaseLocatorContextLoader.java
+++ b/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/BaseLocatorContextLoader.java
@@ -17,7 +17,6 @@ package org.apache.geode.management.internal.rest;
 
 import org.springframework.test.context.web.GenericXmlWebContextLoader;
 import org.springframework.test.context.web.WebMergedContextConfiguration;
-import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.GenericWebApplicationContext;
 
 import org.apache.geode.internal.cache.HttpService;
@@ -44,11 +43,6 @@ public abstract class BaseLocatorContextLoader extends GenericXmlWebContextLoade
         getLocator().getCache().getSecurityService());
     context.getServletContext().setAttribute(HttpService.CLUSTER_MANAGEMENT_SERVICE_CONTEXT_PARAM,
         getLocator().getLocator().getClusterManagementService());
-
     context.getServletContext().setAttribute("locator", getLocator());
   }
-
-  public static LocatorStarterRule getLocatorFromContext(WebApplicationContext context) {
-    return (LocatorStarterRule) context.getServletContext().getAttribute("locator");
-  }
 }
diff --git a/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/LocatorWebContext.java
b/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/LocatorWebContext.java
new file mode 100644
index 0000000..f0762a1
--- /dev/null
+++ b/geode-web-management/src/commonTest/java/org/apache/geode/management/internal/rest/LocatorWebContext.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.management.internal.rest;
+
+import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
+
+import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.RequestBuilder;
+import org.springframework.test.web.servlet.ResultActions;
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
+import org.springframework.test.web.servlet.request.RequestPostProcessor;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+import org.apache.geode.test.junit.rules.LocatorStarterRule;
+
+/**
+ * use in conjunction with any BaseLocatorContextLoader to ease the interaction with the
locator
+ * started when loading the servlet xml to create the web context.
+ */
+public class LocatorWebContext {
+  static RequestPostProcessor POST_PROCESSOR = new StandardRequestPostProcessor();
+  private WebApplicationContext webApplicationContext;
+  private MockMvc mockMvc;
+  private MockMvcClientHttpRequestFactory requestFactory;
+
+
+  /**
+   * this constructor needs to be called in the @Before method of the test since only at
that time
+   * the webApplicationContext loaded by the context loader is available.
+   */
+  public LocatorWebContext(WebApplicationContext webApplicationContext) {
+    this.webApplicationContext = webApplicationContext;
+    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
+        .apply(springSecurity())
+        .addFilter(new ManagementLoggingFilter())
+        .build();
+
+    requestFactory = new MockMvcClientHttpRequestFactory(mockMvc);
+  }
+
+  public LocatorStarterRule getLocator() {
+    return (LocatorStarterRule) webApplicationContext.getServletContext().getAttribute("locator");
+  }
+
+  public MockMvcClientHttpRequestFactory getRequestFactory() {
+    return requestFactory;
+  }
+
+  public ResultActions perform(RequestBuilder builder) throws Exception {
+    ((MockHttpServletRequestBuilder) builder).with(POST_PROCESSOR);
+    return mockMvc.perform(builder);
+  }
+}
diff --git a/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/ClientClusterManagementServiceDUnitTest.java
b/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/ClientClusterManagementServiceDUnitTest.java
index e0c7f28..f8be9de 100644
--- a/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/ClientClusterManagementServiceDUnitTest.java
+++ b/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/ClientClusterManagementServiceDUnitTest.java
@@ -27,16 +27,13 @@ import org.springframework.security.test.context.support.WithMockUser;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
-import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.web.context.WebApplicationContext;
 
 import org.apache.geode.cache.RegionShortcut;
 import org.apache.geode.cache.configuration.RegionConfig;
 import org.apache.geode.management.api.ClusterManagementResult;
 import org.apache.geode.management.api.ClusterManagementService;
-import org.apache.geode.management.internal.rest.BaseLocatorContextLoader;
+import org.apache.geode.management.internal.rest.LocatorWebContext;
 import org.apache.geode.management.internal.rest.PlainLocatorContextLoader;
 import org.apache.geode.test.dunit.rules.ClusterStartupRule;
 import org.apache.geode.test.dunit.rules.MemberVM;
@@ -55,17 +52,14 @@ public class ClientClusterManagementServiceDUnitTest {
 
   private MemberVM server1;
   private ClusterManagementService client;
+  private LocatorWebContext webContext;
 
   @Before
   public void before() {
     cluster.setSkipLocalDistributedSystemCleanup(true);
-    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
-        .build();
-    MockMvcClientHttpRequestFactory requestFactory = new MockMvcClientHttpRequestFactory(mockMvc);
-    client = ClusterManagementServiceProvider.getService(requestFactory);
-
-    server1 = cluster.startServerVM(0,
-        BaseLocatorContextLoader.getLocatorFromContext(webApplicationContext).getPort());
+    webContext = new LocatorWebContext(webApplicationContext);
+    client = ClusterManagementServiceProvider.getService(webContext.getRequestFactory());
+    server1 = cluster.startServerVM(0, webContext.getLocator().getPort());
   }
 
   @Test
@@ -75,8 +69,16 @@ public class ClientClusterManagementServiceDUnitTest {
     region.setName("customer");
     region.setType(RegionShortcut.REPLICATE);
 
-    ClusterManagementResult result = client.create(region, "");
+    ClusterManagementResult result = client.create(region);
+
+    // in StressNewTest, this will be run multiple times without restarting the locator
+    assertThat(result.getStatusCode()).isIn(ClusterManagementResult.StatusCode.OK,
+        ClusterManagementResult.StatusCode.ENTITY_EXISTS);
+  }
 
+  @Test
+  @WithMockUser
+  public void sanityCheck() {
     assertThat(client.isConnected()).isTrue();
   }
 }
diff --git a/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/MemberManagementServiceDUnitTest.java
b/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/MemberManagementServiceDUnitTest.java
index a0cd8f7..7069977 100644
--- a/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/MemberManagementServiceDUnitTest.java
+++ b/geode-web-management/src/distributedTest/java/org/apache/geode/management/client/MemberManagementServiceDUnitTest.java
@@ -32,20 +32,14 @@ import org.springframework.security.test.context.support.WithMockUser;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
-import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.request.RequestPostProcessor;
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.web.context.WebApplicationContext;
 
 import org.apache.geode.management.api.ClusterManagementResult;
 import org.apache.geode.management.api.ClusterManagementService;
 import org.apache.geode.management.configuration.MemberConfig;
-import org.apache.geode.management.internal.rest.BaseLocatorContextLoader;
+import org.apache.geode.management.internal.rest.LocatorWebContext;
 import org.apache.geode.management.internal.rest.PlainLocatorContextLoader;
-import org.apache.geode.management.internal.rest.StandardRequestPostProcessor;
 import org.apache.geode.test.dunit.rules.ClusterStartupRule;
-import org.apache.geode.test.dunit.rules.MemberVM;
 
 @RunWith(SpringRunner.class)
 @ContextConfiguration(locations = {"classpath*:WEB-INF/geode-management-servlet.xml"},
@@ -53,28 +47,21 @@ import org.apache.geode.test.dunit.rules.MemberVM;
 @WebAppConfiguration
 public class MemberManagementServiceDUnitTest {
 
-  static RequestPostProcessor POST_PROCESSOR = new StandardRequestPostProcessor();
-
   @Autowired
   private WebApplicationContext webApplicationContext;
 
   @Rule
   public ClusterStartupRule cluster = new ClusterStartupRule(1);
 
-  private MemberVM server1;
   private ClusterManagementService client;
-  private MockMvc mockMvc;
+  private LocatorWebContext webContext;
 
   @Before
   public void before() {
     cluster.setSkipLocalDistributedSystemCleanup(true);
-    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
-        .build();
-    MockMvcClientHttpRequestFactory requestFactory = new MockMvcClientHttpRequestFactory(mockMvc);
-    client = ClusterManagementServiceProvider.getService(requestFactory);
-
-    server1 = cluster.startServerVM(0,
-        BaseLocatorContextLoader.getLocatorFromContext(webApplicationContext).getPort());
+    webContext = new LocatorWebContext(webApplicationContext);
+    client = ClusterManagementServiceProvider.getService(webContext.getRequestFactory());
+    cluster.startServerVM(0, webContext.getLocator().getPort());
   }
 
   @Test
@@ -115,7 +102,7 @@ public class MemberManagementServiceDUnitTest {
   @Test
   @WithMockUser
   public void noMatchWithFilter() throws Exception {
-    mockMvc.perform(get("/v2/members?id=server"))
+    webContext.perform(get("/v2/members?id=server"))
         .andExpect(status().isOk())
         .andExpect(jsonPath("$.statusCode", is("OK")))
         .andExpect(jsonPath("$.result", iterableWithSize(0)));
@@ -124,7 +111,7 @@ public class MemberManagementServiceDUnitTest {
   @Test
   @WithMockUser
   public void noMatchWithUriVariable() throws Exception {
-    mockMvc.perform(get("/v2/members/server"))
+    webContext.perform(get("/v2/members/server"))
         .andExpect(status().isNotFound())
         .andExpect(jsonPath("$.statusCode", is("ENTITY_NOT_FOUND")))
         .andExpect(jsonPath("$.statusMessage",
diff --git a/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementIntegrationTest.java
b/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementIntegrationTest.java
index 590eb46..01788c6 100644
--- a/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementIntegrationTest.java
+++ b/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementIntegrationTest.java
@@ -31,9 +31,6 @@ import org.springframework.security.test.context.support.WithMockUser;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.request.RequestPostProcessor;
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.web.context.WebApplicationContext;
 
 import org.apache.geode.cache.RegionShortcut;
@@ -45,17 +42,15 @@ import org.apache.geode.cache.configuration.RegionConfig;
 @WebAppConfiguration
 public class RegionManagementIntegrationTest {
 
-  static RequestPostProcessor POST_PROCESSOR = new StandardRequestPostProcessor();
-
   @Autowired
   private WebApplicationContext webApplicationContext;
 
-  private MockMvc mockMvc;
+  // needs to be used together with any BaseLocatorContextLoader
+  private LocatorWebContext context;
 
   @Before
   public void before() {
-    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
-        .build();
+    context = new LocatorWebContext(webApplicationContext);
   }
 
   @Test
@@ -68,9 +63,7 @@ public class RegionManagementIntegrationTest {
     ObjectMapper mapper = new ObjectMapper();
     String json = mapper.writeValueAsString(regionConfig);
 
-    mockMvc.perform(post("/v2/regions")
-        .with(POST_PROCESSOR)
-        .content(json))
+    context.perform(post("/v2/regions").content(json))
         .andExpect(status().isInternalServerError())
         .andExpect(jsonPath("$.statusCode", is("ERROR")))
         .andExpect(jsonPath("$.statusMessage",
@@ -80,8 +73,7 @@ public class RegionManagementIntegrationTest {
   @Test
   @WithMockUser
   public void ping() throws Exception {
-    mockMvc.perform(get("/v2/ping")
-        .with(POST_PROCESSOR))
+    context.perform(get("/v2/ping"))
         .andExpect(content().string("pong"));
   }
 }
diff --git a/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementSecurityIntegrationTest.java
b/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementSecurityIntegrationTest.java
index 5cc66f3..ac7991a 100644
--- a/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementSecurityIntegrationTest.java
+++ b/geode-web-management/src/integrationTest/java/org/apache/geode/management/internal/rest/RegionManagementSecurityIntegrationTest.java
@@ -17,7 +17,6 @@ package org.apache.geode.management.internal.rest;
 
 import static org.hamcrest.Matchers.is;
 import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
-import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -31,9 +30,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.request.RequestPostProcessor;
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.web.context.WebApplicationContext;
 
 import org.apache.geode.cache.RegionShortcut;
@@ -45,15 +41,13 @@ import org.apache.geode.cache.configuration.RegionConfig;
 @WebAppConfiguration
 public class RegionManagementSecurityIntegrationTest {
 
-  static RequestPostProcessor POST_PROCESSOR =
-      new StandardRequestPostProcessor();
-
   @Autowired
   private WebApplicationContext webApplicationContext;
 
+  private LocatorWebContext context;
+
   private RegionConfig regionConfig;
   private String json;
-  private MockMvc mockMvc;
 
   @Before
   public void before() throws JsonProcessingException {
@@ -62,16 +56,13 @@ public class RegionManagementSecurityIntegrationTest {
     regionConfig.setType(RegionShortcut.REPLICATE);
     ObjectMapper mapper = new ObjectMapper();
     json = mapper.writeValueAsString(regionConfig);
-    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
-        .apply(springSecurity())
-        .build();
+    context = new LocatorWebContext(webApplicationContext);
   }
 
   @Test
   public void sanityCheck_not_authorized() throws Exception {
-    mockMvc.perform(post("/v2/regions")
+    context.perform(post("/v2/regions")
         .with(httpBasic("user", "user"))
-        .with(POST_PROCESSOR)
         .content(json))
         .andExpect(status().isForbidden())
         .andExpect(jsonPath("$.statusCode", is("UNAUTHORIZED")))
@@ -81,8 +72,7 @@ public class RegionManagementSecurityIntegrationTest {
 
   @Test
   public void sanityCheckWithNoCredentials() throws Exception {
-    mockMvc.perform(post("/v2/regions")
-        .with(POST_PROCESSOR)
+    context.perform(post("/v2/regions")
         .content(json))
         .andExpect(status().isUnauthorized())
         .andExpect(jsonPath("$.statusCode", is("UNAUTHENTICATED")))
@@ -92,9 +82,8 @@ public class RegionManagementSecurityIntegrationTest {
 
   @Test
   public void sanityCheckWithWrongCredentials() throws Exception {
-    mockMvc.perform(post("/v2/regions")
+    context.perform(post("/v2/regions")
         .with(httpBasic("user", "wrong_password"))
-        .with(POST_PROCESSOR)
         .content(json))
         .andExpect(status().isUnauthorized())
         .andExpect(jsonPath("$.statusCode", is("UNAUTHENTICATED")))
@@ -104,9 +93,8 @@ public class RegionManagementSecurityIntegrationTest {
 
   @Test
   public void sanityCheck_success() throws Exception {
-    mockMvc.perform(post("/v2/regions")
+    context.perform(post("/v2/regions")
         .with(httpBasic("dataManage", "dataManage"))
-        .with(POST_PROCESSOR)
         .content(json))
         .andExpect(status().isInternalServerError())
         .andExpect(jsonPath("$.statusCode", is("ERROR")))


Mime
View raw message