90e0ed8ad2705c825f05cb3ef99bb450da287529
[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.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
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.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.oransc.policyagent.repository.Policy;
38
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
42
43 @ExtendWith(MockitoExtension.class)
44 public class StdA1ClientTest {
45     private static final String RIC_URL = "RicUrl";
46     private static final String POLICIES_IDENTITIES_URL = "/policies";
47     private static final String POLICIES_URL = "/policies/";
48     private static final String POLICY_TYPE_1_NAME = "type1";
49     private static final String POLICY_1_ID = "policy1";
50     private static final String POLICY_2_ID = "policy2";
51     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
52     private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
53     private static final String POLICY_TYPE = "typeName";
54
55     StdA1ClientVersion1 clientUnderTest;
56
57     @Mock
58     AsyncRestClient asyncRestClientMock;
59
60     @BeforeEach
61     public void init() {
62         clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock);
63     }
64
65     @Test
66     public void testGetPolicyTypeIdentities() {
67         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
68         assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
69         assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
70     }
71
72     @Test
73     public void testGetPolicyIdentities() {
74         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
75         when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
76
77         List<String> result = clientUnderTest.getPolicyIdentities().block();
78         assertEquals(2, result.size(), "");
79
80         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
81     }
82
83     @Test
84     public void testGetValidPolicyType() {
85         String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
86         assertEquals("{}", policyType, "");
87     }
88
89     @Test
90     public void testPutPolicyValidResponse() {
91         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
92
93         Mono<String> policyMono = clientUnderTest
94             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
95
96         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
97         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
98     }
99
100     @Test
101     public void testPutPolicyInvalidResponse() {
102         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
103
104         Mono<String> policyMono = clientUnderTest
105             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
106
107         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
108     }
109
110     @Test
111     public void testDeletePolicy() {
112         when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
113
114         Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
115         Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
116         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
117         StepVerifier.create(responseMono).expectComplete().verify();
118     }
119
120     @Test
121     public void testDeleteAllPolicies() {
122         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
123         when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
124         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
125
126         Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
127         StepVerifier.create(responseFlux).expectComplete().verify();
128         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
129         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
130         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);
131     }
132 }