Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds findByRank IT #18

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.openliberty.sample.application;

import java.util.List;
import java.util.stream.Stream;

import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.DataRepository;
import jakarta.data.repository.OrderBy;
import jakarta.data.repository.Repository;

@Repository
public interface CrewMembers extends DataRepository<CrewMember, String> {
public interface CrewMembers extends BasicRepository<CrewMember, String> {

List<CrewMember> findByRank(String rank);

@OrderBy("name")
List<CrewMember> findAll();
Stream<CrewMember> findAll();

void deleteByCrewID(String crewID);

void save(CrewMember a);
}
12 changes: 8 additions & 4 deletions src/main/java/io/openliberty/sample/application/CrewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,21 @@ public String add(CrewMember crewMember) {

@DELETE
@Path("/{id}")
public String remove(@PathParam("id") String id) {
public void remove(@PathParam("id") String id) {
crewMembers.deleteByCrewID(id);
return "";
}

@GET
public String retrieve() {
JsonArrayBuilder jab = Json.createArrayBuilder();
for (CrewMember c : crewMembers.findAll()) {
crewMembers.findAll().forEach( c -> {
JsonObject json = Json.createObjectBuilder()
.add("Name", c.getName())
.add("CrewID", c.getCrewID())
.add("Rank",c.getRank()).build();
jab.add(json);

}
});
return jab.build().toString();
}

Expand All @@ -91,4 +90,9 @@ public String retrieveByRank(@PathParam("rank") String rank) {
}
return jab.build().toString();
}

@DELETE
public void remove() {
crewMembers.deleteAll();
}
}
66 changes: 56 additions & 10 deletions src/test/java/io/openliberty/sample/it/CrewServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.ws.rs.client.Client;
Expand All @@ -42,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 @@ -51,20 +52,22 @@ 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
public void teardown() {
client.close();
client.close();
}

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


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

//Check Add
response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Captain\",\"crewID\":\"75\"}"));
Expand All @@ -83,7 +86,7 @@ public void testAddGetDeleteCrewMember() {

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

//Confirm Delete
response = client.target(baseURL + "db/crew").request().get();
Expand All @@ -99,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 @@ -127,12 +128,57 @@ public void testValidationCrewMember() {

}

/**
* 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 boolean isPostgresAvailable() {
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