ceaffc83a759e09307769177f2176bbf5255fd91
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / OscA1ClientTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29 import java.util.List;
30
31 import org.json.JSONException;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.junit.jupiter.MockitoExtension;
36
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40
41 @ExtendWith(MockitoExtension.class)
42 public class OscA1ClientTest {
43     private static final String RIC_URL = "RicUrl";
44     private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes";
45     private static final String POLICIES_IDENTITIES_URL = "/policies";
46     private static final String POLICYTYPES_URL = "/policytypes/";
47     private static final String POLICIES_URL = "/policies/";
48
49     private static final String POLICY_TYPE_1_ID = "type1";
50     private static final String POLICY_TYPE_2_ID = "type2";
51     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
52     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
53     private static final String POLICY_1_ID = "policy1";
54     private static final String POLICY_2_ID = "policy2";
55     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
56
57     OscA1Client clientUnderTest;
58
59     AsyncRestClient asyncRestClientMock;
60
61     @BeforeEach
62     public void init() {
63         asyncRestClientMock = mock(AsyncRestClient.class);
64         clientUnderTest = new OscA1Client(asyncRestClientMock);
65     }
66
67     @Test
68     public void testGetPolicyTypeIdentities() {
69         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
70         Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
71         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
72
73         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
74         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
75         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
76     }
77
78     @Test
79     public void testGetPolicyIdentities() {
80         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
81         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
82         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
83         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
84             .thenReturn(policyIdsType2Resp);
85
86         Mono<List<String>> returnedMono = clientUnderTest.getPolicyIdentities();
87         StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify();
88         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
89         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL);
90         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL);
91     }
92
93     @Test
94     public void testGetValidPolicyType() {
95         String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
96         Mono<String> policyTypeResp = Mono.just(policyType);
97
98         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
99
100         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
101         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
102         StepVerifier.create(returnedMono).expectNext(A1ClientHelper.getCreateSchema(policyType, POLICY_TYPE_1_ID))
103             .expectComplete().verify();
104     }
105
106     @Test
107     public void testGetInValidPolicyTypeJson() {
108         String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
109         Mono<String> policyTypeResp = Mono.just(policyType);
110
111         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
112
113         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
114         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
115         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
116     }
117
118     @Test
119     public void testGetPolicyTypeWithoutCreateSchema() {
120         Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
121
122         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
123
124         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
125         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
126         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
127     }
128
129     @Test
130     public void testPutPolicy() {
131         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
132
133         Mono<String> returnedMono = clientUnderTest
134             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
135         verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID,
136             POLICY_JSON_VALID);
137         StepVerifier.create(returnedMono).expectComplete().verify();
138     }
139
140     @Test
141     public void testDeletePolicy() {
142         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
143
144         Mono<String> returnedMono = clientUnderTest
145             .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
146         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID);
147         StepVerifier.create(returnedMono).expectComplete().verify();
148     }
149
150     @Test
151     public void testDeleteAllPolicies() {
152         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
153         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
154         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
155         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
156             .thenReturn(policyIdsType2Resp);
157         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
158
159         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
160         StepVerifier.create(returnedFlux).expectComplete().verify();
161         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
162         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL);
163         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID);
164         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL);
165         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_URL + POLICY_2_ID);
166     }
167 }