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