Add unit test for A1 client 18/2118/1
authorRehanRaza <muhammad.rehan.raza@est.tech>
Fri, 27 Dec 2019 12:46:38 +0000 (13:46 +0100)
committerRehanRaza <muhammad.rehan.raza@est.tech>
Fri, 27 Dec 2019 12:52:19 +0000 (13:52 +0100)
Change-Id: Ib26fc1d407f309e282e002ee8fedd20319d94d5a
Issue-ID: NONRTRIC-80
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientImpl.java
policy-agent/src/test/java/org/oransc/policyagent/clients/A1ClientImplTest.java [new file with mode: 0644]

index 7bbb499..5f2d3f2 100644 (file)
@@ -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<String> getPolicyTypeIdentities(String nearRtRicUrl) {
         logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
-        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        AsyncRestClient client = createClient(nearRtRicUrl);
         Mono<String> response = client.get("/policytypes/identities");
         return response.flatMapMany(this::createFlux);
     }
@@ -51,7 +55,7 @@ public class A1ClientImpl implements A1Client {
     @Override
     public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
         logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
-        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        AsyncRestClient client = createClient(nearRtRicUrl);
         Mono<String> response = client.get("/policies/identities");
         return response.flatMapMany(this::createFlux);
     }
@@ -59,7 +63,7 @@ public class A1ClientImpl implements A1Client {
     @Override
     public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
         logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
-        AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
+        AsyncRestClient client = createClient(nearRtRicUrl);
         Mono<String> response = client.get("/policytypes/" + policyTypeId);
         return response.flatMap(this::createMono);
     }
@@ -68,7 +72,7 @@ public class A1ClientImpl implements A1Client {
     public Mono<String> 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<String> response = client.put("/policies/" + policyId, policyString);
         return response.flatMap(this::createMono);
     }
@@ -76,7 +80,7 @@ public class A1ClientImpl implements A1Client {
     @Override
     public Mono<Void> 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 (file)
index 0000000..3816a17
--- /dev/null
@@ -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<String> policyTypeIds = Mono.just(Arrays.toString(new String[] {POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME}));
+        when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
+
+        Flux<String> 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<String> policyIds = Mono.just(Arrays.toString(new String[] {POLICY_1_ID, POLICY_2_ID}));
+        when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
+
+        Flux<String> 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<String> 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<String> 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<String> 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<String> 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<Void> responseMono = a1Client.deletePolicy(RIC_URL, POLICY_1_ID);
+        verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
+        StepVerifier.create(responseMono).expectComplete().verify();
+    }
+}