Remove code smells from policyagent/clients
[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.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29
30 import org.json.JSONException;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.oransc.policyagent.repository.Policy;
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 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_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     StdA1Client clientUnderTest;
60
61     @Mock
62     AsyncRestClient asyncRestClientMock;
63
64     @BeforeEach
65     public void init() {
66         clientUnderTest = new StdA1Client(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(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
73
74         Mono<?> policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities();
75         verify(asyncRestClientMock).get(POLICYTYPES_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(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
83
84         Mono<?> policyIdsFlux = clientUnderTest.getPolicyIdentities();
85         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
86         StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify();
87     }
88
89     @Test
90     public void testGetValidPolicyType() {
91         Mono<?> policyTypeResp =
92             Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }");
93
94         doReturn(policyTypeResp).when(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
95
96         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
97         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
98         StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
99     }
100
101     @Test
102     public void testGetInvalidPolicyType() {
103         when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
104             .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
105
106         Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
107         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
108         StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
109             .verify();
110     }
111
112     @Test
113     public void testPutPolicyValidResponse() {
114         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
115
116         Mono<String> policyMono = clientUnderTest
117             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
118         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID);
119         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
120     }
121
122     @Test
123     public void testPutPolicyInvalidResponse() {
124         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
125
126         Mono<String> policyMono = clientUnderTest
127             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
128         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
129     }
130
131     @Test
132     public void testDeletePolicy() {
133         when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
134
135         Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
136         Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
137         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
138         StepVerifier.create(responseMono).expectComplete().verify();
139     }
140
141     @Test
142     public void testDeleteAllPolicies() {
143         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
144         when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
145         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
146
147         Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
148         StepVerifier.create(responseFlux).expectComplete().verify();
149         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
150         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
151         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);
152     }
153 }