-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathReadmeIntegrationTest.java
More file actions
96 lines (85 loc) · 3.68 KB
/
ReadmeIntegrationTest.java
File metadata and controls
96 lines (85 loc) · 3.68 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
/**
* © Copyright IBM Corporation 2025. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ReadmeIntegrationTest {
@BeforeClass
void resetWiremockScenarios() throws Exception {
String wiremockUrl = System.getenv("WIREMOCK_URL");
Assert.assertNotNull(wiremockUrl, "WIREMOCK_URL environment variable should be set.");
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockUrl + "/__admin/scenarios/reset"))
.timeout(Duration.ofMinutes(1))
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString("{}"))
.build();
HttpResponse<Void> response = client.send(request, BodyHandlers.discarding());
Assert.assertEquals(response.statusCode(), 200, "Resetting wiremock scenarios should receive status 200.");
}
void runTest(Runnable test, String pathToExpectedOutputFile) throws Exception {
String expectedOutput = new String(
Files.readAllBytes(new File(pathToExpectedOutputFile).toPath()), StandardCharsets.UTF_8);
PrintStream originalStdOut = System.out;
String capturedOutput = null;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream capturedStdOut =
new PrintStream(new BufferedOutputStream(out), true, StandardCharsets.UTF_8)) {
System.setOut(capturedStdOut);
test.run();
capturedOutput = out.toString(StandardCharsets.UTF_8);
} finally {
System.setOut(originalStdOut);
}
Assert.assertEquals(capturedOutput, expectedOutput);
}
@Test
void createDbAndDocFirstTime() throws Exception {
runTest(() -> CreateDbAndDoc.main(new String[0]), "output/CreateDbAndDoc.txt");
}
@Test(dependsOnMethods = {"createDbAndDocFirstTime"})
void getDocumentFromOrdersDatabase() throws Exception {
runTest(() -> GetInfoFromExistingDatabase.main(new String[0]),
"output/GetInfoFromExistingDatabase.txt");
}
@Test(dependsOnMethods = {"getDocumentFromOrdersDatabase"})
void updateDocFirstTime() throws Exception {
runTest(() -> UpdateDoc.main(new String[0]), "output/UpdateDoc.txt");
}
@Test(dependsOnMethods = {"updateDocFirstTime"})
void updateDocSecondTime() throws Exception {
runTest(() -> UpdateDoc.main(new String[0]), "output/UpdateDoc2.txt");
}
@Test(dependsOnMethods = {"updateDocSecondTime"})
void deleteExistingDoc() throws Exception {
runTest(() -> DeleteDoc.main(new String[0]), "output/DeleteDoc.txt");
}
@Test(dependsOnMethods = {"deleteExistingDoc"})
void deleteNonExistantDoc() throws Exception {
runTest(() -> DeleteDoc.main(new String[0]), "output/DeleteDoc2.txt");
}
}