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 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.when;
30 import java.util.Vector;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
38 import org.oransc.policyagent.configuration.ApplicationConfig;
39 import org.oransc.policyagent.configuration.ControllerConfig;
40 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
41 import org.oransc.policyagent.configuration.ImmutableRicConfig;
42 import org.oransc.policyagent.exceptions.ServiceException;
43 import org.oransc.policyagent.repository.Ric;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
48 @ExtendWith(MockitoExtension.class)
49 class A1ClientFactoryTest {
50 private static final String RIC_NAME = "Name";
51 private static final String EXCEPTION_MESSAGE = "Error";
54 private ApplicationConfig applicationConfigMock;
69 private A1ClientFactory factoryUnderTest;
71 private static ImmutableRicConfig ricConfig(String controllerName) {
72 return ImmutableRicConfig.builder() //
74 .baseUrl("baseUrl") //
75 .managedElementIds(new Vector<>()) //
76 .controllerName(controllerName) //
81 void createFactoryUnderTest() {
82 factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
83 this.ric = new Ric(ricConfig(""));
88 void getProtocolVersion_ok() throws ServiceException {
89 whenGetProtocolVersionThrowException(clientMock1);
90 whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
91 doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
93 A1Client client = factoryUnderTest.createA1Client(ric).block();
95 assertEquals(clientMock2, client, "Not correct client returned");
96 assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
100 void getProtocolVersion_ok_Last() throws ServiceException {
101 whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
102 whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
103 doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
105 A1Client client = factoryUnderTest.createA1Client(ric).block();
107 assertEquals(clientMock4, client, "Not correct client returned");
108 assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
112 void getProtocolVersion_error() throws ServiceException {
113 whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
114 doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
116 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
117 .expectSubscription() //
121 assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.name());
124 private A1Client createClient(A1ProtocolType version) throws ServiceException {
125 return factoryUnderTest.createClient(ric, version);
129 void create_check_types() throws ServiceException {
130 assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
131 assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
135 void create_check_types_controllers() throws ServiceException {
136 this.ric = new Ric(ricConfig("anythingButEmpty"));
137 whenGetGetControllerConfigReturn();
138 assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client);
140 whenGetGetControllerConfigReturn();
141 assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client);
143 whenGetGetControllerConfigReturn();
144 assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client);
147 private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
148 for (A1Client clientMock : clientMocks) {
149 when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
153 private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
154 when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
157 private void whenGetGetControllerConfigReturn() throws ServiceException {
158 ControllerConfig controllerCfg = ImmutableControllerConfig.builder() //
160 .baseUrl("baseUrl") //
164 when(applicationConfigMock.getControllerConfig(any())).thenReturn(controllerCfg);