-
Notifications
You must be signed in to change notification settings - Fork 3
/
SeaShellWiremockSoapLauncher.java
136 lines (114 loc) · 6.42 KB
/
SeaShellWiremockSoapLauncher.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.jesperancinha.shell;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Objects;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.http.ContentTypeHeader.KEY;
import static wiremock.org.apache.hc.core5.http.ContentType.TEXT_XML;
public class SeaShellWiremockSoapLauncher {
private final static Logger log = LoggerFactory.getLogger(SeaShellWiremockSoapLauncher.class);
public static void main(String[] args) throws IOException {
log.info("Wiremock service starting...");
createWireMockServer();
log.info("Wiremock service started!");
}
public static WireMockServer createWireMockServer() throws IOException {
final WireMockServer wireMockServer = new WireMockServer(8090);
wireMockServer.start();
configureFor("localhost", 8090);
stubPersons();
stubShells();
stubCostumes();
stubLowers();
stubTops();
stubAccounts();
stupRequestToWSDL("/seashells/accounts?wsdl", "/mock/responses/accounts/SeaShellsWSDLAccounts.wsdl");
stupRequestToWSDL("/seashells/costumes?wsdl", "/mock/responses/costumes/SeaShellsWSDLCostumes.wsdl");
stupRequestToWSDL("/seashells/lowers?wsdl", "/mock/responses/lowers/SeaShellsWSDLLowers.wsdl");
stupRequestToWSDL("/seashells/persons?wsdl", "/mock/responses/persons/SeaShellsWSDLPersons.wsdl");
stupRequestToWSDL("/seashells/shells?wsdl", "/mock/responses/shells/SeaShellsWSDLShells.wsdl");
stupRequestToWSDL("/seashells/tops?wsdl", "/mock/responses/tops/SeaShellsWSDLTops.wsdl");
return wireMockServer;
}
private static void stubShells() throws IOException {
for (int i = 1; i <= 16; i++) {
stubRequestToShell("/mock/responses/shells/shell" + i + ".xml", i);
}
stubFor(post(urlPathEqualTo("/seashells/shells"))
.withRequestBody(matchingXPath("/Envelope/Body/allShellRequest"))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource("/mock/responses/shells/allShells.xml")))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubPersons() throws IOException {
for (int i = 1; i <= 16; i++) {
stubRequestToPerson("/mock/responses/persons/person" + i + ".xml", i);
}
}
private static void stubCostumes() throws IOException {
for (int i = 1; i <= 3; i++) {
stubRequestToCostume("/mock/responses/costumes/costume" + i + ".xml", i);
}
}
private static void stubLowers() throws IOException {
for (int i = 1; i <= 2; i++) {
stubRequestToLower("/mock/responses/lowers/lower" + i + ".xml", i);
}
}
private static void stubTops() throws IOException {
for (int i = 1; i <= 2; i++) {
stubRequestToTop("/mock/responses/tops/top" + i + ".xml", i);
}
}
private static void stubAccounts() throws IOException {
for (int i = 1; i <= 1; i++) {
stubRequestToAccount("/mock/responses/accounts/account" + i + ".xml");
}
}
private static void stupRequestToWSDL(String urlString, String fileLocation) throws IOException {
stubFor(get(urlEqualTo(urlString))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(fileLocation)))));
}
private static void stubRequestToShell(String responseResource, Integer i) throws IOException {
stubFor(post(urlEqualTo("/seashells/shells"))
.withRequestBody(matchingXPath("/Envelope/Body/shellRequest/shellId/text()", equalTo(i.toString())))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubRequestToPerson(String responseResource, Integer i) throws IOException {
stubFor(post(urlEqualTo("/seashells/persons"))
.withRequestBody(matchingXPath("/Envelope/Body/personsRequest/personId/text()", equalTo(i.toString())))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubRequestToCostume(String responseResource, Integer i) throws IOException {
stubFor(post(urlEqualTo("/seashells/costumes"))
.withRequestBody(matchingXPath("/Envelope/Body/costumesRequest/costumeId/text()", equalTo(i.toString())))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubRequestToTop(String responseResource, Integer i) throws IOException {
stubFor(post(urlEqualTo("/seashells/tops"))
.withRequestBody(matchingXPath("/Envelope/Body/TopRequest/TopId/text()", equalTo(i.toString())))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubRequestToLower(String responseResource, Integer i) throws IOException {
stubFor(post(urlEqualTo("/seashells/lowers"))
.withRequestBody(matchingXPath("/Envelope/Body/LowersRequest/LowerId/text()", equalTo(i.toString())))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static void stubRequestToAccount(String responseResource) throws IOException {
stubFor(post(urlEqualTo("/seashells/accounts"))
.withRequestBody(matchingXPath("/Envelope/Body/AccountRequest/accountId/text()"))
.willReturn(aResponse().withBody(CharStreams.toString(getStringFromResource(responseResource)))
.withHeader(KEY, TEXT_XML.getMimeType())));
}
private static byte[] getStringFromResource(String resourceName) throws IOException {
try (var inputStream = SeaShellWiremockSoapLauncher.class.getResourceAsStream(resourceName)) {
return Objects.requireNonNull(inputStream).readAllBytes();
}
}
}