From 28363371d308c2cbd1954835a034533b53d6406a Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Fri, 27 Dec 2019 13:46:38 +0100 Subject: [PATCH] Add unit test for A1 client Change-Id: Ib26fc1d407f309e282e002ee8fedd20319d94d5a Issue-ID: NONRTRIC-80 Signed-off-by: RehanRaza --- .../oransc/policyagent/clients/A1ClientImpl.java | 14 +- .../policyagent/clients/A1ClientImplTest.java | 142 +++++++++++++++++++++ 2 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientImplTest.java diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java index 7bbb4990..5f2d3f29 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java @@ -40,10 +40,14 @@ public class A1ClientImpl implements A1Client { return nearRtRicUrl + "/A1-P/v1"; } + public AsyncRestClient createClient(final String nearRtRicUrl) { + return new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + } + @Override public Flux getPolicyTypeIdentities(String nearRtRicUrl) { logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); Mono response = client.get("/policytypes/identities"); return response.flatMapMany(this::createFlux); } @@ -51,7 +55,7 @@ public class A1ClientImpl implements A1Client { @Override public Flux getPolicyIdentities(String nearRtRicUrl) { logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); Mono response = client.get("/policies/identities"); return response.flatMapMany(this::createFlux); } @@ -59,7 +63,7 @@ public class A1ClientImpl implements A1Client { @Override public Mono getPolicyType(String nearRtRicUrl, String policyTypeId) { logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); Mono response = client.get("/policytypes/" + policyTypeId); return response.flatMap(this::createMono); } @@ -68,7 +72,7 @@ public class A1ClientImpl implements A1Client { public Mono putPolicy(String nearRtRicUrl, String policyId, String policyString) { logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId, policyString); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); Mono response = client.put("/policies/" + policyId, policyString); return response.flatMap(this::createMono); } @@ -76,7 +80,7 @@ public class A1ClientImpl implements A1Client { @Override public Mono deletePolicy(String nearRtRicUrl, String policyId) { logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId); - AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl)); + AsyncRestClient client = createClient(nearRtRicUrl); return client.delete("/policies/" + policyId); } diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientImplTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientImplTest.java new file mode 100644 index 00000000..3816a17d --- /dev/null +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientImplTest.java @@ -0,0 +1,142 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2019 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.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +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.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +@ExtendWith(MockitoExtension.class) +@RunWith(MockitoJUnitRunner.class) +public class A1ClientImplTest { + private static final String RIC_URL = "RicUrl"; + private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes/identities"; + private static final String POLICIES_IDENTITIES_URL = "/policies/identities"; + private static final String POLICYTYPES_URL = "/policytypes/"; + private static final String POLICIES_URL = "/policies/"; + + private static final String POLICY_TYPE_1_NAME = "type1"; + private static final String POLICY_TYPE_2_NAME = "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\"}"; + private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}"; + + @Spy + A1ClientImpl a1Client; + + @Mock + AsyncRestClient asyncRestClientMock; + + @BeforeEach + public void init() { + doReturn(asyncRestClientMock).when(a1Client).createClient(RIC_URL); + } + + @Test + public void testGetPolicyTypeIdentities() { + Mono policyTypeIds = Mono.just(Arrays.toString(new String[] {POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME})); + when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds); + + Flux policyTypeIdsFlux = a1Client.getPolicyTypeIdentities(RIC_URL); + verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL); + StepVerifier.create(policyTypeIdsFlux).expectNext(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).expectComplete() + .verify(); + } + + @Test + public void testGetPolicyIdentities() { + Mono policyIds = Mono.just(Arrays.toString(new String[] {POLICY_1_ID, POLICY_2_ID})); + when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds); + + Flux policyIdsFlux = a1Client.getPolicyIdentities(RIC_URL); + verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); + StepVerifier.create(policyIdsFlux).expectNext(POLICY_1_ID, POLICY_2_ID).expectComplete().verify(); + } + + @Test + public void testGetValidPolicyType() { + when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME)) + .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_VALID)); + + Mono policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME); + StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify(); + } + + @Test + public void testGetInvalidPolicyType() { + when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME)) + .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID)); + + Mono policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME); + verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME); + StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException) + .verify(); + } + + @Test + public void testPutPolicyValidResponse() { + when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID)) + .thenReturn(Mono.just(POLICY_JSON_VALID)); + + Mono policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID); + verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID); + StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify(); + } + + @Test + public void testPutPolicyInvalidResponse() { + when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID)) + .thenReturn(Mono.just(POLICY_JSON_INVALID)); + + Mono policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID); + verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID); + StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + } + + @Test + public void testDeletePolicy() { + when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty()); + + Mono responseMono = a1Client.deletePolicy(RIC_URL, POLICY_1_ID); + verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID); + StepVerifier.create(responseMono).expectComplete().verify(); + } +} -- 2.16.6