X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FStdA1ClientTest.java;h=15fe05ae9e3a79f82eabb89717c6b9b62fbd32ef;hb=2bfc144d136e8895aa5462cc331841a5d8107683;hp=90e0ed8ad2705c825f05cb3ef99bb450da287529;hpb=e2ac0dcf30eaf828a7c7bbd722730410f4110030;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java index 90e0ed8a..15fe05ae 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java @@ -22,18 +22,19 @@ package org.oransc.policyagent.clients; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; 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.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.oransc.policyagent.configuration.RicConfig; import org.oransc.policyagent.repository.Policy; import reactor.core.publisher.Flux; @@ -43,13 +44,10 @@ import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) public class StdA1ClientTest { private static final String RIC_URL = "RicUrl"; - private static final String POLICIES_IDENTITIES_URL = "/policies"; - private static final String POLICIES_URL = "/policies/"; private static final String POLICY_TYPE_1_NAME = "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\"}"; + private static final String POLICY_JSON = "{\"policyId\":\"policy1\"}"; private static final String POLICY_TYPE = "typeName"; StdA1ClientVersion1 clientUnderTest; @@ -57,9 +55,20 @@ public class StdA1ClientTest { @Mock AsyncRestClient asyncRestClientMock; + @Mock + RicConfig ricConfigMock; + @BeforeEach public void init() { - clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock); + clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock); + } + + private String policiesUrl() { + return RIC_URL + "/A1-P/v1/policies"; + } + + private String policiesBaseUrl() { + return policiesUrl() + "/"; } @Test @@ -71,13 +80,14 @@ public class StdA1ClientTest { @Test public void testGetPolicyIdentities() { + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); Mono policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString()); when(asyncRestClientMock.get(anyString())).thenReturn(policyIds); List result = clientUnderTest.getPolicyIdentities().block(); assertEquals(2, result.size(), ""); - verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); + verify(asyncRestClientMock).get(policiesUrl()); } @Test @@ -88,45 +98,39 @@ public class StdA1ClientTest { @Test public void testPutPolicyValidResponse() { - when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID)); - - Mono policyMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE)); - - 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(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID)); + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); + when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON)); - Mono policyMono = clientUnderTest - .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE)); + Mono policyMono = + clientUnderTest.putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE)); - StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify(); + verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON); + StepVerifier.create(policyMono).expectNext(POLICY_JSON).expectComplete().verify(); } @Test public void testDeletePolicy() { - when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty()); + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); + final String url = policiesBaseUrl() + POLICY_1_ID; + when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty()); - Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE); + Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE); Mono responseMono = clientUnderTest.deletePolicy(policy); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID); + verify(asyncRestClientMock).delete(url); StepVerifier.create(responseMono).expectComplete().verify(); } @Test public void testDeleteAllPolicies() { + doReturn(RIC_URL).when(ricConfigMock).baseUrl(); Mono policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString()); - when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds); + when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds); when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty()); Flux responseFlux = clientUnderTest.deleteAllPolicies(); StepVerifier.create(responseFlux).expectComplete().verify(); - verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID); - verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID); + verify(asyncRestClientMock).get(policiesUrl()); + verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID); + verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID); } }