From 527038c8c3cbc1dc2655ef72d636ce0c2cf0fe7c Mon Sep 17 00:00:00 2001 From: Lars Johansson <lars.johansson@ess.eu> Date: Mon, 6 Feb 2023 14:06:22 +0100 Subject: [PATCH] Refactor utilities for integration tests --- .../names/docker/ITUtilNameElement.java | 93 +++++-------------- .../names/docker/ITUtilStructureElement.java | 70 +++++--------- .../org/openepics/names/docker/NamesIT.java | 2 +- 3 files changed, 46 insertions(+), 119 deletions(-) diff --git a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java index 8cc5a56f..2f32dfea 100644 --- a/src/test/java/org/openepics/names/docker/ITUtilNameElement.java +++ b/src/test/java/org/openepics/names/docker/ITUtilNameElement.java @@ -414,66 +414,6 @@ public class ITUtilNameElement { // ---------------------------------------------------------------------------------------------------- - /** - * Utility method to (try to) validate a name element from given json and assert expected response. - * Method expected to NOT succeed to validate - * but instead assert response code (possibly also a non-empty message). - * - * @param json json - * @param nameCommand name command - * @param responseCode response code - */ - public static void assertValidate(String json, NameCommand nameCommand, int responseCode) { - assertValidate(AuthorizationChoice.NONE, json, nameCommand, responseCode, Boolean.FALSE, Boolean.FALSE); - } - /** - * Utility method to (try to) validate a name element from given json and assert expected response. - * Method expected to NOT succeed to validate - * but instead assert response code (possibly also a non-empty message). - * - * @param json json - * @param nameCommand name command - * @param responseCode response code - * @param expected expected - * @param expectedMessageEmpty expected message empty - */ - public static void assertValidate(String json, NameCommand nameCommand, int responseCode, Boolean expected, Boolean expectedMessageEmpty) { - assertValidate(AuthorizationChoice.NONE, json, nameCommand, responseCode, expected, expectedMessageEmpty); - } - /** - * Utility method to (try to) validate a name element from given json and assert expected response. - * Method expected to NOT succeed to validate - * but instead assert response code (possibly also a non-empty message). - * - * @param authorizationChoice authorization choice (none, user, admin) - * @param json json - * @param nameCommand name command - * @param responseCode response code - * @param expected expected - * @param expectedMessageEmpty expected message empty - */ - public static void assertValidate(AuthorizationChoice authorizationChoice, String json, NameCommand nameCommand, int responseCode, Boolean expected, Boolean expectedMessageEmpty) { - String validatePath = getValidatePath(nameCommand); - - try { - String[] response = null; - - response = ITUtil.runShellCommand(ITUtil.curlGetPathJson(authorizationChoice, EndpointChoice.NAMES, validatePath, json)); - ITUtil.assertResponseLength2Code(response, responseCode); - if (response[1] != null) { - ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBooleanList.class), expected, expectedMessageEmpty); - } - } catch (IOException e) { - fail(); - } catch (InterruptedException e) { - fail(); - } catch (Exception e) { - fail(); - } - } - - // -------------------- - /** * Utility method to validate name element command for create and assert expected response. * @@ -484,7 +424,7 @@ public class ITUtilNameElement { */ public static void assertValidate(NameElementCommandCreate nameElementCommand, NameCommand nameCommand, String index, Boolean expected) { nameElementCommand.setIndex(index); - assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandCreate[] {nameElementCommand}), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandCreate[] {nameElementCommand}), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element command for create and assert expected response. @@ -494,7 +434,7 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandCreate nameElementCommand, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandCreate[] {nameElementCommand}), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandCreate[] {nameElementCommand}), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element command for update and assert expected response. @@ -504,7 +444,7 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandUpdate nameElementCommand, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandUpdate[] {nameElementCommand}), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandUpdate[] {nameElementCommand}), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element command for confirm and assert expected response. @@ -514,7 +454,7 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandConfirm nameElementCommand, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandConfirm[] {nameElementCommand}), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new NameElementCommandConfirm[] {nameElementCommand}), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element commands for create and assert expected response. @@ -524,7 +464,7 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandCreate[] nameElementCommands, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element commands for update and assert expected response. @@ -534,7 +474,7 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandUpdate[] nameElementCommands, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate name element commands for confirm and assert expected response. @@ -544,7 +484,17 @@ public class ITUtilNameElement { * @param expected expected response */ public static void assertValidate(NameElementCommandConfirm[] nameElementCommands, NameCommand nameCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(nameElementCommands), nameCommand, HttpURLConnection.HTTP_OK, expected); + } + /** + * Utility method to validate json and assert expected response. + * + * @param json json + * @param nameCommand name command + * @param responseCode response code + */ + public static void assertValidate(String json, NameCommand nameCommand, int responseCode) { + assertValidate(AuthorizationChoice.NONE, json, nameCommand, responseCode, null); } /** * Utility method to validate json and assert expected response. @@ -552,17 +502,20 @@ public class ITUtilNameElement { * @param authorizationChoice authorization choice (none, user, admin) * @param json json * @param nameCommand name command + * @param responseCode response code * @param expected expected response */ - public static void assertValidate(AuthorizationChoice authorizationChoice, String json, NameCommand nameCommand, Boolean expected) { + public static void assertValidate(AuthorizationChoice authorizationChoice, String json, NameCommand nameCommand, int responseCode, Boolean expected) { String path = getValidatePath(nameCommand); try { String[] response = null; response = ITUtil.runShellCommand(ITUtil.curlGetPathJson(authorizationChoice, EndpointChoice.NAMES, path, json)); - ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_OK); - ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBooleanList.class), expected); + ITUtil.assertResponseLength2Code(response, responseCode); + if (expected != null) { + ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBooleanList.class), expected); + } } catch (IOException e) { fail(); } catch (InterruptedException e) { diff --git a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java index 26d78dd8..56389d08 100644 --- a/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java +++ b/src/test/java/org/openepics/names/docker/ITUtilStructureElement.java @@ -403,45 +403,6 @@ public class ITUtilStructureElement { // ---------------------------------------------------------------------------------------------------- - /** - * Utility method to (try to) validate a structure element from given json and assert expected response. - * Method expected to NOT succeed to validate - * but instead assert response code (possibly also a non-empty message). - * - * @param json json - * @param structureCommand structure command - * @param responseCode response code - */ - public static void assertValidate(String json, StructureCommand structureCommand, int responseCode) { - assertValidate(AuthorizationChoice.NONE, json, structureCommand, responseCode); - } - /** - * Utility method to (try to) validate a structure element from given json and assert expected response. - * Method expected to NOT succeed to validate - * but instead assert response code (possibly also a non-empty message). - * - * @param authorizationChoice authorization choice (none, user, admin) - * @param json json - * @param structureCommand structure command - * @param responseCode response code - */ - public static void assertValidate(AuthorizationChoice authorizationChoice, String json, StructureCommand structureCommand, int responseCode) { - String validatePath = getValidatePath(structureCommand); - - try { - String[] response = ITUtil.runShellCommand(ITUtil.curlGetPathJson(authorizationChoice, EndpointChoice.STRUCTURES, validatePath, json)); - ITUtil.assertResponseLength2Code(response, responseCode); - } catch (IOException e) { - fail(); - } catch (InterruptedException e) { - fail(); - } catch (Exception e) { - fail(); - } - } - - // -------------------- - /** * Utility method to validate structure element command for create and assert expected response. * @@ -450,7 +411,7 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandCreate structureElementCommand, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandCreate[] {structureElementCommand}), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandCreate[] {structureElementCommand}), structureCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate structure element command for update and assert expected response. @@ -460,7 +421,7 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandUpdate structureElementCommand, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandUpdate[] {structureElementCommand}), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandUpdate[] {structureElementCommand}), structureCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate structure element command for confirm and assert expected response. @@ -470,7 +431,7 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandConfirm structureElementCommand, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandConfirm[] {structureElementCommand}), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(new StructureElementCommandConfirm[] {structureElementCommand}), structureCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate structure element commands for create and assert expected response. @@ -480,7 +441,7 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandCreate[] structureElementCommands, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate structure element commands for update and assert expected response. @@ -490,7 +451,7 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandUpdate[] structureElementCommands, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, HttpURLConnection.HTTP_OK, expected); } /** * Utility method to validate structure element commands for confirm and assert expected response. @@ -500,7 +461,17 @@ public class ITUtilStructureElement { * @param expected expected response */ public static void assertValidate(StructureElementCommandConfirm[] structureElementCommands, StructureCommand structureCommand, Boolean expected) { - assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, expected); + assertValidate(AuthorizationChoice.NONE, object2Json(structureElementCommands), structureCommand, HttpURLConnection.HTTP_OK, expected); + } + /** + * Utility method to validate structure elements and assert expected response. + * + * @param json json + * @param structureCommand structure command + * @param responseCode response code + */ + public static void assertValidate(String json, StructureCommand structureCommand, int responseCode) { + assertValidate(AuthorizationChoice.NONE, json, structureCommand, responseCode, null); } /** * Utility method to validate structure elements and assert expected response. @@ -508,17 +479,20 @@ public class ITUtilStructureElement { * @param authorizationChoice authorization choice (none, user, admin) * @param json json * @param structureCommand structure command + * @param responseCode response code * @param expected expected response */ - public static void assertValidate(AuthorizationChoice authorizationChoice, String json, StructureCommand structureCommand, Boolean expected) { + public static void assertValidate(AuthorizationChoice authorizationChoice, String json, StructureCommand structureCommand, int responseCode, Boolean expected) { String path = getValidatePath(structureCommand); try { String[] response = null; response = ITUtil.runShellCommand(ITUtil.curlGetPathJson(authorizationChoice, EndpointChoice.STRUCTURES, path, json)); - ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_OK); - ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBooleanList.class), expected); + ITUtil.assertResponseLength2Code(response, responseCode); + if (expected != null) { + ITUtil.assertEqualsResponseBoolean(mapper.readValue(response[1], ResponseBooleanList.class), expected); + } } catch (IOException e) { fail(); } catch (InterruptedException e) { diff --git a/src/test/java/org/openepics/names/docker/NamesIT.java b/src/test/java/org/openepics/names/docker/NamesIT.java index 810c4a48..e5671591 100644 --- a/src/test/java/org/openepics/names/docker/NamesIT.java +++ b/src/test/java/org/openepics/names/docker/NamesIT.java @@ -504,7 +504,7 @@ class NamesIT { ITUtilNameElement.assertValidate( "[" + mapper.writeValueAsString(nameElementCommandCreate) + "," + mapper.writeValueAsString(nameElementCommandCreate2) + "]", - NameCommand.CREATE, HttpURLConnection.HTTP_OK, Boolean.TRUE, Boolean.TRUE); + NameCommand.CREATE, HttpURLConnection.HTTP_OK); ITUtilNameElement.assertValidate(nameElementCommandCreate, NameCommand.CREATE, Boolean.TRUE); ITUtilNameElement.assertValidate(nameElementCommandCreate2, NameCommand.CREATE, Boolean.TRUE); -- GitLab