Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .fleet/run.json

This file was deleted.

Empty file removed .fleet/settings.json
Empty file.
18 changes: 18 additions & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.compile.on.save>all</netbeans.compile.on.save>
</properties>
</project-shared-configuration>
10 changes: 6 additions & 4 deletions src/main/java/net/dontcode/data/DataResource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.dontcode.data;

import com.mongodb.client.model.FindOneAndReplaceOptions;
import com.mongodb.client.model.ReturnDocument;
import io.quarkus.mongodb.MongoClientName;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
Expand Down Expand Up @@ -65,9 +67,9 @@ public Uni<Response> getEntity (@PathParam("entityName") String entityName, @Pat
@Path("/{entityName}/{entityId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> updateProject (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName, Document body) {
public Uni<Response> replaceEntity (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName, Document body) {
changeIdToObjectId(body);
Uni<Response> ret = getEntities(entityName, dbName).findOneAndReplace(new Document().append("_id", body.get("_id")), body).map(document -> {
Uni<Response> ret = getEntities(entityName, dbName).findOneAndReplace(new Document().append("_id", body.get("_id")), body, new FindOneAndReplaceOptions().upsert(false).returnDocument(ReturnDocument.AFTER)).map(document -> {
if( document != null) {
changeIdToString(document);
return Response.ok(document).build();
Expand All @@ -90,7 +92,7 @@ protected void changeIdToString(Document body) {
@Path("/{entityName}/{entityId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> deleteProject (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName) {
public Uni<Response> deleteEntity (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName) {
Uni<Response> ret = getEntities(entityName, dbName).findOneAndDelete(new Document().append("_id", new ObjectId(entityId))).map(document -> {
if( document != null) {
changeIdToString(document);
Expand All @@ -106,7 +108,7 @@ public Uni<Response> deleteProject (@PathParam("entityName") String entityName,
@Path("/{entityName}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> insertProject(Document body, @PathParam("entityName") String entityName, @HeaderParam("DbName") String dbName) {
public Uni<Response> insertEntity(Document body, @PathParam("entityName") String entityName, @HeaderParam("DbName") String dbName) {
//System.out.println("Received"+ body);
return getEntities(entityName, dbName).insertOne(body).map(result -> {
changeIdToString(body);
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/net/dontcode/data/DataResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public void testList () {
Document doc = new Document();
AtomicReference<Throwable> error = new AtomicReference<>();

doc.append("name","TestProject1").append("creation", new Date());
doc.append("name","TestData1").append("creation", new Date());
removeEntities(entityName);
removeEntities(otherEntityName);
getEntities(entityName).insertOne(doc).onFailure().invoke(throwable -> {
error.set(throwable);
}).await().atMost(Duration.ofSeconds(10));
doc.put("name","TestProject2");
doc.put("name","TestData2");
doc.remove("_id");
getEntities(entityName).insertOne(doc).onFailure().invoke(throwable -> {
error.set(throwable);
}).await().atMost(Duration.ofSeconds(10));
doc.put("name","OtherTestProject");
doc.put("name","OtherData");
doc.remove("_id");
getEntities(otherEntityName).insertOne(doc).onFailure().invoke(throwable -> {
error.set(throwable);
Expand All @@ -58,10 +58,10 @@ public void testList () {

Assertions.assertNull(isError, "Error writing test data to Mongo "+errorMessage);
given().accept(ContentType.JSON).when().get("/{entityName}",entityName).then().statusCode(HttpStatus.SC_OK)
.body("[0].name", Matchers.equalTo("TestProject1")).body( "[1].name", Matchers.equalTo("TestProject2") );
.body("[0].name", Matchers.equalTo("TestData1")).body( "[1].name", Matchers.equalTo("TestData2") );

given().accept(ContentType.JSON).when().get("/{entityName}",otherEntityName).then().statusCode(HttpStatus.SC_OK)
.body("[0].name", Matchers.equalTo("OtherTestProject") );
.body("[0].name", Matchers.equalTo("OtherData") );
}

@Test
Expand All @@ -70,15 +70,15 @@ public void testCreateAndRead () {

removeEntities(entityName);
Document resp = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"name\":\"PrjCreated1\"," +
"\"name\":\"DataCreated1\"," +
"\"creation\":\"2021-03-04\"" +
"}").when().post("/{entityName}", entityName).then().statusCode(HttpStatus.SC_OK)
.body("_id", Matchers.notNullValue() )
.and().extract().as(Document.class);


given().accept(ContentType.JSON).when().get("/{entityName}/{entityId}",entityName, resp.get("_id").toString()).then().statusCode(HttpStatus.SC_OK)
.body("name", Matchers.is("PrjCreated1"));
.body("name", Matchers.is("DataCreated1"));
}

@Test
Expand All @@ -94,27 +94,28 @@ public void testCompleteFlow () {

removeEntities(entityName);
Document created = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"name\":\"PrjCreated2\"," +
"\"name\":\"DataCreated2\"," +
"\"creation\":\"2021-05-05\"" +
"}").when().post("/{entityName}", entityName).then().statusCode(HttpStatus.SC_OK)
.body("_id", Matchers.notNullValue() )
.and().extract().as(Document.class);
String entityId = created.get("_id").toString();

given().accept(ContentType.JSON).when().get("/{entityName}/{entityId}", entityName, entityId).then().statusCode(HttpStatus.SC_OK)
.body("name", Matchers.is("PrjCreated2"));
.body("name", Matchers.is("DataCreated2"));

Document updated = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"_id\":\""+entityId+"\","+
"\"name\":\"PrjUpdated2\"," +
"\"name\":\"DataUpdated2\"," +
"\"creation\":\"2021-06-07\"" +
"}").when().put("/{entityName}/{entityId}",entityName, entityId).then().statusCode(HttpStatus.SC_OK)
.body("_id", Matchers.notNullValue() )
.and().extract().as(Document.class);
Assertions.assertEquals(entityId, updated.get("_id").toString());
Assertions.assertEquals("DataUpdated2", updated.get("name"));

given().accept(ContentType.JSON).when().get("/{entityName}/{entityId}",entityName, entityId).then().statusCode(HttpStatus.SC_OK)
.body("name", Matchers.is("PrjUpdated2"))
.body("name", Matchers.is("DataUpdated2"))
.body("_id", Matchers.equalTo(created.get("_id")));

given().accept(ContentType.JSON).when().delete("/{entityName}/{entityId}",entityName, entityId).then().statusCode(HttpStatus.SC_OK);
Expand Down
Loading