From 988f2af648a069ae86a4d9fc9499b80716ca8443 Mon Sep 17 00:00:00 2001 From: ecaiyanlinux Date: Tue, 27 Oct 2020 20:40:57 +0100 Subject: Remove code for sdnc onap controller sdnc onap controller is dismissed rename SdncOscA1Client to CcsdkA1AdapterClient rename SdncJsonHelper to A1AdapterJsonHelper update comments accordingly Signed-off-by: ecaiyanlinux Issue-ID: CCSDK-2502 Change-Id: If525dab599f9fd28203e85dcd57c010c67e20a8b --- .../clients/A1ClientFactoryTest.java | 5 +- .../clients/CcsdkA1AdapterClientTest.java | 414 +++++++++++++++++++++ .../clients/SdncOnapA1ClientTest.java | 271 -------------- .../clients/SdncOscA1ClientTest.java | 414 --------------------- 4 files changed, 416 insertions(+), 688 deletions(-) create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClientTest.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOnapA1ClientTest.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java (limited to 'a1-policy-management/src/test/java') diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java index 89adf68a..3ca35430 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java @@ -135,13 +135,12 @@ class A1ClientFactoryTest { void create_check_types_controllers() throws ServiceException { this.ric = new Ric(ricConfig("anythingButEmpty")); whenGetGetControllerConfigReturn(); - assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client); whenGetGetControllerConfigReturn(); - assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client); + assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient); whenGetGetControllerConfigReturn(); - assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client); + assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient); } private void whenGetProtocolVersionThrowException(A1Client... clientMocks) { diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClientTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClientTest.java new file mode 100644 index 00000000..87615f68 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClientTest.java @@ -0,0 +1,414 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2020 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.clients; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.stubbing.OngoingStubbing; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.ImmutableAdapterOutput.Builder; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterOutput; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterRequest; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableControllerConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClientResponseException; + +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +class CcsdkA1AdapterClientTest { + private static final String CONTROLLER_USERNAME = "username"; + private static final String CONTROLLER_PASSWORD = "password"; + private static final String RIC_1_URL = "RicUrl"; + private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy"; + private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy"; + private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy"; + private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus"; + private static final String POLICY_TYPE_1_ID = "type1"; + private static final String POLICY_1_ID = "policy1"; + private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; + + CcsdkA1AdapterClient clientUnderTest; + + @Mock + AsyncRestClient asyncRestClientMock; + + private ControllerConfig controllerConfig() { + return ImmutableControllerConfig.builder() // + .name("name") // + .baseUrl("baseUrl") // + .password(CONTROLLER_PASSWORD) // + .userName(CONTROLLER_USERNAME) // + .build(); + } + + @Test + void createClientWithWrongProtocol_thenErrorIsThrown() { + assertThrows(IllegalArgumentException.class, () -> { + new CcsdkA1AdapterClient(A1ProtocolType.STD_V1_1, null, null, new AsyncRestClient("", null)); + }); + } + + @Test + void getPolicyTypeIdentities_STD_V1() { + clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); + assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); + assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); + } + + private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new CcsdkA1AdapterClient(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); + whenAsyncPostThenReturn(Mono.just(response)); + + List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); + + assertEquals(1, policyTypeIds.size()); + assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + + @Test + void getPolicyTypeIdentities_OSC() { + testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, RIC_1_URL + "/a1-p/policytypes"); + } + + @Test + void getPolicyTypeIdentities_STD_V2() { + testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, RIC_1_URL + "/A1-P/v2/policytypes"); + } + + @Test + void getTypeSchema_STD_V1() { + + clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String policyType = clientUnderTest.getPolicyTypeSchema("").block(); + + assertEquals("{}", policyType); + } + + private void testGetTypeSchema(A1ProtocolType protocolType, String expUrl, String policyTypeId, + String getSchemaResponseFile) throws IOException { + clientUnderTest = new CcsdkA1AdapterClient(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + String ricResponse = loadFile(getSchemaResponseFile); + JsonElement elem = gson().fromJson(ricResponse, JsonElement.class); + String responseFromController = createOkResponseWithBody(elem); + whenAsyncPostThenReturn(Mono.just(responseFromController)); + + String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block(); + + JsonElement respJson = gson().fromJson(response, JsonElement.class); + assertEquals(policyTypeId, respJson.getAsJsonObject().get("title").getAsString(), + "title should be updated to contain policyType ID"); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams); + + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + + @Test + void getTypeSchema_OSC() throws IOException { + String expUrl = RIC_1_URL + "/a1-p/policytypes/policyTypeId"; + testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl, "policyTypeId", "test_osc_get_schema_response.json"); + } + + @Test + void getTypeSchema_STD_V2() throws IOException { + String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/policyTypeId"; + testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl, "policyTypeId", + "test_oran_get_schema_response.json"); + } + + @Test + void parseJsonArrayOfString() { + // One integer and one string + String inputString = "[1, \"1\" ]"; + + List result = A1AdapterJsonHelper.parseJsonArrayOfString(inputString).collectList().block(); + assertEquals(2, result.size()); + assertEquals("1", result.get(0)); + assertEquals("1", result.get(1)); + } + + private void getPolicyIdentities(A1ProtocolType protocolType, String... expUrls) { + clientUnderTest = new CcsdkA1AdapterClient(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + String resp = createOkResponseWithBody(Arrays.asList("xxx")); + whenAsyncPostThenReturn(Mono.just(resp)); + + List returned = clientUnderTest.getPolicyIdentities().block(); + + assertEquals(1, returned.size()); + for (String expUrl : expUrls) { + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + } + + @Test + void getPolicyIdentities_STD_V1() { + String expUrl = RIC_1_URL + "/A1-P/v1/policies"; + getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl); + } + + @Test + void getPolicyIdentities_STD_V2() { + String expUrlPolicies = RIC_1_URL + "/A1-P/v2/policytypes"; + String expUrlInstances = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies"; + getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrlPolicies, expUrlInstances); + } + + @Test + void getPolicyIdentities_OSC() { + String expUrlTypes = RIC_1_URL + "/a1-p/policytypes"; + String expUrlInstances = RIC_1_URL + "/a1-p/policytypes/xxx/policies"; + getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrlTypes, expUrlInstances); + } + + private void putPolicy(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new CcsdkA1AdapterClient(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + whenPostReturnOkResponse(); + + String returned = clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); + + assertEquals("OK", returned); + AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .body(POLICY_JSON_VALID) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedInputParams); + + verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); + + } + + @Test + void putPolicy_OSC() { + String expUrl = RIC_1_URL + "/a1-p/policytypes/type1/policies/policy1"; + putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl); + } + + @Test + void putPolicy_STD_V1() { + String expUrl = RIC_1_URL + "/A1-P/v1/policies/policy1"; + putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl); + } + + @Test + void putPolicy_STD_V2() { + String expUrl = + RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1?notificationDestination=https://test.com"; + putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl); + } + + @Test + void postRejected() { + clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + final String policyJson = "{}"; + AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // + .body("NOK") // + .httpStatus(HttpStatus.BAD_REQUEST.value()) // ERROR + .build(); + + String resp = A1AdapterJsonHelper.createOutputJsonString(adapterOutput); + whenAsyncPostThenReturn(Mono.just(resp)); + + Mono returnedMono = clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID)); + StepVerifier.create(returnedMono) // + .expectSubscription() // + .expectErrorMatches(t -> t instanceof WebClientResponseException) // + .verify(); + + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> { + return throwable instanceof WebClientResponseException; + }).verify(); + } + + private void deleteAllPolicies(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new CcsdkA1AdapterClient(protocolType, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + String resp = createOkResponseWithBody(Arrays.asList("xxx")); + whenAsyncPostThenReturn(Mono.just(resp)); + + clientUnderTest.deleteAllPolicies().blockLast(); + + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + } + + @Test + void deleteAllPolicies_STD_V2() { + String expUrl1 = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies/xxx"; + deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl1); + } + + @Test + void deleteAllPolicies_STD_V1() { + String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx"; + deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl1); + } + + @Test + void deleteAllPolicies_OSC() { + String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx"; + deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl1); + } + + @Test + void getVersion_OSC() { + clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, // Version irrelevant here + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + + whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); + + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + + assertEquals(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, returnedVersion); + } + + @Test + void testGetStatus() { + clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, // + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); + whenPostReturnOkResponse(); + + Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); + + String response = clientUnderTest.getPolicyStatus(policy).block(); + assertEquals("OK", response); + + String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1/status"; + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // + .nearRtRicUrl(expUrl) // + .build(); + String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams); + verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, + CONTROLLER_PASSWORD); + + } + + private Gson gson() { + return CcsdkA1AdapterClient.gson; + } + + private String loadFile(String fileName) throws IOException { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL url = loader.getResource(fileName); + File file = new File(url.getFile()); + return new String(Files.readAllBytes(file.toPath())); + } + + private void whenPostReturnOkResponse() { + whenAsyncPostThenReturn(Mono.just(createOkResponseString(true))); + } + + void whenPostReturnOkResponseNoBody() { + whenAsyncPostThenReturn(Mono.just(createOkResponseString(false))); + } + + private String createOkResponseWithBody(Object body) { + AdapterOutput output = ImmutableAdapterOutput.builder() // + .body(gson().toJson(body)) // + .httpStatus(HttpStatus.OK.value()) // + .build(); + return A1AdapterJsonHelper.createOutputJsonString(output); + } + + private String createOkResponseString(boolean withBody) { + Builder responseBuilder = ImmutableAdapterOutput.builder().httpStatus(HttpStatus.OK.value()); + if (withBody) { + responseBuilder.body(HttpStatus.OK.name()); + } else { + responseBuilder.body(Optional.empty()); + } + return A1AdapterJsonHelper.createOutputJsonString(responseBuilder.build()); + } + + private OngoingStubbing> whenAsyncPostThenReturn(Mono response) { + return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString())) + .thenReturn(response); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOnapA1ClientTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOnapA1ClientTest.java deleted file mode 100644 index c2118c12..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOnapA1ClientTest.java +++ /dev/null @@ -1,271 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2020 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.clients; - -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.stubbing.OngoingStubbing; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableControllerConfig; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - -@ExtendWith(MockitoExtension.class) -class SdncOnapA1ClientTest { - private static final String CONTROLLER_USERNAME = "username"; - private static final String CONTROLLER_PASSWORD = "password"; - private static final String RIC_1_URL = "RicUrl"; - private static final String POLICYTYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypes"; - private static final String POLICIES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyInstances"; - private static final String POLICYTYPES_URL = "/A1-ADAPTER-API:getPolicyType"; - private static final String PUT_POLICY_URL = "/A1-ADAPTER-API:createPolicyInstance"; - private static final String DELETE_POLICY_URL = "/A1-ADAPTER-API:deletePolicyInstance"; - - private static final String POLICY_TYPE_1_ID = "type1"; - private static final String POLICY_TYPE_2_ID = "type2"; - private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}"; - private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}"; - private static final String POLICY_1_ID = "policy1"; - private static final String POLICY_2_ID = "policy2"; - private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; - - SdncOnapA1Client clientUnderTest; - - AsyncRestClient asyncRestClientMock; - - @BeforeEach - void init() { - asyncRestClientMock = mock(AsyncRestClient.class); - ControllerConfig controllerCfg = ImmutableControllerConfig.builder() // - .name("name") // - .baseUrl("baseUrl") // - .password(CONTROLLER_PASSWORD) // - .userName(CONTROLLER_USERNAME) // - .build(); - - clientUnderTest = new SdncOnapA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), controllerCfg, - asyncRestClientMock); - } - - @Test - void testGetPolicyTypeIdentities() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .build(); - String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams); - - List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); - Mono policyTypeIdsResp = - A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString()); - whenAsyncPostThenReturn(policyTypeIdsResp); - - Mono> returnedMono = clientUnderTest.getPolicyTypeIdentities(); - verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify(); - } - - @Test - void testGetPolicyIdentities() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .build(); - String inputJsonStringGetTypeIds = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonStringGetPolicyIdsType1 = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_2_ID) // - .build(); - String inputJsonStringGetPolicyIdsType2 = SdncJsonHelper.createInputJsonString(inputParams); - - List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); - Mono policyTypeIdsResp = - A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString()); - List policyIdsType1 = Arrays.asList(POLICY_1_ID); - Mono policyIdsType1Resp = - A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType1.toString()); - List policyIdsType2 = Arrays.asList(POLICY_2_ID); - Mono policyIdsType2Resp = - A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType2.toString()); - whenAsyncPostThenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp).thenReturn(policyIdsType2Resp); - - Mono> returnedMono = clientUnderTest.getPolicyIdentities(); - StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify(); - verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonStringGetTypeIds, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType1, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType2, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - } - - @Test - void testGetValidPolicyType() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams); - - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); - - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); - verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify(); - } - - @Test - void testGetInvalidPolicyType() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams); - - String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + ", \"statusSchema\": {} }"; - Mono policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType); - whenAsyncPostThenReturn(policyTypeResp); - - Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); - verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); - } - - @Test - void testPutPolicy() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyInstanceId(POLICY_1_ID) // - .policyInstance(POLICY_JSON_VALID) // - .properties(new ArrayList()) // - .build(); - String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams); - - whenAsyncPostThenReturn(Mono.empty()); - - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); - verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectComplete().verify(); - } - - @Test - void testDeletePolicy() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyInstanceId(POLICY_1_ID) // - .build(); - String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams); - - whenAsyncPostThenReturn(Mono.empty()); - - Mono returnedMono = clientUnderTest - .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonString, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono).expectComplete().verify(); - } - - @Test - void testDeleteAllPolicies() { - SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .build(); - String inputJsonStringGetTypeIds = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .build(); - String inputJsonStringGetPolicyIdsType1 = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_2_ID) // - .build(); - String inputJsonStringGetPolicyIdsType2 = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_1_ID) // - .policyInstanceId(POLICY_1_ID) // - .build(); - String inputJsonStringDeletePolicy1 = SdncJsonHelper.createInputJsonString(inputParams); - inputParams = ImmutableSdncOnapAdapterInput.builder() // - .nearRtRicId(RIC_1_URL) // - .policyTypeId(POLICY_TYPE_2_ID) // - .policyInstanceId(POLICY_2_ID) // - .build(); - String inputJsonStringDeletePolicy2 = SdncJsonHelper.createInputJsonString(inputParams); - - List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); - Mono policyTypeIdsResp = - A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString()); - List policyIdsType1 = Arrays.asList(POLICY_1_ID); - Mono policyIdsType1Resp = - A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType1.toString()); - List policyIdsType2 = Arrays.asList(POLICY_2_ID); - Mono policyIdsType2Resp = - A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType2.toString()); - whenAsyncPostThenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp).thenReturn(Mono.empty()) - .thenReturn(policyIdsType2Resp).thenReturn(Mono.empty()); - - Flux returnedFlux = clientUnderTest.deleteAllPolicies(); - StepVerifier.create(returnedFlux).expectComplete().verify(); - verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonStringGetTypeIds, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType1, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy1, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType2, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy2, - CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - } - - private OngoingStubbing> whenAsyncPostThenReturn(Mono response) { - return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString())) - .thenReturn(response); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java deleted file mode 100644 index 4eca44d4..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/SdncOscA1ClientTest.java +++ /dev/null @@ -1,414 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2020 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.clients; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.stubbing.OngoingStubbing; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.ImmutableAdapterOutput.Builder; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SdncOscA1Client.AdapterOutput; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SdncOscA1Client.AdapterRequest; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableControllerConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.client.WebClientResponseException; - -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - -@ExtendWith(MockitoExtension.class) -class SdncOscA1ClientTest { - private static final String CONTROLLER_USERNAME = "username"; - private static final String CONTROLLER_PASSWORD = "password"; - private static final String RIC_1_URL = "RicUrl"; - private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy"; - private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy"; - private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy"; - private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus"; - private static final String POLICY_TYPE_1_ID = "type1"; - private static final String POLICY_1_ID = "policy1"; - private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; - - SdncOscA1Client clientUnderTest; - - @Mock - AsyncRestClient asyncRestClientMock; - - private ControllerConfig controllerConfig() { - return ImmutableControllerConfig.builder() // - .name("name") // - .baseUrl("baseUrl") // - .password(CONTROLLER_PASSWORD) // - .userName(CONTROLLER_USERNAME) // - .build(); - } - - @Test - void createClientWithWrongProtocol_thenErrorIsThrown() { - assertThrows(IllegalArgumentException.class, () -> { - new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, new AsyncRestClient("", null)); - }); - } - - @Test - void getPolicyTypeIdentities_STD_V1() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); - assertEquals(1, policyTypeIds.size(), "should hardcoded to one"); - assertEquals("", policyTypeIds.get(0), "should hardcoded to empty"); - } - - private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) { - clientUnderTest = new SdncOscA1Client(protocolType, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); - whenAsyncPostThenReturn(Mono.just(response)); - - List policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block(); - - assertEquals(1, policyTypeIds.size()); - assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); - - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - - @Test - void getPolicyTypeIdentities_OSC() { - testGetPolicyTypeIdentities(A1ProtocolType.SDNC_OSC_OSC_V1, RIC_1_URL + "/a1-p/policytypes"); - } - - @Test - void getPolicyTypeIdentities_STD_V2() { - testGetPolicyTypeIdentities(A1ProtocolType.SDNC_OSC_STD_V2_0_0, RIC_1_URL + "/A1-P/v2/policytypes"); - } - - @Test - void getTypeSchema_STD_V1() { - - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - String policyType = clientUnderTest.getPolicyTypeSchema("").block(); - - assertEquals("{}", policyType); - } - - private void testGetTypeSchema(A1ProtocolType protocolType, String expUrl, String policyTypeId, - String getSchemaResponseFile) throws IOException { - clientUnderTest = new SdncOscA1Client(protocolType, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - String ricResponse = loadFile(getSchemaResponseFile); - JsonElement elem = gson().fromJson(ricResponse, JsonElement.class); - String responseFromController = createOkResponseWithBody(elem); - whenAsyncPostThenReturn(Mono.just(responseFromController)); - - String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block(); - - JsonElement respJson = gson().fromJson(response, JsonElement.class); - assertEquals(policyTypeId, respJson.getAsJsonObject().get("title").getAsString(), - "title should be updated to contain policyType ID"); - - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - - @Test - void getTypeSchema_OSC() throws IOException { - String expUrl = RIC_1_URL + "/a1-p/policytypes/policyTypeId"; - testGetTypeSchema(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl, "policyTypeId", "test_osc_get_schema_response.json"); - } - - @Test - void getTypeSchema_STD_V2() throws IOException { - String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/policyTypeId"; - testGetTypeSchema(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl, "policyTypeId", - "test_oran_get_schema_response.json"); - } - - @Test - void parseJsonArrayOfString() { - // One integer and one string - String inputString = "[1, \"1\" ]"; - - List result = SdncJsonHelper.parseJsonArrayOfString(inputString).collectList().block(); - assertEquals(2, result.size()); - assertEquals("1", result.get(0)); - assertEquals("1", result.get(1)); - } - - private void getPolicyIdentities(A1ProtocolType protocolType, String... expUrls) { - clientUnderTest = new SdncOscA1Client(protocolType, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - String resp = createOkResponseWithBody(Arrays.asList("xxx")); - whenAsyncPostThenReturn(Mono.just(resp)); - - List returned = clientUnderTest.getPolicyIdentities().block(); - - assertEquals(1, returned.size()); - for (String expUrl : expUrls) { - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - } - - @Test - void getPolicyIdentities_STD_V1() { - String expUrl = RIC_1_URL + "/A1-P/v1/policies"; - getPolicyIdentities(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl); - } - - @Test - void getPolicyIdentities_STD_V2() { - String expUrlPolicies = RIC_1_URL + "/A1-P/v2/policytypes"; - String expUrlInstances = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies"; - getPolicyIdentities(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrlPolicies, expUrlInstances); - } - - @Test - void getPolicyIdentities_OSC() { - String expUrlTypes = RIC_1_URL + "/a1-p/policytypes"; - String expUrlInstances = RIC_1_URL + "/a1-p/policytypes/xxx/policies"; - getPolicyIdentities(A1ProtocolType.SDNC_OSC_OSC_V1, expUrlTypes, expUrlInstances); - } - - private void putPolicy(A1ProtocolType protocolType, String expUrl) { - clientUnderTest = new SdncOscA1Client(protocolType, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - whenPostReturnOkResponse(); - - String returned = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) - .block(); - - assertEquals("OK", returned); - AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .body(POLICY_JSON_VALID) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - - verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); - - } - - @Test - void putPolicy_OSC() { - String expUrl = RIC_1_URL + "/a1-p/policytypes/type1/policies/policy1"; - putPolicy(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl); - } - - @Test - void putPolicy_STD_V1() { - String expUrl = RIC_1_URL + "/A1-P/v1/policies/policy1"; - putPolicy(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl); - } - - @Test - void putPolicy_STD_V2() { - String expUrl = - RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1?notificationDestination=https://test.com"; - putPolicy(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl); - } - - @Test - void postRejected() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - final String policyJson = "{}"; - AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() // - .body("NOK") // - .httpStatus(HttpStatus.BAD_REQUEST.value()) // ERROR - .build(); - - String resp = SdncJsonHelper.createOutputJsonString(adapterOutput); - whenAsyncPostThenReturn(Mono.just(resp)); - - Mono returnedMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID)); - StepVerifier.create(returnedMono) // - .expectSubscription() // - .expectErrorMatches(t -> t instanceof WebClientResponseException) // - .verify(); - - StepVerifier.create(returnedMono).expectErrorMatches(throwable -> { - return throwable instanceof WebClientResponseException; - }).verify(); - } - - private void deleteAllPolicies(A1ProtocolType protocolType, String expUrl) { - clientUnderTest = new SdncOscA1Client(protocolType, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - String resp = createOkResponseWithBody(Arrays.asList("xxx")); - whenAsyncPostThenReturn(Mono.just(resp)); - - clientUnderTest.deleteAllPolicies().blockLast(); - - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - - @Test - void deleteAllPolicies_STD_V2() { - String expUrl1 = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies/xxx"; - deleteAllPolicies(A1ProtocolType.SDNC_OSC_STD_V2_0_0, expUrl1); - } - - @Test - void deleteAllPolicies_STD_V1() { - String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx"; - deleteAllPolicies(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl1); - } - - @Test - void deleteAllPolicies_OSC() { - String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx"; - deleteAllPolicies(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl1); - } - - @Test - void getVersion_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // Version irrelevant here - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - - whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); - - A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); - - assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); - } - - @Test - void testGetStatus() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V2_0_0, // - A1ClientHelper.createRic(RIC_1_URL).getConfig(), // - controllerConfig(), asyncRestClientMock); - whenPostReturnOkResponse(); - - Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); - - String response = clientUnderTest.getPolicyStatus(policy).block(); - assertEquals("OK", response); - - String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1/status"; - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - - } - - private Gson gson() { - return SdncOscA1Client.gson; - } - - private String loadFile(String fileName) throws IOException { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - URL url = loader.getResource(fileName); - File file = new File(url.getFile()); - return new String(Files.readAllBytes(file.toPath())); - } - - private void whenPostReturnOkResponse() { - whenAsyncPostThenReturn(Mono.just(createOkResponseString(true))); - } - - void whenPostReturnOkResponseNoBody() { - whenAsyncPostThenReturn(Mono.just(createOkResponseString(false))); - } - - private String createOkResponseWithBody(Object body) { - AdapterOutput output = ImmutableAdapterOutput.builder() // - .body(gson().toJson(body)) // - .httpStatus(HttpStatus.OK.value()) // - .build(); - return SdncJsonHelper.createOutputJsonString(output); - } - - private String createOkResponseString(boolean withBody) { - Builder responseBuilder = ImmutableAdapterOutput.builder().httpStatus(HttpStatus.OK.value()); - if (withBody) { - responseBuilder.body(HttpStatus.OK.name()); - } else { - responseBuilder.body(Optional.empty()); - } - return SdncJsonHelper.createOutputJsonString(responseBuilder.build()); - } - - private OngoingStubbing> whenAsyncPostThenReturn(Mono response) { - return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString())) - .thenReturn(response); - } -} -- cgit 1.2.3-korg