Skip to content

Commit

Permalink
Add findByRank test
Browse files Browse the repository at this point in the history
  • Loading branch information
mswatosh committed Nov 20, 2023
1 parent 0bfb50a commit 0bc0bef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public String retrieveByRank(@PathParam("rank") String rank) {
}

@DELETE
@Path("/")
public void remove() {
crewMembers.deleteAll();
}
Expand Down
60 changes: 53 additions & 7 deletions src/test/java/io/openliberty/sample/it/CrewServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CrewServiceIT {
private Response response;

@BeforeAll
public static void init() {
public static void init() {
String port = System.getProperty("http.port");
baseURL = "http://localhost:" + port + "/";
}
Expand All @@ -52,6 +52,9 @@ public static void init() {
@BeforeEach
public void setup() {
client = ClientBuilder.newClient();
assumeTrue(isPostgresAvailable(), "Postgres is not Available");
response = client.target(baseURL + "db/crew/").request().delete(); //Delete All before each test
assertEquals(204, response.getStatus(), "output: " + response.readEntity(String.class));
}

@AfterEach
Expand All @@ -61,8 +64,7 @@ public void teardown() {

@Test
public void testAddGetDeleteCrewMember() {
assumeTrue(isPostgresAvailable(), "Postgres is not Available");


//Remove Existing
response = client.target(baseURL + "db/crew/75").request().delete();
assertEquals(204, response.getStatus(), "output: " + response.readEntity(String.class));
Expand Down Expand Up @@ -100,8 +102,6 @@ public void testAddGetDeleteCrewMember() {

@Test
public void testValidationCrewMember() {
assumeTrue(isPostgresAvailable(), "Postgres is not Available");

//Name Validation
response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"\",\"rank\":\"Captain\",\"crewID\":\"75\"}"));

Expand All @@ -128,11 +128,57 @@ public void testValidationCrewMember() {

}

private boolean isPostgresAvailable() {
/**
* Test findByRank, currently expects ordering to remain the same, which the spec doesn't require.
*/
@Test
public void testFindByRank() {
response = client.target(baseURL + "db/crew/it1").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Engineer\",\"crewID\":\"75\"}"));
assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class));
response = client.target(baseURL + "db/crew/it2").request().post(Entity.json("{\"name\":\"Jim\",\"rank\":\"Captain\",\"crewID\":\"64\"}"));
assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class));
response = client.target(baseURL + "db/crew/it3").request().post(Entity.json("{\"name\":\"Alex\",\"rank\":\"Engineer\",\"crewID\":\"15\"}"));
assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class));

//Check findByRank("Captain")
response = client.target(baseURL + "db/crew/rank/Captain").request().get();
JsonReader reader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonArray array = reader.readArray();
JsonArray expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Jim").add("CrewID", "64").build()).build();
assertEquals(expectedArray, array);

//Check findByRank("Engineer")
response = client.target(baseURL + "db/crew/rank/Engineer").request().get();
reader = Json.createReader(new StringReader(response.readEntity(String.class)));
array = reader.readArray();
expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Mark").add("CrewID", "75").build())
.add(Json.createObjectBuilder().add("Name", "Alex").add("CrewID", "15").build()).build();
assertEquals(expectedArray, array);

//Check findByRank("Officer")
response = client.target(baseURL + "db/crew/rank/Officer").request().get();
reader = Json.createReader(new StringReader(response.readEntity(String.class)));
array = reader.readArray();
expectedArray = Json.createArrayBuilder().build();
assertEquals(expectedArray, array);

//Check find

response = client.target(baseURL + "db/crew/75").request().delete();
assertEquals(204, response.getStatus(), "output: " + response.readEntity(String.class));
response = client.target(baseURL + "db/crew/64").request().delete();
assertEquals(204, response.getStatus(), "output: " + response.readEntity(String.class));
response = client.target(baseURL + "db/crew/15").request().delete();
assertEquals(204, response.getStatus(), "output: " + response.readEntity(String.class));

}


private static boolean isPostgresAvailable() {
return checkHostAndPort("localhost", 5432);
}

private boolean checkHostAndPort(String host, int port) {
private static boolean checkHostAndPort(String host, int port) {
try (Socket s = new Socket(host, port)) {
return true;
} catch (IOException e) {
Expand Down

0 comments on commit 0bc0bef

Please sign in to comment.