diff options
author | 2025-01-27 18:09:21 +0530 | |
---|---|---|
committer | 2025-01-27 18:39:45 +0530 | |
commit | fcab8a73c875f9fd86823c8060ee66444e3efa61 (patch) | |
tree | 365b0a05d8158aa3a5ef6d97d13b738bb69c3d8c /aai-schema-service/src/test/java/org | |
parent | 520e47ff8893872f484acb4f0f0e6f36b6a0d630 (diff) |
[AAI] Improve test coverage for A&AI component aai-schema-service
- to Improve test coverage for A&AI component aai-schema-service <=80%
Issue-ID: AAI-4107
Change-Id: I23f499d09c70cb637d214937b8617e161863e414
Signed-off-by: nisha.gangore <nisha.gangore@accenture.com>
Diffstat (limited to 'aai-schema-service/src/test/java/org')
5 files changed, 460 insertions, 0 deletions
diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/AAIContainerFilterTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/AAIContainerFilterTest.java new file mode 100644 index 0000000..b549c5f --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/AAIContainerFilterTest.java @@ -0,0 +1,61 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.schemaservice.interceptors; + +import java.util.UUID; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class AAIContainerFilterTest { + private class TestAAIContainerFilter extends AAIContainerFilter { + // This subclass allows us to test the protected methods + } + + @Test + public void testGenDate() { + TestAAIContainerFilter filter = new TestAAIContainerFilter(); + + // Get the generated date-time string + String generatedDate = filter.genDate(); + + // Assert that the generated date matches the expected format + assertNotNull(generatedDate); + assertTrue(generatedDate.matches("\\d{6}-\\d{2}:\\d{2}:\\d{2}:\\d{3}")); + } + + @Test + public void testIsValidUUID_ValidUUID() { + TestAAIContainerFilter filter = new TestAAIContainerFilter(); + + // Test with a valid UUID + String validUUID = UUID.randomUUID().toString(); + assertTrue(filter.isValidUUID(validUUID)); + } + + @Test + public void testIsValidUUID_InvalidUUID() { + TestAAIContainerFilter filter = new TestAAIContainerFilter(); + + // Test with an invalid UUID (not a valid UUID string) + String invalidUUID = "invalid-uuid-string"; + assertFalse(filter.isValidUUID(invalidUUID)); + } + +} diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/pre/OneWaySslAuthorizationTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/pre/OneWaySslAuthorizationTest.java new file mode 100644 index 0000000..9696e69 --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/interceptors/pre/OneWaySslAuthorizationTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.schemaservice.interceptors.pre; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.onap.aai.schemaservice.service.AuthorizationService; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public class OneWaySslAuthorizationTest { + + @Mock + private AuthorizationService authorizationService; + + @Mock + private ContainerRequestContext containerRequestContext; + + @InjectMocks + private OneWaySslAuthorization oneWaySslAuthorization; + + @BeforeEach + public void setUp() { + lenient().when(authorizationService.checkIfUserAuthorized(anyString())).thenReturn(true); + } + + @Test + public void testFilterWithValidBasicAuth() throws Exception { + String basicAuth = "Basic validToken"; + List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE); + + when(containerRequestContext.getHeaderString("Authorization")).thenReturn(basicAuth); + when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues); + + UriInfo uriInfoMock = mock(UriInfo.class); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock); + when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path")); + when(uriInfoMock.getPath()).thenReturn("/some/other/path"); + + oneWaySslAuthorization.filter(containerRequestContext); + + verify(containerRequestContext, times(0)).abortWith(any()); + } + + @Test + public void testFilterWithInvalidBasicAuth() throws Exception { + String basicAuth = "Basic invalidToken"; + List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE); + + UriInfo uriInfoMock = mock(UriInfo.class); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock); + when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path")); + when(uriInfoMock.getPath()).thenReturn("/some/other/path"); + + when(containerRequestContext.getHeaderString("Authorization")).thenReturn(basicAuth); + when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues); + + when(authorizationService.checkIfUserAuthorized("invalidToken")).thenReturn(false); + + oneWaySslAuthorization.filter(containerRequestContext); + + verify(containerRequestContext, times(1)).abortWith(any(Response.class)); + } + + @Test + public void testFilterWithNoAuthorizationHeader() throws Exception { + String basicAuth = null; + List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE); + + when(containerRequestContext.getHeaderString("Authorization")).thenReturn(basicAuth); + when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues); + + UriInfo uriInfoMock = mock(UriInfo.class); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock); + when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path")); + when(uriInfoMock.getPath()).thenReturn("/some/other/path"); + + oneWaySslAuthorization.filter(containerRequestContext); + + verify(containerRequestContext, times(1)).abortWith(any(Response.class)); + } + + @Test + public void testFilterWithInvalidAuthorizationHeaderFormat() throws Exception { + String basicAuth = "Bearer invalidToken"; // Header doesn't start with "Basic " + List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE); + + when(containerRequestContext.getHeaderString("Authorization")).thenReturn(basicAuth); + when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues); + + UriInfo uriInfoMock = mock(UriInfo.class); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock); + when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path")); + when(uriInfoMock.getPath()).thenReturn("/some/other/path"); + + oneWaySslAuthorization.filter(containerRequestContext); + + verify(containerRequestContext, times(1)).abortWith(any(Response.class)); + } + + @Test + public void testFilterForEchoPath() throws Exception { + String path = "/util/echo"; + + UriInfo uriInfoMock = mock(UriInfo.class); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock); + when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost" + path)); + when(uriInfoMock.getPath()).thenReturn(path); + + oneWaySslAuthorization.filter(containerRequestContext); + + verify(containerRequestContext, times(0)).abortWith(any()); + } + + @Test + public void testErrorResponse() throws Exception { + Method errorResponseMethod = OneWaySslAuthorization.class.getDeclaredMethod("errorResponse", String.class, List.class); + errorResponseMethod.setAccessible(true); + + String errorCode = "AAI_3300"; + List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE); + + Object result = errorResponseMethod.invoke(oneWaySslAuthorization, errorCode, acceptHeaderValues); + + assertTrue(result instanceof Optional); + Optional<Response> responseOptional = (Optional<Response>) result; + assertTrue(responseOptional.isPresent()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), responseOptional.get().getStatus()); + } +} + diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/FailFastStrategyTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/FailFastStrategyTest.java new file mode 100644 index 0000000..9c75a59 --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/FailFastStrategyTest.java @@ -0,0 +1,56 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.schemaservice.nodeschema.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class FailFastStrategyTest { + + private FailFastStrategy failFastStrategy; + + @BeforeEach + void setUp() { + failFastStrategy = new FailFastStrategy(); + } + + @Test + void testIsOK_WhenNoErrorOccurred() { + assertTrue(failFastStrategy.isOK()); + } + + @Test + void testGetErrorMsg_WhenNoErrorOccurred() { + assertEquals("No errors found.", failFastStrategy.getErrorMsg()); + } + + @Test + void testNotifyOnError_ShouldSetErrorState() { + String errorMessage = "Validation failed!"; + AAISchemaValidationException exception = assertThrows(AAISchemaValidationException.class, () -> { + failFastStrategy.notifyOnError(errorMessage); + }); + + assertEquals(errorMessage, exception.getMessage()); + assertFalse(failFastStrategy.isOK()); + assertEquals(errorMessage, failFastStrategy.getErrorMsg()); + } +} diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/NodeValidatorTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/NodeValidatorTest.java new file mode 100644 index 0000000..2e2b48b --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/NodeValidatorTest.java @@ -0,0 +1,101 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.schemaservice.nodeschema.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import org.onap.aai.schemaservice.config.ConfigTranslator; +import org.onap.aai.schemaservice.nodeschema.SchemaVersion; +import java.util.*; +import static org.mockito.Mockito.*; + +import static org.junit.jupiter.api.Assertions.*; + +class NodeValidatorTest { + + @Mock + private ConfigTranslator mockTranslator; + + @Mock + private SchemaErrorStrategy mockErrorStrategy; + + @Mock + private DuplicateNodeDefinitionValidationModule mockDupChecker; + + private NodeValidator nodeValidator; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + nodeValidator = new NodeValidator(mockTranslator, mockErrorStrategy, mockDupChecker); + } + + @Test + void testValidate_NoDuplicates_ShouldReturnTrue() { + SchemaVersion schemaVersion = mock(SchemaVersion.class); + List<String> nodeFiles = Arrays.asList("file1", "file2", "file3"); + + Map<SchemaVersion, List<String>> mockNodeFiles = new HashMap<>(); + mockNodeFiles.put(schemaVersion, nodeFiles); + + when(mockTranslator.getNodeFiles()).thenReturn(mockNodeFiles); + when(mockDupChecker.findDuplicates(nodeFiles, schemaVersion)).thenReturn(""); + + boolean result = nodeValidator.validate(); + assertFalse(result); + + verify(mockErrorStrategy, never()).notifyOnError(anyString()); + } + + @Test + void testValidate_WithDuplicates_ShouldReturnFalse() { + SchemaVersion schemaVersion = mock(SchemaVersion.class); + List<String> nodeFiles = Arrays.asList("file1", "file2", "file3"); + + Map<SchemaVersion, List<String>> mockNodeFiles = new HashMap<>(); + mockNodeFiles.put(schemaVersion, nodeFiles); + + when(mockTranslator.getNodeFiles()).thenReturn(mockNodeFiles); + when(mockDupChecker.findDuplicates(nodeFiles, schemaVersion)).thenReturn("Duplicate found!"); + boolean result = nodeValidator.validate(); + + assertFalse(result); + verify(mockErrorStrategy).notifyOnError("Duplicate found!"); + } + + @Test + void testGetErrorMsg_WithoutError_ShouldReturnEmptyString() { + when(mockErrorStrategy.getErrorMsg()).thenReturn(""); + + String errorMsg = nodeValidator.getErrorMsg(); + + assertEquals("", errorMsg); + } + + @Test + void testGetErrorMsg_WithError_ShouldReturnErrorMessage() { + when(mockErrorStrategy.getErrorMsg()).thenReturn("Some error occurred"); + + String errorMsg = nodeValidator.getErrorMsg(); + + assertEquals("Some error occurred", errorMsg); + } +} diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/VersionValidatorTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/VersionValidatorTest.java new file mode 100644 index 0000000..5db1a26 --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/validation/VersionValidatorTest.java @@ -0,0 +1,76 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.schemaservice.nodeschema.validation; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class VersionValidatorTest { + + @Mock + private SchemaErrorStrategy mockErrorStrategy; + + @Mock + private VersionValidationModule mockVersionValidationModule; + + private VersionValidator versionValidator; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + versionValidator = new VersionValidator(mockErrorStrategy, mockVersionValidationModule); + } + + @Test + void testValidate_NoError_ShouldReturnTrue() { + when(mockVersionValidationModule.validate()).thenReturn(""); + when(mockErrorStrategy.isOK()).thenReturn(true); + + boolean result = versionValidator.validate(); + + assertTrue(result); + verify(mockErrorStrategy, never()).notifyOnError(anyString()); + } + + @Test + void testValidate_WithError_ShouldReturnFalse() { + String errorMessage = "Version validation failed"; + when(mockVersionValidationModule.validate()).thenReturn(errorMessage); + when(mockErrorStrategy.isOK()).thenReturn(false); + + boolean result = versionValidator.validate(); + + assertFalse(result); + verify(mockErrorStrategy).notifyOnError(errorMessage); + } + + @Test + void testGetErrorMsg() { + String errorMessage = "Error in version validation"; + when(mockErrorStrategy.getErrorMsg()).thenReturn(errorMessage); + + String errorMsg = versionValidator.getErrorMsg(); + + assertEquals(errorMessage, errorMsg); + } +} |