From b7054f4f7bd410eef0f6a7fdda5794832613572e Mon Sep 17 00:00:00 2001 From: Graham Packer Date: Wed, 18 Dec 2024 10:15:26 +0000 Subject: [PATCH 1/2] Change tests to use throwables instead of class references in tests --- .../TestArchitectureResourceShould.java | 26 +++++++++---------- .../TestPatternResourcePutEnabledShould.java | 7 +++-- .../resources/TestPatternResourceShould.java | 26 +++++++++---------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java b/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java index 4e13e65..b242848 100644 --- a/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java +++ b/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java @@ -34,7 +34,7 @@ public class TestArchitectureResourceShould { @Test void return_a_404_when_an_invalid_namespace_is_provided_on_get_architectures() throws NamespaceNotFoundException { - when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenThrow(NamespaceNotFoundException.class); + when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenThrow(new NamespaceNotFoundException()); given() .when() @@ -62,7 +62,7 @@ void return_list_of_architecture_ids_when_valid_namespace_provided_on_get_archit @Test void return_a_404_when_invalid_namespace_is_provided_on_create_architecture() throws NamespaceNotFoundException { when(mockArchitectureStore.createArchitectureForNamespace(any(Architecture.class))) - .thenThrow(NamespaceNotFoundException.class); + .thenThrow(new NamespaceNotFoundException()); String architecture = "{ \"test\": \"json\" }"; @@ -127,15 +127,15 @@ private void verifyExpectedArchitectureForVersions(String namespace) throws Name static Stream provideParametersForArchitectureVersionTests() { return Stream.of( - Arguments.of("invalid", NamespaceNotFoundException.class, 404), - Arguments.of("valid", ArchitectureNotFoundException.class, 404), + Arguments.of("invalid", new NamespaceNotFoundException(), 404), + Arguments.of("valid", new ArchitectureNotFoundException(), 404), Arguments.of("valid", null, 200) ); } @ParameterizedTest @MethodSource("provideParametersForArchitectureVersionTests") - void respond_correctly_to_get_architecture_versions_query(String namespace, Class exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, NamespaceNotFoundException { + void respond_correctly_to_get_architecture_versions_query(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, NamespaceNotFoundException { var versions = List.of("1.0.0", "1.0.1"); if (exceptionToThrow != null) { when(mockArchitectureStore.getArchitectureVersions(any(Architecture.class))).thenThrow(exceptionToThrow); @@ -174,16 +174,16 @@ private void verifyExpectedGetArchitecture(String namespace) throws Architecture static Stream provideParametersForGetArchitectureTests() { return Stream.of( - Arguments.of("invalid", NamespaceNotFoundException.class, 404), - Arguments.of("valid", ArchitectureNotFoundException.class, 404), - Arguments.of("valid", ArchitectureVersionNotFoundException.class, 404), + Arguments.of("invalid", new NamespaceNotFoundException(), 404), + Arguments.of("valid", new ArchitectureNotFoundException(), 404), + Arguments.of("valid", new ArchitectureVersionNotFoundException(), 404), Arguments.of("valid", null, 200) ); } @ParameterizedTest @MethodSource("provideParametersForGetArchitectureTests") - void respond_correctly_to_get_architecture_for_a_specific_version_correctly(String namespace, Class exceptionToThrow, int expectedStatusCode) throws ArchitectureVersionNotFoundException, ArchitectureNotFoundException, NamespaceNotFoundException { + void respond_correctly_to_get_architecture_for_a_specific_version_correctly(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureVersionNotFoundException, ArchitectureNotFoundException, NamespaceNotFoundException { if (exceptionToThrow != null) { when(mockArchitectureStore.getArchitectureForVersion(any(Architecture.class))).thenThrow(exceptionToThrow); } else { @@ -211,16 +211,16 @@ void respond_correctly_to_get_architecture_for_a_specific_version_correctly(Stri static Stream provideParametersForCreateArchitectureTests() { return Stream.of( - Arguments.of( NamespaceNotFoundException.class, 404), - Arguments.of( ArchitectureNotFoundException.class, 404), - Arguments.of(ArchitectureVersionExistsException.class, 409), + Arguments.of( new NamespaceNotFoundException(), 404), + Arguments.of( new ArchitectureNotFoundException(), 404), + Arguments.of(new ArchitectureVersionExistsException(), 409), Arguments.of(null, 201) ); } @ParameterizedTest @MethodSource("provideParametersForCreateArchitectureTests") - void respond_correctly_to_create_architecture(Class exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, ArchitectureVersionExistsException, NamespaceNotFoundException { + void respond_correctly_to_create_architecture(Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, ArchitectureVersionExistsException, NamespaceNotFoundException { Architecture expectedArchitecture = new Architecture.ArchitectureBuilder() .setNamespace("test") .setVersion("1.0.1") diff --git a/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java b/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java index 1881e40..494a2a0 100644 --- a/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java +++ b/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java @@ -23,7 +23,6 @@ @QuarkusTest @ExtendWith(MockitoExtension.class) @TestProfile(AllowPutProfile.class) -@Disabled("This test fails due to a multiple injection problem with a conflicting mock in TestPatternResourceShould, will need to find a fix. (the tests do pass)") public class TestPatternResourcePutEnabledShould { @InjectMock @@ -31,15 +30,15 @@ public class TestPatternResourcePutEnabledShould { static Stream provideParametersForPutPatternTests() { return Stream.of( - Arguments.of( NamespaceNotFoundException.class, 404), - Arguments.of( PatternNotFoundException.class, 404), + Arguments.of( new NamespaceNotFoundException(), 404), + Arguments.of( new PatternNotFoundException(), 404), Arguments.of(null, 201) ); } @ParameterizedTest @MethodSource("provideParametersForPutPatternTests") - void respond_correctly_to_put_pattern_correctly(Class exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException { + void respond_correctly_to_put_pattern_correctly(Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException { Pattern expectedPattern = new Pattern.PatternBuilder() .setNamespace("test") diff --git a/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java b/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java index 190343d..4cfa052 100644 --- a/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java +++ b/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java @@ -34,7 +34,7 @@ public class TestPatternResourceShould { @Test void return_a_404_when_an_invalid_namespace_is_provided_on_get_patterns() throws NamespaceNotFoundException { - when(mockPatternStore.getPatternsForNamespace(anyString())).thenThrow(NamespaceNotFoundException.class); + when(mockPatternStore.getPatternsForNamespace(anyString())).thenThrow(new NamespaceNotFoundException()); given() .when() @@ -62,7 +62,7 @@ void return_list_of_pattern_ids_when_valid_namespace_provided_on_get_patterns() @Test void return_a_404_when_invalid_namespace_is_provided_on_create_pattern() throws NamespaceNotFoundException { when(mockPatternStore.createPatternForNamespace(any(Pattern.class))) - .thenThrow(NamespaceNotFoundException.class); + .thenThrow(new NamespaceNotFoundException()); String pattern = "{ \"test\": \"json\" }"; @@ -127,15 +127,15 @@ private void verifyExpectedPatternForVersions(String namespace) throws PatternNo static Stream provideParametersForPatternVersionTests() { return Stream.of( - Arguments.of("invalid", NamespaceNotFoundException.class, 404), - Arguments.of("valid", PatternNotFoundException.class, 404), + Arguments.of("invalid", new NamespaceNotFoundException(), 404), + Arguments.of("valid", new PatternNotFoundException(), 404), Arguments.of("valid", null, 200) ); } @ParameterizedTest @MethodSource("provideParametersForPatternVersionTests") - void respond_correctly_to_get_pattern_versions_query(String namespace, Class exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException { + void respond_correctly_to_get_pattern_versions_query(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException { var versions = List.of("1.0.0", "1.0.1"); if (exceptionToThrow != null) { when(mockPatternStore.getPatternVersions(any(Pattern.class))).thenThrow(exceptionToThrow); @@ -174,16 +174,16 @@ private void verifyExpectedGetPattern(String namespace) throws PatternNotFoundEx static Stream provideParametersForGetPatternTests() { return Stream.of( - Arguments.of("invalid", NamespaceNotFoundException.class, 404), - Arguments.of("valid", PatternNotFoundException.class, 404), - Arguments.of("valid", PatternVersionNotFoundException.class, 404), + Arguments.of("invalid", new NamespaceNotFoundException(), 404), + Arguments.of("valid", new PatternNotFoundException(), 404), + Arguments.of("valid", new PatternVersionNotFoundException(), 404), Arguments.of("valid", null, 200) ); } @ParameterizedTest @MethodSource("provideParametersForGetPatternTests") - void respond_correct_to_get_pattern_for_a_specific_version_correctly(String namespace, Class exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException, PatternVersionNotFoundException { + void respond_correct_to_get_pattern_for_a_specific_version_correctly(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException, PatternVersionNotFoundException { if (exceptionToThrow != null) { when(mockPatternStore.getPatternForVersion(any(Pattern.class))).thenThrow(exceptionToThrow); } else { @@ -211,16 +211,16 @@ void respond_correct_to_get_pattern_for_a_specific_version_correctly(String name static Stream provideParametersForCreatePatternTests() { return Stream.of( - Arguments.of( NamespaceNotFoundException.class, 404), - Arguments.of( PatternNotFoundException.class, 404), - Arguments.of(PatternVersionExistsException.class, 409), + Arguments.of( new NamespaceNotFoundException(), 404), + Arguments.of( new PatternNotFoundException(), 404), + Arguments.of(new PatternVersionExistsException(), 409), Arguments.of(null, 201) ); } @ParameterizedTest @MethodSource("provideParametersForCreatePatternTests") - void respond_correctly_to_create_pattern_correctly(Class exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, PatternVersionExistsException, NamespaceNotFoundException { + void respond_correctly_to_create_pattern_correctly(Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, PatternVersionExistsException, NamespaceNotFoundException { Pattern expectedPattern = new Pattern.PatternBuilder() .setNamespace("test") .setVersion("1.0.1") From 9888b1ffaf03f9a57d3669805175185856b20bba Mon Sep 17 00:00:00 2001 From: Graham Packer Date: Wed, 18 Dec 2024 10:33:44 +0000 Subject: [PATCH 2/2] Fix code format inconsistencies --- .../TestArchitectureResourceShould.java | 16 ++++++++-------- .../TestPatternResourcePutEnabledShould.java | 8 ++++---- .../resources/TestPatternResourceShould.java | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java b/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java index b242848..2975fd1 100644 --- a/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java +++ b/src/test/java/org/finos/calm/resources/TestArchitectureResourceShould.java @@ -47,7 +47,7 @@ void return_a_404_when_an_invalid_namespace_is_provided_on_get_architectures() t @Test void return_list_of_architecture_ids_when_valid_namespace_provided_on_get_architectures() throws NamespaceNotFoundException { - when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenReturn(Arrays.asList(12345,54321)); + when(mockArchitectureStore.getArchitecturesForNamespace(anyString())).thenReturn(Arrays.asList(12345, 54321)); given() .when() @@ -137,13 +137,13 @@ static Stream provideParametersForArchitectureVersionTests() { @MethodSource("provideParametersForArchitectureVersionTests") void respond_correctly_to_get_architecture_versions_query(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureNotFoundException, NamespaceNotFoundException { var versions = List.of("1.0.0", "1.0.1"); - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockArchitectureStore.getArchitectureVersions(any(Architecture.class))).thenThrow(exceptionToThrow); } else { when(mockArchitectureStore.getArchitectureVersions(any(Architecture.class))).thenReturn(versions); } - if (expectedStatusCode == 200 ) { + if(expectedStatusCode == 200) { String expectedBody = "{\"values\":[\"1.0.0\",\"1.0.1\"]}"; given() .when() @@ -184,14 +184,14 @@ static Stream provideParametersForGetArchitectureTests() { @ParameterizedTest @MethodSource("provideParametersForGetArchitectureTests") void respond_correctly_to_get_architecture_for_a_specific_version_correctly(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws ArchitectureVersionNotFoundException, ArchitectureNotFoundException, NamespaceNotFoundException { - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockArchitectureStore.getArchitectureForVersion(any(Architecture.class))).thenThrow(exceptionToThrow); } else { String architecture = "{ \"test\": \"json\" }"; when(mockArchitectureStore.getArchitectureForVersion(any(Architecture.class))).thenReturn(architecture); } - if (expectedStatusCode == 200) { + if(expectedStatusCode == 200) { given() .when() .get("/calm/namespaces/" + namespace + "/architectures/12/versions/1.0.0") @@ -211,8 +211,8 @@ void respond_correctly_to_get_architecture_for_a_specific_version_correctly(Stri static Stream provideParametersForCreateArchitectureTests() { return Stream.of( - Arguments.of( new NamespaceNotFoundException(), 404), - Arguments.of( new ArchitectureNotFoundException(), 404), + Arguments.of(new NamespaceNotFoundException(), 404), + Arguments.of(new ArchitectureNotFoundException(), 404), Arguments.of(new ArchitectureVersionExistsException(), 409), Arguments.of(null, 201) ); @@ -228,7 +228,7 @@ void respond_correctly_to_create_architecture(Throwable exceptionToThrow, int ex .setId(20) .build(); - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockArchitectureStore.createArchitectureForVersion(expectedArchitecture)).thenThrow(exceptionToThrow); } else { when(mockArchitectureStore.createArchitectureForVersion(expectedArchitecture)).thenReturn(expectedArchitecture); diff --git a/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java b/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java index 494a2a0..6c95b1d 100644 --- a/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java +++ b/src/test/java/org/finos/calm/resources/TestPatternResourcePutEnabledShould.java @@ -30,8 +30,8 @@ public class TestPatternResourcePutEnabledShould { static Stream provideParametersForPutPatternTests() { return Stream.of( - Arguments.of( new NamespaceNotFoundException(), 404), - Arguments.of( new PatternNotFoundException(), 404), + Arguments.of(new NamespaceNotFoundException(), 404), + Arguments.of(new PatternNotFoundException(), 404), Arguments.of(null, 201) ); } @@ -49,13 +49,13 @@ void respond_correctly_to_put_pattern_correctly(Throwable exceptionToThrow, int System.out.println("TestPatterResourcePutEnabled mock: " + mockPatternStore); - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockPatternStore.updatePatternForVersion(expectedPattern)).thenThrow(exceptionToThrow); } else { when(mockPatternStore.updatePatternForVersion(expectedPattern)).thenReturn(expectedPattern); } - if (expectedStatusCode == 201) { + if(expectedStatusCode == 201) { given() .header("Content-Type", "application/json") .body("{ \"test\": \"json\" }") diff --git a/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java b/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java index 4cfa052..4410c0c 100644 --- a/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java +++ b/src/test/java/org/finos/calm/resources/TestPatternResourceShould.java @@ -47,7 +47,7 @@ void return_a_404_when_an_invalid_namespace_is_provided_on_get_patterns() throws @Test void return_list_of_pattern_ids_when_valid_namespace_provided_on_get_patterns() throws NamespaceNotFoundException { - when(mockPatternStore.getPatternsForNamespace(anyString())).thenReturn(Arrays.asList(12345,54321)); + when(mockPatternStore.getPatternsForNamespace(anyString())).thenReturn(Arrays.asList(12345, 54321)); given() .when() @@ -137,13 +137,13 @@ static Stream provideParametersForPatternVersionTests() { @MethodSource("provideParametersForPatternVersionTests") void respond_correctly_to_get_pattern_versions_query(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException { var versions = List.of("1.0.0", "1.0.1"); - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockPatternStore.getPatternVersions(any(Pattern.class))).thenThrow(exceptionToThrow); } else { when(mockPatternStore.getPatternVersions(any(Pattern.class))).thenReturn(versions); } - if (expectedStatusCode == 200 ) { + if(expectedStatusCode == 200) { String expectedBody = "{\"values\":[\"1.0.0\",\"1.0.1\"]}"; given() .when() @@ -184,14 +184,14 @@ static Stream provideParametersForGetPatternTests() { @ParameterizedTest @MethodSource("provideParametersForGetPatternTests") void respond_correct_to_get_pattern_for_a_specific_version_correctly(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws PatternNotFoundException, NamespaceNotFoundException, PatternVersionNotFoundException { - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockPatternStore.getPatternForVersion(any(Pattern.class))).thenThrow(exceptionToThrow); } else { String pattern = "{ \"test\": \"json\" }"; when(mockPatternStore.getPatternForVersion(any(Pattern.class))).thenReturn(pattern); } - if (expectedStatusCode == 200) { + if(expectedStatusCode == 200) { given() .when() .get("/calm/namespaces/" + namespace + "/patterns/12/versions/1.0.0") @@ -211,8 +211,8 @@ void respond_correct_to_get_pattern_for_a_specific_version_correctly(String name static Stream provideParametersForCreatePatternTests() { return Stream.of( - Arguments.of( new NamespaceNotFoundException(), 404), - Arguments.of( new PatternNotFoundException(), 404), + Arguments.of(new NamespaceNotFoundException(), 404), + Arguments.of(new PatternNotFoundException(), 404), Arguments.of(new PatternVersionExistsException(), 409), Arguments.of(null, 201) ); @@ -230,7 +230,7 @@ void respond_correctly_to_create_pattern_correctly(Throwable exceptionToThrow, i System.out.println("TestPatternResourceShould mock: " + mockPatternStore); - if (exceptionToThrow != null) { + if(exceptionToThrow != null) { when(mockPatternStore.createPatternForVersion(expectedPattern)).thenThrow(exceptionToThrow); } else { when(mockPatternStore.createPatternForVersion(expectedPattern)).thenReturn(expectedPattern);