From: RehanRaza Date: Mon, 24 Feb 2020 08:27:26 +0000 (+0100) Subject: Add unit tests for OscA1Client X-Git-Tag: 2.0.0~161 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=e763d1b43b321b50e1c8f0e0ddf4a4e10f99ff42;p=nonrtric.git Add unit tests for OscA1Client Change-Id: I3141ae1a92a263f019fbe68f0319429aac6827e7 Issue-ID: NONRTRIC-125 Signed-off-by: RehanRaza --- diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java index 5c18103d..f7fd2326 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java @@ -46,6 +46,10 @@ public class OscA1Client implements A1Client { logger.debug("OscA1Client for ric: {}", ricConfig.name()); } + public OscA1Client(AsyncRestClient restClient) { + this.restClient = restClient; + } + @Override public Mono> getPolicyTypeIdentities() { return restClient.get("/policytypes") // diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientHelper.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientHelper.java index e0f885d4..79064467 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientHelper.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientHelper.java @@ -82,4 +82,10 @@ public class A1ClientHelper { return ImmutablePolicyType.builder().name(name).schema("schema").build(); } + protected static String getCreateSchema(String policyType, String policyTypeId) { + JSONObject obj = new JSONObject(policyType); + JSONObject schemaObj = obj.getJSONObject("create_schema"); + schemaObj.put("title", policyTypeId); + return schemaObj.toString(); + } } diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java new file mode 100644 index 00000000..ceaffc83 --- /dev/null +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/OscA1ClientTest.java @@ -0,0 +1,167 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2020 Nordix Foundation + * %% + * 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.oransc.policyagent.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.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 reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +public class OscA1ClientTest { + private static final String RIC_URL = "RicUrl"; + private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes"; + private static final String POLICIES_IDENTITIES_URL = "/policies"; + private static final String POLICYTYPES_URL = "/policytypes/"; + private static final String POLICIES_URL = "/policies/"; + + 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\"}"; + + OscA1Client clientUnderTest; + + AsyncRestClient asyncRestClientMock; + + @BeforeEach + public void init() { + asyncRestClientMock = mock(AsyncRestClient.class); + clientUnderTest = new OscA1Client(asyncRestClientMock); + } + + @Test + public 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 + public 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); + + Mono> returnedMono = clientUnderTest.getPolicyIdentities(); + StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify(); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL); + } + + @Test + public void testGetValidPolicyType() { + String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}"; + 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).expectNext(A1ClientHelper.getCreateSchema(policyType, POLICY_TYPE_1_ID)) + .expectComplete().verify(); + } + + @Test + public void testGetInValidPolicyTypeJson() { + String policyType = "{\"create_schema\": " + 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 + public 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 + public void testPutPolicy() { + when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty()); + + Mono returnedMono = clientUnderTest + .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID)); + verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID, + POLICY_JSON_VALID); + StepVerifier.create(returnedMono).expectComplete().verify(); + } + + @Test + public 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_URL + POLICY_1_ID); + StepVerifier.create(returnedMono).expectComplete().verify(); + } + + @Test + public 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_IDENTITIES_URL); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL); + verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_URL + POLICY_2_ID); + } +}