1010*******************************************************************************/
1111package io .openliberty .sample .it ;
1212
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 ;
1515
1616import java .io .StringReader ;
17- import java .net .URI ;
18- import java .util .concurrent .TimeUnit ;
1917
2018import org .junit .jupiter .api .AfterAll ;
2119import org .junit .jupiter .api .BeforeAll ;
2523import jakarta .json .JsonArray ;
2624import jakarta .json .JsonObject ;
2725import jakarta .json .JsonReader ;
26+ import jakarta .json .JsonValue ;
2827import jakarta .ws .rs .client .Client ;
2928import jakarta .ws .rs .client .ClientBuilder ;
3029import jakarta .ws .rs .client .Entity ;
3332public class MongoIT {
3433
3534 private static Client restClient ;
36-
37-
3835 private static String baseURL ;
3936
4037 @ BeforeAll
4138 public static void setup () throws Exception {
4239 String port = System .getProperty ("http.port" );
4340 baseURL = "http://localhost:" + port + "/db/crew/" ;
44-
41+
4542 restClient = ClientBuilder .newClient ();
4643 }
4744
@@ -56,12 +53,32 @@ public static void teardown() throws Exception {
5653 */
5754 @ Test
5855 public void CreateRetrieveDeleteTest () throws InterruptedException {
59- //{"name":"Test","rank":"Captain","crewID":"12345"}
6056 restClient .target (baseURL + "it" ).request ().post (Entity .json ("{\" name\" :\" Test\" ,\" rank\" :\" Captain\" ,\" crewID\" :\" 12345\" }" ));
6157
6258 Response response = restClient .target (baseURL ).request ().get ();
6359 JsonReader reader = Json .createReader (new StringReader (response .readEntity (String .class )));
6460 JsonArray array = reader .readArray ();
6561 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+ }
6683 }
6784}
0 commit comments