Skip to content

Commit

Permalink
Merge pull request #3 from mswatosh/actions
Browse files Browse the repository at this point in the history
Add actions and Create Retrieve and Delete test
  • Loading branch information
mswatosh authored Sep 16, 2024
2 parents 5f0279c + abea60f commit 3b85e95
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 20 deletions.
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily

- package-ecosystem: maven
directory: /
schedule:
interval: daily

- package-ecosystem: docker
directory: /mongo
schedule:
interval: daily
67 changes: 67 additions & 0 deletions .github/workflows/ci-pull.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/actions/automating-builds-and-tests/building-and-testing-java-with-maven

name: Java CI with Maven on Pull

on:
pull_request:
branches: [ "main" ]
push:
branches: [ "actions"] #Allows testing Actions changes in a fork
schedule:
- cron: "0 12 * * 1-5"

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest

services:
mongo:
#Update Readme to match
image: mongo:7.0.7
env:
MONGO_INITDB_ROOT_USERNAME: sampleUser
MONGO_INITDB_ROOT_PASSWORD: openliberty
# Set health checks to wait until mongo has started
options: >-
--health-cmd "echo 'db.runCommand("ping").ok' | mongosh --quiet"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 27017:27017

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up JDK 21
uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1
with:
java-version: 21
distribution: 'temurin'
cache: maven
- name: Compile App and Tests
run: |
mvn -B compiler:compile
mvn -B compiler:testCompile
- name: Create Server and Deploy App
run: |
mvn -B liberty:create
mvn -B liberty:deploy
- name: Start Server
run: mvn -B liberty:start
- name: Run Integration Tests
run: mvn -B failsafe:integration-test
- name: Stop Server
run: mvn -B liberty:stop
- name: Verify Integration Test Results
run: mvn -B failsafe:verify
- name: Archive Liberty logs
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: liberty-logs
path: target/liberty/wlp/usr/servers/MongoServer/logs/
retention-days: 1
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ git clone [email protected]:OpenLiberty/sample-mongodb.git
You will also need a MongoDB instance to use this sample. If you have Docker installed, you can use the following:

```
docker build -t liberty_mongo mongo
docker run --name liberty_mongo -d -p 27017:27017 liberty_mongo
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
```

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

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

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

```
mongosh
use testdb
db.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'testdb'}]})
db.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'admin'}]})
```

You should see the following:
```
{ ok: 1 }
```
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.
Now you are ready to run the sample. Type `exit` to get out of the mongo shell.

## Running the Sample
From inside the sample-mongodb directory, build and start the application in Open Liberty with the following command:
Expand Down
5 changes: 0 additions & 5 deletions mongo/Dockerfile

This file was deleted.

1 change: 0 additions & 1 deletion mongo/init.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MongoProducer {
int port;

@Inject
@ConfigProperty(name = "mongo.dbname", defaultValue = "testdb")
@ConfigProperty(name = "mongo.dbname")
String dbName;

@Inject
Expand Down
1 change: 1 addition & 0 deletions src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

<variable name="mongo.user" value="sampleUser"/>
<variable name="mongo.pass.encoded" value="{aes}APtt+/vYxxPa0jE1rhmZue9wBm3JGqFK3JR4oJdSDGWM1wLr1ckvqkqKjSB2Voty8g=="/>
<variable name="mongo.dbname" value="admin"/> <!-- Use admin to avoid configuring the container with users-->
</server>
33 changes: 25 additions & 8 deletions src/test/java/io/openliberty/sample/it/MongoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
*******************************************************************************/
package io.openliberty.sample.it;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.StringReader;
import java.net.URI;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -25,6 +23,7 @@
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
Expand All @@ -33,15 +32,13 @@
public class MongoIT {

private static Client restClient;


private static String baseURL;

@BeforeAll
public static void setup() throws Exception {
String port = System.getProperty("http.port");
baseURL = "http://localhost:" + port + "/db/crew/";

restClient = ClientBuilder.newClient();
}

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

Response response = restClient.target(baseURL).request().get();
JsonReader reader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonArray array = reader.readArray();
System.out.println(array);
String id = null;
for (JsonValue value : array) {
JsonObject obj = value.asJsonObject();
if (obj.getString("Name").equals("Test") &&
obj.getString("Rank").equals("Captain") &&
obj.getString("CrewID").equals("12345"))
id = obj.getJsonObject("_id").getString("$oid");
}
assertNotNull(id, "CrewMember not found in returned value: " + array);

restClient.target(baseURL + id).request().delete();

response = restClient.target(baseURL).request().get();
reader = Json.createReader(new StringReader(response.readEntity(String.class)));
array = reader.readArray();

for (JsonValue value : array) {
System.out.println(value.asJsonObject().getJsonObject("_id").getString("$oid"));
if (id == value.asJsonObject().getJsonObject("_id").getString("$oid"))
fail("CrewMember should have been deleted, but id was found: " + id);
}
}
}

0 comments on commit 3b85e95

Please sign in to comment.