Merge "Add unit test for A1 client"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / A1ClientImplTest.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.Mockito.doReturn;
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.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.mockito.junit.jupiter.MockitoExtension;
38
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
42
43 @ExtendWith(MockitoExtension.class)
44 @RunWith(MockitoJUnitRunner.class)
45 public class A1ClientImplTest {
46     private static final String RIC_URL = "RicUrl";
47     private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes/identities";
48     private static final String POLICIES_IDENTITIES_URL = "/policies/identities";
49     private static final String POLICYTYPES_URL = "/policytypes/";
50     private static final String POLICIES_URL = "/policies/";
51
52     private static final String POLICY_TYPE_1_NAME = "type1";
53     private static final String POLICY_TYPE_2_NAME = "type2";
54     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
55     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
56     private static final String POLICY_1_ID = "policy1";
57     private static final String POLICY_2_ID = "policy2";
58     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
59     private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
60
61     @Spy
62     A1ClientImpl a1Client;
63
64     @Mock
65     AsyncRestClient asyncRestClientMock;
66
67     @BeforeEach
68     public void init() {
69         doReturn(asyncRestClientMock).when(a1Client).createClient(RIC_URL);
70     }
71
72     @Test
73     public void testGetPolicyTypeIdentities() {
74         Mono<String> policyTypeIds = Mono.just(Arrays.toString(new String[] {POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME}));
75         when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
76
77         Flux<String> policyTypeIdsFlux = a1Client.getPolicyTypeIdentities(RIC_URL);
78         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
79         StepVerifier.create(policyTypeIdsFlux).expectNext(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).expectComplete()
80             .verify();
81     }
82
83     @Test
84     public void testGetPolicyIdentities() {
85         Mono<String> policyIds = Mono.just(Arrays.toString(new String[] {POLICY_1_ID, POLICY_2_ID}));
86         when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
87
88         Flux<String> policyIdsFlux = a1Client.getPolicyIdentities(RIC_URL);
89         verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
90         StepVerifier.create(policyIdsFlux).expectNext(POLICY_1_ID, POLICY_2_ID).expectComplete().verify();
91     }
92
93     @Test
94     public void testGetValidPolicyType() {
95         when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
96             .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_VALID));
97
98         Mono<String> policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME);
99         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
100         StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
101     }
102
103     @Test
104     public void testGetInvalidPolicyType() {
105         when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
106             .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
107
108         Mono<String> policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME);
109         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
110         StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
111             .verify();
112     }
113
114     @Test
115     public void testPutPolicyValidResponse() {
116         when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID))
117             .thenReturn(Mono.just(POLICY_JSON_VALID));
118
119         Mono<String> policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID);
120         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
121         StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
122     }
123
124     @Test
125     public void testPutPolicyInvalidResponse() {
126         when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID))
127             .thenReturn(Mono.just(POLICY_JSON_INVALID));
128
129         Mono<String> policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID);
130         verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
131         StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
132     }
133
134     @Test
135     public void testDeletePolicy() {
136         when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
137
138         Mono<Void> responseMono = a1Client.deletePolicy(RIC_URL, POLICY_1_ID);
139         verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
140         StepVerifier.create(responseMono).expectComplete().verify();
141     }
142 }