Merge "Remove unused exceptions from dashboard backend"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / StdA1ClientTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.clients;
22
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import java.util.Arrays;
28
29 import org.json.JSONException;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.oransc.policyagent.repository.Policy;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38 import reactor.test.StepVerifier;
39
40 @ExtendWith(MockitoExtension.class)
41 public class StdA1ClientTest {
42     private static final String RIC_URL = "RicUrl";
43     private static final String POLICY_TYPES_IDENTITIES_URL = "/policytypes";
44     private static final String POLICIES_IDENTITIES_URL = "/policies";
45     private static final String POLICY_TYPES_URL = "/policytypes/";
46     private static final String POLICIES_URL = "/policies/";
47
48     private static final String POLICY_TYPE_1_NAME = "type1";
49     private static final String POLICY_TYPE_2_NAME = "type2";
50     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
51     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
52     private static final String POLICY_1_ID = "policy1";
53     private static final String POLICY_2_ID = "policy2";
54     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
55     private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
56     private static final String POLICY_TYPE = "typeName";
57
58     StdA1Client clientUnderTest;
59
60     @Mock
61     AsyncRestClient asyncRestClientMock;
62
63     @BeforeEach
64     public void init() {
65         clientUnderTest = new StdA1Client(asyncRestClientMock);
66     }
67
68     @Test
69     public void testGetPolicyTypeIdentities() {
70         Mono<String> policyTypeIds = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).toString());
71         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIds);
72
73         Mono<?> policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities();
74
75         verify(asyncRestClientMock).get(POLICY_TYPES_IDENTITIES_URL);
76         StepVerifier.create(policyTypeIdsFlux).expectNextCount(1).expectComplete().verify();
77     }
78
79     @Test
80     public void testGetPolicyIdentities() {
81         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
82         when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
83
84         Mono<?> policyIdsFlux = clientUnderTest.getPolicyIdentities();
85
86         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
87         StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify();
88     }
89
90     @Test
91     public void testGetValidPolicyType() {
92         Mono<String> policyTypeResp =
93             Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }");
94
95         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
96
97         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
98
99         verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
100         StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
101     }
102
103     @Test
104     public void testGetInvalidPolicyType() {
105         when(asyncRestClientMock.get(anyString())).thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
106
107         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
108
109         verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
110         StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
111             .verify();
112     }
113
114     @Test
115     public void testPutPolicyValidResponse() {
116         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
117
118         Mono<String> policyMono = clientUnderTest
119             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
120
121         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID);
122         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
123     }
124
125     @Test
126     public void testPutPolicyInvalidResponse() {
127         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
128
129         Mono<String> policyMono = clientUnderTest
130             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
131
132         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
133     }
134
135     @Test
136     public void testDeletePolicy() {
137         when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
138
139         Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
140         Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
141         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
142         StepVerifier.create(responseMono).expectComplete().verify();
143     }
144
145     @Test
146     public void testDeleteAllPolicies() {
147         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
148         when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
149         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
150
151         Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
152         StepVerifier.create(responseFlux).expectComplete().verify();
153         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
154         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
155         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);
156     }
157 }