From e1a5af10dc98cd5a7ad888d5e0944cfaafeffbb1 Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Tue, 12 Apr 2022 10:07:58 +0200
Subject: [PATCH] Refactor test utilities

---
 .../openepics/names/util/ValidateUtil.java    |  4 ++-
 .../org/openepics/names/docker/ITUtil.java    |  7 -----
 .../names/docker/ITUtilNameElement.java       | 28 +++++++++++++-----
 .../names/docker/ITUtilStructureElement.java  | 29 +++++++++++++++----
 .../org/openepics/names/docker/NamesIT.java   |  8 ++---
 .../names/docker/StructuresDeviceGroupIT.java |  4 +--
 .../names/docker/StructuresDeviceTypeIT.java  | 10 +++----
 .../names/docker/StructuresDisciplineIT.java  |  4 +--
 .../names/docker/StructuresSubsystemIT.java   | 14 ++++-----
 .../names/docker/StructuresSystemGroupIT.java |  4 +--
 .../names/docker/StructuresSystemIT.java      | 12 ++++----
 11 files changed, 76 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/openepics/names/util/ValidateUtil.java b/src/main/java/org/openepics/names/util/ValidateUtil.java
index 46b3bc1d..4291100a 100644
--- a/src/main/java/org/openepics/names/util/ValidateUtil.java
+++ b/src/main/java/org/openepics/names/util/ValidateUtil.java
@@ -1379,7 +1379,9 @@ public class ValidateUtil {
             validateCondition(path.length == 2 || path.length == 3, HttpStatus.BAD_REQUEST, ValidateUtil.SUBSYSTEM + " mnemonic path " + ValidateUtil.IS_NOT_VALID, path.length + ", " + mnemonicpath);
 
             // mnemonic
-            if (path.length == 2) {
+            if (path.length == 3) {
+                throw ExceptionUtil.createServiceHttpStatusExceptionBadRequest(ValidateUtil.SUBSYSTEM + " mnemonic path " + ValidateUtil.IS_NOT_VALID, path.length + ", " + mnemonicpath, null);
+            } else if (path.length == 2) {
                 validateCondition(!StringUtils.equals(path[0], path[1]), HttpStatus.BAD_REQUEST, ValidateUtil.SYSTEM + " mnemonic path duplicate", mnemonicpath);
             } else {
                 validateCondition(!StringUtils.equals(path[0], path[1]), HttpStatus.BAD_REQUEST, ValidateUtil.SYSTEM + " mnemonic path duplicate", mnemonicpath);
diff --git a/src/test/java/org/openepics/names/docker/ITUtil.java b/src/test/java/org/openepics/names/docker/ITUtil.java
index 56c2a047..ddde23b4 100644
--- a/src/test/java/org/openepics/names/docker/ITUtil.java
+++ b/src/test/java/org/openepics/names/docker/ITUtil.java
@@ -19,7 +19,6 @@
 package org.openepics.names.docker;
 
 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 java.io.BufferedReader;
@@ -267,9 +266,6 @@ public class ITUtil {
     static void assertEqualsResponseBoolean(ResponseBoolean actual, Boolean expected) {
         assertNotNull(actual);
         assertEquals(expected, actual.getValue());
-        if (Boolean.FALSE.equals(expected)) {
-            assertFalse(StringUtils.isEmpty(actual.getMessage()));
-        }
     }
     /**
      * Assert response boolean available with expected response and expected message behavior.
@@ -293,9 +289,6 @@ public class ITUtil {
     static void assertEqualsResponseBoolean(ResponseBooleanList actual, Boolean expected) {
         assertNotNull(actual);
         assertEquals(expected, actual.getValue());
-        if (Boolean.FALSE.equals(expected)) {
-            assertFalse(StringUtils.isEmpty(actual.getMessage()));
-        }
     }
     /**
      * Assert response boolean list available with expected response and expected message behavior.
diff --git a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java
index 235dce30..fbeba623 100644
--- a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java
+++ b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java
@@ -216,6 +216,16 @@ public class ITUtilNameElement {
         }
     }
 
+    /**
+     * Utility method to check if name exists.
+     *
+     * @param name name
+     * @param expected expected response
+     */
+    public static void assertExists(String name, Boolean expected) {
+        assertExists(name, expected, null);
+    }
+
     /**
      * Utility method to check if name exists.
      *
@@ -230,7 +240,11 @@ public class ITUtilNameElement {
 
             response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_NAMES + "/exists/" + name);
             ITUtil.assertResponseLength2CodeOK(response);
-            ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected, expectedMessageEmpty);
+            if (expectedMessageEmpty != null) {
+                ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected, expectedMessageEmpty);
+            } else {
+                ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
+            }
         } catch (IOException e) {
             fail();
         } catch (Exception e) {
@@ -249,9 +263,9 @@ public class ITUtilNameElement {
             ObjectMapper mapper = new ObjectMapper();
             String[] response = null;
 
-          response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_NAMES + "/islegacy/" + name);
-          ITUtil.assertResponseLength2CodeOK(response);
-          ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
+            response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_NAMES + "/islegacy/" + name);
+            ITUtil.assertResponseLength2CodeOK(response);
+            ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
         } catch (IOException e) {
             fail();
         } catch (Exception e) {
@@ -270,9 +284,9 @@ public class ITUtilNameElement {
             ObjectMapper mapper = new ObjectMapper();
             String[] response = null;
 
-          response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_NAMES + "/isvalidtocreate/" + name);
-          ITUtil.assertResponseLength2CodeOK(response);
-          ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
+            response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_NAMES + "/isvalidtocreate/" + name);
+            ITUtil.assertResponseLength2CodeOK(response);
+            ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
         } catch (IOException e) {
             fail();
         } catch (Exception e) {
diff --git a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java
index 3f847044..97f78820 100644
--- a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java
+++ b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java
@@ -219,19 +219,34 @@ public class ITUtilStructureElement {
     /**
      * Utility method to check if structure exists.
      *
+     * @param type type
      * @param mnemonic mnemonic
+     * @param expected expected response
+     */
+    public static void assertExists(Type type, String mnemonic, Boolean expected) {
+        assertExists(type, mnemonic, expected, null);
+    }
+
+    /**
+     * Utility method to check if structure exists.
+     *
      * @param type type
+     * @param mnemonic mnemonic
      * @param expected expected response
      * @param expectedMessageEmpty expected message empty
      */
-    public static void assertExists(String mnemonic, Type type, Boolean expected, Boolean expectedMessageEmpty) {
+    public static void assertExists(Type type, String mnemonic, Boolean expected, Boolean expectedMessageEmpty) {
         try {
             ObjectMapper mapper = new ObjectMapper();
             String[] response = null;
 
             response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_STRUCTURES + "/exists/" + type.toString() + "/" + mnemonic);
             ITUtil.assertResponseLength2CodeOK(response);
-            ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected, expectedMessageEmpty);
+            if (expectedMessageEmpty != null) {
+                ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected, expectedMessageEmpty);
+            } else {
+                ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
+            }
         } catch (IOException e) {
             fail();
         } catch (Exception e) {
@@ -243,14 +258,18 @@ public class ITUtilStructureElement {
      * Utility method to check if structure is valid to create.
      * Method will not produce a message.
      *
-     * @param mnemonicpath mnemonic path
      * @param type type
+     * @param mnemonicpath mnemonic path
      * @param expected expected response
      */
-    public static void assertIsValidToCreate(String mnemonicpath, Type type, Boolean expected) {
+    public static void assertIsValidToCreate(Type type, String mnemonicpath, Boolean expected) {
         try {
-            String[] response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_STRUCTURES + "/isvalidtocreate/" + type.toString() + "/" + mnemonicpath);
+            ObjectMapper mapper = new ObjectMapper();
+            String[] response = null;
+
+            response = ITUtil.doGetJson(ITUtil.HTTP_IP_PORT_NAMING_API_V1_STRUCTURES + "/isvalidtocreate/" + type.toString() + "/" + mnemonicpath);
             ITUtil.assertResponseLength2CodeOK(response);
+            ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBoolean.class), expected);
         } catch (IOException e) {
             fail();
         } catch (Exception e) {
diff --git a/src/test/java/org/openepics/names/docker/NamesIT.java b/src/test/java/org/openepics/names/docker/NamesIT.java
index 0697e6c9..8b393d43 100644
--- a/src/test/java/org/openepics/names/docker/NamesIT.java
+++ b/src/test/java/org/openepics/names/docker/NamesIT.java
@@ -176,7 +176,7 @@ public class NamesIT {
         // what
         //     read        exists name
 
-        ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-071", Boolean.FALSE, Boolean.TRUE);
+        ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-071", Boolean.FALSE);
     }
 
     @Test
@@ -216,7 +216,7 @@ public class NamesIT {
         try {
             NameElement nameElement = new NameElement();
 
-            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-051", Boolean.FALSE, Boolean.TRUE);
+            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-051", Boolean.FALSE);
             ITUtilNameElement.assertIsLegacy("RFQ-010PRL:EMR-RFA-051", Boolean.FALSE);
             ITUtilNameElement.assertIsValidToCreate("RFQ-010PRL:EMR-RFA-051", Boolean.TRUE);
 
@@ -358,7 +358,7 @@ public class NamesIT {
                     "description", Status.APPROVED, Boolean.TRUE, Boolean.FALSE,
                     null, "test who", "comment");
 
-            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-052", Boolean.FALSE, Boolean.TRUE);
+            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-052", Boolean.FALSE);
             ITUtilNameElement.assertIsLegacy("RFQ-010PRL:EMR-RFA-052", Boolean.FALSE);
             ITUtilNameElement.assertIsValidToCreate("RFQ-010PRL:EMR-RFA-052", Boolean.TRUE);
 
@@ -372,7 +372,7 @@ public class NamesIT {
             createdNameElement = ITUtilNameElement.assertCreate(nameElement);
             nameElement = createdNameElement;
 
-            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-052", Boolean.TRUE, Boolean.TRUE);
+            ITUtilNameElement.assertExists("RFQ-010PRL:EMR-RFA-052", Boolean.TRUE);
             ITUtilNameElement.assertIsLegacy("RFQ-010PRL:EMR-RFA-052", Boolean.FALSE);
             ITUtilNameElement.assertIsValidToCreate("RFQ-010PRL:EMR-RFA-052", Boolean.FALSE);
 
diff --git a/src/test/java/org/openepics/names/docker/StructuresDeviceGroupIT.java b/src/test/java/org/openepics/names/docker/StructuresDeviceGroupIT.java
index 42580d07..98d497b4 100644
--- a/src/test/java/org/openepics/names/docker/StructuresDeviceGroupIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresDeviceGroupIT.java
@@ -126,8 +126,8 @@ public class StructuresDeviceGroupIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.DEVICEGROUP, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.DEVICEGROUP, Boolean.FALSE);
+            ITUtilStructureElement.assertExists(Type.DEVICEGROUP, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DEVICEGROUP, "Cc", Boolean.FALSE);
 
             ITUtilStructureElement.assertValidate("[{asdf]", StructureChoice.CREATE, HttpURLConnection.HTTP_BAD_REQUEST);
             ITUtilStructureElement.assertCreate("[{asdf]", HttpURLConnection.HTTP_BAD_REQUEST);
diff --git a/src/test/java/org/openepics/names/docker/StructuresDeviceTypeIT.java b/src/test/java/org/openepics/names/docker/StructuresDeviceTypeIT.java
index 93b75aa7..dec25c60 100644
--- a/src/test/java/org/openepics/names/docker/StructuresDeviceTypeIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresDeviceTypeIT.java
@@ -126,11 +126,11 @@ public class StructuresDeviceTypeIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.DEVICETYPE, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.DEVICETYPE, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Db-Cc", Type.DEVICETYPE, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Di-Cc", Type.DEVICETYPE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Di-Di", Type.DEVICETYPE, Boolean.FALSE);
+            ITUtilStructureElement.assertExists(Type.DEVICETYPE, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DEVICETYPE, "Cc",    Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DEVICETYPE, "Db-Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DEVICETYPE, "Di-Cc", Boolean.TRUE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DEVICETYPE, "Di-Di", Boolean.FALSE);
 
             ITUtilStructureElement.assertValidate("[{asdf]", StructureChoice.CREATE, HttpURLConnection.HTTP_BAD_REQUEST);
             ITUtilStructureElement.assertCreate("[{asdf]", HttpURLConnection.HTTP_BAD_REQUEST);
diff --git a/src/test/java/org/openepics/names/docker/StructuresDisciplineIT.java b/src/test/java/org/openepics/names/docker/StructuresDisciplineIT.java
index 3f8bf24b..da7701ad 100644
--- a/src/test/java/org/openepics/names/docker/StructuresDisciplineIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresDisciplineIT.java
@@ -98,8 +98,8 @@ public class StructuresDisciplineIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.DISCIPLINE, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.DISCIPLINE, Boolean.TRUE);
+            ITUtilStructureElement.assertExists(Type.DISCIPLINE, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.DISCIPLINE, "Cc", Boolean.TRUE);
 
             ITUtilStructureElement.assertValidate("[{asdf]", StructureChoice.CREATE, HttpURLConnection.HTTP_BAD_REQUEST);
             ITUtilStructureElement.assertCreate("[{asdf]", HttpURLConnection.HTTP_BAD_REQUEST);
diff --git a/src/test/java/org/openepics/names/docker/StructuresSubsystemIT.java b/src/test/java/org/openepics/names/docker/StructuresSubsystemIT.java
index e42cb7dc..1a6bf3b2 100644
--- a/src/test/java/org/openepics/names/docker/StructuresSubsystemIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresSubsystemIT.java
@@ -130,13 +130,13 @@ public class StructuresSubsystemIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.SUBSYSTEM, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.SUBSYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Sys-Sys", Type.SUBSYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Sys-Cc", Type.SUBSYSTEM, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-Sys-Sg", Type.SUBSYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-Sys-Sys", Type.SUBSYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-Sys-Cc", Type.SUBSYSTEM, Boolean.FALSE);
+            ITUtilStructureElement.assertExists(Type.SUBSYSTEM, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Cc",         Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Sys-Sys",    Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Sys-Cc",     Boolean.TRUE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Sg-Sys-Sg",  Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Sg-Sys-Sys", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SUBSYSTEM, "Sg-Sys-Cc",  Boolean.FALSE);
 
             ITUtilStructureElement.assertValidate("[{asdf]", StructureChoice.CREATE, HttpURLConnection.HTTP_BAD_REQUEST);
             ITUtilStructureElement.assertCreate("[{asdf]", HttpURLConnection.HTTP_BAD_REQUEST);
diff --git a/src/test/java/org/openepics/names/docker/StructuresSystemGroupIT.java b/src/test/java/org/openepics/names/docker/StructuresSystemGroupIT.java
index 60738c8c..affcd380 100644
--- a/src/test/java/org/openepics/names/docker/StructuresSystemGroupIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresSystemGroupIT.java
@@ -102,8 +102,8 @@ public class StructuresSystemGroupIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.SYSTEMGROUP, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.SYSTEMGROUP, Boolean.TRUE);
+            ITUtilStructureElement.assertExists(Type.SYSTEMGROUP, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEMGROUP, "Cc", Boolean.TRUE);
 
             // validate create
 
diff --git a/src/test/java/org/openepics/names/docker/StructuresSystemIT.java b/src/test/java/org/openepics/names/docker/StructuresSystemIT.java
index b25511a9..f19f039b 100644
--- a/src/test/java/org/openepics/names/docker/StructuresSystemIT.java
+++ b/src/test/java/org/openepics/names/docker/StructuresSystemIT.java
@@ -120,12 +120,12 @@ public class StructuresSystemIT {
         try {
             StructureElement structureElement = new StructureElement();
 
-            ITUtilStructureElement.assertExists("Cc", Type.SYSTEM, Boolean.FALSE, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-Cc", Type.SYSTEM, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("SG-Cc", Type.SYSTEM, Boolean.TRUE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-Sg", Type.SYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Sg-SG", Type.SYSTEM, Boolean.FALSE);
-            ITUtilStructureElement.assertIsValidToCreate("Cc", Type.SYSTEM, Boolean.TRUE);
+            ITUtilStructureElement.assertExists(Type.SYSTEM, "Cc", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEM, "Sg-Cc", Boolean.TRUE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEM, "SG-Cc", Boolean.TRUE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEM, "Sg-Sg", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEM, "Sg-SG", Boolean.FALSE);
+            ITUtilStructureElement.assertIsValidToCreate(Type.SYSTEM, "Cc",    Boolean.TRUE);
 
             ITUtilStructureElement.assertValidate("[{asdf]", StructureChoice.CREATE, HttpURLConnection.HTTP_BAD_REQUEST);
             ITUtilStructureElement.assertCreate("[{asdf]", HttpURLConnection.HTTP_BAD_REQUEST);
-- 
GitLab