package com.millicom.microservice.service.mock; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.MappingBuilder; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.matching.StringValuePattern; import java.util.Map; import static com.github.tomakehurst.wiremock.client.WireMock.*; public class ExternalSystemsMock { public static WireMockServer openPort(WireMockServer server) throws InterruptedException { return server; } public static WireMockServer closePort(WireMockServer server) { server.stop(); return server; } public static void externalSystemsResponse(WireMockServer mockServer, String protocol, String operation, String endpoint, String response, int httpStatus, Map params) { String header = protocol == "SOAP" ? "text/xml" : "application/json"; MappingBuilder mappingBuilder = switch (operation) { case "POST" -> post(urlPathMatching(endpoint)); case "GET" -> get(urlPathMatching(endpoint)); case "PATCH" -> patch(urlPathMatching(endpoint)); case "PUT" -> put(urlPathMatching(endpoint)); case "DELETE" -> delete(urlPathMatching(endpoint)); default -> get(urlPathMatching(endpoint)); }; if (params != null) mappingBuilder.withQueryParams(params); if(endpoint.endsWith("/slowresponse")) try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ResponseDefinitionBuilder responseDefinitionBuilder = aResponse() .withStatus(httpStatus) .withHeader("Content-Type", header); if (response != null) responseDefinitionBuilder.withBody(response); mappingBuilder .willReturn(responseDefinitionBuilder); mockServer.stubFor(mappingBuilder); } }