From c5f81cf8dace9637702d6934db1a9f4ed32ff131 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Fri, 7 Aug 2020 09:15:10 +0200 Subject: Added support for ORAN A1-P Version 2.0 Change-Id: I82a00dced95b76c97bf93c61a65a8c9d8157a00f Issue-ID: CCSDK-2502 Signed-off-by: PatrikBuhr --- .../clients/SdncOscA1ClientTest.java | 263 ++++++++++++--------- .../clients/StdA1ClientV2Test.java | 180 ++++++++++++++ 2 files changed, 332 insertions(+), 111 deletions(-) create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java (limited to 'a1-policy-management/src/test/java/org/onap') 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 index bbb2b8e9..a0e95177 100644 --- 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 @@ -37,7 +37,6 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -50,7 +49,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.clients.SdncOscA1Client.Ada 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.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClientResponseException; @@ -68,7 +66,6 @@ class SdncOscA1ClientTest { 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_2_ID = "policy2"; private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}"; SdncOscA1Client clientUnderTest; @@ -85,32 +82,25 @@ class SdncOscA1ClientTest { .build(); } - @BeforeEach - void init() { - Ric ric = A1ClientHelper.createRic(RIC_1_URL); - - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(), - asyncRestClientMock); - } - @Test void createClientWithWrongProtocol_thenErrorIsThrown() { - AsyncRestClient asyncRestClient = new AsyncRestClient("", null); assertThrows(IllegalArgumentException.class, () -> { - new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, asyncRestClient); + new SdncOscA1Client(A1ProtocolType.STD_V1_1, null, null, new AsyncRestClient("", null)); }); } @Test - void getPolicyTypeIdentities_STD() { + 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"); } - @Test - void getPolicyTypeIdentities_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) { + clientUnderTest = new SdncOscA1Client(protocolType, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); @@ -122,7 +112,6 @@ class SdncOscA1ClientTest { assertEquals(1, policyTypeIds.size()); assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0)); - String expUrl = RIC_1_URL + "/a1-p/policytypes"; ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); @@ -132,28 +121,64 @@ class SdncOscA1ClientTest { } @Test - void getTypeSchema_STD() { + 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); } - @Test - void getTypeSchema_OSC() throws IOException { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + 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("test_osc_get_schema_response.json"); + 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(); + String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block(); JsonElement respJson = gson().fromJson(response, JsonElement.class); - assertEquals("policyTypeId", respJson.getAsJsonObject().get("title").getAsString(), + 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 @@ -167,49 +192,51 @@ class SdncOscA1ClientTest { assertEquals("1", result.get(1)); } - @Test - void getPolicyIdentities_STD() { - - String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); - whenAsyncPostThenReturn(Mono.just(policyIdsResp)); + 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(2, returned.size()); + 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); + } + } - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(policiesUrl()) // - .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() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + 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); - String policytypeIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID)); - String policyIdsResp = createOkResponseWithBody(Arrays.asList(POLICY_1_ID, POLICY_2_ID)); - whenAsyncPostThenReturn(Mono.just(policytypeIdsResp)).thenReturn(Mono.just(policyIdsResp)); - - List returned = clientUnderTest.getPolicyIdentities().block(); - - assertEquals(2, returned.size()); - - ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(RIC_1_URL + "/a1-p/policytypes/type1/policies") // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedParams); - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - } - - @Test - void putPolicyValidResponse() { whenPostReturnOkResponse(); String returned = clientUnderTest @@ -217,7 +244,6 @@ class SdncOscA1ClientTest { .block(); assertEquals("OK", returned); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .body(POLICY_JSON_VALID) // @@ -225,10 +251,33 @@ class SdncOscA1ClientTest { String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); + } @Test - void putPolicyRejected() { + 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"; + 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") // @@ -245,87 +294,79 @@ class SdncOscA1ClientTest { .expectErrorMatches(t -> t instanceof WebClientResponseException) // .verify(); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; - AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .body(policyJson) // - .build(); - String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams); - verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); - StepVerifier.create(returnedMono) - .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify(); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> { + return throwable instanceof WebClientResponseException; + }).verify(); } - @Test - void deletePolicy() { - whenPostReturnOkResponse(); + 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)); - String returned = clientUnderTest - .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) - .block(); + clientUnderTest.deleteAllPolicies().blockLast(); - assertEquals("OK", returned); - final String expUrl = policiesUrl() + "/" + POLICY_1_ID; - AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // + ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() // .nearRtRicUrl(expUrl) // .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - + String expInput = SdncJsonHelper.createInputJsonString(expectedParams); verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD); } @Test - void getStatus() { - whenPostReturnOkResponse(); - - Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); - - String returnedStatus = clientUnderTest.getPolicyStatus(policy).block(); - - assertEquals("OK", returnedStatus, "unexpected status"); - - final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status"; - AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() // - .nearRtRicUrl(expUrl) // - .build(); - String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams); - - verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME, - CONTROLLER_PASSWORD); + 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 getVersion_STD() { - whenPostReturnOkResponse(); + void deleteAllPolicies_STD_V1() { + String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx"; + deleteAllPolicies(A1ProtocolType.SDNC_OSC_STD_V1_1, expUrl1); + } - A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + @Test + void deleteAllPolicies_OSC() { + String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx"; + deleteAllPolicies(A1ProtocolType.SDNC_OSC_OSC_V1, expUrl1); + } - assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); + @Test + void getVersion_OSC() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // Version irrelevant here + A1ClientHelper.createRic(RIC_1_URL).getConfig(), // + controllerConfig(), asyncRestClientMock); - whenPostReturnOkResponseNoBody(); + whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); - returnedVersion = clientUnderTest.getProtocolVersion().block(); + A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion); } @Test - void getVersion_OSC() { - clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, // + void testGetStatus() { + clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V2_0_0, // A1ClientHelper.createRic(RIC_1_URL).getConfig(), // controllerConfig(), asyncRestClientMock); + whenPostReturnOkResponse(); - whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true))); + Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID); - A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block(); + String response = clientUnderTest.getPolicyStatus(policy).block(); + assertEquals("OK", response); - assertEquals(A1ProtocolType.SDNC_OSC_OSC_V1, returnedVersion); - } + 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 String policiesUrl() { - return RIC_1_URL + "/A1-P/v1/policies"; } private Gson gson() { @@ -343,7 +384,7 @@ class SdncOscA1ClientTest { whenAsyncPostThenReturn(Mono.just(createOkResponseString(true))); } - private void whenPostReturnOkResponseNoBody() { + void whenPostReturnOkResponseNoBody() { whenAsyncPostThenReturn(Mono.just(createOkResponseString(false))); } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java new file mode 100644 index 00000000..d9238d64 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientV2Test.java @@ -0,0 +1,180 @@ +/*- + * ========================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.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +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.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +class StdA1ClientV2Test { + + private static final String RIC_URL = "RicUrl"; + + private static final String RIC_BASE_URL = "RicBaseUrl/A1-P/v2"; + + private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes"; + private static final String POLICIES = "/policies"; + private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/"; + 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 = "{\"policyId\":\"policy1\"}"; + + StdA1ClientVersion2 clientUnderTest; + + AsyncRestClient asyncRestClientMock; + + @BeforeEach + void init() { + RicConfig ricConfig = ImmutableRicConfig.builder() // + .ricId("name") // + .baseUrl("RicBaseUrl") // + .managedElementIds(new ArrayList<>()) // + .controllerName("") // + .build(); + asyncRestClientMock = mock(AsyncRestClient.class); + clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock); + } + + @Test + void testGetPolicyTypeIdentities() { + List policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID); + Mono policyTypeIdsResp = Mono.just(policyTypeIds.toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp); + + Mono> returnedMono = clientUnderTest.getPolicyTypeIdentities(); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify(); + } + + @Test + void testGetPolicyIdentities() { + Mono policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString()); + Mono policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString()); + Mono policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp) + .thenReturn(policyIdsType2Resp); + + List returned = clientUnderTest.getPolicyIdentities().block(); + + assertEquals(2, returned.size(), ""); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES); + } + + @Test + void testGetValidPolicyType() { + String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}"; + Mono policyTypeResp = Mono.just(policyType); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block(); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + assertThat(response).contains("title"); + } + + @Test + void testGetInValidPolicyTypeJson() { + String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}"; + Mono policyTypeResp = Mono.just(policyType); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + } + + @Test + void testGetPolicyTypeWithoutCreateSchema() { + Mono policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID); + + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp); + + Mono returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID); + StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify(); + } + + @Test + void testPutPolicy() { + when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty()); + + clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)) + .block(); + verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID, + POLICY_JSON_VALID); + } + + @Test + void testDeletePolicy() { + when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); + + Mono returnedMono = clientUnderTest + .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID); + StepVerifier.create(returnedMono).expectComplete().verify(); + } + + @Test + void testDeleteAllPolicies() { + Mono policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString()); + Mono policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString()); + Mono policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString()); + when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp) + .thenReturn(policyIdsType2Resp); + when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); + + Flux returnedFlux = clientUnderTest.deleteAllPolicies(); + StepVerifier.create(returnedFlux).expectComplete().verify(); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID); + } +} -- cgit 1.2.3-korg