From 2b8776db1e9a0328e528f233da58f7d04ba83678 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Thu, 30 Nov 2023 14:39:49 -0600 Subject: [PATCH 1/8] Revert "Revert "Ship"" This reverts commit adb4de34fe38789f1b6ee6753f67000a5b88bf85. --- .../sample/application/CrewMember.java | 6 +++- .../sample/application/CrewService.java | 3 +- .../sample/application/RankDeserializer.java | 3 +- .../openliberty/sample/application/Ship.java | 22 +++++++++++++ .../sample/application/ShipDeserializer.java | 26 +++++++++++++++ src/main/webapp/index.html | 8 +++++ src/main/webapp/js/app.js | 1 + src/main/webapp/js/find-all.js | 3 +- src/main/webapp/js/find-by-rank.js | 3 +- .../openliberty/sample/it/CrewServiceIT.java | 33 ++++++++++++------- 10 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 src/main/java/io/openliberty/sample/application/Ship.java create mode 100644 src/main/java/io/openliberty/sample/application/ShipDeserializer.java diff --git a/src/main/java/io/openliberty/sample/application/CrewMember.java b/src/main/java/io/openliberty/sample/application/CrewMember.java index 4a91e60..6c12caa 100644 --- a/src/main/java/io/openliberty/sample/application/CrewMember.java +++ b/src/main/java/io/openliberty/sample/application/CrewMember.java @@ -30,8 +30,12 @@ public class CrewMember { @Positive(message = "ID Number must be a non-negative integer!") public int crewID; + @NotNull(message = "Crew member must be assigned to one of the listed ships!") + public Ship ship; + + public String toString() { - return "Name: " + name + "
CrewID: " + crewID + "
Rank: " + rank; + return "Name: " + name + "
CrewID: " + crewID + "
Rank: " + rank + ship.name != null ? "
Ship: " + ship.name : ""; } diff --git a/src/main/java/io/openliberty/sample/application/CrewService.java b/src/main/java/io/openliberty/sample/application/CrewService.java index 52dbb48..75c74ba 100755 --- a/src/main/java/io/openliberty/sample/application/CrewService.java +++ b/src/main/java/io/openliberty/sample/application/CrewService.java @@ -108,7 +108,8 @@ private String crewMembersToJsonArray(Iterable crewMembers) { JsonObject json = Json.createObjectBuilder() .add("Name", c.name) .add("CrewID", c.crewID) - .add("Rank",c.rank.toString()).build(); + .add("Rank",c.rank.toString()) + .add("Ship", c.ship.name).build(); jab.add(json); } return jab.build().toString(); diff --git a/src/main/java/io/openliberty/sample/application/RankDeserializer.java b/src/main/java/io/openliberty/sample/application/RankDeserializer.java index 6d68f1e..947451d 100644 --- a/src/main/java/io/openliberty/sample/application/RankDeserializer.java +++ b/src/main/java/io/openliberty/sample/application/RankDeserializer.java @@ -11,8 +11,7 @@ public class RankDeserializer implements JsonbDeserializer { @Override public Rank deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { - String name = parser.getValue().toString().replaceAll("\"", ""); - return Rank.fromString(name); + return Rank.fromString(parser.getString()); } } \ No newline at end of file diff --git a/src/main/java/io/openliberty/sample/application/Ship.java b/src/main/java/io/openliberty/sample/application/Ship.java new file mode 100644 index 0000000..56bfbfb --- /dev/null +++ b/src/main/java/io/openliberty/sample/application/Ship.java @@ -0,0 +1,22 @@ +package io.openliberty.sample.application; + +import java.io.Serializable; + +import jakarta.json.bind.annotation.JsonbTypeDeserializer; + +@JsonbTypeDeserializer(ShipDeserializer.class) +public class Ship implements Serializable { + public static enum Size { + small, large + } + + public String name; + + public Size size; + + Ship(String name, Size size) { + this.name = name; + this.size = size; + } + +} diff --git a/src/main/java/io/openliberty/sample/application/ShipDeserializer.java b/src/main/java/io/openliberty/sample/application/ShipDeserializer.java new file mode 100644 index 0000000..02e0205 --- /dev/null +++ b/src/main/java/io/openliberty/sample/application/ShipDeserializer.java @@ -0,0 +1,26 @@ +package io.openliberty.sample.application; + +import java.lang.reflect.Type; + +import jakarta.json.bind.serializer.DeserializationContext; +import jakarta.json.bind.serializer.JsonbDeserializer; +import jakarta.json.stream.JsonParser; + +public class ShipDeserializer implements JsonbDeserializer { + + @Override + public Ship deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { + String shipName = parser.getString(); + if (shipName.equals("Liberty Saucer")) { + return new Ship(shipName, Ship.Size.small); + } else if (shipName.equals("Jakarta Sailboat")) { + return new Ship(shipName, Ship.Size.small); + } else if (shipName.equals("WebSphere Battleship")) { + return new Ship(shipName, Ship.Size.large); + } else { + return null; + } + + } + +} diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 23cd342..781b34c 100755 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -54,6 +54,14 @@

Congrats on your shiny, new Open Liberty sample app!

+
+ Ship Assignment + +
diff --git a/src/main/webapp/js/app.js b/src/main/webapp/js/app.js index 5442809..a2b850d 100644 --- a/src/main/webapp/js/app.js +++ b/src/main/webapp/js/app.js @@ -16,6 +16,7 @@ async function addCrewMember() { var rank = document.getElementById("crewMemberRank"); crewMember.rank = rank.options[rank.selectedIndex].text; crewMember.crewID = document.getElementById("crewMemberID").value; + crewMember.ship = document.getElementById("crewMemberShip").value const response = await fetch("db/crew/"+crewMember.crewID, { diff --git a/src/main/webapp/js/find-all.js b/src/main/webapp/js/find-all.js index 957f86a..192ecdc 100644 --- a/src/main/webapp/js/find-all.js +++ b/src/main/webapp/js/find-all.js @@ -21,7 +21,8 @@ async function refreshFindAll() { function addToCrewMembers(entry){ var userHtml = "
Name: " + entry.Name + "
" + "
ID: " + entry.CrewID + "
" + - "
Rank: " + entry.Rank + "
"; + "
Rank: " + entry.Rank + "
" + + "
Ship: " + entry.Ship + "
"; var userDiv = document.createElement("div"); userDiv.setAttribute("class","user flexbox"); diff --git a/src/main/webapp/js/find-by-rank.js b/src/main/webapp/js/find-by-rank.js index dd2602f..798ef01 100644 --- a/src/main/webapp/js/find-by-rank.js +++ b/src/main/webapp/js/find-by-rank.js @@ -35,7 +35,8 @@ async function parseFindByRank(rank, response) { function addToCrewMembersByRank(entry, rank) { var userHtml = "
Name: " + entry.Name + "
" + - "
ID: " + entry.CrewID + "
"; + "
ID: " + entry.CrewID + "
" + + "
Ship: " + entry.Ship + "
"; var userDiv = document.createElement("div"); userDiv.setAttribute("class","user flexbox"); diff --git a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java index 134e599..ae6f128 100644 --- a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java +++ b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java @@ -71,13 +71,13 @@ public void testAddGetDeleteCrewMember() { 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\"}")); + response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Captain\",\"crewID\":\"75\",\"ship\":\"Liberty Saucer\"}")); assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class)); //Check Get response = client.target(baseURL + "db/crew").request().get(); JsonReader reader = Json.createReader(new StringReader(response.readEntity(String.class))); - JsonObject expectedObject = Json.createObjectBuilder().add("Name", "Mark").add("CrewID", 75).add("Rank", "Captain").build(); + JsonObject expectedObject = Json.createObjectBuilder().add("Name", "Mark").add("CrewID", 75).add("Rank", "Captain").add("Ship", "Liberty Saucer").build(); boolean found = false; for (JsonValue value : reader.readArray()) { @@ -92,7 +92,7 @@ public void testAddGetDeleteCrewMember() { //Confirm Delete response = client.target(baseURL + "db/crew").request().get(); reader = Json.createReader(new StringReader(response.readEntity(String.class))); - expectedObject = Json.createObjectBuilder().add("Name", "Mark").add("CrewID", 75).add("Rank", "Captain").build(); + expectedObject = Json.createObjectBuilder().add("Name", "Mark").add("CrewID", 75).add("Rank", "Captain").add("Ship", "Liberty Saucer").build(); found = false; for (JsonValue value : reader.readArray()) { @@ -104,7 +104,7 @@ public void testAddGetDeleteCrewMember() { @Test public void testValidationCrewMember() { //Name Validation - response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"\",\"rank\":\"Captain\",\"crewID\":\"75\"}")); + response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"\",\"rank\":\"Captain\",\"crewID\":\"75\",\"ship\":\"Liberty Saucer\"}")); JsonReader reader = Json.createReader(new StringReader(response.readEntity(String.class))); JsonArray array = reader.readArray(); @@ -112,7 +112,7 @@ public void testValidationCrewMember() { assertEquals("\"All crew members must have a name!\"", array.get(0).toString()); //Rank Validation - response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Scientist\",\"crewID\":\"75\"}")); + response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Scientist\",\"crewID\":\"75\",\"ship\":\"Liberty Saucer\"}")); reader = Json.createReader(new StringReader(response.readEntity(String.class))); array = reader.readArray(); @@ -120,13 +120,22 @@ public void testValidationCrewMember() { assertEquals("\"Crew member must be one of the listed ranks!\"", array.get(0).toString()); //CrewID Validation - response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Captain\",\"crewID\":\"-1\"}")); + response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Captain\",\"crewID\":\"-1\",\"ship\":\"Liberty Saucer\"}")); reader = Json.createReader(new StringReader(response.readEntity(String.class))); array = reader.readArray(); assertEquals(1, array.size(), "Validation array should have only contained 1 message"); assertEquals("\"ID Number must be a non-negative integer!\"", array.get(0).toString()); + //Ship Validation + response = client.target(baseURL + "db/crew/it").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Captain\",\"crewID\":\"75\",\"ship\":\"Quarkus Speedboat\"}")); + + reader = Json.createReader(new StringReader(response.readEntity(String.class))); + array = reader.readArray(); + assertEquals(1, array.size(), "Validation array should have only contained 1 message"); + assertEquals("\"Crew member must be assigned to one of the listed ships!\"", array.get(0).toString()); + + } /** @@ -134,26 +143,26 @@ public void testValidationCrewMember() { */ @Test public void testFindByRank() { - response = client.target(baseURL + "db/crew/it1").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Engineer\",\"crewID\":75}")); + response = client.target(baseURL + "db/crew/it1").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Engineer\",\"crewID\":75,\"ship\":\"Liberty Saucer\"}")); 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}")); + response = client.target(baseURL + "db/crew/it2").request().post(Entity.json("{\"name\":\"Jim\",\"rank\":\"Captain\",\"crewID\":64,\"ship\":\"Liberty Saucer\"}")); 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}")); + response = client.target(baseURL + "db/crew/it3").request().post(Entity.json("{\"name\":\"Alex\",\"rank\":\"Engineer\",\"crewID\":15,\"ship\":\"Jakarta Sailboat\"}")); 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).add("Rank", "Captain").build()).build(); + JsonArray expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Jim").add("CrewID", 64.add("Rank", "Captain").add("Ship", "Liberty Saucer")).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).add("Rank", "Engineer").build()) - .add(Json.createObjectBuilder().add("Name", "Alex").add("CrewID", 15).add("Rank", "Engineer").build()).build(); + expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Mark").add("CrewID", 75).add("Rank", "Engineer").add("Ship", "Liberty Saucer").build()) + .add(Json.createObjectBuilder().add("Name", "Alex").add("CrewID", 15).add("Rank", "Engineer").add("Ship", "Jakarta Sailboat").build()).build(); assertEquals(expectedArray, array); //Check findByRank("Officer") From 5bf4255ecff83fa036e9da1c82973030f99d16de Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Thu, 30 Nov 2023 11:39:48 -0600 Subject: [PATCH 2/8] findByShipSizeAndRank started, cannot find ShipSize --- .../sample/application/CrewMembers.java | 2 ++ .../sample/application/CrewService.java | 14 ++++++++++++++ src/main/webapp/css/app.css | 2 +- src/main/webapp/index.html | 16 ++++++++++++++++ src/main/webapp/js/app.js | 8 +++++++- src/main/webapp/js/find-by-ship-size-and-rank.js | 15 +++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/webapp/js/find-by-ship-size-and-rank.js diff --git a/src/main/java/io/openliberty/sample/application/CrewMembers.java b/src/main/java/io/openliberty/sample/application/CrewMembers.java index 934d08f..d1a3a3e 100644 --- a/src/main/java/io/openliberty/sample/application/CrewMembers.java +++ b/src/main/java/io/openliberty/sample/application/CrewMembers.java @@ -19,6 +19,8 @@ public interface CrewMembers extends DataRepository { List findByRank(Rank rank); Page findByRank(Rank rank, Pageable pageRequest); + + List findByShipSizeAndRank(Ship.Size size, Rank rank); @OrderBy("name") Stream findAll(); diff --git a/src/main/java/io/openliberty/sample/application/CrewService.java b/src/main/java/io/openliberty/sample/application/CrewService.java index 75c74ba..b63a6df 100755 --- a/src/main/java/io/openliberty/sample/application/CrewService.java +++ b/src/main/java/io/openliberty/sample/application/CrewService.java @@ -97,6 +97,20 @@ public String retrieveByRank(@PathParam("rank") String rank, return crewMembersToJsonArray(page); } + @GET + @Path("/shipSize/{shipSize}/rank/{rank}") + public String retrieveByShipSizeAndRank(@PathParam("shipSize") Ship.Size size, @PathParam("rank") String rank) { + JsonArrayBuilder jab = Json.createArrayBuilder(); + for (CrewMember c : crewMembers.findByShipSizeAndRank(size, Rank.fromString(rank))) { + JsonObject json = Json.createObjectBuilder() + .add("Name", c.getName()) + .add("CrewID", c.getCrewID()) + .add("Ship", c.getShip().name).build(); + jab.add(json); + } + return jab.build().toString(); + } + @DELETE public void remove() { crewMembers.deleteAll(); diff --git a/src/main/webapp/css/app.css b/src/main/webapp/css/app.css index 6c93a3a..1f3f7aa 100644 --- a/src/main/webapp/css/app.css +++ b/src/main/webapp/css/app.css @@ -189,7 +189,7 @@ button:hover { justify-content: space-between; } -#findByRank, #findAll { +.queryContainer { background-color: white; margin: 1em; padding: .5em; diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 781b34c..b28e30f 100755 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -20,6 +20,7 @@ Jakarta Data Sample + @@ -70,6 +71,7 @@

Congrats on your shiny, new Open Liberty sample app!

+
@@ -86,6 +88,20 @@

Congrats on your shiny, new Open Liberty sample app!

+
+ Crew Members by Ship Size and Rank
CrewMembers.findByShipSizeAndRank()
+
+ + +
+
diff --git a/src/main/webapp/js/app.js b/src/main/webapp/js/app.js index a2b850d..51de0dd 100644 --- a/src/main/webapp/js/app.js +++ b/src/main/webapp/js/app.js @@ -66,7 +66,13 @@ async function refreshDisplay() { if (document.getElementById("findAll").style.display == 'flex') refreshFindAll(); if (document.getElementById("findByRank").style.display == 'flex') refreshFindByRank(); - + if (document.getElementById("findByShipSizeAndRank").style.display == 'flex') { + var shipSizeSelector = document.getElementById("shipSizeSelector"); + var shipSize = shipSizeSelector.options[shipSizeSelector.selectedIndex].text; + var rankSelector = document.getElementById("rankSelector"); + var rank = rankSelector.options[rankSelector.selectedIndex].text; + refreshFindByShipSizeAndRank(shipSize, rank); + } } function clearDisplay(){ diff --git a/src/main/webapp/js/find-by-ship-size-and-rank.js b/src/main/webapp/js/find-by-ship-size-and-rank.js new file mode 100644 index 0000000..ebc836d --- /dev/null +++ b/src/main/webapp/js/find-by-ship-size-and-rank.js @@ -0,0 +1,15 @@ +/******************************************************************************* +* Copyright (c) 2023 IBM Corporation and others. +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License v1.0 +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v10.html +* +* Contributors: +* IBM Corporation - initial API and implementation +*******************************************************************************/ + +async function refreshFindByShipSizeAndRank(shipSize, rank) { + response = await fetch("db/crew/shipSize/" +shipSize+"/rank/" +rank); + +} \ No newline at end of file From 30ff56c9e6f57fe7f419064f915a9585fc50edd4 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Thu, 30 Nov 2023 11:51:44 -0600 Subject: [PATCH 3/8] ship name matching --- src/main/webapp/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index b28e30f..f40d84e 100755 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -60,7 +60,7 @@

Congrats on your shiny, new Open Liberty sample app!

From bc2dda19e9b33cb6e6b2955667878e653384e8bf Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Thu, 30 Nov 2023 14:14:43 -0600 Subject: [PATCH 4/8] Ship being found --- .../java/io/openliberty/sample/application/CrewMember.java | 2 ++ .../java/io/openliberty/sample/application/CrewService.java | 2 +- src/main/java/io/openliberty/sample/application/Ship.java | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/sample/application/CrewMember.java b/src/main/java/io/openliberty/sample/application/CrewMember.java index 6c12caa..c60fdd1 100644 --- a/src/main/java/io/openliberty/sample/application/CrewMember.java +++ b/src/main/java/io/openliberty/sample/application/CrewMember.java @@ -11,6 +11,7 @@ package io.openliberty.sample.application; +import jakarta.persistence.Embedded; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.validation.constraints.NotEmpty; @@ -30,6 +31,7 @@ public class CrewMember { @Positive(message = "ID Number must be a non-negative integer!") public int crewID; + @Embedded @NotNull(message = "Crew member must be assigned to one of the listed ships!") public Ship ship; diff --git a/src/main/java/io/openliberty/sample/application/CrewService.java b/src/main/java/io/openliberty/sample/application/CrewService.java index b63a6df..07c8289 100755 --- a/src/main/java/io/openliberty/sample/application/CrewService.java +++ b/src/main/java/io/openliberty/sample/application/CrewService.java @@ -97,7 +97,7 @@ public String retrieveByRank(@PathParam("rank") String rank, return crewMembersToJsonArray(page); } - @GET + @GET @Path("/shipSize/{shipSize}/rank/{rank}") public String retrieveByShipSizeAndRank(@PathParam("shipSize") Ship.Size size, @PathParam("rank") String rank) { JsonArrayBuilder jab = Json.createArrayBuilder(); diff --git a/src/main/java/io/openliberty/sample/application/Ship.java b/src/main/java/io/openliberty/sample/application/Ship.java index 56bfbfb..ae3ae25 100644 --- a/src/main/java/io/openliberty/sample/application/Ship.java +++ b/src/main/java/io/openliberty/sample/application/Ship.java @@ -3,7 +3,9 @@ import java.io.Serializable; import jakarta.json.bind.annotation.JsonbTypeDeserializer; +import jakarta.persistence.Embeddable; +@Embeddable @JsonbTypeDeserializer(ShipDeserializer.class) public class Ship implements Serializable { public static enum Size { @@ -14,6 +16,10 @@ public static enum Size { public Size size; + Ship() { + + } + Ship(String name, Size size) { this.name = name; this.size = size; From a78c39581c59aeb80295c41ceef6378a3048b778 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Thu, 30 Nov 2023 14:38:53 -0600 Subject: [PATCH 5/8] Ship updates --- .../io/openliberty/sample/application/CrewMember.java | 2 +- .../io/openliberty/sample/application/CrewService.java | 8 ++++---- src/main/java/io/openliberty/sample/application/Ship.java | 8 +++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/openliberty/sample/application/CrewMember.java b/src/main/java/io/openliberty/sample/application/CrewMember.java index c60fdd1..ee2f2f4 100644 --- a/src/main/java/io/openliberty/sample/application/CrewMember.java +++ b/src/main/java/io/openliberty/sample/application/CrewMember.java @@ -37,7 +37,7 @@ public class CrewMember { public String toString() { - return "Name: " + name + "
CrewID: " + crewID + "
Rank: " + rank + ship.name != null ? "
Ship: " + ship.name : ""; + return "Name: " + name + "
CrewID: " + crewID + "
Rank: " + rank + ship.shipName != null ? "
Ship: " + ship.shipName : ""; } diff --git a/src/main/java/io/openliberty/sample/application/CrewService.java b/src/main/java/io/openliberty/sample/application/CrewService.java index 07c8289..3f7dfe6 100755 --- a/src/main/java/io/openliberty/sample/application/CrewService.java +++ b/src/main/java/io/openliberty/sample/application/CrewService.java @@ -103,9 +103,9 @@ public String retrieveByShipSizeAndRank(@PathParam("shipSize") Ship.Size size, @ JsonArrayBuilder jab = Json.createArrayBuilder(); for (CrewMember c : crewMembers.findByShipSizeAndRank(size, Rank.fromString(rank))) { JsonObject json = Json.createObjectBuilder() - .add("Name", c.getName()) - .add("CrewID", c.getCrewID()) - .add("Ship", c.getShip().name).build(); + .add("Name", c.name) + .add("CrewID", c.crewID) + .add("Ship", c.ship.shipName).build(); jab.add(json); } return jab.build().toString(); @@ -123,7 +123,7 @@ private String crewMembersToJsonArray(Iterable crewMembers) { .add("Name", c.name) .add("CrewID", c.crewID) .add("Rank",c.rank.toString()) - .add("Ship", c.ship.name).build(); + .add("Ship", c.ship.shipName).build(); jab.add(json); } return jab.build().toString(); diff --git a/src/main/java/io/openliberty/sample/application/Ship.java b/src/main/java/io/openliberty/sample/application/Ship.java index ae3ae25..4497bc2 100644 --- a/src/main/java/io/openliberty/sample/application/Ship.java +++ b/src/main/java/io/openliberty/sample/application/Ship.java @@ -1,18 +1,16 @@ package io.openliberty.sample.application; -import java.io.Serializable; - import jakarta.json.bind.annotation.JsonbTypeDeserializer; import jakarta.persistence.Embeddable; @Embeddable @JsonbTypeDeserializer(ShipDeserializer.class) -public class Ship implements Serializable { +public class Ship { public static enum Size { small, large } - public String name; + public String shipName; public Size size; @@ -21,7 +19,7 @@ public static enum Size { } Ship(String name, Size size) { - this.name = name; + this.shipName = name; this.size = size; } From e2ac55f15694f800c45d256e6439e14315439a55 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Mon, 11 Dec 2023 10:18:39 -0600 Subject: [PATCH 6/8] Upgrade to Java 17, adding FindByRank IT --- pom.xml | 4 +-- .../sample/application/CrewMembers.java | 10 ++++++ .../openliberty/sample/it/CrewServiceIT.java | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 19b4b10..27409be 100644 --- a/pom.xml +++ b/pom.xml @@ -11,8 +11,8 @@ UTF-8 UTF-8 - 1.8 - 1.8 + 17 + 17 3.10 diff --git a/src/main/java/io/openliberty/sample/application/CrewMembers.java b/src/main/java/io/openliberty/sample/application/CrewMembers.java index d1a3a3e..6d50c82 100644 --- a/src/main/java/io/openliberty/sample/application/CrewMembers.java +++ b/src/main/java/io/openliberty/sample/application/CrewMembers.java @@ -1,3 +1,13 @@ +/******************************************************************************* +* Copyright (c) 2023 IBM Corporation and others. +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License v1.0 +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v10.html +* +* Contributors: +* IBM Corporation - initial API and implementation +*******************************************************************************/ package io.openliberty.sample.application; import java.util.List; diff --git a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java index ae6f128..209e1a9 100644 --- a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java +++ b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java @@ -183,6 +183,41 @@ public void testFindByRank() { } + /** + * Test findByShipSizeAndRank, currently expects ordering to remain the same, which the spec doesn't require. + */ + @Test + public void testFindByShipSizeAndRank() { + response = client.target(baseURL + "db/crew/it1").request().post(Entity.json("{\"name\":\"Mark\",\"rank\":\"Engineer\",\"crewID\":75,\"ship\":\"Liberty Saucer\"}")); + 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,\"ship\":\"Liberty Saucer\"}")); + 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,\"ship\":\"WebSphere Battleship\"}")); + assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class)); + response = client.target(baseURL + "db/crew/it4").request().post(Entity.json("{\"name\":\"Nathan\",\"rank\":\"Captain\",\"crewID\":30,\"ship\":\"Jakarta Sailboat\"}")); + assertEquals(200, response.getStatus(), "output: " + response.readEntity(String.class)); + + //Check findByShipSizeAndRank + response = client.target(baseURL + "db/crew/shipSize/small/rank/Engineer").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", "Mark").add("CrewID", 75).add("Ship", "Liberty Saucer").build()).build(); + assertEquals(expectedArray, array); + + response = client.target(baseURL + "db/crew/shipSize/large/rank/Engineer").request().get(); + reader = Json.createReader(new StringReader(response.readEntity(String.class))); + array = reader.readArray(); + expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Alex").add("CrewID", 15).add("Ship", "WebSphere Battleship").build()).build(); + assertEquals(expectedArray, array); + + response = client.target(baseURL + "db/crew/shipSize/small/rank/Captain").request().get(); + reader = Json.createReader(new StringReader(response.readEntity(String.class))); + array = reader.readArray(); + expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Jim").add("CrewID", 64).add("Ship", "Liberty Saucer").build()) + .add(Json.createObjectBuilder().add("Name", "Nathan").add("CrewID", 30).add("Ship", "Jakarta Sailboat").build()).build(); + assertEquals(expectedArray, array); + } + private static boolean isPostgresAvailable() { return checkHostAndPort("localhost", 5432); From b4933e62e46d02a45cf46077b90caddbed5b8684 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Mon, 11 Dec 2023 13:07:37 -0600 Subject: [PATCH 7/8] FindByShipAndRank working --- src/main/webapp/index.html | 5 +++-- src/main/webapp/js/app.js | 2 +- .../webapp/js/find-by-ship-size-and-rank.js | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index f40d84e..88c18dc 100755 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -91,16 +91,17 @@

Congrats on your shiny, new Open Liberty sample app!

Crew Members by Ship Size and Rank
CrewMembers.findByShipSizeAndRank()
- -
+
diff --git a/src/main/webapp/js/app.js b/src/main/webapp/js/app.js index 51de0dd..b53bff7 100644 --- a/src/main/webapp/js/app.js +++ b/src/main/webapp/js/app.js @@ -76,7 +76,7 @@ async function refreshDisplay() { } function clearDisplay(){ - var elements = ["userBoxes", "Captain", "Engineer", "Officer"]; + var elements = ["userBoxes", "Captain", "Engineer", "Officer", "shipAndRankBoxes"]; //TODO this should use a .class for (let i = 0; i < elements.length; i++) { var usersDiv = document.getElementById(elements[i]); while (usersDiv.firstChild) { diff --git a/src/main/webapp/js/find-by-ship-size-and-rank.js b/src/main/webapp/js/find-by-ship-size-and-rank.js index ebc836d..d437799 100644 --- a/src/main/webapp/js/find-by-ship-size-and-rank.js +++ b/src/main/webapp/js/find-by-ship-size-and-rank.js @@ -12,4 +12,21 @@ async function refreshFindByShipSizeAndRank(shipSize, rank) { response = await fetch("db/crew/shipSize/" +shipSize+"/rank/" +rank); + if (response.ok) { + const doc = await response.json(); + doc.forEach(addToCrewMembersFindByShipSizeAndRank); + } +} + +function addToCrewMembersFindByShipSizeAndRank(entry){ + var userHtml = "
Name: " + entry.Name + "
" + + "
ID: " + entry.CrewID + "
" + + "
Ship: " + entry.Ship + "
"; + + var userDiv = document.createElement("div"); + userDiv.setAttribute("class","user flexbox"); + userDiv.setAttribute("id",entry.CrewID); + userDiv.setAttribute("onclick","remove('"+entry.CrewID+"')"); + userDiv.innerHTML=userHtml; + document.getElementById("shipAndRankBoxes").appendChild(userDiv); } \ No newline at end of file From c18a6b6eab8d1e50b613ab917e4dc1e490d74060 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Mon, 11 Dec 2023 13:51:10 -0600 Subject: [PATCH 8/8] Rebase fixes --- src/test/java/io/openliberty/sample/it/CrewServiceIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java index 209e1a9..550c804 100644 --- a/src/test/java/io/openliberty/sample/it/CrewServiceIT.java +++ b/src/test/java/io/openliberty/sample/it/CrewServiceIT.java @@ -154,7 +154,7 @@ public void testFindByRank() { 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.add("Rank", "Captain").add("Ship", "Liberty Saucer")).build()).build(); + JsonArray expectedArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("Name", "Jim").add("CrewID", 64).add("Rank", "Captain").add("Ship", "Liberty Saucer").build()).build(); assertEquals(expectedArray, array); //Check findByRank("Engineer")