build: mock test resource pool

This commit is contained in:
CaptainB 2023-08-14 19:24:03 +08:00 committed by 刘瑞斌
parent aa5370292a
commit 5d71c96833
1 changed files with 16 additions and 15 deletions

View File

@ -18,6 +18,7 @@ import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.mockserver.client.MockServerClient; import org.mockserver.client.MockServerClient;
import org.mockserver.model.Header; import org.mockserver.model.Header;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -32,12 +33,9 @@ import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response; 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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -55,7 +53,10 @@ class TestResourcePoolControllerTests extends BaseTest {
@Resource @Resource
private MockServerClient mockServerClient; private MockServerClient mockServerClient;
@Value("${embedded.mockserver.host}")
private String host;
@Value("${embedded.mockserver.port}")
private int port;
private static final String TEST_RESOURCE_POOL_ADD = "/test/resource/pool/add"; private static final String TEST_RESOURCE_POOL_ADD = "/test/resource/pool/add";
private static final String TEST_RESOURCE_POOL_UPDATE = "/test/resource/pool/update"; private static final String TEST_RESOURCE_POOL_UPDATE = "/test/resource/pool/update";
@ -434,10 +435,14 @@ class TestResourcePoolControllerTests extends BaseTest {
listByKeyWord("test_pool_update"); listByKeyWord("test_pool_update");
} }
private static void setResources(TestResourcePoolRequest testResourcePoolDTO, boolean isPart) { private void setResources(TestResourcePoolRequest testResourcePoolDTO, boolean isPart) {
TestResourceDTO testResourceDTO; TestResourceDTO testResourceDTO;
if (isPart) { if (isPart) {
testResourceDTO = JSON.parseObject(configuration, TestResourceDTO.class); testResourceDTO = JSON.parseObject(configuration, TestResourceDTO.class);
testResourceDTO.getNodesList().forEach(testResourceNodeDTO -> {
testResourceNodeDTO.setIp(host);
testResourceNodeDTO.setPort(port + "");
});
} else { } else {
testResourceDTO = JSON.parseObject(configurationWidthOutOrgIds, TestResourceDTO.class); testResourceDTO = JSON.parseObject(configurationWidthOutOrgIds, TestResourceDTO.class);
} }
@ -556,19 +561,15 @@ class TestResourcePoolControllerTests extends BaseTest {
mockServerClient mockServerClient
.when( .when(
request() request()
.withMethod("POST") .withMethod("GET")
.withPath("172.16.200.8") .withPath("/status"))
.withHeader("Content-type", "application/json")
.withBody(exact("OK")),
exactly(1))
.respond( .respond(
response() response()
.withStatusCode(401) .withStatusCode(200)
.withHeaders( .withHeaders(
new Header("Content-Type", "application/json; charset=utf-8"), new Header("Content-Type", "application/json; charset=utf-8"),
new Header("Cache-Control", "public, max-age=86400")) new Header("Cache-Control", "public, max-age=86400"))
.withBody("OK") .withBody(JSON.toJSONString(ResultHolder.success("OK")))
.withDelay(TimeUnit.SECONDS,1)
); );
MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob3", false, true, true, false, false, ResourcePoolTypeEnum.NODE.name()); MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob3", false, true, true, false, false, ResourcePoolTypeEnum.NODE.name());
TestResourcePool testResourcePoolRequest1 = getResult(testPoolBlob); TestResourcePool testResourcePoolRequest1 = getResult(testPoolBlob);
@ -650,12 +651,12 @@ class TestResourcePoolControllerTests extends BaseTest {
if (noIp) { if (noIp) {
testResourceNodeDTO.setIp(""); testResourceNodeDTO.setIp("");
} else { } else {
testResourceNodeDTO.setIp("172.2.130.1"); testResourceNodeDTO.setIp(host);
} }
if (noPort) { if (noPort) {
testResourceNodeDTO.setPort(""); testResourceNodeDTO.setPort("");
} else { } else {
testResourceNodeDTO.setPort("3306"); testResourceNodeDTO.setPort(port + "");
} }
if (noMonitor) { if (noMonitor) {
testResourceNodeDTO.setMonitor(" "); testResourceNodeDTO.setMonitor(" ");