10
10
*******************************************************************************/
11
11
package io .openliberty .sample .it ;
12
12
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 ;
15
15
16
16
import java .io .StringReader ;
17
- import java .net .URI ;
18
- import java .util .concurrent .TimeUnit ;
19
17
20
18
import org .junit .jupiter .api .AfterAll ;
21
19
import org .junit .jupiter .api .BeforeAll ;
25
23
import jakarta .json .JsonArray ;
26
24
import jakarta .json .JsonObject ;
27
25
import jakarta .json .JsonReader ;
26
+ import jakarta .json .JsonValue ;
28
27
import jakarta .ws .rs .client .Client ;
29
28
import jakarta .ws .rs .client .ClientBuilder ;
30
29
import jakarta .ws .rs .client .Entity ;
33
32
public class MongoIT {
34
33
35
34
private static Client restClient ;
36
-
37
-
38
35
private static String baseURL ;
39
36
40
37
@ BeforeAll
41
38
public static void setup () throws Exception {
42
39
String port = System .getProperty ("http.port" );
43
40
baseURL = "http://localhost:" + port + "/db/crew/" ;
44
-
41
+
45
42
restClient = ClientBuilder .newClient ();
46
43
}
47
44
@@ -56,12 +53,32 @@ public static void teardown() throws Exception {
56
53
*/
57
54
@ Test
58
55
public void CreateRetrieveDeleteTest () throws InterruptedException {
59
- //{"name":"Test","rank":"Captain","crewID":"12345"}
60
56
restClient .target (baseURL + "it" ).request ().post (Entity .json ("{\" name\" :\" Test\" ,\" rank\" :\" Captain\" ,\" crewID\" :\" 12345\" }" ));
61
57
62
58
Response response = restClient .target (baseURL ).request ().get ();
63
59
JsonReader reader = Json .createReader (new StringReader (response .readEntity (String .class )));
64
60
JsonArray array = reader .readArray ();
65
61
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
+ }
66
83
}
67
84
}
0 commit comments