2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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 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;
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;
48 @ExtendWith(MockitoExtension.class)
49 public class A1ClientFactoryTest {
50 private static final String RIC_NAME = "Name";
51 private static final String EXCEPTION_MESSAGE = "Error";
54 private ApplicationConfig applicationConfigMock;
57 A1Client stdA1ClientMock;
60 A1Client oscA1ClientMock;
63 A1Client sdncOscA1ClientMock;
66 A1Client sdnrOnapA1ClientMock;
68 private ImmutableRicConfig ricConfig =
69 ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build();
70 private Ric ric = new Ric(ricConfig);
72 private A1ClientFactory factoryUnderTest;
75 public void createFactoryUnderTest() {
76 factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
80 public void createStd_ok() {
81 whenGetProtocolVersionSdnrOnapA1ClientThrowException();
82 whenGetProtocolVersionSdncOscA1ClientThrowException();
83 whenGetProtocolVersionOscA1ClientThrowException();
84 whenGetProtocolVersionStdA1ClientReturnCorrectProtocol();
86 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
87 .expectSubscription() //
88 .expectNext(stdA1ClientMock) //
91 assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
95 public void createOsc_ok() {
96 whenGetProtocolVersionSdnrOnapA1ClientThrowException();
97 whenGetProtocolVersionSdncOscA1ClientThrowException();
98 whenGetProtocolVersionOscA1ClientReturnCorrectProtocol();
100 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
101 .expectSubscription() //
102 .expectNext(oscA1ClientMock) //
105 assertEquals(A1ProtocolType.OSC_V1, ric.getProtocolVersion(), "Not correct protocol");
109 public void createSdncOsc_ok() {
110 whenGetProtocolVersionSdnrOnapA1ClientThrowException();
111 whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol();
113 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
114 .expectSubscription() //
115 .expectNext(sdncOscA1ClientMock) //
118 assertEquals(A1ProtocolType.SDNC_OSC, ric.getProtocolVersion(), "Not correct protocol");
122 public void createSdnrOnap_ok() {
123 whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol();
125 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
126 .expectSubscription() //
127 .expectNext(sdnrOnapA1ClientMock) //
130 assertEquals(A1ProtocolType.SDNR_ONAP, ric.getProtocolVersion(), "Not correct protocol");
134 public void createWithNoProtocol_error() {
135 whenGetProtocolVersionSdnrOnapA1ClientThrowException();
136 whenGetProtocolVersionSdncOscA1ClientThrowException();
137 whenGetProtocolVersionOscA1ClientThrowException();
138 whenGetProtocolVersionStdA1ClientThrowException();
140 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(A1ClientFactory.class);
141 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
142 .expectSubscription() //
144 throwable -> throwable instanceof Exception && throwable.getMessage().equals(EXCEPTION_MESSAGE))
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");
151 assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Not correct protocol");
155 public void createWithProtocolInRic_noTrialAndError() {
156 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
158 ric.setProtocolVersion(A1ProtocolType.STD_V1);
160 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
161 .expectSubscription() //
162 .expectNext(stdA1ClientMock) //
165 assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
167 verifyNoMoreInteractions(sdnrOnapA1ClientMock);
168 verifyNoMoreInteractions(sdncOscA1ClientMock);
169 verifyNoMoreInteractions(oscA1ClientMock);
170 verifyNoMoreInteractions(stdA1ClientMock);
173 private void whenGetProtocolVersionSdnrOnapA1ClientThrowException() {
174 doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(ric);
175 when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
178 private void whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol() {
179 doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(any(Ric.class));
180 when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNR_ONAP));
183 private void whenGetProtocolVersionSdncOscA1ClientThrowException() {
184 doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
185 when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
188 private void whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol() {
189 doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
190 when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_OSC));
193 private void whenGetProtocolVersionOscA1ClientThrowException() {
194 doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
195 when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
198 private void whenGetProtocolVersionOscA1ClientReturnCorrectProtocol() {
199 doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
200 when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.OSC_V1));
203 private void whenGetProtocolVersionStdA1ClientThrowException() {
204 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
205 when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
208 private void whenGetProtocolVersionStdA1ClientReturnCorrectProtocol() {
209 doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
210 when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.STD_V1));