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