-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dariusz Czajkiewicz
committed
Feb 20, 2025
1 parent
dddb077
commit c377cc3
Showing
7 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...aywright-framework/src/test/java/com/capgemini/infrastructure/resources/MyMockServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.capgemini.infrastructure.resources; | ||
|
||
import com.capgemini.infrastructure.Configuration; | ||
import org.testcontainers.containers.MockServerContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
public class MyMockServer extends MockServerContainer { | ||
private static final String NETWORK_ALIAS = "mock-server"; | ||
|
||
public MyMockServer(Network network) { | ||
super(DockerImageName.parse("mockserver/mockserver")); | ||
withReuse(Configuration.DEBUG); | ||
withNetwork(network) | ||
.withNetworkAliases(NETWORK_ALIAS) | ||
.withExposedPorts(1080) | ||
.withStartupTimeout(Duration.ofMinutes(2)); | ||
withCreateContainerCmdModifier(cmd -> cmd.withName(Configuration.MY_MOCK_NAME)); | ||
setPortBindings(Arrays.asList("1080:1080")); | ||
} | ||
|
||
@Override | ||
public MyMockServer withReuse(boolean reusable) { | ||
if (reusable) { | ||
var aliases = new ArrayList<String>(); | ||
aliases.add(NETWORK_ALIAS); | ||
this.setNetworkAliases(aliases); | ||
} | ||
return (MyMockServer) super.withReuse(reusable); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...ht-framework/src/test/java/com/capgemini/infrastructure/resources/MyMockServerClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.capgemini.infrastructure.resources; | ||
|
||
import com.capgemini.infrastructure.Configuration; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.matchers.TimeToLive; | ||
import org.mockserver.matchers.Times; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
|
||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
import static org.mockserver.model.StringBody.subString; | ||
|
||
public class MyMockServerClient { | ||
private final Logger logger = LoggerFactory.getLogger(MyMockServerClient.class); | ||
private final MyMockServer mockServer = Configuration.getInstance().getMyMockServer(); | ||
private static MyMockServerClient instance = null; | ||
private MockServerClient mockServerClient = null; | ||
|
||
private MyMockServerClient() { | ||
} | ||
|
||
public static MyMockServerClient getInstance() { | ||
if (instance == null) { | ||
instance = new MyMockServerClient(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void startMockClient() { | ||
try { | ||
mockServerClient = new MockServerClient(mockServer.getHost(), mockServer.getServerPort()); | ||
|
||
// Add rule by file | ||
// http://localhost:1080/my-api/sample-1 | ||
/* | ||
{ | ||
"source": "MySystem", | ||
"transportMode": "AIR", | ||
"totalPackages": 1000, | ||
"commercialValue": { | ||
"cost": 200, | ||
"currency": "USD" | ||
}, | ||
"isCancelled": false | ||
} | ||
*/ | ||
addRule("/my-api/sample-1", "sample-mock-response-1.json"); | ||
|
||
// Add conditional rule | ||
addRuleConditionTxt("/cst/integration/api/createRequestV3", "20220415-43872", Map.of( | ||
"AI24000595", "20220415-66666", | ||
"BY24001263", "20220415-66666" | ||
)); | ||
|
||
// Add text response | ||
addRuleTxt("/cst/integration/api/closeRequest", "CST request closed"); | ||
addRuleTxt("/cst/integration/api/proceedNextStep", "CST request proceeded to the next step"); | ||
|
||
logger.info("Mock URL: {}:{}", mockServer.getHost(), mockServer.getServerPort()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
//customResponseBody: key -> condition string, value -> response body to return | ||
private void addRuleConditionTxt(String uriPath, String defaultResponse, Map<String, String> customResponseBody) { | ||
for (var condition : customResponseBody.entrySet()) { | ||
addConditionExpectation(uriPath, condition.getKey(), condition.getValue()); | ||
logger.info("Mock rule added for path: http://{}:{}{} and condition: {} -> {}", mockServer.getHost(), mockServer.getServerPort(), uriPath, condition.getKey(), condition.getValue()); | ||
} | ||
|
||
mockServerClient.when( | ||
request().withPath(uriPath), | ||
Times.unlimited(), | ||
TimeToLive.unlimited(), | ||
-100 | ||
) | ||
.respond(response() | ||
.withBody(defaultResponse) | ||
.withHeader("Content-Type", "application/json;charset=UTF-8") | ||
); | ||
logger.info("Mock rule added for path: http://{}:{}{} default response {}", mockServer.getHost(), mockServer.getServerPort(), uriPath, defaultResponse); | ||
} | ||
|
||
private void addConditionExpectation(String uriPath, String condtion, String responseBody) { | ||
mockServerClient | ||
.when( | ||
request() | ||
.withPath(uriPath) | ||
.withBody(subString(condtion)) | ||
) | ||
.respond(response() | ||
.withBody(responseBody) | ||
.withHeader("Content-Type", "application/json;charset=UTF-8") | ||
); | ||
} | ||
|
||
private void addRuleTxt(String uriPath, String responseBody) { | ||
logger.info("Mock rule added for path: http://{}:{}{}", mockServer.getHost(), mockServer.getServerPort(), uriPath); | ||
mockServerClient | ||
.when(request().withPath(uriPath)) | ||
.respond(response() | ||
.withBody(responseBody) | ||
.withHeader("Content-Type", "application/json;charset=UTF-8")); | ||
} | ||
|
||
private void addRule(String uriPath, String filePath) { | ||
logger.info("Mock rule added for path: http://{}:{}{}", mockServer.getHost(), mockServer.getServerPort(), uriPath); | ||
mockServerClient | ||
.when(request().withPath(uriPath)) | ||
.respond(response() | ||
.withBody(getMyResponse(filePath)) | ||
.withHeader("Content-Type", "application/json;charset=UTF-8")); | ||
} | ||
|
||
private String getMyResponse(String fileToMock) { | ||
try (var inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mock/" + fileToMock)) { | ||
assert inputStream != null; | ||
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
mrchecker-playwright-framework/src/test/resources/mock/sample-mock-response-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"source": "MySystem", | ||
"transportMode": "AIR", | ||
"totalPackages": 1000, | ||
"commercialValue": { | ||
"cost": 200, | ||
"currency": "USD" | ||
}, | ||
"isCancelled": false | ||
} |