Handling of A1 STD 1.1
[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
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 StdA1ClientTest {
43     private static final String RIC_URL = "RicUrl";
44     private static final String POLICY_TYPES_IDENTITIES_URL = "/policytypes";
45     private static final String POLICIES_IDENTITIES_URL = "/policies";
46     private static final String POLICY_TYPES_URL = "/policytypes/";
47     private static final String POLICIES_URL = "/policies/";
48
49     private static final String POLICY_TYPE_1_NAME = "type1";
50     private static final String POLICY_TYPE_2_NAME = "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     private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
57     private static final String POLICY_TYPE = "typeName";
58
59     StdA1ClientVersion1 clientUnderTest;
60
61     @Mock
62     AsyncRestClient asyncRestClientMock;
63
64     @BeforeEach
65     public void init() {
66         clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock);
67     }
68
69     @Test
70     public void testGetPolicyTypeIdentities() {
71         Mono<String> policyTypeIds = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).toString());
72         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIds);
73
74         Mono<?> policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities();
75
76         verify(asyncRestClientMock).get(POLICY_TYPES_IDENTITIES_URL);
77         StepVerifier.create(policyTypeIdsFlux).expectNextCount(1).expectComplete().verify();
78     }
79
80     @Test
81     public void testGetPolicyIdentities() {
82         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
83         when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
84
85         Mono<?> policyIdsFlux = clientUnderTest.getPolicyIdentities();
86
87         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
88         StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify();
89     }
90
91     @Test
92     public void testGetValidPolicyType() {
93         Mono<String> policyTypeResp =
94             Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }");
95
96         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
97
98         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
99
100         verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
101         StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
102     }
103
104     @Test
105     public void testGetInvalidPolicyType() {
106         when(asyncRestClientMock.get(anyString())).thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
107
108         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
109
110         verify(asyncRestClientMock).get(POLICY_TYPES_URL + POLICY_TYPE_1_NAME);
111         StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
112             .verify();
113     }
114
115     @Test
116     public void testPutPolicyValidResponse() {
117         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
118
119         Mono<String> policyMono = clientUnderTest
120             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
121
122         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID);
123         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
124     }
125
126     @Test
127     public void testPutPolicyInvalidResponse() {
128         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
129
130         Mono<String> policyMono = clientUnderTest
131             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
132
133         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
134     }
135
136     @Test
137     public void testDeletePolicy() {
138         when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
139
140         Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
141         Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
142         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
143         StepVerifier.create(responseMono).expectComplete().verify();
144     }
145
146     @Test
147     public void testDeleteAllPolicies() {
148         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
149         when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
150         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
151
152         Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
153         StepVerifier.create(responseFlux).expectComplete().verify();
154         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
155         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
156         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);
157     }
158 }