From 10948b6d86645c7e30d86f787fcf17a0eefd22be Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Tue, 30 Apr 2024 22:15:58 +0100 Subject: Implementing other V3 Controllers Issue-ID: CCSDK-4008 Change-Id: I00408d0189f26142f50c8ee3ae90fc98647e7a0d Signed-off-by: Raviteja Karumuri --- .../v3/ConfigurationControllerTestV3.java | 128 ++++++++++ .../controllers/v3/PolicyControllerTest.java | 232 ------------------- .../controllers/v3/PolicyControllerTestV3.java | 257 +++++++++++++++++++++ .../v3/RicRepositoryControllerTestV3.java | 123 ++++++++++ .../controllers/v3/ServiceControllerTestV3.java | 141 +++++++++++ .../utils/v3/TestHelper.java | 14 +- 6 files changed, 661 insertions(+), 234 deletions(-) create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java create 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/RicRepositoryControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java (limited to 'a1-policy-management/src/test/java/org') 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 new file mode 100644 index 00000000..c634ecc5 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java @@ -0,0 +1,128 @@ +/*- + * ========================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/PolicyControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java deleted file mode 100644 index d92097bd..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java +++ /dev/null @@ -1,232 +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.PolicyObjectInformation; -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.v3.TestHelper; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; -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 PolicyControllerTest { - 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 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/PolicyControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java new file mode 100644 index 00000000..a8c5fbae --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.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. +}) +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/RicRepositoryControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java new file mode 100644 index 00000000..576ae7f6 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java @@ -0,0 +1,123 @@ +/*- + * ========================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/ServiceControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java new file mode 100644 index 00000000..9eeaa3af --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.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. +}) +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/utils/v3/TestHelper.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java index bfec3f6b..5463b0a1 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java @@ -25,6 +25,7 @@ import com.google.common.io.CharStreams; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.io.FileUtils; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; @@ -45,6 +46,7 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -161,13 +163,15 @@ public class TestHelper { return type; } - public String postPolicyBody(String nearRtRicId, String policyTypeName) { + public String postPolicyBody(String nearRtRicId, String policyTypeName, String policyId) { PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyPolicyObject(), policyTypeName); + if (policyId != null && !policyId.isEmpty() && !policyId.isBlank()) + policyObjectInfo.setPolicyId(policyId); return gson.toJson(policyObjectInfo); } public PolicyObjectInformation policyObjectInfo(String nearRtRicId, String policyTypeName) { - return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName), PolicyObjectInformation.class); + return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName, ""), PolicyObjectInformation.class); } public JsonObject dummyPolicyObject() { @@ -251,4 +255,10 @@ public class TestHelper { }) .verify(); } + + public String configAsString() throws Exception { + File configFile = + new File(Objects.requireNonNull(getClass().getClassLoader().getResource("test_application_configuration.json")).getFile()); + return FileUtils.readFileToString(configFile, "UTF-8"); + } } -- cgit 1.2.3-korg