Skip to content

Commit 3b85e95

Browse files
authored
Merge pull request #3 from mswatosh/actions
Add actions and Create Retrieve and Delete test
2 parents 5f0279c + abea60f commit 3b85e95

File tree

8 files changed

+114
-20
lines changed

8 files changed

+114
-20
lines changed

.github/dependabot.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: daily
7+
8+
- package-ecosystem: maven
9+
directory: /
10+
schedule:
11+
interval: daily
12+
13+
- package-ecosystem: docker
14+
directory: /mongo
15+
schedule:
16+
interval: daily

.github/workflows/ci-pull.yml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://docs.github.com/actions/automating-builds-and-tests/building-and-testing-java-with-maven
3+
4+
name: Java CI with Maven on Pull
5+
6+
on:
7+
pull_request:
8+
branches: [ "main" ]
9+
push:
10+
branches: [ "actions"] #Allows testing Actions changes in a fork
11+
schedule:
12+
- cron: "0 12 * * 1-5"
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
21+
services:
22+
mongo:
23+
#Update Readme to match
24+
image: mongo:7.0.7
25+
env:
26+
MONGO_INITDB_ROOT_USERNAME: sampleUser
27+
MONGO_INITDB_ROOT_PASSWORD: openliberty
28+
# Set health checks to wait until mongo has started
29+
options: >-
30+
--health-cmd "echo 'db.runCommand("ping").ok' | mongosh --quiet"
31+
--health-interval 10s
32+
--health-timeout 5s
33+
--health-retries 5
34+
ports:
35+
- 27017:27017
36+
37+
steps:
38+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
39+
- name: Set up JDK 21
40+
uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1
41+
with:
42+
java-version: 21
43+
distribution: 'temurin'
44+
cache: maven
45+
- name: Compile App and Tests
46+
run: |
47+
mvn -B compiler:compile
48+
mvn -B compiler:testCompile
49+
- name: Create Server and Deploy App
50+
run: |
51+
mvn -B liberty:create
52+
mvn -B liberty:deploy
53+
- name: Start Server
54+
run: mvn -B liberty:start
55+
- name: Run Integration Tests
56+
run: mvn -B failsafe:integration-test
57+
- name: Stop Server
58+
run: mvn -B liberty:stop
59+
- name: Verify Integration Test Results
60+
run: mvn -B failsafe:verify
61+
- name: Archive Liberty logs
62+
if: ${{ failure() }}
63+
uses: actions/upload-artifact@v4
64+
with:
65+
name: liberty-logs
66+
path: target/liberty/wlp/usr/servers/MongoServer/logs/
67+
retention-days: 1

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@ git clone [email protected]:OpenLiberty/sample-mongodb.git
1313
You will also need a MongoDB instance to use this sample. If you have Docker installed, you can use the following:
1414

1515
```
16-
docker build -t liberty_mongo mongo
17-
docker run --name liberty_mongo -d -p 27017:27017 liberty_mongo
16+
docker run -d --name liberty_mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=sampleUser -e MONGO_INITDB_ROOT_PASSWORD=openliberty mongo:7.0.7
1817
```
1918

2019
If you don't have Docker, you can install MongoDB manually from [mongodb.com](https://docs.mongodb.com/manual/administration/install-community/)
2120

2221
Next, you will need to create a user for authentication. Issue the following commands from the command line:
2322

24-
If you're using docker, you can skip this step. It is done for you by the `init.js` file.
23+
If you're using docker, you can skip this step.
2524

2625
```
2726
mongosh
2827
use testdb
29-
db.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'testdb'}]})
28+
db.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'admin'}]})
3029
```
3130

3231
You should see the following:
3332
```
3433
{ ok: 1 }
3534
```
36-
Now you are ready to run the sample. Type `exit` to get out of the mongo shell, and if using docker type `exit` again to exit the docker shell.
35+
Now you are ready to run the sample. Type `exit` to get out of the mongo shell.
3736

3837
## Running the Sample
3938
From inside the sample-mongodb directory, build and start the application in Open Liberty with the following command:

mongo/Dockerfile

-5
This file was deleted.

mongo/init.js

-1
This file was deleted.

src/main/java/io/openliberty/sample/mongo/MongoProducer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MongoProducer {
3737
int port;
3838

3939
@Inject
40-
@ConfigProperty(name = "mongo.dbname", defaultValue = "testdb")
40+
@ConfigProperty(name = "mongo.dbname")
4141
String dbName;
4242

4343
@Inject

src/main/liberty/config/server.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
<variable name="mongo.user" value="sampleUser"/>
1616
<variable name="mongo.pass.encoded" value="{aes}APtt+/vYxxPa0jE1rhmZue9wBm3JGqFK3JR4oJdSDGWM1wLr1ckvqkqKjSB2Voty8g=="/>
17+
<variable name="mongo.dbname" value="admin"/> <!-- Use admin to avoid configuring the container with users-->
1718
</server>

src/test/java/io/openliberty/sample/it/MongoIT.java

+25-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
*******************************************************************************/
1111
package io.openliberty.sample.it;
1212

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
import static org.junit.jupiter.api.Assertions.fail;
1515

1616
import java.io.StringReader;
17-
import java.net.URI;
18-
import java.util.concurrent.TimeUnit;
1917

2018
import org.junit.jupiter.api.AfterAll;
2119
import org.junit.jupiter.api.BeforeAll;
@@ -25,6 +23,7 @@
2523
import jakarta.json.JsonArray;
2624
import jakarta.json.JsonObject;
2725
import jakarta.json.JsonReader;
26+
import jakarta.json.JsonValue;
2827
import jakarta.ws.rs.client.Client;
2928
import jakarta.ws.rs.client.ClientBuilder;
3029
import jakarta.ws.rs.client.Entity;
@@ -33,15 +32,13 @@
3332
public class MongoIT {
3433

3534
private static Client restClient;
36-
37-
3835
private static String baseURL;
3936

4037
@BeforeAll
4138
public static void setup() throws Exception {
4239
String port = System.getProperty("http.port");
4340
baseURL = "http://localhost:" + port + "/db/crew/";
44-
41+
4542
restClient = ClientBuilder.newClient();
4643
}
4744

@@ -56,12 +53,32 @@ public static void teardown() throws Exception {
5653
*/
5754
@Test
5855
public void CreateRetrieveDeleteTest() throws InterruptedException {
59-
//{"name":"Test","rank":"Captain","crewID":"12345"}
6056
restClient.target(baseURL + "it").request().post(Entity.json("{\"name\":\"Test\",\"rank\":\"Captain\",\"crewID\":\"12345\"}"));
6157

6258
Response response = restClient.target(baseURL).request().get();
6359
JsonReader reader = Json.createReader(new StringReader(response.readEntity(String.class)));
6460
JsonArray array = reader.readArray();
6561
System.out.println(array);
62+
String id = null;
63+
for (JsonValue value : array) {
64+
JsonObject obj = value.asJsonObject();
65+
if (obj.getString("Name").equals("Test") &&
66+
obj.getString("Rank").equals("Captain") &&
67+
obj.getString("CrewID").equals("12345"))
68+
id = obj.getJsonObject("_id").getString("$oid");
69+
}
70+
assertNotNull(id, "CrewMember not found in returned value: " + array);
71+
72+
restClient.target(baseURL + id).request().delete();
73+
74+
response = restClient.target(baseURL).request().get();
75+
reader = Json.createReader(new StringReader(response.readEntity(String.class)));
76+
array = reader.readArray();
77+
78+
for (JsonValue value : array) {
79+
System.out.println(value.asJsonObject().getJsonObject("_id").getString("$oid"));
80+
if (id == value.asJsonObject().getJsonObject("_id").getString("$oid"))
81+
fail("CrewMember should have been deleted, but id was found: " + id);
82+
}
6683
}
6784
}

0 commit comments

Comments
 (0)