Skip to content

Commit c585745

Browse files
committed
test: readme example checks
1 parent 4261e00 commit c585745

9 files changed

Lines changed: 132 additions & 14 deletions

File tree

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"orders" database created.
2-
You have created the document:
2+
You have created the document. Response body:
33
{
4-
"_id": "example",
5-
"_rev": "1-1b403633540686aa32d013fda9041a5d",
6-
"joined": "2019-01-24T10:42:99.000Z",
7-
"name": "Bob Smith"
8-
}
4+
"id": "example",
5+
"rev": "1-1b403633540686aa32d013fda9041a5d",
6+
"ok": true
7+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
You have deleted the document.
1+
You have deleted the document.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cannot delete document because either "orders" database or the "example" document was not found.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Server Version: 2.1.1
1+
Server Version: 3.2.1
22
Document count in "orders" database is 1.
33
Document retrieved from database:
44
{
55
"_id": "example",
66
"_rev": "1-1b403633540686aa32d013fda9041a5d",
7-
"name": "Bob Smith",
8-
"joined": "2019-01-24T10:42:99.000Z"
9-
}
7+
"joined": "2019-01-24T10:42:59.000Z",
8+
"name": "Bob Smith"
9+
}

modules/examples/output/UpdateDoc.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ You have updated the document:
44
"_rev": "2-4e2178e85cffb32d38ba4e451f6ca376",
55
"address": "19 Front Street, Darlington, DL5 1TY",
66
"name": "Bob Smith"
7-
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You have updated the document:
2+
{
3+
"_id": "example",
4+
"_rev": "3-4f1787d7a0520f825bd36822d7be627a",
5+
"address": "19 Front Street, Darlington, DL5 1TY",
6+
"name": "Bob Smith"
7+
}

modules/examples/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<artifactId>slf4j-jdk14</artifactId>
3434
<version>${slf4j-version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.testng</groupId>
38+
<artifactId>testng</artifactId>
39+
<scope>test</scope>
40+
</dependency>
3641
</dependencies>
3742

3843
<build>
@@ -45,6 +50,16 @@
4550
<doclint>all,-missing</doclint>
4651
</configuration>
4752
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>${surefire-version}</version>
57+
<configuration>
58+
<environmentVariables>
59+
<IBM_CREDENTIALS_FILE>${project.parent.basedir}/cloudant_v1.env</IBM_CREDENTIALS_FILE>
60+
</environmentVariables>
61+
</configuration>
62+
</plugin>
4863
</plugins>
4964
</build>
5065
<developers>

modules/examples/src/main/java/CreateDbAndDoc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void main(String[] args) {
9494
// Keeping track of the revision number of the document object
9595
// is necessary for further UPDATE/DELETE operations:
9696
exampleDocument.setRev(createDocumentResponse.getRev());
97-
System.out.println("You have created the document:\n" +
98-
exampleDocument);
97+
System.out.println("You have created the document. Response body:\n" +
98+
createDocumentResponse);
9999
}
100100
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* © Copyright IBM Corporation 2025. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
import java.io.BufferedOutputStream;
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
17+
import java.io.PrintStream;
18+
import java.net.URI;
19+
import java.net.http.HttpClient;
20+
import java.net.http.HttpRequest;
21+
import java.net.http.HttpRequest.BodyPublishers;
22+
import java.net.http.HttpResponse;
23+
import java.net.http.HttpResponse.BodyHandlers;
24+
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
26+
import java.time.Duration;
27+
import org.testng.Assert;
28+
import org.testng.annotations.BeforeClass;
29+
import org.testng.annotations.Test;
30+
31+
public class ReadmeIntegrationTest {
32+
33+
@BeforeClass
34+
void resetWiremockScenarios() throws Exception {
35+
String wiremockUrl = System.getenv("WIREMOCK_URL");
36+
Assert.assertNotNull(wiremockUrl, "WIREMOCK_URL environment variable should be set.");
37+
HttpClient client = HttpClient.newHttpClient();
38+
HttpRequest request = HttpRequest.newBuilder()
39+
.uri(URI.create(wiremockUrl + "/__admin/scenarios/reset"))
40+
.timeout(Duration.ofMinutes(1))
41+
.header("Content-Type", "application/json")
42+
.POST(BodyPublishers.ofString("{}"))
43+
.build();
44+
HttpResponse<Void> response = client.send(request, BodyHandlers.discarding());
45+
Assert.assertEquals(response.statusCode(), 200, "Resetting wiremock scenarios should receive status 200.");
46+
}
47+
48+
void runTest(Runnable test, String pathToExpectedOutputFile) throws Exception {
49+
String expectedOutput = new String(
50+
Files.readAllBytes(new File(pathToExpectedOutputFile).toPath()), StandardCharsets.UTF_8);
51+
PrintStream originalStdOut = System.out;
52+
String capturedOutput = null;
53+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
54+
PrintStream capturedStdOut =
55+
new PrintStream(new BufferedOutputStream(out), true, StandardCharsets.UTF_8)) {
56+
System.setOut(capturedStdOut);
57+
test.run();
58+
capturedOutput = out.toString(StandardCharsets.UTF_8);
59+
} finally {
60+
System.setOut(originalStdOut);
61+
}
62+
Assert.assertEquals(capturedOutput, expectedOutput);
63+
}
64+
65+
@Test
66+
void createDbAndDocFirstTime() throws Exception {
67+
runTest(() -> CreateDbAndDoc.main(new String[0]), "output/CreateDbAndDoc.txt");
68+
}
69+
70+
@Test(dependsOnMethods = {"createDbAndDocFirstTime"})
71+
void getDocumentFromOrdersDatabase() throws Exception {
72+
runTest(() -> GetInfoFromExistingDatabase.main(new String[0]),
73+
"output/GetInfoFromExistingDatabase.txt");
74+
}
75+
76+
@Test(dependsOnMethods = {"getDocumentFromOrdersDatabase"})
77+
void updateDocFirstTime() throws Exception {
78+
runTest(() -> UpdateDoc.main(new String[0]), "output/UpdateDoc.txt");
79+
}
80+
81+
@Test(dependsOnMethods = {"updateDocFirstTime"})
82+
void updateDocSecondTime() throws Exception {
83+
runTest(() -> UpdateDoc.main(new String[0]), "output/UpdateDoc2.txt");
84+
}
85+
86+
@Test(dependsOnMethods = {"updateDocSecondTime"})
87+
void deleteExistingDoc() throws Exception {
88+
runTest(() -> DeleteDoc.main(new String[0]), "output/DeleteDoc.txt");
89+
}
90+
91+
@Test(dependsOnMethods = {"deleteExistingDoc"})
92+
void deleteNonExistantDoc() throws Exception {
93+
runTest(() -> DeleteDoc.main(new String[0]), "output/DeleteDoc2.txt");
94+
}
95+
96+
}

0 commit comments

Comments
 (0)