From 4a799420ad89fed0c7f09225de51d088ca57c9c3 Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Fri, 2 Aug 2024 16:24:44 +0100 Subject: Improve tests/issues found in Sonar report - A1 Oslo Issue-ID: CCSDK-4037 Change-Id: Ic8e13141f2e0dcb32c51134f96ce883bc05701ee Signed-off-by: Raviteja Karumuri --- .../a1policymanagementservice/Application.java | 6 +- .../v3/ConfigurationControllerTestV3.java | 128 ---------- .../v3/ConfigurationControllerV3Test.java | 127 ++++++++++ .../controllers/v3/PolicyControllerTestV3.java | 257 --------------------- .../controllers/v3/PolicyControllerV3Test.java | 257 +++++++++++++++++++++ .../v3/RicRepositoryControllerTestV3.java | 123 ---------- .../v3/RicRepositoryControllerV3Test.java | 124 ++++++++++ .../controllers/v3/ServiceControllerTestV3.java | 141 ----------- .../controllers/v3/ServiceControllerV3Test.java | 141 +++++++++++ 9 files changed, 653 insertions(+), 651 deletions(-) delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3Test.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3Test.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3Test.java diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java index 6f458647..714fd2e4 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java @@ -36,8 +36,10 @@ import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { - @Autowired - private RefreshConfigTask configRefresh; + private final RefreshConfigTask configRefresh; + Application(RefreshConfigTask configRefresh) { + this.configRefresh = configRefresh; + } private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java deleted file mode 100644 index c634ecc5..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java +++ /dev/null @@ -1,128 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2020-2023 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; - -import org.junit.jupiter.api.*; -import org.junit.jupiter.api.io.TempDir; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; -import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; -import org.springframework.boot.web.servlet.server.ServletWebServerFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.TestPropertySource; -import reactor.core.publisher.Mono; - -import java.io.File; -import java.lang.reflect.Field; -import java.time.Duration; -import java.util.Objects; - -import static org.awaitility.Awaitility.await; -import static org.hamcrest.CoreMatchers.equalTo; - -@TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@TestPropertySource(properties = { // - "server.ssl.key-store=./config/keystore.jks", // - "app.webclient.trust-store=./config/truststore.jks", // - "app.vardata-directory=./target", // - "app.config-file-schema-path=/application_configuration_schema.json" // -}) -class ConfigurationControllerTestV3 { - @Autowired - ApplicationContext context; - - @Autowired - ApplicationConfig applicationConfig; - - @Autowired - private Rics rics; - - @Autowired - private TestHelper testHelper; - - @TempDir - public static File temporaryFolder; - private static File configFile; - - @LocalServerPort - private int port; - - @BeforeEach - void init() { - testHelper.port = port; - } - @BeforeAll - static void setup() throws Exception { - Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval"); - f1.setAccessible(true); - f1.set(null, Duration.ofSeconds(1)); - } - - public static class MockApplicationConfig extends ApplicationConfig { - @Override - public String getLocalConfigurationFilePath() { - configFile = new File(temporaryFolder, "config.json"); - return configFile.getAbsolutePath(); - } - } - - /** - * Overrides the BeanFactory. - */ - @TestConfiguration - static class TestBeanFactory { - @Bean - public ApplicationConfig getApplicationConfig() { - return new MockApplicationConfig(); - } - - @Bean - public ServletWebServerFactory servletContainer() { - return new TomcatServletWebServerFactory(); - } - } - - @Test - void testPutConfiguration() throws Exception { - Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/configuration", - testHelper.configAsString()); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, Objects::isNull); - //put Valid Configuration With New Ric should Update Repository. So, will wait until the ric size is 2 - await().until(rics::size, equalTo(2)); - //test Get Configuration - Mono> responseGetConfigMono = testHelper.restClientV3().getForEntity("/configuration"); - testHelper.testSuccessResponse(responseGetConfigMono, HttpStatus.OK, responseBody -> responseBody.contains("config")); - } - - @Test - public void testHealthCheck() { - Mono> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status"); - testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status")); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3Test.java new file mode 100644 index 00000000..40990f62 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3Test.java @@ -0,0 +1,127 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2020-2023 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.io.TempDir; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.io.File; +import java.lang.reflect.Field; +import java.time.Duration; +import java.util.Objects; + +import static org.awaitility.Awaitility.await; +import static org.hamcrest.CoreMatchers.equalTo; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.vardata-directory=./target", // + "app.config-file-schema-path=/application_configuration_schema.json" // +}) +class ConfigurationControllerV3Test { + @Autowired + ApplicationContext context; + + @Autowired + ApplicationConfig applicationConfig; + + @Autowired + private Rics rics; + + @Autowired + private TestHelper testHelper; + + @TempDir + public static File temporaryFolder; + private static File configFile; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + } + @BeforeAll + static void setup() throws Exception { + Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval"); + f1.setAccessible(true); + f1.set(null, Duration.ofSeconds(1)); + } + + public static class MockApplicationConfig extends ApplicationConfig { + @Override + public String getLocalConfigurationFilePath() { + configFile = new File(temporaryFolder, "config.json"); + return configFile.getAbsolutePath(); + } + } + + /** + * Overrides the BeanFactory. + */ + @TestConfiguration + static class TestBeanFactory { + @Bean + public ApplicationConfig getApplicationConfig() { + return new MockApplicationConfig(); + } + + @Bean + public ServletWebServerFactory servletContainer() { + return new TomcatServletWebServerFactory(); + } + } + + @Test + void testPutConfiguration() throws Exception { + Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/configuration", + testHelper.configAsString()); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, Objects::isNull); + //put Valid Configuration With New Ric should Update Repository. So, will wait until the ric size is 2 + await().until(rics::size, equalTo(2)); + //test Get Configuration + Mono> responseGetConfigMono = testHelper.restClientV3().getForEntity("/configuration"); + testHelper.testSuccessResponse(responseGetConfigMono, HttpStatus.OK, responseBody -> responseBody.contains("config")); + } + + @Test + public void testHealthCheck() { + Mono> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status"); + testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status")); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java deleted file mode 100644 index a8c5fbae..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java +++ /dev/null @@ -1,257 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; - -import com.google.gson.Gson; -import org.junit.jupiter.api.*; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; -import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; -import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.util.FileSystemUtils; -import reactor.core.publisher.Mono; - -import java.lang.invoke.MethodHandles; -import java.nio.file.Path; -import java.util.Objects; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -@TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ContextConfiguration(classes = TestConfig.class) -@TestPropertySource(properties = { // - "server.ssl.key-store=./config/keystore.jks", // - "app.webclient.trust-store=./config/truststore.jks", // - "app.webclient.trust-store-used=true", // - "app.vardata-directory=/tmp/pmstestv3", // - "app.filepath=", // - "app.s3.bucket=" // If this is set, S3 will be used to store data. -}) -public class PolicyControllerTestV3 { - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - private ApplicationConfig applicationConfig; - - @Autowired - private TestHelper testHelper; - - @Autowired - private Rics rics; - - @Autowired - private Policies policies; - - @Autowired - private PolicyTypes policyTypes; - - @Autowired - private Services services; - - @Autowired - private MockA1ClientFactory a1ClientFactory; - - @Autowired - private RappSimulatorController rAppSimulator; - - @Autowired - private SecurityContext securityContext; - - @Autowired - private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; - - @Autowired - private Gson gson; - - @LocalServerPort - private int port; - - @SpyBean - private Helper helper; - - @BeforeEach - void init() { - testHelper.port = port; - this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); - } - - @AfterEach - void reset() { - rics.clear(); - policies.clear(); - policyTypes.clear(); - services.clear(); - a1ClientFactory.reset(); - this.rAppSimulator.getTestResults().clear(); - this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app - this.securityContext.setAuthTokenFilePath(null); - this.openPolicyAgentSimulatorController.getTestResults().reset(); - } - - @AfterAll - static void clearTestDir() { - try { - FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3")); - } catch (Exception e) { - logger.warn("Could test directory : {}", e.getMessage()); - } - } - - @Test - @DisplayName("test Create Policy") - void testPostPolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> - responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); - testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/")); - } - - @Test - @DisplayName("test Create Policy with PolicyID sending") - void testPostPolicyWithPolicyID() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, "1"); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/1")); - } - - @Test - @DisplayName("test Create Policy with exisitng policy id") - void testPostPolicyWithExistingPolicyID() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - String policyId = "policy_5g"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, policyId); - testHelper.restClientV3().postForEntity(url, policyBody).block(); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId); - } - - @Test - @DisplayName("test delete Policy") - void testDeletePolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() - .get("location")).get(0).split("/"); - String policyID = locationHeader[(locationHeader.length) - 1]; - Mono> responseMonoDelete = testHelper.restClientV3().deleteForEntity(url+"/" +policyID); - testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true); - } - - @Test - @DisplayName("test Create Policy schema validation fail case") - void testPolicySchemaValidationFail() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed"); - } - - @Test - @DisplayName("test Create Policy No Ric fail case") - void testCreatePolicyNoRic() throws Exception { - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, " "); - when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); - String policyBody = testHelper.postPolicyBody("noRic", policyTypeName, ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic"); - } - - @Test - @DisplayName("test Create Policy with No Policy Type fail case") - void testCreatePolicyNoPolicyType() throws Exception { - String policyTypeName = "type1_1.2.3"; - String nonRtRicId = "ricOne"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); - String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType", ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); - } - - @Test - public void testGetPolicyTypesNoRicFound() throws Exception{ - String policyTypeName = "type1_1.2.3"; - String nonRtRicId = "ricOne"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - Mono> responseMono = testHelper.restClientV3().getForEntity("/policytypes" + "?nearRtRicId=\"noRic\""); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:"); - } - - @Test - @DisplayName("test get Policy") - void testGetPolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() - .get("location")).get(0).split("/"); - String policyID = locationHeader[(locationHeader.length) - 1]; - Mono> responseMonoGet = testHelper.restClientV3().getForEntity(url+"/" +policyID); - testHelper.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> - responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java new file mode 100644 index 00000000..e81a936d --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java @@ -0,0 +1,257 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import com.google.gson.Gson; +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; +import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.util.FileSystemUtils; +import reactor.core.publisher.Mono; + +import java.lang.invoke.MethodHandles; +import java.nio.file.Path; +import java.util.Objects; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", // + "app.filepath=", // + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +class PolicyControllerV3Test { + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private TestHelper testHelper; + + @Autowired + private Rics rics; + + @Autowired + private Policies policies; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Services services; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private Gson gson; + + @LocalServerPort + private int port; + + @SpyBean + private Helper helper; + + @BeforeEach + void init() { + testHelper.port = port; + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + rics.clear(); + policies.clear(); + policyTypes.clear(); + services.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @AfterAll + static void clearTestDir() { + try { + FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3")); + } catch (Exception e) { + logger.warn("Could test directory : {}", e.getMessage()); + } + } + + @Test + @DisplayName("test Create Policy") + void testPostPolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + + @Test + @DisplayName("test Create Policy with PolicyID sending") + void testPostPolicyWithPolicyID() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, "1"); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/1")); + } + + @Test + @DisplayName("test Create Policy with existing policy id") + void testPostPolicyWithExistingPolicyID() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + String policyId = "policy_5g"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, policyId); + testHelper.restClientV3().postForEntity(url, policyBody).block(); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId); + } + + @Test + @DisplayName("test delete Policy") + void testDeletePolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono> responseMonoDelete = testHelper.restClientV3().deleteForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true); + } + + @Test + @DisplayName("test Create Policy schema validation fail case") + void testPolicySchemaValidationFail() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed"); + } + + @Test + @DisplayName("test Create Policy No Ric fail case") + void testCreatePolicyNoRic() throws Exception { + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, " "); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); + String policyBody = testHelper.postPolicyBody("noRic", policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic"); + } + + @Test + @DisplayName("test Create Policy with No Policy Type fail case") + void testCreatePolicyNoPolicyType() throws Exception { + String policyTypeName = "type1_1.2.3"; + String nonRtRicId = "ricOne"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); + String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType", ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); + } + + @Test + public void testGetPolicyTypesNoRicFound() throws Exception{ + String policyTypeName = "type1_1.2.3"; + String nonRtRicId = "ricOne"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Mono> responseMono = testHelper.restClientV3().getForEntity("/policy-types" + "?nearRtRicId=\"noRic\""); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:"); + } + + @Test + @DisplayName("test get Policy") + void testGetPolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono> responseMonoGet = testHelper.restClientV3().getForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java deleted file mode 100644 index 576ae7f6..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; - -import org.junit.jupiter.api.*; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; -import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import reactor.core.publisher.Mono; - -import java.io.IOException; -import java.lang.invoke.MethodHandles; - -@TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ContextConfiguration(classes = TestConfig.class) -@TestPropertySource(properties = { // - "server.ssl.key-store=./config/keystore.jks", // - "app.webclient.trust-store=./config/truststore.jks", // - "app.webclient.trust-store-used=true", // - "app.vardata-directory=/tmp/pmstestv3", //a - "app.filepath=", // - "app.s3.bucket=" // If this is set, S3 will be used to store data. -}) -public class RicRepositoryControllerTestV3 { - - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - private TestHelper testHelper; - - @Autowired - private ApplicationConfig applicationConfig; - - @Autowired - private MockA1ClientFactory a1ClientFactory; - - @Autowired - private RappSimulatorController rAppSimulator; - - @Autowired - private SecurityContext securityContext; - - @Autowired - private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; - - @Autowired - private PolicyTypes policyTypes; - - @Autowired - private Rics rics; - - @LocalServerPort - private int port; - - @BeforeEach - void init() { - testHelper.port = port; - this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); - } - - @AfterEach - void reset() { - rics.clear(); - a1ClientFactory.reset(); - this.rAppSimulator.getTestResults().clear(); - this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app - this.securityContext.setAuthTokenFilePath(null); - this.openPolicyAgentSimulatorController.getTestResults().reset(); - } - - @Test - public void testGetRic() throws IOException { - testHelper.addPolicyType("1", "ricAdded"); - Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics/ric?ricId=ricAdded"); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody - .contains("{\"ricId\":\"ricAdded\",\"managedElementIds\":[],\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]}")); - } - - @Test - public void testGetRics() throws IOException { - testHelper.addPolicyType("1", "ricAddedOne"); - testHelper.addPolicyType("2", "ricAddedTwo"); - Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics"); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody - .contains("{\"rics\":[{\"ricId\":\"ricAddedTwo\",\"managedElementIds\":[],\"state\":\"AVAILABLE\"," + - "\"policyTypeIds\":[\"2\"]},{\"ricId\":\"ricAddedOne\",\"managedElementIds\":[]," + - "\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]}]}")); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3Test.java new file mode 100644 index 00000000..5f97df0e --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3Test.java @@ -0,0 +1,124 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", //a + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +class RicRepositoryControllerV3Test { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private TestHelper testHelper; + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Rics rics; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + rics.clear(); + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + rics.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @Test + public void testGetRic() throws IOException { + testHelper.addPolicyType("1", "ricAdded"); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics/ric?ricId=ricAdded"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody + .contains("{\"ricId\":\"ricAdded\",\"managedElementIds\":[],\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]}")); + } + + @Test + public void testGetRics() throws IOException { + testHelper.addPolicyType("1", "ricAddedOne"); + testHelper.addPolicyType("2", "ricAddedTwo"); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody + .contains("{\"ricId\":\"ricAddedTwo\",\"managedElementIds\":[],\"state\":\"AVAILABLE\"," + + "\"policyTypeIds\":[\"2\"]},{\"ricId\":\"ricAddedOne\",\"managedElementIds\":[]," + + "\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]")); + } +} + diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java deleted file mode 100644 index 9eeaa3af..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java +++ /dev/null @@ -1,141 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; - -import com.google.gson.Gson; -import org.junit.jupiter.api.*; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; -import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import reactor.core.publisher.Mono; - -import java.lang.invoke.MethodHandles; -import java.time.Duration; - -@TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ContextConfiguration(classes = TestConfig.class) -@TestPropertySource(properties = { // - "server.ssl.key-store=./config/keystore.jks", // - "app.webclient.trust-store=./config/truststore.jks", // - "app.webclient.trust-store-used=true", // - "app.vardata-directory=/tmp/pmstestv3", //a - "app.filepath=", // - "app.s3.bucket=" // If this is set, S3 will be used to store data. -}) -public class ServiceControllerTestV3 { - - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - private TestHelper testHelper; - - @Autowired - private ApplicationConfig applicationConfig; - - @Autowired - private Services services; - - @Autowired - private MockA1ClientFactory a1ClientFactory; - - @Autowired - private RappSimulatorController rAppSimulator; - - @Autowired - private SecurityContext securityContext; - - @Autowired - private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; - - @Autowired - private PolicyTypes policyTypes; - - @Autowired - private Gson gson; - - @LocalServerPort - private int port; - - @BeforeEach - void init() { - testHelper.port = port; - this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); - } - - @AfterEach - void reset() { - services.clear(); - a1ClientFactory.reset(); - this.rAppSimulator.getTestResults().clear(); - this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app - this.securityContext.setAuthTokenFilePath(null); - this.openPolicyAgentSimulatorController.getTestResults().reset(); - } - - @Test - public void testPutService() { - ServiceRegistrationInfo serviceRegistrationInfo = new ServiceRegistrationInfo("serviceId"); - serviceRegistrationInfo.callbackUrl("http://callback.com/").keepAliveIntervalSeconds(10L); - Mono> responseEntityMono = testHelper.restClientV3() - .putForEntity("/services", gson.toJson(serviceRegistrationInfo)); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.CREATED, responseBody -> services.size() == 1); - } - - @Test - public void testGetService() { - services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); - Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/services"); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBoy -> responseBoy - .contains("http://callback.com/")); - } - - @Test - public void testDeleteService() { - services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); - Mono> responseEntityMono = testHelper.restClientV3().deleteForEntity("/services/newServiceId"); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.NO_CONTENT, responseBody -> services.size() == 0); - } - - @Test - public void testKeepAliveService() { - services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); - Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/services/newServiceId/keepalive", ""); - testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> services.size() == 1); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3Test.java new file mode 100644 index 00000000..bfc0b683 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3Test.java @@ -0,0 +1,141 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 OpenInfra Foundation Europe. 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.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import com.google.gson.Gson; +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.lang.invoke.MethodHandles; +import java.time.Duration; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", //a + "app.filepath=", // + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +class ServiceControllerV3Test { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private TestHelper testHelper; + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private Services services; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Gson gson; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + services.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @Test + public void testPutService() { + ServiceRegistrationInfo serviceRegistrationInfo = new ServiceRegistrationInfo("serviceId"); + serviceRegistrationInfo.callbackUrl("http://callback.com/").keepAliveIntervalSeconds(10L); + Mono> responseEntityMono = testHelper.restClientV3() + .putForEntity("/services", gson.toJson(serviceRegistrationInfo)); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.CREATED, responseBody -> services.size() == 1); + } + + @Test + public void testGetService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/services"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBoy -> responseBoy + .contains("http://callback.com/")); + } + + @Test + public void testDeleteService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().deleteForEntity("/services/newServiceId"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.NO_CONTENT, responseBody -> services.size() == 0); + } + + @Test + public void testKeepAliveService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/services/newServiceId/keepalive", ""); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> services.size() == 1); + } +} -- cgit 1.2.3-korg