Skip to content

Commit 36c1613

Browse files
authored
Merge Fixed update data returning old values
Fixed update data returning old values
2 parents 8ade38d + e0f14aa commit 36c1613

5 files changed

Lines changed: 36 additions & 25 deletions

File tree

.fleet/run.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

.fleet/settings.json

Whitespace-only changes.

nb-configuration.xml

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

src/main/java/net/dontcode/data/DataResource.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.dontcode.data;
22

3+
import com.mongodb.client.model.FindOneAndReplaceOptions;
4+
import com.mongodb.client.model.ReturnDocument;
35
import io.quarkus.mongodb.MongoClientName;
46
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
57
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
@@ -65,9 +67,9 @@ public Uni<Response> getEntity (@PathParam("entityName") String entityName, @Pat
6567
@Path("/{entityName}/{entityId}")
6668
@Consumes(MediaType.APPLICATION_JSON)
6769
@Produces(MediaType.APPLICATION_JSON)
68-
public Uni<Response> updateProject (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName, Document body) {
70+
public Uni<Response> replaceEntity (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName, Document body) {
6971
changeIdToObjectId(body);
70-
Uni<Response> ret = getEntities(entityName, dbName).findOneAndReplace(new Document().append("_id", body.get("_id")), body).map(document -> {
72+
Uni<Response> ret = getEntities(entityName, dbName).findOneAndReplace(new Document().append("_id", body.get("_id")), body, new FindOneAndReplaceOptions().upsert(false).returnDocument(ReturnDocument.AFTER)).map(document -> {
7173
if( document != null) {
7274
changeIdToString(document);
7375
return Response.ok(document).build();
@@ -90,7 +92,7 @@ protected void changeIdToString(Document body) {
9092
@Path("/{entityName}/{entityId}")
9193
@Consumes(MediaType.APPLICATION_JSON)
9294
@Produces(MediaType.APPLICATION_JSON)
93-
public Uni<Response> deleteProject (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName) {
95+
public Uni<Response> deleteEntity (@PathParam("entityName") String entityName, @PathParam("entityId") String entityId, @HeaderParam("DbName") String dbName) {
9496
Uni<Response> ret = getEntities(entityName, dbName).findOneAndDelete(new Document().append("_id", new ObjectId(entityId))).map(document -> {
9597
if( document != null) {
9698
changeIdToString(document);
@@ -106,7 +108,7 @@ public Uni<Response> deleteProject (@PathParam("entityName") String entityName,
106108
@Path("/{entityName}")
107109
@Consumes(MediaType.APPLICATION_JSON)
108110
@Produces(MediaType.APPLICATION_JSON)
109-
public Uni<Response> insertProject(Document body, @PathParam("entityName") String entityName, @HeaderParam("DbName") String dbName) {
111+
public Uni<Response> insertEntity(Document body, @PathParam("entityName") String entityName, @HeaderParam("DbName") String dbName) {
110112
//System.out.println("Received"+ body);
111113
return getEntities(entityName, dbName).insertOne(body).map(result -> {
112114
changeIdToString(body);

src/test/java/net/dontcode/data/DataResourceTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public void testList () {
3636
Document doc = new Document();
3737
AtomicReference<Throwable> error = new AtomicReference<>();
3838

39-
doc.append("name","TestProject1").append("creation", new Date());
39+
doc.append("name","TestData1").append("creation", new Date());
4040
removeEntities(entityName);
4141
removeEntities(otherEntityName);
4242
getEntities(entityName).insertOne(doc).onFailure().invoke(throwable -> {
4343
error.set(throwable);
4444
}).await().atMost(Duration.ofSeconds(10));
45-
doc.put("name","TestProject2");
45+
doc.put("name","TestData2");
4646
doc.remove("_id");
4747
getEntities(entityName).insertOne(doc).onFailure().invoke(throwable -> {
4848
error.set(throwable);
4949
}).await().atMost(Duration.ofSeconds(10));
50-
doc.put("name","OtherTestProject");
50+
doc.put("name","OtherData");
5151
doc.remove("_id");
5252
getEntities(otherEntityName).insertOne(doc).onFailure().invoke(throwable -> {
5353
error.set(throwable);
@@ -58,10 +58,10 @@ public void testList () {
5858

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

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

6767
@Test
@@ -70,15 +70,15 @@ public void testCreateAndRead () {
7070

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

7979

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

8484
@Test
@@ -94,27 +94,28 @@ public void testCompleteFlow () {
9494

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

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

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

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

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

0 commit comments

Comments
 (0)