|
| 1 | +package org.gitlab4j.api.mockserver; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.TestInfo; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | + |
| 14 | +import software.xdev.mockserver.client.MockServerClient; |
| 15 | +import software.xdev.mockserver.mock.Expectation; |
| 16 | +import software.xdev.mockserver.model.Header; |
| 17 | +import software.xdev.mockserver.model.Headers; |
| 18 | +import software.xdev.mockserver.model.HttpForward; |
| 19 | +import software.xdev.mockserver.model.HttpForward.Scheme; |
| 20 | +import software.xdev.mockserver.model.HttpRequest; |
| 21 | +import software.xdev.mockserver.netty.MockServer; |
| 22 | +import software.xdev.mockserver.serialization.ObjectMapperFactory; |
| 23 | +import software.xdev.mockserver.serialization.model.ExpectationDTO; |
| 24 | +import software.xdev.mockserver.serialization.model.HttpRequestDTO; |
| 25 | +import software.xdev.mockserver.serialization.model.HttpResponseDTO; |
| 26 | +import software.xdev.mockserver.serialization.model.RequestDefinitionDTO; |
| 27 | + |
| 28 | +public class MockserverUtil { |
| 29 | + |
| 30 | + private static final String MOCKSERVER_FORWARD_TO_REAL_SERVER = "MOCKSERVER_FORWARD_TO_REAL_SERVER"; |
| 31 | + private static final String MOCKDATA_FILE = "MOCKDATA_FILE"; |
| 32 | + |
| 33 | + private static final Path MOCK_SERVER_EXPECTATIONS_ROOT = Paths.get("src/test/resources/mock-server"); |
| 34 | + |
| 35 | + public static MockServerClient initBeforeEach(TestInfo info, String remoteHost) { |
| 36 | + MockServer server = new MockServer(9999); |
| 37 | + MockServerClient mockServerClient = new MockServerClient("localhost", server.getLocalPort()); |
| 38 | + |
| 39 | + System.out.println("Mock server running on port " + server.getLocalPort()); |
| 40 | + |
| 41 | + mockServerClient.reset(); |
| 42 | + |
| 43 | + if (mockserverForwardToRealServer()) { |
| 44 | + mockServerClient |
| 45 | + .when(HttpRequest.request()) |
| 46 | + .forward(HttpForward.forward() |
| 47 | + .withScheme(Scheme.HTTPS) |
| 48 | + .withHost("localhost") |
| 49 | + .withPort(8888)); |
| 50 | + } else { |
| 51 | + if (Files.isDirectory(MOCK_SERVER_EXPECTATIONS_ROOT)) { |
| 52 | + ObjectMapper objectMapper = ObjectMapperFactory.createObjectMapper(); |
| 53 | + String prefix = getExpectationFilePrefix(info); |
| 54 | + try { |
| 55 | + Files.list(MOCK_SERVER_EXPECTATIONS_ROOT) |
| 56 | + .filter(p -> p.getFileName().toString().startsWith(prefix)) |
| 57 | + .sorted() |
| 58 | + .map(p -> readExpectation(objectMapper, p)) |
| 59 | + .forEach(e -> mockServerClient.upsert(e)); |
| 60 | + } catch (IOException e) { |
| 61 | + throw new IllegalStateException("Can not read expectation files", e); |
| 62 | + } |
| 63 | + } else { |
| 64 | + System.out.println("No expectactions found, because " + MOCK_SERVER_EXPECTATIONS_ROOT.toAbsolutePath() |
| 65 | + + " is not a directory"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return mockServerClient; |
| 70 | + } |
| 71 | + |
| 72 | + public static void recordExpectationsAfterEach(TestInfo info, MockServerClient mockServer) throws IOException { |
| 73 | + if (mockserverForwardToRealServer()) { |
| 74 | + Expectation[] list = mockServer.retrieveRecordedExpectations(HttpRequest.request()); |
| 75 | + |
| 76 | + String prefix = getExpectationFilePrefix(info); |
| 77 | + Files.createDirectories(MOCK_SERVER_EXPECTATIONS_ROOT); |
| 78 | + for (int i = 0; i < list.length; i++) { |
| 79 | + String id = prefix + String.format("%03d", i + 1); |
| 80 | + ExpectationDTO item = new ExpectationDTO(list[i]); |
| 81 | + item.setId(id); |
| 82 | + RequestDefinitionDTO request = item.getHttpRequest(); |
| 83 | + if (request instanceof HttpRequestDTO) { |
| 84 | + HttpRequestDTO httpRequest = (HttpRequestDTO) request; |
| 85 | + httpRequest.setHeaders(new Headers()); |
| 86 | + httpRequest.setKeepAlive(null); |
| 87 | + httpRequest.setProtocol(null); |
| 88 | + } |
| 89 | + HttpResponseDTO httpResponse = item.getHttpResponse(); |
| 90 | + List<String> contentType = httpResponse.getHeaders().getValues("Content-Type"); |
| 91 | + httpResponse.setHeaders(new Headers(new Header("Content-Type", contentType))); |
| 92 | + Path file = MOCK_SERVER_EXPECTATIONS_ROOT.resolve(id + ".json"); |
| 93 | + Files.writeString(file, item.toString() + "\n"); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static boolean mockserverForwardToRealServer() { |
| 99 | + String value = System.getenv(MOCKSERVER_FORWARD_TO_REAL_SERVER); |
| 100 | + return value != null && value.toLowerCase().equals("true"); |
| 101 | + } |
| 102 | + |
| 103 | + private static Expectation readExpectation(ObjectMapper objectMapper, Path file) { |
| 104 | + try { |
| 105 | + String content = Files.readString(file); |
| 106 | + ExpectationDTO dto = objectMapper.readValue(content, ExpectationDTO.class); |
| 107 | + return dto.buildObject(); |
| 108 | + } catch (IOException e) { |
| 109 | + throw new IllegalStateException("Could not read the expectation file " + file, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static String getExpectationFilePrefix(TestInfo info) { |
| 114 | + String testMethodName = getTestMethodName(info); |
| 115 | + String testMethodClass = getTestMethodClass(info); |
| 116 | + String prefix = testMethodClass + "_" + testMethodName + "_"; |
| 117 | + return prefix; |
| 118 | + } |
| 119 | + |
| 120 | + private static String getTestMethodClass(TestInfo info) { |
| 121 | + String testMethodName = info.getTestClass() |
| 122 | + .map(Class::getSimpleName) |
| 123 | + .orElseThrow(() -> new IllegalStateException("Could not find the test method class")); |
| 124 | + return testMethodName; |
| 125 | + } |
| 126 | + |
| 127 | + private static String getTestMethodName(TestInfo info) { |
| 128 | + String testMethodName = info.getTestMethod() |
| 129 | + .map(Method::getName) |
| 130 | + .orElseThrow(() -> new IllegalStateException("Could not find the test method name")); |
| 131 | + return testMethodName; |
| 132 | + } |
| 133 | + |
| 134 | + public static GitlabMockData loadMockData() { |
| 135 | + String value = System.getenv(MOCKDATA_FILE); |
| 136 | + Path file; |
| 137 | + if (value != null) { |
| 138 | + file = Paths.get(value); |
| 139 | + if (!Files.isReadable(file)) { |
| 140 | + throw new IllegalStateException("Can not load mock data file: " + file.toAbsolutePath() + ", check the " |
| 141 | + + MOCKDATA_FILE + " environement value"); |
| 142 | + } |
| 143 | + } else { |
| 144 | + file = Paths.get("src/test/resources/mock-config.properties"); |
| 145 | + } |
| 146 | + return new GitlabMockData(file); |
| 147 | + } |
| 148 | +} |
0 commit comments