2  * ========================LICENSE_START=================================
 
   4  * ======================================================================
 
   5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
 
   6  * ======================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ========================LICENSE_END===================================
 
  21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
 
  23 import static org.junit.jupiter.api.Assertions.assertEquals;
 
  24 import static org.mockito.ArgumentMatchers.anyString;
 
  25 import static org.mockito.Mockito.mock;
 
  26 import static org.mockito.Mockito.verify;
 
  27 import static org.mockito.Mockito.when;
 
  29 import java.util.Arrays;
 
  30 import java.util.List;
 
  32 import org.json.JSONException;
 
  33 import org.junit.jupiter.api.BeforeEach;
 
  34 import org.junit.jupiter.api.Test;
 
  35 import org.junit.jupiter.api.extension.ExtendWith;
 
  36 import org.mockito.junit.jupiter.MockitoExtension;
 
  37 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
 
  39 import reactor.core.publisher.Flux;
 
  40 import reactor.core.publisher.Mono;
 
  41 import reactor.test.StepVerifier;
 
  43 @ExtendWith(MockitoExtension.class)
 
  44 class OscA1ClientTest {
 
  46     private static final String RIC_URL = "RicUrl";
 
  48     private static final String RIC_BASE_URL = "RicBaseUrl/a1-p";
 
  50     private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
 
  51     private static final String POLICIES = "/policies";
 
  52     private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
 
  53     private static final String POLICY_TYPE_1_ID = "type1";
 
  54     private static final String POLICY_TYPE_2_ID = "type2";
 
  55     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
 
  56     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
 
  57     private static final String POLICY_1_ID = "policy1";
 
  58     private static final String POLICY_2_ID = "policy2";
 
  59     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
 
  61     OscA1Client clientUnderTest;
 
  63     AsyncRestClient asyncRestClientMock;
 
  67         RicConfig ricConfig = RicConfig.builder() //
 
  69                 .baseUrl("RicBaseUrl") //
 
  71         asyncRestClientMock = mock(AsyncRestClient.class);
 
  72         clientUnderTest = new OscA1Client(ricConfig, asyncRestClientMock);
 
  76     void testGetPolicyTypeIdentities() {
 
  77         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
 
  78         Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
 
  79         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
 
  81         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
 
  82         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
 
  83         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
 
  87     void testGetPolicyIdentities() {
 
  88         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
 
  89         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
 
  90         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
 
  91         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
 
  92                 .thenReturn(policyIdsType2Resp);
 
  94         List<String> returned = clientUnderTest.getPolicyIdentities().block();
 
  96         assertEquals(2, returned.size(), "");
 
  97         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
 
  98         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
 
  99         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
 
 103     void testGetValidPolicyType() {
 
 104         String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
 
 105         Mono<String> policyTypeResp = Mono.just(policyType);
 
 107         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
 
 109         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
 
 110         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
 
 111         StepVerifier.create(returnedMono).expectNext(A1ClientHelper.getCreateSchema(policyType, POLICY_TYPE_1_ID))
 
 112                 .expectComplete().verify();
 
 116     void testGetInValidPolicyTypeJson() {
 
 117         String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
 
 118         Mono<String> policyTypeResp = Mono.just(policyType);
 
 120         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
 
 122         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
 
 123         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
 
 124         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
 
 128     void testGetPolicyTypeWithoutCreateSchema() {
 
 129         Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
 
 131         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
 
 133         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
 
 134         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
 
 135         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
 
 139     void testPutPolicy() {
 
 140         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
 
 143                 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
 
 145         verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID,
 
 150     void testDeletePolicy() {
 
 151         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
 
 153         Mono<String> returnedMono = clientUnderTest
 
 154                 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
 
 155         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
 
 156         StepVerifier.create(returnedMono).expectComplete().verify();
 
 160     void testDeleteAllPolicies() {
 
 161         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
 
 162         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
 
 163         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
 
 164         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
 
 165                 .thenReturn(policyIdsType2Resp);
 
 166         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
 
 168         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
 
 169         StepVerifier.create(returnedFlux).expectComplete().verify();
 
 170         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
 
 171         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
 
 172         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
 
 173         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
 
 174         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);