-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataResourceTest.java
More file actions
134 lines (110 loc) · 5.93 KB
/
DataResourceTest.java
File metadata and controls
134 lines (110 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package net.dontcode.data;
import io.quarkus.mongodb.MongoClientName;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.http.ContentType;
import org.apache.http.HttpStatus;
import org.bson.Document;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import jakarta.inject.Inject;
import java.time.Duration;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
import static io.restassured.RestAssured.given;
@QuarkusTest
@TestHTTPEndpoint(DataResource.class)
@TestProfile(MongoTestProfile.class)
public class DataResourceTest {
@Inject
@MongoClientName("data")
ReactiveMongoClient mongoClient;
@Test
public void testList () {
String entityName="TestEntity";
String otherEntityName="OtherTestEntity";
Document doc = new Document();
AtomicReference<Throwable> error = new AtomicReference<>();
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","TestData2");
doc.remove("_id");
getEntities(entityName).insertOne(doc).onFailure().invoke(throwable -> {
error.set(throwable);
}).await().atMost(Duration.ofSeconds(10));
doc.put("name","OtherData");
doc.remove("_id");
getEntities(otherEntityName).insertOne(doc).onFailure().invoke(throwable -> {
error.set(throwable);
}).await().atMost(Duration.ofSeconds(10));
Throwable isError = error.get();
String errorMessage=(isError!=null)? isError.getMessage() : "";
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("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("OtherData") );
}
@Test
public void testCreateAndRead () {
String entityName="TestEntityCreate";
removeEntities(entityName);
Document resp = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"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("DataCreated1"));
}
@Test
public void testNotFound () {
String entityName="TestEntityCreate";
removeEntities(entityName);
given().accept(ContentType.JSON).when().get("/{entityName}/{entityId}",entityName, "60b7b06ba5f1da79a15dc448").then().statusCode(HttpStatus.SC_NOT_FOUND);
}
@Test
public void testCompleteFlow () {
String entityName="TestCompleteFlow";
removeEntities(entityName);
Document created = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"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("DataCreated2"));
Document updated = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{" +
"\"_id\":\""+entityId+"\","+
"\"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("DataUpdated2"))
.body("_id", Matchers.equalTo(created.get("_id")));
given().accept(ContentType.JSON).when().delete("/{entityName}/{entityId}",entityName, entityId).then().statusCode(HttpStatus.SC_OK);
given().accept(ContentType.JSON).when().get("/{entityName}/{entityId}",entityName, entityId).then().statusCode(HttpStatus.SC_NOT_FOUND);
}
protected ReactiveMongoCollection<Document> getEntities(String entityName) {
return mongoClient.getDatabase("unitTestDataDb").getCollection(entityName);
}
protected void removeEntities(String entityName) {
if( mongoClient.getDatabase("unitTestDataDb").listCollectionNames().collect().asList().await().indefinitely().contains("data-"+entityName))
mongoClient.getDatabase("unitTestDataDb").getCollection(entityName).drop().await().indefinitely();
}
}