Generalization of controller
[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.json.JSONException;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.oransc.policyagent.configuration.RicConfig;
39 import org.oransc.policyagent.repository.Policy;
40
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
44
45 @ExtendWith(MockitoExtension.class)
46 public class StdA1ClientTest {
47     private static final String RIC_URL = "RicUrl";
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     @Mock
61     RicConfig ricConfigMock;
62
63     @BeforeEach
64     public void init() {
65         clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock);
66     }
67
68     private String policiesUrl() {
69         return RIC_URL + "/A1-P/v1/policies";
70     }
71
72     private String policiesBaseUrl() {
73         return policiesUrl() + "/";
74     }
75
76     @Test
77     public void testGetPolicyTypeIdentities() {
78         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
79         assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
80         assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
81     }
82
83     @Test
84     public void testGetPolicyIdentities() {
85         doReturn(RIC_URL).when(ricConfigMock).baseUrl();
86         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
87         when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
88
89         List<String> result = clientUnderTest.getPolicyIdentities().block();
90         assertEquals(2, result.size(), "");
91
92         verify(asyncRestClientMock).get(policiesUrl());
93     }
94
95     @Test
96     public void testGetValidPolicyType() {
97         String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
98         assertEquals("{}", policyType, "");
99     }
100
101     @Test
102     public void testPutPolicyValidResponse() {
103         doReturn(RIC_URL).when(ricConfigMock).baseUrl();
104         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
105
106         Mono<String> policyMono = clientUnderTest
107             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
108
109         verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON_VALID);
110         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
111     }
112
113     @Test
114     public void testPutPolicyInvalidResponse() {
115         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
116
117         Mono<String> policyMono = clientUnderTest
118             .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
119
120         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
121     }
122
123     @Test
124     public void testDeletePolicy() {
125         doReturn(RIC_URL).when(ricConfigMock).baseUrl();
126         final String url = policiesBaseUrl() + POLICY_1_ID;
127         when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty());
128
129         Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
130         Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
131         verify(asyncRestClientMock).delete(url);
132         StepVerifier.create(responseMono).expectComplete().verify();
133     }
134
135     @Test
136     public void testDeleteAllPolicies() {
137         doReturn(RIC_URL).when(ricConfigMock).baseUrl();
138         Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
139         when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds);
140         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
141
142         Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
143         StepVerifier.create(responseFlux).expectComplete().verify();
144         verify(asyncRestClientMock).get(policiesUrl());
145         verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID);
146         verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID);
147     }
148 }