-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVerifyMocksTest.java
100 lines (80 loc) · 3.4 KB
/
VerifyMocksTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.petstore.karate;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import com.intuit.karate.RuntimeHook;
import com.intuit.karate.cli.IdeMain;
import com.intuit.karate.core.ScenarioRuntime;
import com.intuit.karate.http.HttpRequest;
import com.intuit.karate.http.Response;
import io.github.apimock.MockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.intuit.karate.StringUtils.join;
public class VerifyMocksTest {
static io.github.apimock.MockServer server;
static Map<String, List> httpCallsMap = new HashMap<>();
@BeforeClass
public static void setup() throws Exception {
server = MockServer.builder()
.openapi("petstore-openapi.yml")
.features("classpath:mocks/PetMock.feature")
.pathPrefix("api/v3")
.http(0).build();
System.setProperty("karate.server.port", server.getPort() + "");
}
@AfterClass
public static void tearDown() throws Exception {
server.stop();
// Here you can verify how your mock behaves compared to the live API and calculate the quality and coverage of your mocks
System.out.println("Successful Http Calls to API Server:\n\t" + join(httpCallsMap.get("HttpCalls"), "\n\t"));
System.out.println("Failed Http Calls to API Server:\n\t" + join(httpCallsMap.get("FailedHttpCalls"), "\n\t"));
System.out.println("Successful Http Calls to Mock Server:\n\t" + join(httpCallsMap.get("mockHttpCalls"), "\n\t"));
System.out.println("Failed Http Calls to Mock Server:\n\t" + join(httpCallsMap.get("mockFailedHttpCalls"), "\n\t"));
}
@Test
public void verifyMockServer() throws Exception {
verifyMocks("mock");
}
@Test
public void verifyAPIServer() throws Exception {
verifyMocks("");
}
public void verifyMocks(String karateEnv) throws Exception {
CoverageRuntimeHook coverageRuntimeHook = new CoverageRuntimeHook();
Results results = Runner.path(Arrays.asList("classpath:/mocks"))
.hook(coverageRuntimeHook)
.tags("@mock-validation")
.karateEnv(karateEnv)
.parallel(1);
httpCallsMap.put(karateEnv + "HttpCalls", coverageRuntimeHook.httpCalls);
httpCallsMap.put(karateEnv + "FailedHttpCalls", coverageRuntimeHook.failedHttpCalls);
}
/**
* Accumulates successful and failed http calls.
*/
private class CoverageRuntimeHook implements RuntimeHook {
List<String> httpCalls = new ArrayList<>();
List<String> failedHttpCalls = new ArrayList<>();
List<String> scenarioHttpCalls = null;
@Override
public boolean beforeScenario(ScenarioRuntime sr) {
scenarioHttpCalls = new ArrayList<>();
return true;
}
@Override
public void afterHttpCall(HttpRequest request, Response response, ScenarioRuntime sr) {
scenarioHttpCalls.add(String.format("%s %s %s", request.getMethod(), request.getUrl(), response.getStatus()));
}
@Override
public void afterScenario(ScenarioRuntime sr) {
(sr.isFailed()? failedHttpCalls : httpCalls).addAll(scenarioHttpCalls);
}
};
}