-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathBaseWiremockTest.java
More file actions
261 lines (236 loc) · 9.66 KB
/
BaseWiremockTest.java
File metadata and controls
261 lines (236 loc) · 9.66 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package net.snowflake.client.jdbc;
import static net.snowflake.client.AbstractDriverIT.getConnectionParameters;
import static net.snowflake.client.AssumptionUtils.assumeNotRunningOnGithubActionsMac;
import static net.snowflake.client.AssumptionUtils.assumeNotRunningOnJava21;
import static net.snowflake.client.AssumptionUtils.assumeNotRunningOnJava8;
import static net.snowflake.client.jdbc.SnowflakeUtil.systemGetProperty;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.tomakehurst.wiremock.client.WireMock;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import net.snowflake.client.core.HttpUtil;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
abstract class BaseWiremockTest {
protected static final SFLogger logger = SFLoggerFactory.getLogger(BaseWiremockTest.class);
protected static final String WIREMOCK_HOME_DIR = ".wiremock";
protected static final String WIREMOCK_M2_PATH =
"/.m2/repository/org/wiremock/wiremock-standalone/3.8.0/wiremock-standalone-3.8.0.jar";
protected static final String WIREMOCK_HOST = "localhost";
protected static final String TRUST_STORE_PROPERTY = "javax.net.ssl.trustStore";
protected static int wiremockHttpPort;
protected static int wiremockHttpsPort;
private static String originalTrustStorePath;
protected static Process wiremockStandalone;
@BeforeAll
public static void setUpClass() {
assumeNotRunningOnJava8();
assumeNotRunningOnJava21();
assumeNotRunningOnGithubActionsMac(); // disabled until issue with access to localhost
// (https://github.com/snowflakedb/snowflake-jdbc/pull/1807#discussion_r1686229430) is fixed on
// github actions mac image. Ticket to enable when fixed: SNOW-1555950
originalTrustStorePath = systemGetProperty(TRUST_STORE_PROPERTY);
startWiremockStandAlone();
}
@AfterEach
public void tearDown() {
restoreTrustStorePathProperty();
resetWiremock();
HttpUtil.httpClient.clear();
}
@AfterAll
public static void tearDownClass() {
stopWiremockStandAlone();
}
protected static void startWiremockStandAlone() {
// retrying in case of fail in port bindings
await()
.alias("wait for wiremock responding")
.atMost(Duration.ofSeconds(10))
.until(
() -> {
try {
wiremockHttpPort = findFreePort();
wiremockHttpsPort = findFreePort();
wiremockStandalone =
new ProcessBuilder(
"java",
"-jar",
getWiremockStandAlonePath(),
"--root-dir",
System.getProperty("user.dir")
+ File.separator
+ WIREMOCK_HOME_DIR
+ File.separator,
"--enable-browser-proxying", // work as forward proxy
"--proxy-pass-through",
"false", // pass through only matched requests
"--port",
String.valueOf(wiremockHttpPort),
"--https-port",
String.valueOf(wiremockHttpsPort),
"--https-keystore",
getResourceURL("wiremock" + File.separator + "ca-cert.jks"),
"--ca-keystore",
getResourceURL("wiremock" + File.separator + "ca-cert.jks"))
.inheritIO()
.start();
waitForWiremock();
return true;
} catch (Exception e) {
logger.warn("Failed to start wiremock, retrying: ", e);
return false;
}
});
}
protected void resetWiremock() {
HttpPost postRequest;
postRequest = new HttpPost("http://" + WIREMOCK_HOST + ":" + getAdminPort() + "/__admin/reset");
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(postRequest)) {
assertEquals(200, response.getStatusLine().getStatusCode());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected void configureWiremock() {
WireMock.configureFor(WIREMOCK_HOST, wiremockHttpPort);
}
private static String getWiremockStandAlonePath() {
return System.getProperty("user.home") + WIREMOCK_M2_PATH;
}
private static void waitForWiremock() {
await()
.pollDelay(Duration.ofSeconds(1))
.atMost(Duration.ofSeconds(3))
.until(BaseWiremockTest::isWiremockResponding);
}
private static boolean isWiremockResponding() {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet request =
new HttpGet(
String.format("http://%s:%d/__admin/mappings", WIREMOCK_HOST, wiremockHttpPort));
CloseableHttpResponse response = httpClient.execute(request);
return response.getStatusLine().getStatusCode() == 200;
} catch (Exception e) {
logger.warn("Waiting for wiremock to respond: ", e);
}
return false;
}
protected static void stopWiremockStandAlone() {
if (wiremockStandalone != null) {
wiremockStandalone.destroyForcibly();
await()
.alias("stop wiremock")
.atMost(Duration.ofSeconds(10))
.until(() -> !wiremockStandalone.isAlive());
}
}
private static int findFreePort() {
try {
ServerSocket socket = new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
return port;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected Properties getProperties() {
Map<String, String> params = getConnectionParameters();
Properties props = new Properties();
props.put("host", params.get("host"));
props.put("port", params.get("port"));
props.put("account", params.get("account"));
props.put("user", params.get("user"));
props.put("role", params.get("role"));
props.put("password", params.get("password"));
props.put("warehouse", params.get("warehouse"));
props.put("db", params.get("database"));
props.put("ssl", params.get("ssl"));
props.put("insecureMode", true); // OCSP disabled for wiremock proxy tests
return props;
}
protected HttpPost createWiremockPostRequest(String body, String path) {
HttpPost postRequest = new HttpPost("http://" + WIREMOCK_HOST + ":" + getAdminPort() + path);
final StringEntity entity;
try {
entity = new StringEntity(body);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
postRequest.setEntity(entity);
postRequest.setHeader("Accept", "application/json");
postRequest.setHeader("Content-type", "application/json");
return postRequest;
}
protected static void restoreTrustStorePathProperty() {
if (originalTrustStorePath != null) {
System.setProperty(TRUST_STORE_PROPERTY, originalTrustStorePath);
} else {
System.clearProperty(TRUST_STORE_PROPERTY);
}
}
private int getAdminPort() {
return wiremockHttpPort;
}
private static String getResourceURL(String relativePath) {
return Paths.get(systemGetProperty("user.dir"), "src", "test", "resources", relativePath)
.toAbsolutePath()
.toString();
}
protected void setCustomTrustStorePropertyPath() {
System.setProperty(
TRUST_STORE_PROPERTY, getResourceURL("wiremock" + File.separator + "ca-cert.jks"));
}
protected void importMapping(String mappingImport) {
HttpPost request = createWiremockPostRequest(mappingImport, "/__admin/mappings/import");
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(request)) {
Assumptions.assumeTrue(response.getStatusLine().getStatusCode() == 200);
} catch (Exception e) {
logger.error("Importing mapping failed", e);
Assumptions.abort("Importing mapping failed");
}
}
protected void addMapping(String mapping) {
HttpPost postRequest = createWiremockPostRequest(mapping, "/__admin/mappings");
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(postRequest)) {
assertEquals(201, response.getStatusLine().getStatusCode());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected void importMappingFromResources(String relativePath) {
try (InputStream is =
new FileInputStream(String.valueOf(Paths.get("src/test/resources/", relativePath)))) {
String scenario = IOUtils.toString(Objects.requireNonNull(is), StandardCharsets.UTF_8);
importMapping(scenario);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}