diff options
author | 2025-02-03 08:15:39 +0000 | |
---|---|---|
committer | 2025-02-03 08:15:39 +0000 | |
commit | f99ae7ec8b859b1b13a98209e361f03358b6898a (patch) | |
tree | 23727204cc182543321cff2c0dee4bc41c7e553d | |
parent | ee5e371c44ad1381d1d5269568edae4607ff5843 (diff) | |
parent | e2bbed63bfbd7d48423ba71217bcf86627ded007 (diff) |
Merge "[AAI] Improve test coverage for A&AI component aai-traversal"
5 files changed, 462 insertions, 0 deletions
diff --git a/aai-traversal/src/test/java/org/onap/aai/interceptors/AAIContainerFilterTest.java b/aai-traversal/src/test/java/org/onap/aai/interceptors/AAIContainerFilterTest.java new file mode 100644 index 0000000..1bb90b2 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/interceptors/AAIContainerFilterTest.java @@ -0,0 +1,59 @@ +/** + * ============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.interceptors; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.UUID; + +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(); + + 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-traversal/src/test/java/org/onap/aai/interceptors/pre/HttpHeaderInterceptorTest.java b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/HttpHeaderInterceptorTest.java new file mode 100644 index 0000000..e7a3923 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/HttpHeaderInterceptorTest.java @@ -0,0 +1,67 @@ +/** + * ============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.interceptors.pre; + +import org.junit.jupiter.api.Test; +import org.onap.aai.interceptors.AAIHeaderProperties; +import javax.ws.rs.container.ContainerRequestContext; +import static org.mockito.Mockito.*; + +class HttpHeaderInterceptorTest { + + @Test + void testOverridePostToPatch() throws Exception { + ContainerRequestContext mockRequestContext = mock(ContainerRequestContext.class); + when(mockRequestContext.getMethod()).thenReturn("POST"); + when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn("PATCH"); + + HttpHeaderInterceptor interceptor = new HttpHeaderInterceptor(); + + interceptor.filter(mockRequestContext); + + verify(mockRequestContext).setMethod(HttpHeaderInterceptor.patchMethod); + } + + @Test + void testNoOverrideHeader() throws Exception { + ContainerRequestContext mockRequestContext = mock(ContainerRequestContext.class); + when(mockRequestContext.getMethod()).thenReturn("POST"); + when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn(null); + + HttpHeaderInterceptor interceptor = new HttpHeaderInterceptor(); + + interceptor.filter(mockRequestContext); + + verify(mockRequestContext, never()).setMethod(anyString()); + } + + @Test + void testNonPostMethodNoChange() throws Exception { + ContainerRequestContext mockRequestContext = mock(ContainerRequestContext.class); + when(mockRequestContext.getMethod()).thenReturn("GET"); + when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn("PATCH"); + + HttpHeaderInterceptor interceptor = new HttpHeaderInterceptor(); + + interceptor.filter(mockRequestContext); + + verify(mockRequestContext, never()).setMethod(anyString()); + } +} diff --git a/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RequestModificationTest.java b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RequestModificationTest.java new file mode 100644 index 0000000..2701822 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RequestModificationTest.java @@ -0,0 +1,119 @@ +/** + * ============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.interceptors.pre; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.*; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class RequestModificationTest { + + @InjectMocks + private RequestModification requestModification; + + @Mock + private ContainerRequestContext mockRequestContext; + + @Mock + private UriInfo mockUriInfo; + + @Mock + private UriBuilder mockUriBuilder; + + @Mock + private MultivaluedMap<String, String> mockQueryParams; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + when(mockRequestContext.getUriInfo()).thenReturn(mockUriInfo); + when(mockUriInfo.getRequestUriBuilder()).thenReturn(mockUriBuilder); + when(mockUriInfo.getQueryParameters()).thenReturn(mockQueryParams); + } + + @Test + void testCleanDME2QueryParams_DoesNotRemoveNonBlacklistedParams() throws IOException { + when(mockQueryParams.containsKey("nonBlacklistedKey")).thenReturn(true); + when(mockQueryParams.containsKey("version")).thenReturn(false); + when(mockQueryParams.containsKey("envContext")).thenReturn(false); + when(mockQueryParams.containsKey("routeOffer")).thenReturn(false); + when(mockUriBuilder.replaceQueryParam("nonBlacklistedKey")).thenReturn(mockUriBuilder); + + requestModification.filter(mockRequestContext); + + verify(mockUriBuilder, never()).replaceQueryParam(eq("nonBlacklistedKey")); + assertTrue(true, "Non-blacklisted query parameters were not removed."); // Assert the expected behavior + } + + @Test + void testCleanDME2QueryParams_NoBlacklistParams() throws IOException { + when(mockQueryParams.isEmpty()).thenReturn(true); + + requestModification.filter(mockRequestContext); + + verify(mockUriBuilder, never()).replaceQueryParam(eq("version")); + verify(mockUriBuilder, never()).replaceQueryParam(eq("envContext")); + verify(mockUriBuilder, never()).replaceQueryParam(eq("routeOffer")); + + assertTrue(mockQueryParams.isEmpty(), "Query parameters should be empty when no blacklisted params are present."); + } + + @Test + void testCleanDME2QueryParams_AllBlacklistParamsPresent() throws IOException { + when(mockQueryParams.containsKey("version")).thenReturn(true); + when(mockQueryParams.containsKey("envContext")).thenReturn(true); + when(mockQueryParams.containsKey("routeOffer")).thenReturn(true); + when(mockQueryParams.entrySet()).thenReturn(Set.of( + Map.entry("version", List.of("1.0")), + Map.entry("envContext", List.of("DEV")), + Map.entry("routeOffer", List.of("A")) + )); + + when(mockUriBuilder.replaceQueryParam("version")).thenReturn(mockUriBuilder); + when(mockUriBuilder.replaceQueryParam("envContext")).thenReturn(mockUriBuilder); + when(mockUriBuilder.replaceQueryParam("routeOffer")).thenReturn(mockUriBuilder); + + requestModification.filter(mockRequestContext); + + verify(mockUriBuilder).replaceQueryParam("version"); + verify(mockUriBuilder).replaceQueryParam("envContext"); + verify(mockUriBuilder).replaceQueryParam("routeOffer"); + verify(mockRequestContext).setRequestUri(any()); + + assertTrue(true, "All blacklisted query parameters should have been processed."); + } +}
\ No newline at end of file diff --git a/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RetiredInterceptorTest.java b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RetiredInterceptorTest.java new file mode 100644 index 0000000..caaaf22 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/RetiredInterceptorTest.java @@ -0,0 +1,112 @@ +/** + * ============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.interceptors.pre; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aai.service.RetiredService; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +class RetiredInterceptorTest { + + @Mock + private RetiredService retiredService; + + @Mock + private ContainerRequestContext containerRequestContext; + + @Mock + private UriInfo uriInfo; + + private RetiredInterceptor retiredInterceptor; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + String basePath = "/aai/v15"; + retiredInterceptor = new RetiredInterceptor(retiredService, basePath); + } + + @Test + void testFilter_withRetiredUri_allVersions() throws Exception { + String requestURI = "/aai/v15/service/retired"; + String version = "v15"; + + List<Pattern> retiredAllVersionPatterns = new ArrayList<>(); + retiredAllVersionPatterns.add(Pattern.compile("/aai/v.*service/retired")); + + List<Pattern> retiredVersionPatterns = new ArrayList<>(); + retiredVersionPatterns.add(Pattern.compile("/aai/v15/service/.*")); + + when(retiredService.getRetiredAllVersionList()).thenReturn(retiredAllVersionPatterns); + when(retiredService.getRetiredPatterns()).thenReturn(retiredVersionPatterns); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfo); + when(uriInfo.getAbsolutePath()).thenReturn(java.net.URI.create(requestURI)); + + retiredInterceptor.filter(containerRequestContext); + + verify(containerRequestContext).abortWith(any(Response.class)); + } + + @Test + void testFilter_withActiveUri_doesNotAbort() throws Exception { + String requestURI = "/aai/v15/service/active"; + String version = "v15"; + + List<Pattern> retiredAllVersionPatterns = new ArrayList<>(); + retiredAllVersionPatterns.add(Pattern.compile("/aai/v.*service/retired")); + + List<Pattern> retiredVersionPatterns = new ArrayList<>(); + retiredVersionPatterns.add(Pattern.compile("/aai/v15/service/retired")); + + when(retiredService.getRetiredAllVersionList()).thenReturn(retiredAllVersionPatterns); + when(retiredService.getRetiredPatterns()).thenReturn(retiredVersionPatterns); + when(containerRequestContext.getUriInfo()).thenReturn(uriInfo); + when(uriInfo.getAbsolutePath()).thenReturn(java.net.URI.create(requestURI)); + + retiredInterceptor.filter(containerRequestContext); + + verify(containerRequestContext, never()).abortWith(any(Response.class)); + } + + @Test + void testExtractVersionFromPath_withValidVersion() { + String requestURI = "/aai/v15/service/active"; + String extractedVersion = retiredInterceptor.extractVersionFromPath(requestURI); + assertEquals("v15", extractedVersion); + } + + @Test + void testExtractVersionFromPath_withNoVersion() { + String requestURI = "/aai/service/active"; + String extractedVersion = retiredInterceptor.extractVersionFromPath(requestURI); + assertEquals(null, extractedVersion); + } +} + diff --git a/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/VersionInterceptorTest.java b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/VersionInterceptorTest.java new file mode 100644 index 0000000..1918170 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/interceptors/pre/VersionInterceptorTest.java @@ -0,0 +1,105 @@ +/** + * ============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.interceptors.pre; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class VersionInterceptorTest { + + @Mock + private SchemaVersions mockSchemaVersions; + + @Mock + private ContainerRequestContext mockRequestContext; + + @Mock + private UriInfo mockUriInfo; + + private VersionInterceptor versionInterceptor; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + versionInterceptor = new VersionInterceptor(mockSchemaVersions); + } + + @Test + public void testAllowedVersion() { + when(mockSchemaVersions.getVersions()).thenReturn(Arrays.asList(new SchemaVersion("v1"), new SchemaVersion("v2"))); + versionInterceptor = new VersionInterceptor(mockSchemaVersions); + + String uri = "/v1/test"; + when(mockRequestContext.getUriInfo()).thenReturn(mockUriInfo); + when(mockUriInfo.getPath()).thenReturn(uri); + + try { + versionInterceptor.filter(mockRequestContext); + } catch (Exception e) { + fail("abortWith() was called unexpectedly for valid version"); + } + } + + @Test + public void testInvalidVersionFormat() { + when(mockSchemaVersions.getVersions()).thenReturn(Arrays.asList(new SchemaVersion("v1"), new SchemaVersion("v2"))); + versionInterceptor = new VersionInterceptor(mockSchemaVersions); + + String uri = "/invalid-version/test"; + when(mockRequestContext.getUriInfo()).thenReturn(mockUriInfo); + when(mockUriInfo.getPath()).thenReturn(uri); + + versionInterceptor.filter(mockRequestContext); + + ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class); + verify(mockRequestContext).abortWith(responseCaptor.capture()); + Response response = responseCaptor.getValue(); + assertEquals(400, response.getStatus()); + } + + @Test + public void testNoVersionInUri() { + when(mockSchemaVersions.getVersions()).thenReturn(Arrays.asList(new SchemaVersion("v1"), new SchemaVersion("v2"))); + versionInterceptor = new VersionInterceptor(mockSchemaVersions); + + String uri = "/test"; + when(mockRequestContext.getUriInfo()).thenReturn(mockUriInfo); + when(mockUriInfo.getPath()).thenReturn(uri); + + versionInterceptor.filter(mockRequestContext); + ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class); + verify(mockRequestContext).abortWith(responseCaptor.capture()); + Response response = responseCaptor.getValue(); + assertEquals(400, response.getStatus()); + } +} |