diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/config/TestConfiguration.java b/backend/services/system-setting/src/test/java/io/metersphere/system/config/TestConfiguration.java new file mode 100644 index 0000000000..bc455fbddf --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/config/TestConfiguration.java @@ -0,0 +1,17 @@ +package io.metersphere.system.config; + +import org.mockserver.client.MockServerClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class TestConfiguration { + + @Bean(destroyMethod = "close") + public MockServerClient mockServerClient(@Value("${embedded.mockserver.host}") String host, + @Value("${embedded.mockserver.port}") int port) { + + return new MockServerClient(host, port); + } + } \ No newline at end of file diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java index 47f211fc9f..5643126ee2 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java @@ -14,7 +14,10 @@ import io.metersphere.system.mapper.TestResourcePoolOrganizationMapper; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; + import org.junit.jupiter.api.*; +import org.mockserver.client.MockServerClient; +import org.mockserver.model.Header; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; @@ -29,7 +32,12 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; +import static org.mockserver.model.StringBody.exact; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -44,6 +52,10 @@ class TestResourcePoolControllerTests extends BaseTest { private MockMvc mockMvc; @Resource private TestResourcePoolOrganizationMapper testResourcePoolOrganizationMapper; + @Resource + private MockServerClient mockServerClient; + + private static final String TEST_RESOURCE_POOL_ADD = "/test/resource/pool/add"; private static final String TEST_RESOURCE_POOL_UPDATE = "/test/resource/pool/update"; @@ -527,7 +539,7 @@ class TestResourcePoolControllerTests extends BaseTest { @Test @Order(21) void unableTestResourcePoolSuccess() throws Exception { - MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob3", false, true, true, false, false, ResourcePoolTypeEnum.K8S.name()); + MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob4", false, true, true, false, false, ResourcePoolTypeEnum.K8S.name()); TestResourcePool testResourcePoolRequest1 = getResult(testPoolBlob); String id = testResourcePoolRequest1.getId(); mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/"+id) @@ -540,6 +552,37 @@ class TestResourcePoolControllerTests extends BaseTest { @Test @Order(22) + void createExpectationForInvalidAuth() throws Exception { + mockServerClient + .when( + request() + .withMethod("POST") + .withPath("172.16.200.8") + .withHeader("Content-type", "application/json") + .withBody(exact("OK")), + exactly(1)) + .respond( + response() + .withStatusCode(401) + .withHeaders( + new Header("Content-Type", "application/json; charset=utf-8"), + new Header("Cache-Control", "public, max-age=86400")) + .withBody("OK") + .withDelay(TimeUnit.SECONDS,1) + ); + MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob3", false, true, true, false, false, ResourcePoolTypeEnum.NODE.name()); + TestResourcePool testResourcePoolRequest1 = getResult(testPoolBlob); + String id = testResourcePoolRequest1.getId(); + mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/"+id) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + + } + + @Test + @Order(23) void unableTestResourcePoolFiled() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/105") .header(SessionConstants.HEADER_TOKEN, sessionId) @@ -550,6 +593,7 @@ class TestResourcePoolControllerTests extends BaseTest { } + void listByKeyWord(String keyWord) throws Exception { QueryResourcePoolRequest request = new QueryResourcePoolRequest(); request.setCurrent(1); diff --git a/pom.xml b/pom.xml index ceb665618f..bc7e374cb1 100644 --- a/pom.xml +++ b/pom.xml @@ -124,6 +124,7 @@ org.mock-server mockserver-netty + test ${mockserver-client-java.version}