2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.clients;
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;
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
35 import java.util.Vector;
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;
48 import reactor.core.publisher.Mono;
49 import reactor.test.StepVerifier;
51 @ExtendWith(MockitoExtension.class)
52 public class A1ClientFactoryTest {
53 private static final String RIC_NAME = "Name";
54 private static final String EXCEPTION_MESSAGE = "Error";
57 private ApplicationConfig applicationConfigMock;
60 A1Client stdA1ClientMock;
63 A1Client oscA1ClientMock;
66 A1Client sdncOscA1ClientMock;
69 A1Client sdncOnapA1ClientMock;
71 private ImmutableRicConfig ricConfig =
72 ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build();
73 private Ric ric = new Ric(ricConfig);
75 private A1ClientFactory factoryUnderTest;
78 public void createFactoryUnderTest() {
79 factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
83 public void createStd_ok() {
84 whenGetProtocolVersionSdncOnapA1ClientThrowException();
85 whenGetProtocolVersionSdncOscA1ClientThrowException();
86 whenGetProtocolVersionOscA1ClientThrowException();
87 whenGetProtocolVersionStdA1ClientReturnCorrectProtocol();
89 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
90 .expectSubscription() //
91 .expectNext(stdA1ClientMock) //
94 assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
98 public void createOsc_ok() {
99 whenGetProtocolVersionSdncOnapA1ClientThrowException();
100 whenGetProtocolVersionSdncOscA1ClientThrowException();
101 whenGetProtocolVersionOscA1ClientReturnCorrectProtocol();
103 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
104 .expectSubscription() //
105 .expectNext(oscA1ClientMock) //
108 assertEquals(A1ProtocolType.OSC_V1, ric.getProtocolVersion(), "Not correct protocol");
112 public void createSdncOsc_ok() {
113 whenGetProtocolVersionSdncOnapA1ClientThrowException();
114 whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol();
116 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
117 .expectSubscription() //
118 .expectNext(sdncOscA1ClientMock) //
121 assertEquals(A1ProtocolType.SDNC_OSC, ric.getProtocolVersion(), "Not correct protocol");
125 public void createSdncOnap_ok() {
126 whenGetProtocolVersionSdncOnapA1ClientReturnCorrectProtocol();
128 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
129 .expectSubscription() //
130 .expectNext(sdncOnapA1ClientMock) //
133 assertEquals(A1ProtocolType.SDNC_ONAP, ric.getProtocolVersion(), "Not correct protocol");
137 public void createWithNoProtocol_error() {
138 whenGetProtocolVersionSdncOnapA1ClientThrowException();
139 whenGetProtocolVersionSdncOscA1ClientThrowException();
140 whenGetProtocolVersionOscA1ClientThrowException();
141 whenGetProtocolVersionStdA1ClientThrowException();
143 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(A1ClientFactory.class, WARN);
144 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
145 .expectSubscription() //
147 throwable -> throwable instanceof Exception && throwable.getMessage().equals(EXCEPTION_MESSAGE))
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");
154 assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Not correct protocol");
158 public void createWithProtocolInRic_noTrialAndError() {
159 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
161 ric.setProtocolVersion(A1ProtocolType.STD_V1);
163 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
164 .expectSubscription() //
165 .expectNext(stdA1ClientMock) //
168 assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
170 verifyNoMoreInteractions(sdncOnapA1ClientMock);
171 verifyNoMoreInteractions(sdncOscA1ClientMock);
172 verifyNoMoreInteractions(oscA1ClientMock);
173 verifyNoMoreInteractions(stdA1ClientMock);
176 private void whenGetProtocolVersionSdncOnapA1ClientThrowException() {
177 doReturn(sdncOnapA1ClientMock).when(factoryUnderTest).createSdncOnapA1Client(ric);
178 when(sdncOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
181 private void whenGetProtocolVersionSdncOnapA1ClientReturnCorrectProtocol() {
182 doReturn(sdncOnapA1ClientMock).when(factoryUnderTest).createSdncOnapA1Client(any(Ric.class));
183 when(sdncOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_ONAP));
186 private void whenGetProtocolVersionSdncOscA1ClientThrowException() {
187 doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
188 when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
191 private void whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol() {
192 doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
193 when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_OSC));
196 private void whenGetProtocolVersionOscA1ClientThrowException() {
197 doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
198 when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
201 private void whenGetProtocolVersionOscA1ClientReturnCorrectProtocol() {
202 doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
203 when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.OSC_V1));
206 private void whenGetProtocolVersionStdA1ClientThrowException() {
207 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
208 when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
211 private void whenGetProtocolVersionStdA1ClientReturnCorrectProtocol() {
212 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
213 when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.STD_V1));