2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
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
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.onap.ccsdk.oran.a1policymanagementservice.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 org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.DisplayName;
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.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
38 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
39 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
40 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
41 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
42 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
47 @ExtendWith(MockitoExtension.class)
48 class A1ClientFactoryTest {
49 private static final String RIC_NAME = "Name";
50 private static final String EXCEPTION_MESSAGE = "Error";
53 private ApplicationConfig applicationConfigMock;
68 private A1ClientFactory factoryUnderTest;
70 private static RicConfig ricConfig(String controllerName, String customAdapter) {
71 ControllerConfig controllerConfig = null;
72 if (!controllerName.isEmpty()) {
73 controllerConfig = ControllerConfig.builder().baseUrl("baseUrl").name(controllerName).build();
75 return RicConfig.builder() //
77 .baseUrl("baseUrl") //
78 .controllerConfig(controllerConfig) //
79 .customAdapterClass(customAdapter) //
83 private static RicConfig ricConfig(String controllerName) {
84 return ricConfig(controllerName, "");
88 void createFactoryUnderTest() {
89 SecurityContext sec = new SecurityContext("");
90 factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock, sec));
91 this.ric = new Ric(ricConfig(""));
95 @DisplayName("test get Protocol Version ok")
96 void getProtocolVersion_ok() throws ServiceException {
97 whenGetProtocolVersionThrowException(clientMock1);
98 whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
99 doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
101 A1Client client = factoryUnderTest.createA1Client(ric).block();
103 assertEquals(clientMock2, client, "Not correct client returned");
104 assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
108 @DisplayName("test get Protocol Version ok Last")
109 void getProtocolVersion_ok_Last() throws ServiceException {
110 whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
111 whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
112 doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
114 A1Client client = factoryUnderTest.createA1Client(ric).block();
116 assertEquals(clientMock4, client, "Not correct client returned");
117 assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
120 public static class CustomA1AdapterFactory implements A1Client.Factory {
122 public A1Client create(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
123 return new StdA1ClientVersion2(ricConfig, restClientFactory);
128 @DisplayName("test Custom Adapter Creation")
129 void testCustomAdapterCreation() {
131 Ric ric = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName()));
132 A1Client client = factoryUnderTest.createA1Client(ric).block();
134 assertEquals(client.getClass(), StdA1ClientVersion2.class);
136 ric = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2"));
137 client = factoryUnderTest.createA1Client(ric).block();
139 assertEquals(client.getClass(), StdA1ClientVersion2.class);
142 ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2$Factory"));
143 client = factoryUnderTest.createA1Client(ric).block();
145 assertEquals(client.getClass(), StdA1ClientVersion2.class);
147 Exception e = Assertions.assertThrows(Exception.class, () -> {
148 factoryUnderTest.createClient(new Ric(ricConfig("", "junk")), A1ProtocolType.CUSTOM_PROTOCOL);
150 assertEquals("Could not find class: junk", e.getMessage());
154 @DisplayName("test get Protocol Version error")
155 void getProtocolVersion_error() throws ServiceException {
156 whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
157 doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
159 StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
160 .expectSubscription() //
164 assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.id());
167 private A1Client createClient(A1ProtocolType version) throws ServiceException {
168 return factoryUnderTest.createClient(ric, version);
172 @DisplayName("tes create check types")
173 void create_check_types() throws ServiceException {
174 assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
175 assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
179 @DisplayName("test create check types controllers")
180 void create_check_types_controllers() throws ServiceException {
181 this.ric = new Ric(ricConfig("anythingButEmpty"));
183 assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient);
185 assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient);
188 private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
189 for (A1Client clientMock : clientMocks) {
190 when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
194 private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
195 when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));