Add unit tests for AsyncRestClient
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / A1ClientFactoryTest.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.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29 import static org.mockito.Mockito.when;
30
31 import ch.qos.logback.classic.Level;
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
34 import java.util.Vector;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
41 import org.oransc.policyagent.configuration.ApplicationConfig;
42 import org.oransc.policyagent.configuration.ImmutableRicConfig;
43 import org.oransc.policyagent.repository.Ric;
44 import org.oransc.policyagent.utils.LoggingUtils;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
47
48 @ExtendWith(MockitoExtension.class)
49 public class A1ClientFactoryTest {
50     private static final String RIC_NAME = "Name";
51     private static final String EXCEPTION_MESSAGE = "Error";
52
53     @Mock
54     private ApplicationConfig applicationConfigMock;
55
56     @Mock
57     A1Client stdA1ClientMock;
58
59     @Mock
60     A1Client oscA1ClientMock;
61
62     @Mock
63     A1Client sdncOscA1ClientMock;
64
65     @Mock
66     A1Client sdnrOnapA1ClientMock;
67
68     private ImmutableRicConfig ricConfig =
69         ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build();
70     private Ric ric = new Ric(ricConfig);
71
72     private A1ClientFactory factoryUnderTest;
73
74     @BeforeEach
75     public void createFactoryUnderTest() {
76         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
77     }
78
79     @Test
80     public void createStd_ok() {
81         whenGetProtocolVersionSdnrOnapA1ClientThrowException();
82         whenGetProtocolVersionSdncOscA1ClientThrowException();
83         whenGetProtocolVersionOscA1ClientThrowException();
84         whenGetProtocolVersionStdA1ClientReturnCorrectProtocol();
85
86         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
87             .expectSubscription() //
88             .expectNext(stdA1ClientMock) //
89             .verifyComplete();
90
91         assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
92     }
93
94     @Test
95     public void createOsc_ok() {
96         whenGetProtocolVersionSdnrOnapA1ClientThrowException();
97         whenGetProtocolVersionSdncOscA1ClientThrowException();
98         whenGetProtocolVersionOscA1ClientReturnCorrectProtocol();
99
100         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
101             .expectSubscription() //
102             .expectNext(oscA1ClientMock) //
103             .verifyComplete();
104
105         assertEquals(A1ProtocolType.OSC_V1, ric.getProtocolVersion(), "Not correct protocol");
106     }
107
108     @Test
109     public void createSdncOsc_ok() {
110         whenGetProtocolVersionSdnrOnapA1ClientThrowException();
111         whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol();
112
113         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
114             .expectSubscription() //
115             .expectNext(sdncOscA1ClientMock) //
116             .verifyComplete();
117
118         assertEquals(A1ProtocolType.SDNC_OSC, ric.getProtocolVersion(), "Not correct protocol");
119     }
120
121     @Test
122     public void createSdnrOnap_ok() {
123         whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol();
124
125         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
126             .expectSubscription() //
127             .expectNext(sdnrOnapA1ClientMock) //
128             .verifyComplete();
129
130         assertEquals(A1ProtocolType.SDNR_ONAP, ric.getProtocolVersion(), "Not correct protocol");
131     }
132
133     @Test
134     public void createWithNoProtocol_error() {
135         whenGetProtocolVersionSdnrOnapA1ClientThrowException();
136         whenGetProtocolVersionSdncOscA1ClientThrowException();
137         whenGetProtocolVersionOscA1ClientThrowException();
138         whenGetProtocolVersionStdA1ClientThrowException();
139
140         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(A1ClientFactory.class);
141         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
142             .expectSubscription() //
143             .expectErrorMatches(
144                 throwable -> throwable instanceof Exception && throwable.getMessage().equals(EXCEPTION_MESSAGE))
145             .verify();
146
147         assertEquals(Level.WARN, logAppender.list.get(0).getLevel(), "Warning not logged");
148         assertTrue(logAppender.list.toString().contains("Could not get protocol version from RIC: " + RIC_NAME),
149             "Correct message not logged");
150
151         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Not correct protocol");
152     }
153
154     @Test
155     public void createWithProtocolInRic_noTrialAndError() {
156         doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
157
158         ric.setProtocolVersion(A1ProtocolType.STD_V1);
159
160         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
161             .expectSubscription() //
162             .expectNext(stdA1ClientMock) //
163             .verifyComplete();
164
165         assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
166
167         verifyNoMoreInteractions(sdnrOnapA1ClientMock);
168         verifyNoMoreInteractions(sdncOscA1ClientMock);
169         verifyNoMoreInteractions(oscA1ClientMock);
170         verifyNoMoreInteractions(stdA1ClientMock);
171     }
172
173     private void whenGetProtocolVersionSdnrOnapA1ClientThrowException() {
174         doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(ric);
175         when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
176     }
177
178     private void whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol() {
179         doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(any(Ric.class));
180         when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNR_ONAP));
181     }
182
183     private void whenGetProtocolVersionSdncOscA1ClientThrowException() {
184         doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
185         when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
186     }
187
188     private void whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol() {
189         doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
190         when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_OSC));
191     }
192
193     private void whenGetProtocolVersionOscA1ClientThrowException() {
194         doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
195         when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
196     }
197
198     private void whenGetProtocolVersionOscA1ClientReturnCorrectProtocol() {
199         doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
200         when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.OSC_V1));
201     }
202
203     private void whenGetProtocolVersionStdA1ClientThrowException() {
204         doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
205         when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
206     }
207
208     private void whenGetProtocolVersionStdA1ClientReturnCorrectProtocol() {
209         doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
210         when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.STD_V1));
211     }
212 }