From d4a1b3cbd2144dbf31abf71cf75a3c15ca202195 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Thu, 19 Dec 2024 13:52:18 -0800 Subject: [PATCH 1/7] Implemented NetlistResources Controller refactor --- .../opendcs/odcsapi/res/NetlistResources.java | 281 ++++++++++++------ .../odcsapi/res/NetlistResourcesTest.java | 106 +++++++ 2 files changed, 304 insertions(+), 83 deletions(-) create mode 100644 opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index 1aea16ebd..cc7e10be7 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -17,8 +17,10 @@ import java.io.LineNumberReader; import java.io.StringReader; -import java.sql.SQLException; -import java.util.logging.Logger; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import javax.annotation.security.RolesAllowed; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; @@ -33,19 +35,24 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import decodes.db.DatabaseException; +import decodes.db.DatabaseIO; +import decodes.db.NetworkList; +import decodes.db.NetworkListEntry; +import decodes.db.NetworkListList; +import decodes.db.RoutingSpec; +import decodes.db.RoutingSpecList; +import decodes.sql.DbKey; import org.opendcs.odcsapi.beans.ApiNetList; import org.opendcs.odcsapi.beans.ApiNetListItem; -import org.opendcs.odcsapi.dao.ApiNetlistDAO; +import org.opendcs.odcsapi.beans.ApiNetlistRef; import org.opendcs.odcsapi.dao.DbException; -import org.opendcs.odcsapi.errorhandling.ErrorCodes; import org.opendcs.odcsapi.errorhandling.WebAppException; -import org.opendcs.odcsapi.hydrojson.DbInterface; import org.opendcs.odcsapi.util.ApiConstants; -import org.opendcs.odcsapi.util.ApiHttpUtil; @Path("/") -public class NetlistResources +public class NetlistResources extends OpenDcsResource { @Context HttpHeaders httpHeaders; @@ -54,86 +61,198 @@ public class NetlistResources @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_GUEST}) public Response getNetlistRefs(@QueryParam("tmtype") String tmtype) - throws DbException, SQLException + throws DbException { - Logger.getLogger(ApiConstants.loggerName).fine("getNetlistRefs tmtype=" + tmtype); - try ( - DbInterface dbi = new DbInterface(); - ApiNetlistDAO netlistDAO = new ApiNetlistDAO(dbi)) + // TODO: Implement filtering by transport medium type. + // TODO: Investigate non-functioning endpoint (no data returned). + try + { + DatabaseIO dbIo = getLegacyDatabase(); + NetworkListList nlList = new NetworkListList(); + dbIo.readNetworkListList(nlList); + return Response.status(HttpServletResponse.SC_OK).entity(map(nlList)).build(); + } + catch(DatabaseException ex) { - return ApiHttpUtil.createResponse(netlistDAO.readNetlistRefs(tmtype)); + throw new DbException("Unable to retrieve network list", ex); } } + static List map(NetworkListList nlList) + { + List ret = new ArrayList<>(); + for (NetworkList nl : nlList.getList()) + { + ApiNetlistRef anlr = new ApiNetlistRef(); + if (nl.getId() != null) + { + anlr.setNetlistId(nl.getId().getValue()); + } + else + { + anlr.setNetlistId(DbKey.NullKey.getValue()); + } + anlr.setName(nl.getDisplayName()); + anlr.setLastModifyTime(nl.lastModifyTime); + anlr.setTransportMediumType(nl.transportMediumType); + anlr.setSiteNameTypePref(nl.siteNameTypePref); + ret.add(anlr); + } + return ret; + } + @GET @Path("netlist") @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_GUEST}) public Response getNetList(@QueryParam("netlistid") Long netlistId) - throws WebAppException, DbException + throws WebAppException, DbException { if (netlistId == null) - throw new WebAppException(ErrorCodes.MISSING_ID, "Missing required netlistid parameter."); - - Logger.getLogger(ApiConstants.loggerName).fine("getNetList netlistid=" + netlistId); - - try ( - DbInterface dbi = new DbInterface(); - ApiNetlistDAO netlistDAO = new ApiNetlistDAO(dbi)) - { - ApiNetList ret = netlistDAO.readNetworkList(netlistId); - if (ret == null) - throw new WebAppException(ErrorCodes.NO_SUCH_OBJECT, - "No such network list with id=" + netlistId + "."); - return ApiHttpUtil.createResponse(ret); + { + throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required netlistid parameter."); + } + + try + { + DatabaseIO dbIo = getLegacyDatabase(); + NetworkListList nlList = new NetworkListList(); + dbIo.readNetworkListList(nlList); + NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); + if (nl.networkListEntries.isEmpty()) + throw new WebAppException(HttpServletResponse.SC_NOT_FOUND, + "No such network list with id=" + netlistId + "."); + ApiNetList ret = map(nl); + return Response.status(HttpServletResponse.SC_OK).entity(ret).build(); + } + catch(DatabaseException ex) + { + throw new DbException(String.format("Unable to retrieve network list of ID: %s", netlistId), ex); } } + static ApiNetList map(NetworkList nl) + { + ApiNetList ret = new ApiNetList(); + if (nl.getId() != null) + { + ret.setNetlistId(nl.getId().getValue()); + } + else + { + ret.setNetlistId(DbKey.NullKey.getValue()); + } + ret.setName(nl.getDisplayName()); + ret.setLastModifyTime(nl.lastModifyTime); + ret.setTransportMediumType(nl.transportMediumType); + ret.setSiteNameTypePref(nl.siteNameTypePref); + for (Map.Entry entry : nl.networkListEntries.entrySet()) + { + ApiNetListItem anli = new ApiNetListItem(); + anli.setTransportId(entry.getValue().getTransportId()); + anli.setDescription(entry.getValue().getDescription()); + anli.setPlatformName(entry.getValue().getPlatformName()); + ret.getItems().put(anli.getTransportId().toUpperCase(), anli); + } + return ret; + } + @POST @Path("netlist") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_ADMIN, ApiConstants.ODCS_API_USER}) public Response postNetlist(ApiNetList netList) - throws WebAppException, DbException, SQLException + throws DbException { - Logger.getLogger(ApiConstants.loggerName).fine( - "post netlist received netlist " + netList.getName() - + " with tm type " + netList.getTransportMediumType() + " containing " - + netList.getItems().size() + " TMs"); - - // Use username and password to attempt to connect to the database - try (DbInterface dbi = new DbInterface(); - ApiNetlistDAO netlistDAO = new ApiNetlistDAO(dbi)) - { - netlistDAO.writeNetlist(netList); - return ApiHttpUtil.createResponse(netList); + try + { + DatabaseIO dbIo = getLegacyDatabase(); + NetworkList nlList = map(netList); + dbIo.writeNetworkList(nlList); + return Response.status(HttpServletResponse.SC_OK).entity(map(nlList)).build(); + } + catch(DatabaseException ex) + { + throw new DbException("Unable to store network list", ex); } } + static NetworkList map(ApiNetList netList) throws DatabaseException + { + NetworkList ret = new NetworkList(netList.getName()); + if (netList.getNetlistId() != null) + { + ret.setId(DbKey.createDbKey(netList.getNetlistId())); + } + else + { + ret.setId(DbKey.NullKey); + } + ret.name = netList.getName(); + ret.transportMediumType = netList.getTransportMediumType(); + ret.siteNameTypePref = netList.getSiteNameTypePref(); + ret.lastModifyTime = netList.getLastModifyTime(); + for (Map.Entry anli : netList.getItems().entrySet()) + { + NetworkListEntry nle = new NetworkListEntry(ret, anli.getValue().getTransportId()); + nle.transportId = anli.getValue().getTransportId(); + nle.setDescription(anli.getValue().getDescription()); + nle.setPlatformName(anli.getValue().getPlatformName()); + ret.networkListEntries.put(anli.getKey().toUpperCase(), nle); + } + return ret; + } + @DELETE @Path("netlist") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_ADMIN, ApiConstants.ODCS_API_USER}) public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) - throws DbException + throws DbException, WebAppException { - Logger.getLogger(ApiConstants.loggerName) - .fine("DELETE netlist received netlistid=" + netlistId); - - // Use username and password to attempt to connect to the database - try (DbInterface dbi = new DbInterface(); - ApiNetlistDAO netlistDAO = new ApiNetlistDAO(dbi)) - { - String errmsg = netlistDAO.netlistUsedByRs(netlistId); - if (errmsg != null) - return ApiHttpUtil.createResponse(" Cannot delete network list with ID " + netlistId - + " because it is used by the following routing specs: " - + errmsg, ErrorCodes.NOT_ALLOWED); - - netlistDAO.deleteNetlist(netlistId); - return ApiHttpUtil.createResponse("ID " + netlistId + " deleted"); + if (netlistId == null) + { + throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required netlistid parameter."); + } + + try + { + DatabaseIO dbIo = getLegacyDatabase(); + NetworkListList nlList = new NetworkListList(); + dbIo.readNetworkListList(nlList); + NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); + + RoutingSpecList routingSpecList = new RoutingSpecList(); + dbIo.readRoutingSpecList(routingSpecList); + + StringBuilder errmsg = new StringBuilder(); + + for (RoutingSpec spec : routingSpecList.getList()) + { + for (NetworkList list : spec.networkLists) + { + if (list.getId().equals(nl.getId())) + { + errmsg.append((errmsg.length() > 0) ? ", " : "").append(spec.getName()); + } + } + } + + if (errmsg.length() > 0) + { + return Response.status(HttpServletResponse.SC_OK) + .entity(" Cannot delete network list with ID " + netlistId + + " because it is used by the following routing specs: " + + errmsg).build(); + } + dbIo.deleteNetworkList(nl); + return Response.status(HttpServletResponse.SC_OK).entity("ID " + netlistId + " deleted").build(); + } + catch (DatabaseException ex) + { + throw new DbException("Unable to delete network list", ex); } } @@ -142,42 +261,38 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_GUEST}) - public Response cnvtNL(String nldata) - throws WebAppException + public Response cnvtANL(String nldata) + throws WebAppException { - Logger.getLogger(ApiConstants.loggerName).fine("cnvtnl nldata='" + nldata + "'"); - ApiNetList ret = new ApiNetList(); - - LineNumberReader rdr = new LineNumberReader(new StringReader(nldata)); - String ln = null; - try - { - while( (ln = rdr.readLine()) != null) - { - ln = ln.trim(); - if (ln.length() <= 0 - || ln.charAt(0) == '#' || ln.charAt(0) == ':') // skip comment lines. - continue; - else - { - ApiNetListItem anli = ApiNetListItem.fromString(ln); - ret.getItems().put(anli.getTransportId(), anli); - } - } - } + + LineNumberReader rdr = new LineNumberReader(new StringReader(nldata)); + String ln = null; + try + { + while( (ln = rdr.readLine()) != null) + { + ln = ln.trim(); + if (!(ln.length() <= 0 + || ln.charAt(0) == '#' || ln.charAt(0) == ':')) // skip comment lines. + { + ApiNetListItem anli = ApiNetListItem.fromString(ln); + ret.getItems().put(anli.getTransportId(), anli); + } + } + } catch(Exception ex) { - String msg = - "NL File Parsing Failed on line " + rdr.getLineNumber() + ": " + ex - + (ln == null ? "" : (" -- " + ln)); + String msg = + "NL File Parsing Failed on line " + rdr.getLineNumber() + ": " + ex + + (ln == null ? "" : (" -- " + ln)); throw new WebAppException(HttpServletResponse.SC_NOT_ACCEPTABLE, msg); } - + try { rdr.close(); } catch (Exception e) {} - return ApiHttpUtil.createResponse(ret); + return Response.status(HttpServletResponse.SC_OK).entity(ret).build(); } -} +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java new file mode 100644 index 000000000..e32d9e365 --- /dev/null +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java @@ -0,0 +1,106 @@ +package org.opendcs.odcsapi.res; + +import java.time.Instant; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +import decodes.db.NetworkList; +import decodes.db.NetworkListEntry; +import decodes.db.NetworkListList; +import decodes.sql.DbKey; +import org.junit.jupiter.api.Test; +import org.opendcs.odcsapi.beans.ApiNetList; +import org.opendcs.odcsapi.beans.ApiNetListItem; +import org.opendcs.odcsapi.beans.ApiNetlistRef; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.opendcs.odcsapi.res.NetlistResources.map; + +final class NetlistResourcesTest +{ + @Test + void testNetworkListMap() throws Exception + { + NetworkListList nll = new NetworkListList(); + NetworkList nl = new NetworkList("Test"); + nl.lastModifyTime = Date.from(Instant.parse("2021-07-01T00:00:00Z")); + nl.siteNameTypePref = "TestPref"; + nl.transportMediumType = "GOES"; + nl.setId(DbKey.createDbKey(750556L)); + + nll.add(nl); + + List netlistRefs = map(nll); + + assertNotNull(netlistRefs); + assertEquals(1, netlistRefs.size()); + assertEquals(nl.getDisplayName(), netlistRefs.get(0).getName()); + assertEquals(nl.getKey().getValue(), netlistRefs.get(0).getNetlistId()); + assertEquals(nl.transportMediumType, netlistRefs.get(0).getTransportMediumType()); + assertEquals(nl.lastModifyTime, netlistRefs.get(0).getLastModifyTime()); + assertEquals(nl.siteNameTypePref, netlistRefs.get(0).getSiteNameTypePref()); + } + + @Test + void testNetworkMap() throws Exception + { + NetworkList nl = new NetworkList("Test"); + nl.setId(DbKey.createDbKey(750556L)); + nl.name = "Test"; + nl.transportMediumType = "GOES"; + nl.lastModifyTime = Date.from(Instant.parse("2021-07-01T00:00:00Z")); + nl.siteNameTypePref = "TestPref"; + HashMap entries = new HashMap<>(); + NetworkListEntry nle = new NetworkListEntry(nl, "TestEntry"); + entries.put("TestEntry", nle); + nl.networkListEntries = entries; + + ApiNetList apiNetList = map(nl); + + assertNotNull(apiNetList); + assertEquals(nl.getDisplayName(), apiNetList.getName()); + assertEquals(nl.getKey().getValue(), apiNetList.getNetlistId()); + assertEquals(nl.transportMediumType, apiNetList.getTransportMediumType()); + assertEquals(nl.lastModifyTime, apiNetList.getLastModifyTime()); + assertEquals(nl.siteNameTypePref, apiNetList.getSiteNameTypePref()); + assertEquals(1, apiNetList.getItems().size()); + ApiNetListItem nle2 = apiNetList.getItems().get("TestEntry".toUpperCase()); + assertNotNull(nle2); + assertEquals(nle.getDescription(), nle2.getDescription()); + assertEquals(nle.getPlatformName(), nle2.getPlatformName()); + assertEquals(nle.getTransportId(), nle2.getTransportId()); + } + + @Test + void testApiNetListMap() throws Exception + { + ApiNetList apiNetList = new ApiNetList(); + apiNetList.setName("Test"); + apiNetList.setNetlistId(750556L); + apiNetList.setTransportMediumType("GOES"); + apiNetList.setLastModifyTime(Date.from(Instant.parse("2021-07-01T00:00:00Z"))); + HashMap entries = new HashMap<>(); + ApiNetListItem nle = new ApiNetListItem(); + nle.setDescription("Test Entry"); + nle.setPlatformName("Test Platform"); + nle.setTransportId("Test Transport ID"); + entries.put("Test Transport ID".toUpperCase(), nle); + apiNetList.setItems(entries); + + NetworkList nl = map(apiNetList); + + assertNotNull(nl); + assertEquals(apiNetList.getName(), nl.getDisplayName()); + assertEquals(apiNetList.getTransportMediumType(), nl.transportMediumType); + assertEquals(apiNetList.getNetlistId(), nl.getKey().getValue()); + assertEquals(apiNetList.getLastModifyTime(), nl.lastModifyTime); + assertEquals(1, nl.networkListEntries.size()); + NetworkListEntry nle2 = nl.networkListEntries.get("Test Transport ID".toUpperCase()); + assertNotNull(nle2); + assertEquals(nle.getDescription(), nle2.getDescription()); + assertEquals(nle.getPlatformName(), nle2.getPlatformName()); + assertEquals(nle.getTransportId(), nle2.getTransportId()); + } +} \ No newline at end of file From 89d630240c2e685e94e6e42135931d720d721cdd Mon Sep 17 00:00:00 2001 From: zack-rma Date: Thu, 2 Jan 2025 16:52:23 -0800 Subject: [PATCH 2/7] Fixed return code --- .../main/java/org/opendcs/odcsapi/res/NetlistResources.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index cc7e10be7..f6eca67f2 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -242,7 +242,7 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) if (errmsg.length() > 0) { - return Response.status(HttpServletResponse.SC_OK) + return Response.status(HttpServletResponse.SC_CONFLICT) .entity(" Cannot delete network list with ID " + netlistId + " because it is used by the following routing specs: " + errmsg).build(); @@ -273,7 +273,7 @@ public Response cnvtANL(String nldata) while( (ln = rdr.readLine()) != null) { ln = ln.trim(); - if (!(ln.length() <= 0 + if (!(ln.isEmpty() || ln.charAt(0) == '#' || ln.charAt(0) == ':')) // skip comment lines. { ApiNetListItem anli = ApiNetListItem.fromString(ln); From a2e7f3f20a62b4c263a4cd3933020cbdac9c55db Mon Sep 17 00:00:00 2001 From: zack-rma Date: Wed, 8 Jan 2025 13:28:16 -0800 Subject: [PATCH 3/7] Updated mapping and DB closure --- .../opendcs/odcsapi/res/NetlistResources.java | 65 ++++++++++++++++--- .../odcsapi/res/NetlistResourcesTest.java | 21 ++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index f6eca67f2..52e8737ee 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -55,6 +55,7 @@ public class NetlistResources extends OpenDcsResource { @Context HttpHeaders httpHeaders; + DatabaseIO dbIo; @GET @Path("netlistrefs") @@ -63,19 +64,28 @@ public class NetlistResources extends OpenDcsResource public Response getNetlistRefs(@QueryParam("tmtype") String tmtype) throws DbException { - // TODO: Implement filtering by transport medium type. - // TODO: Investigate non-functioning endpoint (no data returned). try { - DatabaseIO dbIo = getLegacyDatabase(); + dbIo = getLegacyDatabase(); NetworkListList nlList = new NetworkListList(); - dbIo.readNetworkListList(nlList); + if (tmtype == null) + { + dbIo.readNetworkListList(nlList); + } + else + { + dbIo.readNetworkListList(nlList, getSingleWord(tmtype).toLowerCase()); + } return Response.status(HttpServletResponse.SC_OK).entity(map(nlList)).build(); } catch(DatabaseException ex) { throw new DbException("Unable to retrieve network list", ex); } + finally + { + dbIo.close(); + } } static List map(NetworkListList nlList) @@ -115,13 +125,15 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) try { - DatabaseIO dbIo = getLegacyDatabase(); + dbIo = getLegacyDatabase(); NetworkListList nlList = new NetworkListList(); dbIo.readNetworkListList(nlList); NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); - if (nl.networkListEntries.isEmpty()) + if (nl == null || nl.networkListEntries == null || nl.networkListEntries.isEmpty()) + { throw new WebAppException(HttpServletResponse.SC_NOT_FOUND, "No such network list with id=" + netlistId + "."); + } ApiNetList ret = map(nl); return Response.status(HttpServletResponse.SC_OK).entity(ret).build(); } @@ -129,6 +141,10 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) { throw new DbException(String.format("Unable to retrieve network list of ID: %s", netlistId), ex); } + finally + { + dbIo.close(); + } } static ApiNetList map(NetworkList nl) @@ -163,11 +179,15 @@ static ApiNetList map(NetworkList nl) @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ApiConstants.ODCS_API_ADMIN, ApiConstants.ODCS_API_USER}) public Response postNetlist(ApiNetList netList) - throws DbException + throws DbException, WebAppException { try { - DatabaseIO dbIo = getLegacyDatabase(); + if (netList == null) + { + throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required request body."); + } + dbIo = getLegacyDatabase(); NetworkList nlList = map(netList); dbIo.writeNetworkList(nlList); return Response.status(HttpServletResponse.SC_OK).entity(map(nlList)).build(); @@ -176,6 +196,10 @@ public Response postNetlist(ApiNetList netList) { throw new DbException("Unable to store network list", ex); } + finally + { + dbIo.close(); + } } static NetworkList map(ApiNetList netList) throws DatabaseException @@ -219,7 +243,7 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) try { - DatabaseIO dbIo = getLegacyDatabase(); + dbIo = getLegacyDatabase(); NetworkListList nlList = new NetworkListList(); dbIo.readNetworkListList(nlList); NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); @@ -254,6 +278,10 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) { throw new DbException("Unable to delete network list", ex); } + finally + { + dbIo.close(); + } } @POST @@ -289,10 +317,27 @@ public Response cnvtANL(String nldata) throw new WebAppException(HttpServletResponse.SC_NOT_ACCEPTABLE, msg); } - try { rdr.close(); } catch (Exception e) {} + try { rdr.close(); } catch (Exception ignored) + { + // Ignored + } return Response.status(HttpServletResponse.SC_OK).entity(ret).build(); } + // Helper method to extract the first word from a string. + public static String getSingleWord(String arg) + { + String special = "(){}[]'\"|,"; + + StringBuilder sb = new StringBuilder(arg.trim()); + for(int idx = 0; idx < sb.length(); idx++) + { + char c = sb.charAt(idx); + if (Character.isWhitespace(c) || special.indexOf(c) >= 0) + return idx == 0 ? "" : sb.substring(0, idx); + } + return sb.toString(); + } } \ No newline at end of file diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java index e32d9e365..d8a91574b 100644 --- a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java @@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.opendcs.odcsapi.res.NetlistResources.map; final class NetlistResourcesTest @@ -103,4 +104,24 @@ void testApiNetListMap() throws Exception assertEquals(nle.getPlatformName(), nle2.getPlatformName()); assertEquals(nle.getTransportId(), nle2.getTransportId()); } + + @Test + void testGetSingleWord() + { + String input = "This is a test input"; + String output = NetlistResources.getSingleWord(input); + assertEquals("This", output); + + input = "Val[{ue: status}]"; + output = NetlistResources.getSingleWord(input); + assertEquals("Val", output); + + input = ""; + output = NetlistResources.getSingleWord(input); + assertNull(output); + + input = "goes-self-timed{}"; + output = NetlistResources.getSingleWord(input); + assertEquals("goes-self-timed", output); + } } \ No newline at end of file From cebd3bf6928efce09d252cb8936e4782d222bf16 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Wed, 8 Jan 2025 13:45:32 -0800 Subject: [PATCH 4/7] IT in progress - retrieval bug under investigation --- .../org/opendcs/odcsapi/res/it/BaseIT.java | 2 +- .../odcsapi/res/it/NetlistResourcesIT.java | 362 ++++++++++++++++++ .../OPEN_TSDB/netlist_cnvtanl_expected.json | 0 .../OPEN_TSDB/netlist_cnvtanl_input_data.json | 0 .../it/OPEN_TSDB/netlist_get_expected.json | 0 .../OPEN_TSDB/netlist_get_refs_expected.json | 0 .../netlist_get_refs_filtered_expected.json | 0 .../res/it/OPEN_TSDB/netlist_insert_data.json | 14 + .../netlist_platform_insert_data.json | 32 ++ .../netlist_post_delete_expected.json | 0 .../netlist_post_delete_insert_data.json | 14 + .../OPEN_TSDB/netlist_site_insert_data.json | 21 + 12 files changed, 444 insertions(+), 1 deletion(-) create mode 100644 opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_platform_insert_data.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_insert_data.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_site_insert_data.json diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/BaseIT.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/BaseIT.java index 2decd7786..32b018375 100644 --- a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/BaseIT.java +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/BaseIT.java @@ -145,7 +145,7 @@ void authenticate(SessionFilter sessionFilter) void logout(SessionFilter sessionFilter) { - if(DatabaseSetupExtension.getCurrentDbType() == DbType.OPEN_TSDB) + if (DatabaseSetupExtension.getCurrentDbType() == DbType.OPEN_TSDB) { given() .log().ifValidationFails(LogDetail.ALL, true) diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java new file mode 100644 index 000000000..aa036197e --- /dev/null +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java @@ -0,0 +1,362 @@ +package org.opendcs.odcsapi.res.it; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.restassured.filter.log.LogDetail; +import io.restassured.filter.session.SessionFilter; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.opendcs.odcsapi.beans.ApiNetList; +import org.opendcs.odcsapi.beans.ApiPlatform; +import org.opendcs.odcsapi.fixtures.DatabaseContextProvider; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Tag("integration-opentsdb-only") +@ExtendWith(DatabaseContextProvider.class) +final class NetlistResourcesIT extends BaseIT +{ + private static SessionFilter sessionFilter; + private Long netlistId; + private Long platformId; + private Long siteId; + + @BeforeEach + void setUp() throws Exception + { + setUpCreds(); + sessionFilter = new SessionFilter(); + authenticate(sessionFilter); + + ObjectMapper mapper = new ObjectMapper(); + + siteId = storeSite("netlist_site_insert_data.json"); + + ApiPlatform platform = getDtoFromResource("netlist_platform_insert_data.json", ApiPlatform.class); + platform.setSiteId(siteId); + String platformJson = mapper.writeValueAsString(platform); + + ExtractableResponse response = given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .body(platformJson) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("platform") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract() + ; + + platformId = response.body().jsonPath().getLong("platformId"); + + ApiNetList netlist = getDtoFromResource("netlist_insert_data.json", ApiNetList.class); + netlist.getItems().get("6698948").setPlatformName(platform.getName()); + netlist.getItems().get("6698948").setTransportId("6698948"); + netlist.getItems().get("6698948").setDescription(platform.getDescription()); + + String netlistJson = mapper.writeValueAsString(netlist); + + response = given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .body(netlistJson) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract(); + + netlistId = response.body().jsonPath().getLong("netlistId"); + } + + @AfterEach + void tearDown() + { + // Delete the netlist + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .queryParam("netlistId", netlistId) + .when() + .redirects().follow(true) + .redirects().max(3) + .delete("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + ; + + // Delete the platform + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .queryParam("platformid", platformId) + .when() + .redirects().follow(true) + .redirects().max(3) + .delete("platform") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract() + ; + + tearDownSite(siteId); + + logout(sessionFilter); + } + + @TestTemplate + void testGetNetlistRefs() + { + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlistrefs") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body(hasItem(getJsonPathFromResource("netlist_get_refs_expected.json"))) + ; + + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .queryParam("tmtype", "GOES") + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlistrefs") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body(hasItem(getJsonPathFromResource("netlist_get_refs_filtered_expected.json"))) + ; + + } + + @TestTemplate + void testGetNetlist() + { + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .queryParam("netlistid", netlistId) + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body(hasItem(getJsonPathFromResource("netlist_get_expected.json"))) + ; + } + + @TestTemplate + void testPostAndDeleteNetlist() throws Exception + { + String netlistJson = getJsonFromResource("netlist_post_delete_insert_data.json"); + + // Insert a new netlist + ExtractableResponse response = given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .body(netlistJson) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract() + ; + + Long newNetlistId = response.body().jsonPath().getLong("netlistId"); + + // Get the new netlist and assert it is as expected + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .queryParam("netlistid", newNetlistId) + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body(hasItem(getJsonPathFromResource("netlist_post_delete_expected.json"))) + ; + + // Delete the new netlist + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .filter(sessionFilter) + .queryParam("netlistid", newNetlistId) + .when() + .redirects().follow(true) + .redirects().max(3) + .delete("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + ; + + // Get the new netlist and assert it is not found + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .queryParam("netlistid", newNetlistId) + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_NOT_FOUND)) + ; + } + + @TestTemplate + void testCnvtANL() throws Exception + { + String inputJson = getJsonFromResource("netlist_cnvtanl_input_data.json"); + + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .body(inputJson) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("netlist") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body(hasItem(getJsonPathFromResource("netlist_cnvtanl_expected.json"))) + ; + } + + private Long storeSite(String jsonPath) throws Exception + { + assertNotNull(jsonPath); + String siteJson = getJsonFromResource(jsonPath); + + ExtractableResponse response = given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .contentType(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .body(siteJson) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("site") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract() + ; + + return response.body().jsonPath().getLong("siteId"); + } + + private void tearDownSite(Long siteId) + { + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .queryParam("siteid", siteId) + .filter(sessionFilter) + .when() + .redirects().follow(true) + .redirects().max(3) + .delete("site") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + ; + + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .header("Authorization", authHeader) + .queryParam("siteid", siteId) + .filter(sessionFilter) + .when() + .redirects().follow(true) + .redirects().max(3) + .get("site") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_NOT_FOUND)) + ; + } +} diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json new file mode 100644 index 000000000..37b7ee68d --- /dev/null +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json @@ -0,0 +1,14 @@ +{ + "netlistId" : null, + "name" : "Distributed Network List", + "transportMediumType" : "GOES", + "siteNameTypePref" : "Stream", + "lastModifyTime" : 1736356819000, + "items" : { + "6698948" : { + "transportId" : "6698948", + "platformName" : "Stream test site", + "description" : "platform associated with stream" + } + } +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_platform_insert_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_platform_insert_data.json new file mode 100644 index 000000000..4ddfe7264 --- /dev/null +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_platform_insert_data.json @@ -0,0 +1,32 @@ +{ + "platformId" : null, + "name" : "Stream Platform", + "siteId" : null, + "agency" : null, + "configId" : null, + "description" : "Test Platform Description", + "designator" : null, + "lastModified" : null, + "production" : false, + "properties" : { }, + "platformSensors" : [ ], + "transportMedia" : [ { + "mediumType" : "GOES", + "mediumId" : "6698948", + "scriptName" : "test script", + "channelNum" : 21, + "assignedTime" : 1736361778, + "transportWindow" : 20, + "transportInterval" : 2, + "timeAdjustment" : 0, + "timezone" : "UTC", + "loggerType" : "None", + "baud" : 9600, + "stopBits" : 2, + "parity" : "N", + "dataBits" : 8, + "doLogin" : true, + "username" : "aTestUser", + "password" : "aBadTestPass123" + } ] +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json new file mode 100644 index 000000000..e69de29bb diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_insert_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_insert_data.json new file mode 100644 index 000000000..7e77a3a2e --- /dev/null +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_insert_data.json @@ -0,0 +1,14 @@ +{ + "netlistId" : null, + "name" : "Platform Network List", + "transportMediumType" : "GOES", + "siteNameTypePref" : "Stream", + "lastModifyTime" : 1736356812200, + "items" : { + "6698952" : { + "transportId" : "6698952", + "platformName" : "Stream test site", + "description" : "platform associated with stream" + } + } +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_site_insert_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_site_insert_data.json new file mode 100644 index 000000000..5d2d40fc1 --- /dev/null +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_site_insert_data.json @@ -0,0 +1,21 @@ +{ + "siteId" : null, + "sitenames" : { + "Stream" : "Stream test site" + }, + "description" : "A stream site", + "latitude" : "76.3", + "longitude" : "-121.8", + "elevation" : 150.0, + "elevUnits" : "ft", + "nearestCity" : "Sacramento", + "timezone" : "UTC", + "state" : "CA", + "country" : "USA", + "region" : "Test", + "active" : true, + "locationType" : "Stream", + "publicName" : "Stream test site", + "properties" : { }, + "lastModified" : 1736361778000 +} \ No newline at end of file From 05f6f93df9dc19d11651f6f55715c7720781b5b7 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Thu, 16 Jan 2025 17:11:28 -0800 Subject: [PATCH 5/7] Added exceptions, Netlist retrieval bug in progress --- .../opendcs/odcsapi/res/NetlistResources.java | 18 ++++++++++++------ .../odcsapi/res/NetlistResourcesTest.java | 3 +-- .../odcsapi/res/it/NetlistResourcesIT.java | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index 52e8737ee..a703f96a8 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -47,6 +47,8 @@ import org.opendcs.odcsapi.beans.ApiNetListItem; import org.opendcs.odcsapi.beans.ApiNetlistRef; import org.opendcs.odcsapi.dao.DbException; +import org.opendcs.odcsapi.errorhandling.DatabaseItemNotFoundException; +import org.opendcs.odcsapi.errorhandling.MissingParameterException; import org.opendcs.odcsapi.errorhandling.WebAppException; import org.opendcs.odcsapi.util.ApiConstants; @@ -120,19 +122,18 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) { if (netlistId == null) { - throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required netlistid parameter."); + throw new MissingParameterException("Missing required netlistid parameter."); } try { dbIo = getLegacyDatabase(); NetworkListList nlList = new NetworkListList(); - dbIo.readNetworkListList(nlList); + nlList = dbIo.readNetworkListList(nlList); NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); if (nl == null || nl.networkListEntries == null || nl.networkListEntries.isEmpty()) { - throw new WebAppException(HttpServletResponse.SC_NOT_FOUND, - "No such network list with id=" + netlistId + "."); + throw new DatabaseItemNotFoundException("No such network list with id=" + netlistId + "."); } ApiNetList ret = map(nl); return Response.status(HttpServletResponse.SC_OK).entity(ret).build(); @@ -185,7 +186,7 @@ public Response postNetlist(ApiNetList netList) { if (netList == null) { - throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required request body."); + throw new MissingParameterException("Missing required request body."); } dbIo = getLegacyDatabase(); NetworkList nlList = map(netList); @@ -238,7 +239,7 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) { if (netlistId == null) { - throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required netlistid parameter."); + throw new MissingParameterException("Missing required netlistid parameter."); } try @@ -271,6 +272,11 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) + " because it is used by the following routing specs: " + errmsg).build(); } + if (nl == null) + { + nl = new NetworkList(); + nl.setId(DbKey.createDbKey(netlistId)); + } dbIo.deleteNetworkList(nl); return Response.status(HttpServletResponse.SC_OK).entity("ID " + netlistId + " deleted").build(); } diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java index d8a91574b..398b02948 100644 --- a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/NetlistResourcesTest.java @@ -16,7 +16,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.opendcs.odcsapi.res.NetlistResources.map; final class NetlistResourcesTest @@ -118,7 +117,7 @@ void testGetSingleWord() input = ""; output = NetlistResources.getSingleWord(input); - assertNull(output); + assertEquals("", output); input = "goes-self-timed{}"; output = NetlistResources.getSingleWord(input); diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java index aa036197e..9a40c1420 100644 --- a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java @@ -102,7 +102,7 @@ void tearDown() .accept(MediaType.APPLICATION_JSON) .header("Authorization", authHeader) .filter(sessionFilter) - .queryParam("netlistId", netlistId) + .queryParam("netlistid", netlistId) .when() .redirects().follow(true) .redirects().max(3) From 340c983b5b21d83a44bcac08168667d2cbed5113 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 17 Jan 2025 14:29:23 -0800 Subject: [PATCH 6/7] Updated IT, resolved retrieval bug --- .../opendcs/odcsapi/res/NetlistResources.java | 6 +- .../odcsapi/res/it/NetlistResourcesIT.java | 174 ++++++++++++++++-- .../OPEN_TSDB/netlist_cnvtanl_expected.json | 5 + .../OPEN_TSDB/netlist_cnvtanl_input_data.json | 0 .../OPEN_TSDB/netlist_cnvtanl_input_data.txt | 4 + .../it/OPEN_TSDB/netlist_get_expected.json | 14 ++ .../OPEN_TSDB/netlist_get_refs_expected.json | 0 .../netlist_get_refs_filtered_expected.json | 0 .../res/it/OPEN_TSDB/netlist_insert_data.json | 2 +- .../netlist_post_delete_expected.json | 0 10 files changed, 181 insertions(+), 24 deletions(-) delete mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json create mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.txt delete mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json delete mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json delete mode 100644 opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index a703f96a8..34758cc94 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -129,7 +129,7 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) { dbIo = getLegacyDatabase(); NetworkListList nlList = new NetworkListList(); - nlList = dbIo.readNetworkListList(nlList); + dbIo.readNetworkListList(nlList); NetworkList nl = nlList.getById(DbKey.createDbKey(netlistId)); if (nl == null || nl.networkListEntries == null || nl.networkListEntries.isEmpty()) { @@ -191,7 +191,7 @@ public Response postNetlist(ApiNetList netList) dbIo = getLegacyDatabase(); NetworkList nlList = map(netList); dbIo.writeNetworkList(nlList); - return Response.status(HttpServletResponse.SC_OK).entity(map(nlList)).build(); + return Response.status(HttpServletResponse.SC_CREATED).entity(map(nlList)).build(); } catch(DatabaseException ex) { @@ -278,7 +278,7 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) nl.setId(DbKey.createDbKey(netlistId)); } dbIo.deleteNetworkList(nl); - return Response.status(HttpServletResponse.SC_OK).entity("ID " + netlistId + " deleted").build(); + return Response.status(HttpServletResponse.SC_NO_CONTENT).entity("ID " + netlistId + " deleted").build(); } catch (DatabaseException ex) { diff --git a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java index 9a40c1420..af4b3e882 100644 --- a/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java +++ b/opendcs-rest-api/src/test/java/org/opendcs/odcsapi/res/it/NetlistResourcesIT.java @@ -1,11 +1,14 @@ package org.opendcs.odcsapi.res.it; +import java.util.List; +import java.util.Map; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.MediaType; import com.fasterxml.jackson.databind.ObjectMapper; import io.restassured.filter.log.LogDetail; import io.restassured.filter.session.SessionFilter; +import io.restassured.path.json.JsonPath; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; import org.junit.jupiter.api.AfterEach; @@ -18,9 +21,11 @@ import org.opendcs.odcsapi.fixtures.DatabaseContextProvider; import static io.restassured.RestAssured.given; -import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("integration-opentsdb-only") @ExtendWith(DatabaseContextProvider.class) @@ -87,7 +92,7 @@ void setUp() throws Exception .then() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() - .statusCode(is(HttpServletResponse.SC_OK)) + .statusCode(is(HttpServletResponse.SC_CREATED)) .extract(); netlistId = response.body().jsonPath().getLong("netlistId"); @@ -110,7 +115,7 @@ void tearDown() .then() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() - .statusCode(is(HttpServletResponse.SC_OK)) + .statusCode(is(HttpServletResponse.SC_NO_CONTENT)) ; // Delete the platform @@ -140,7 +145,13 @@ void tearDown() @TestTemplate void testGetNetlistRefs() { - given() + JsonPath expected = getJsonPathFromResource("netlist_insert_data.json"); + + assertNotNull(expected); + + Map expectedMap = expected.getMap(""); + + ExtractableResponse response = given() .log().ifValidationFails(LogDetail.ALL, true) .accept(MediaType.APPLICATION_JSON) .filter(sessionFilter) @@ -153,15 +164,76 @@ void testGetNetlistRefs() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) - .body(hasItem(getJsonPathFromResource("netlist_get_refs_expected.json"))) + .extract() ; - given() + JsonPath actual = response.jsonPath(); + List> actualList = actual.getList(""); + + assertNotNull(actual); + assertNotNull(actualList); + + boolean found = false; + for (Map actualMap : actualList) + { + if (actualMap.get("name").equals(expectedMap.get("name"))) + { + assertEquals(expectedMap.get("name"), actualMap.get("name")); + assertEquals(expectedMap.get("transportMediumType"), actualMap.get("transportMediumType")); + assertEquals(expectedMap.get("siteNameTypePref"), actualMap.get("siteNameTypePref")); + assertEquals(expectedMap.get("items[6698948].transportId"), actualMap.get("items[6698948].transportId")); + assertEquals(expectedMap.get("items[6698948].description"), actualMap.get("items[6698948].description")); + assertEquals(expectedMap.get("items[6698948].platformName"), actualMap.get("items[6698948].platformName")); + found = true; + } + } + assertTrue(found); + + response = given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(MediaType.APPLICATION_JSON) + .filter(sessionFilter) + .header("Authorization", authHeader) + .queryParam("tmtype", "goes") + .when() + .redirects().follow(true) + .redirects().max(3) + .get("netlistrefs") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .extract() + ; + + actual = response.jsonPath(); + actualList = actual.getList(""); + + assertNotNull(actual); + + found = false; + for (Map actualMap : actualList) + { + if (actualMap.get("name").equals(expectedMap.get("name"))) + { + assertEquals(expectedMap.get("name"), actualMap.get("name")); + assertEquals(expectedMap.get("transportMediumType"), actualMap.get("transportMediumType")); + assertEquals(expectedMap.get("siteNameTypePref"), actualMap.get("siteNameTypePref")); + assertEquals(expectedMap.get("items[6698948].transportId"), actualMap.get("items[6698948].transportId")); + assertEquals(expectedMap.get("items[6698948].description"), actualMap.get("items[6698948].description")); + assertEquals(expectedMap.get("items[6698948].platformName"), actualMap.get("items[6698948].platformName")); + found = true; + } + } + + assertTrue(found); + + response = given() .log().ifValidationFails(LogDetail.ALL, true) .accept(MediaType.APPLICATION_JSON) .filter(sessionFilter) .header("Authorization", authHeader) - .queryParam("tmtype", "GOES") + .queryParam("tmtype", "goes-random") .when() .redirects().follow(true) .redirects().max(3) @@ -170,15 +242,40 @@ void testGetNetlistRefs() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) - .body(hasItem(getJsonPathFromResource("netlist_get_refs_filtered_expected.json"))) + .extract() ; + actual = response.jsonPath(); + actualList = actual.getList(""); + + assertNotNull(actual); + + found = false; + for (Map actualMap : actualList) + { + if (actualMap.get("name").equals(expectedMap.get("name"))) + { + assertEquals(expectedMap.get("name"), actualMap.get("name")); + assertEquals(expectedMap.get("transportMediumType"), actualMap.get("transportMediumType")); + assertEquals(expectedMap.get("siteNameTypePref"), actualMap.get("siteNameTypePref")); + assertEquals(expectedMap.get("items[6698948].transportId"), actualMap.get("items[6698948].transportId")); + assertEquals(expectedMap.get("items[6698948].description"), actualMap.get("items[6698948].description")); + assertEquals(expectedMap.get("items[6698948].platformName"), actualMap.get("items[6698948].platformName")); + found = true; + } + } + + assertFalse(found); } @TestTemplate void testGetNetlist() { - given() + JsonPath expected = getJsonPathFromResource("netlist_get_expected.json"); + + assertNotNull(expected); + + ExtractableResponse response = given() .log().ifValidationFails(LogDetail.ALL, true) .accept(MediaType.APPLICATION_JSON) .header("Authorization", authHeader) @@ -192,8 +289,21 @@ void testGetNetlist() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) - .body(hasItem(getJsonPathFromResource("netlist_get_expected.json"))) + .extract() ; + + JsonPath actual = response.jsonPath(); + + assertNotNull(actual); + assertEquals(expected.getString("name"), actual.getString("name")); + assertEquals(expected.getString("transportMediumType"), actual.getString("transportMediumType")); + assertEquals(expected.getString("siteNameTypePref"), actual.getString("siteNameTypePref")); + assertEquals(expected.getString("items[6698948].transportId"), + actual.getString("items[6698948].transportId")); + assertEquals(expected.getString("items[6698948].description"), + actual.getString("items[6698948].description")); + assertEquals(expected.getString("items[6698948].platformName"), + actual.getString("items[6698948].platformName")); } @TestTemplate @@ -216,14 +326,16 @@ void testPostAndDeleteNetlist() throws Exception .then() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() - .statusCode(is(HttpServletResponse.SC_OK)) + .statusCode(is(HttpServletResponse.SC_CREATED)) .extract() ; Long newNetlistId = response.body().jsonPath().getLong("netlistId"); + JsonPath expected = new JsonPath(netlistJson); + // Get the new netlist and assert it is as expected - given() + response = given() .log().ifValidationFails(LogDetail.ALL, true) .accept(MediaType.APPLICATION_JSON) .filter(sessionFilter) @@ -237,9 +349,23 @@ void testPostAndDeleteNetlist() throws Exception .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) - .body(hasItem(getJsonPathFromResource("netlist_post_delete_expected.json"))) + .extract() ; + JsonPath actual = response.jsonPath(); + + assertNotNull(actual); + + assertEquals(expected.getString("name"), actual.getString("name")); + assertEquals(expected.getString("transportMediumType"), actual.getString("transportMediumType")); + assertEquals(expected.getString("siteNameTypePref"), actual.getString("siteNameTypePref")); + assertEquals(expected.getString("items[6698948].transportId"), + actual.getString("items[6698948].transportId")); + assertEquals(expected.getString("items[6698948].description"), + actual.getString("items[6698948].description")); + assertEquals(expected.getString("items[6698948].platformName"), + actual.getString("items[6698948].platformName")); + // Delete the new netlist given() .log().ifValidationFails(LogDetail.ALL, true) @@ -254,7 +380,7 @@ void testPostAndDeleteNetlist() throws Exception .then() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() - .statusCode(is(HttpServletResponse.SC_OK)) + .statusCode(is(HttpServletResponse.SC_NO_CONTENT)) ; // Get the new netlist and assert it is not found @@ -278,25 +404,33 @@ void testPostAndDeleteNetlist() throws Exception @TestTemplate void testCnvtANL() throws Exception { - String inputJson = getJsonFromResource("netlist_cnvtanl_input_data.json"); + String inputTxt = getJsonFromResource("netlist_cnvtanl_input_data.txt"); - given() + JsonPath expected = getJsonPathFromResource("netlist_cnvtanl_expected.json"); + + ExtractableResponse response = given() .log().ifValidationFails(LogDetail.ALL, true) .accept(MediaType.APPLICATION_JSON) - .contentType(MediaType.APPLICATION_JSON) + .contentType(MediaType.TEXT_PLAIN) .filter(sessionFilter) .header("Authorization", authHeader) - .body(inputJson) + .body(inputTxt) .when() .redirects().follow(true) .redirects().max(3) - .post("netlist") + .post("cnvtnl") .then() .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) - .body(hasItem(getJsonPathFromResource("netlist_cnvtanl_expected.json"))) + .extract() ; + + Map actual = response.jsonPath().getMap("items.6698948"); + assertNotNull(actual); + assertEquals(expected.get("transportId"), actual.get("transportId")); + assertEquals(expected.get("platformName"), actual.get("platformName")); + assertEquals(expected.get("description"), actual.get("description")); } private Long storeSite(String jsonPath) throws Exception diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json index e69de29bb..b8b0e6926 100644 --- a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_expected.json @@ -0,0 +1,5 @@ +{ + "transportId" : "6698948", + "platformName" : "Stream_test_site", + "description" : "platform associated with stream" +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.json deleted file mode 100644 index e69de29bb..000000000 diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.txt b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.txt new file mode 100644 index 000000000..f14d83ae3 --- /dev/null +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_cnvtanl_input_data.txt @@ -0,0 +1,4 @@ +# This is a test comment line +# +# +6698948: Stream_test_site platform associated with stream diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json index e69de29bb..30fd1b9b9 100644 --- a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_expected.json @@ -0,0 +1,14 @@ +{ + "netlistId" : null, + "name" : "Distributed Network List", + "transportMediumType" : "goes", + "siteNameTypePref" : "Stream", + "lastModifyTime" : 1736356819000, + "items" : { + "6698948" : { + "transportId" : "6698948", + "platformName" : "Stream test site", + "description" : "platform associated with stream" + } + } +} \ No newline at end of file diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_expected.json deleted file mode 100644 index e69de29bb..000000000 diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_get_refs_filtered_expected.json deleted file mode 100644 index e69de29bb..000000000 diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json index 37b7ee68d..30fd1b9b9 100644 --- a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json +++ b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_insert_data.json @@ -1,7 +1,7 @@ { "netlistId" : null, "name" : "Distributed Network List", - "transportMediumType" : "GOES", + "transportMediumType" : "goes", "siteNameTypePref" : "Stream", "lastModifyTime" : 1736356819000, "items" : { diff --git a/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json b/opendcs-rest-api/src/test/resources/org/opendcs/odcsapi/res/it/OPEN_TSDB/netlist_post_delete_expected.json deleted file mode 100644 index e69de29bb..000000000 From 03849a33e9561544279dc7f1c3d3f9338d3e7eae Mon Sep 17 00:00:00 2001 From: zack-rma Date: Mon, 20 Jan 2025 14:31:24 -0800 Subject: [PATCH 7/7] Replaced DatabaseIO class variable with local variable --- .../opendcs/odcsapi/res/NetlistResources.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java index 34758cc94..2760ff51a 100644 --- a/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java +++ b/opendcs-rest-api/src/main/java/org/opendcs/odcsapi/res/NetlistResources.java @@ -57,7 +57,7 @@ public class NetlistResources extends OpenDcsResource { @Context HttpHeaders httpHeaders; - DatabaseIO dbIo; + @GET @Path("netlistrefs") @@ -66,6 +66,7 @@ public class NetlistResources extends OpenDcsResource public Response getNetlistRefs(@QueryParam("tmtype") String tmtype) throws DbException { + DatabaseIO dbIo = null; try { dbIo = getLegacyDatabase(); @@ -86,7 +87,10 @@ public Response getNetlistRefs(@QueryParam("tmtype") String tmtype) } finally { - dbIo.close(); + if (dbIo != null) + { + dbIo.close(); + } } } @@ -125,6 +129,7 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) throw new MissingParameterException("Missing required netlistid parameter."); } + DatabaseIO dbIo = null; try { dbIo = getLegacyDatabase(); @@ -144,7 +149,10 @@ public Response getNetList(@QueryParam("netlistid") Long netlistId) } finally { - dbIo.close(); + if (dbIo != null) + { + dbIo.close(); + } } } @@ -182,6 +190,7 @@ static ApiNetList map(NetworkList nl) public Response postNetlist(ApiNetList netList) throws DbException, WebAppException { + DatabaseIO dbIo = null; try { if (netList == null) @@ -199,7 +208,10 @@ public Response postNetlist(ApiNetList netList) } finally { - dbIo.close(); + if (dbIo != null) + { + dbIo.close(); + } } } @@ -242,6 +254,7 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) throw new MissingParameterException("Missing required netlistid parameter."); } + DatabaseIO dbIo = null; try { dbIo = getLegacyDatabase(); @@ -286,7 +299,10 @@ public Response deleteNetlist(@QueryParam("netlistid") Long netlistId) } finally { - dbIo.close(); + if (dbIo != null) + { + dbIo.close(); + } } }