From 6caf2f9a2f9daa05592a94a317d583f27355a042 Mon Sep 17 00:00:00 2001 From: Lars Johansson <lars.johansson@ess.eu> Date: Thu, 16 Jun 2022 10:11:58 +0200 Subject: [PATCH] Ensure maximum one entry is latest for any given uuid Ensure maximum one entry is latest for any given uuid. Update integration tests. --- .../names/util/StructureElementUtil.java | 12 +++---- .../org/openepics/names/docker/ITUtil.java | 36 +++++++++++++++++++ .../names/docker/ITUtilNameElement.java | 31 ++++++++++++++++ .../names/docker/ITUtilStructureElement.java | 33 +++++++++++++++++ 4 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openepics/names/util/StructureElementUtil.java b/src/main/java/org/openepics/names/util/StructureElementUtil.java index 919abe93..271182e6 100644 --- a/src/main/java/org/openepics/names/util/StructureElementUtil.java +++ b/src/main/java/org/openepics/names/util/StructureElementUtil.java @@ -305,7 +305,7 @@ public class StructureElementUtil { systemGroup.getUuid(), null, systemGroup.getName(), systemGroup.getMnemonic(), mnemonicpath, 1, - systemGroup.getDescription(), Status.PENDING, systemGroup.isLatest(), systemGroup.isDeleted(), + systemGroup.getDescription(), Status.PENDING, Boolean.FALSE, systemGroup.isDeleted(), systemGroup.getRequested(), systemGroup.getRequestedBy(), systemGroup.getRequestedComment()); } /** @@ -349,7 +349,7 @@ public class StructureElementUtil { system.getUuid(), system.getParentUuid(), system.getName(), system.getMnemonic(), mnemonicpath, 2, - system.getDescription(), Status.PENDING, system.isLatest(), system.isDeleted(), + system.getDescription(), Status.PENDING, Boolean.FALSE, system.isDeleted(), system.getRequested(), system.getRequestedBy(), system.getRequestedComment()); } /** @@ -393,7 +393,7 @@ public class StructureElementUtil { subsystem.getUuid(), subsystem.getParentUuid(), subsystem.getName(), subsystem.getMnemonic(), mnemonicpath, 3, - subsystem.getDescription(), Status.PENDING, subsystem.isLatest(), subsystem.isDeleted(), + subsystem.getDescription(), Status.PENDING, Boolean.FALSE, subsystem.isDeleted(), subsystem.getRequested(), subsystem.getRequestedBy(), subsystem.getRequestedComment()); } @@ -438,7 +438,7 @@ public class StructureElementUtil { discipline.getUuid(), null, discipline.getName(), discipline.getMnemonic(), mnemonicpath, 1, - discipline.getDescription(), Status.PENDING, discipline.isLatest(), discipline.isDeleted(), + discipline.getDescription(), Status.PENDING, Boolean.FALSE, discipline.isDeleted(), discipline.getRequested(), discipline.getRequestedBy(), discipline.getRequestedComment()); } /** @@ -482,7 +482,7 @@ public class StructureElementUtil { deviceGroup.getUuid(), deviceGroup.getParentUuid(), deviceGroup.getName(), deviceGroup.getMnemonic(), mnemonicpath, 2, - deviceGroup.getDescription(), Status.PENDING, deviceGroup.isLatest(), deviceGroup.isDeleted(), + deviceGroup.getDescription(), Status.PENDING, Boolean.FALSE, deviceGroup.isDeleted(), deviceGroup.getRequested(), deviceGroup.getRequestedBy(), deviceGroup.getRequestedComment()); } /** @@ -526,7 +526,7 @@ public class StructureElementUtil { deviceType.getUuid(), deviceType.getParentUuid(), deviceType.getName(), deviceType.getMnemonic(), mnemonicpath, 3, - deviceType.getDescription(), Status.PENDING, deviceType.isLatest(), deviceType.isDeleted(), + deviceType.getDescription(), Status.PENDING, Boolean.FALSE, deviceType.isDeleted(), deviceType.getRequested(), deviceType.getRequestedBy(), deviceType.getRequestedComment()); } diff --git a/src/test/java/org/openepics/names/docker/ITUtil.java b/src/test/java/org/openepics/names/docker/ITUtil.java index 4598c419..ec376d2d 100644 --- a/src/test/java/org/openepics/names/docker/ITUtil.java +++ b/src/test/java/org/openepics/names/docker/ITUtil.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Map; +import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -307,6 +309,40 @@ public class ITUtil { // ---------------------------------------------------------------------------------------------------- + /** + * Add one to count for key in map. + * + * @param key key + * @param map map + */ + static void addOne(UUID key, Map<UUID, Long> map) { + addOne(key, map, null); + } + /** + * Add one to count for key in map. + * + * @param key key + * @param map map + * @param mapNok map nok + */ + static void addOne(UUID key, Map<UUID, Long> map, Map<UUID, Long> mapNok) { + if (key == null) { + return; + } + + Long no = map.get(key); + if (mapNok != null && no == null) { + no = mapNok.get(key); + no = no != null ? no + 1 : 1; + mapNok.put(key, no); + } else { + no = no != null ? no + 1 : 1; + map.put(key, no); + } + } + + // ---------------------------------------------------------------------------------------------------- + // enum for http methods static enum MethodChoice {POST, GET, PUT, DELETE, PATCH}; diff --git a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java index 047fe843..a00c0370 100644 --- a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java +++ b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java @@ -19,6 +19,7 @@ package org.openepics.names.docker; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -26,7 +27,10 @@ import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.Map; +import java.util.TreeMap; import java.util.UUID; +import java.util.Map.Entry; import org.openepics.names.docker.ITUtil.AuthorizationChoice; import org.openepics.names.docker.ITUtil.EndpointChoice; @@ -209,6 +213,9 @@ public class ITUtilNameElement { assertTrue(responsePageNameElements.getListSize() >= expectedGreaterThanOrEqual); } + // expected value for latest for items in list + assertLatest(responsePageNameElements); + return responsePageNameElements.getListSize(); } catch (IOException e) { fail(); @@ -218,6 +225,30 @@ public class ITUtilNameElement { return null; } + /** + * Assert that response page with list of name elements has maximum one entry that is latest + * for any given uuid, which may not have status {@link Status#PENDING}. + * + * @param actual response page with list of name elements + */ + public static void assertLatest(ResponsePageNameElements actual) { + // latest + // status pending not with latest + // maximum one item for any given uuid in list may be latest + + assertNotNull(actual); + Map<UUID, Long> mapUuidCountLatest = new TreeMap<>(); + for (NameElement nameElement : actual.getList()) { + if (nameElement.isLatest()) { + ITUtil.addOne(nameElement.getUuid(), mapUuidCountLatest); + assertNotEquals(Status.PENDING, nameElement.getStatus()); + } + } + for (Entry<UUID, Long> entry : mapUuidCountLatest.entrySet()) { + assertTrue(entry.getValue() <= 1); + } + } + /** * Utility method to get name equivalence of a mnemonic or name. * diff --git a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java index fdd64536..cc06611c 100644 --- a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java +++ b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java @@ -19,6 +19,7 @@ package org.openepics.names.docker; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -26,6 +27,9 @@ import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; import java.util.UUID; import org.openepics.names.docker.ITUtil.AuthorizationChoice; @@ -204,6 +208,8 @@ public class ITUtilStructureElement { response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_STRUCTURES + queryString); ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_OK); responsePageStructureElements = mapper.readValue(response[1], ResponsePageStructureElements.class); + + // expected number of items in list if (expectedLessThanOrEqual >= 0) { assertTrue(responsePageStructureElements.getListSize() <= expectedLessThanOrEqual); } @@ -211,6 +217,9 @@ public class ITUtilStructureElement { assertTrue(responsePageStructureElements.getListSize() >= expectedGreaterThanOrEqual); } + // expected value for latest for items in list + assertLatest(responsePageStructureElements); + return responsePageStructureElements.getListSize(); } catch (IOException e) { fail(); @@ -220,6 +229,30 @@ public class ITUtilStructureElement { return null; } + /** + * Assert that response page with list of structure elements has maximum one entry that is latest + * for any given uuid, which may not have status {@link Status#PENDING}. + * + * @param actual response page with list of structure elements + */ + public static void assertLatest(ResponsePageStructureElements actual) { + // latest + // status pending not with latest + // maximum one item for any given uuid in list may be latest + + assertNotNull(actual); + Map<UUID, Long> mapUuidCountLatest = new TreeMap<>(); + for (StructureElement structureElement : actual.getList()) { + if (structureElement.isLatest()) { + ITUtil.addOne(structureElement.getUuid(), mapUuidCountLatest); + assertNotEquals(Status.PENDING, structureElement.getStatus()); + } + } + for (Entry<UUID, Long> entry : mapUuidCountLatest.entrySet()) { + assertTrue(entry.getValue() <= 1); + } + } + /** * Utility method to get name equivalence of a mnemonic or name. * -- GitLab