a23540e50144d26cfb7b9ba2f3038496389f3fec
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
6  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
7  * ======================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
23
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.when;
30
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.DisplayName;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
39 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
40 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
41 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
42 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
43 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
44
45 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
46 import reactor.core.publisher.Mono;
47 import reactor.test.StepVerifier;
48
49 @ExtendWith(MockitoExtension.class)
50 class A1ClientFactoryTest {
51     private static final String RIC_NAME = "Name";
52     private static final String EXCEPTION_MESSAGE = "Error";
53
54     @Mock
55     private ApplicationConfig applicationConfigMock;
56
57     @Mock
58     A1Client clientMock1;
59
60     @Mock
61     A1Client clientMock2;
62
63     @Mock
64     A1Client clientMock3;
65
66     @Mock
67     A1Client clientMock4;
68
69     private Ric ric;
70     private A1ClientFactory factoryUnderTest;
71
72     private static RicConfig ricConfig(String controllerName, String customAdapter) {
73         ControllerConfig controllerConfig = null;
74         if (!controllerName.isEmpty()) {
75             controllerConfig = ControllerConfig.builder().baseUrl("baseUrl").name(controllerName).build();
76         }
77         return RicConfig.builder() //
78                 .ricId(RIC_NAME) //
79                 .baseUrl("baseUrl") //
80                 .controllerConfig(controllerConfig) //
81                 .customAdapterClass(customAdapter) //
82                 .build();
83     }
84
85     private static RicConfig ricConfig(String controllerName) {
86         return ricConfig(controllerName, "");
87     }
88
89     @BeforeEach
90     void createFactoryUnderTest() {
91         SecurityContext sec = new SecurityContext("");
92         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock, sec));
93         this.ric = new Ric(ricConfig(""));
94     }
95
96     @Test
97     @DisplayName("test get Protocol Version ok")
98     void getProtocolVersion_ok() throws ServiceException {
99         whenGetProtocolVersionThrowException(clientMock1);
100         whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
101         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
102
103         A1Client client = factoryUnderTest.createA1Client(ric).block();
104
105         assertEquals(clientMock2, client, "Not correct client returned");
106         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
107     }
108
109     @Test
110     @DisplayName("test get Protocol Version ok Last")
111     void getProtocolVersion_ok_Last() throws ServiceException {
112         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
113         whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
114         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
115
116         A1Client client = factoryUnderTest.createA1Client(ric).block();
117
118         assertEquals(clientMock4, client, "Not correct client returned");
119         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
120     }
121
122     public static class CustomA1AdapterFactory implements A1Client.Factory {
123         @Override
124         public A1Client create(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
125             return new StdA1ClientVersion2(ricConfig, restClientFactory);
126         }
127     }
128
129     @Test
130     @DisplayName("test Custom Adapter Creation")
131     void testCustomAdapterCreation() {
132
133         Ric ric = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName()));
134         A1Client client = factoryUnderTest.createA1Client(ric).block();
135
136         assertEquals(client.getClass(), StdA1ClientVersion2.class);
137
138         ric = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2"));
139         client = factoryUnderTest.createA1Client(ric).block();
140
141         assertEquals(client.getClass(), StdA1ClientVersion2.class);
142
143         ric = new Ric(
144                 ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2$Factory"));
145         client = factoryUnderTest.createA1Client(ric).block();
146
147         assertEquals(client.getClass(), StdA1ClientVersion2.class);
148
149         Exception e = Assertions.assertThrows(Exception.class, () -> {
150             factoryUnderTest.createClient(new Ric(ricConfig("", "junk")), A1ProtocolType.CUSTOM_PROTOCOL);
151         });
152         assertEquals("Could not find class: junk", e.getMessage());
153
154         Exception exceptionNoSuchMethod = Assertions.assertThrows(Exception.class, () -> {
155             factoryUnderTest.createClient(new Ric(ricConfig("", MockA1Client.class.getName())),
156                     A1ProtocolType.CUSTOM_PROTOCOL);
157         });
158         assertEquals("Could not find the required constructor in class " + MockA1Client.class.getName(),
159                 exceptionNoSuchMethod.getMessage());
160
161         Exception exceptionNullCustomAdaptor = Assertions.assertThrows(Exception.class, () -> {
162             factoryUnderTest.createClient(new Ric(ricConfig("", null)), A1ProtocolType.CUSTOM_PROTOCOL);
163         });
164         assertEquals("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL", exceptionNullCustomAdaptor.getMessage());
165     }
166
167     @Test
168     @DisplayName("test get Protocol Version error")
169     void getProtocolVersion_error() throws ServiceException {
170         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
171         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
172
173         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
174                 .expectSubscription() //
175                 .expectError() //
176                 .verify();
177
178         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.id());
179     }
180
181     private A1Client createClient(A1ProtocolType version) throws ServiceException {
182         return factoryUnderTest.createClient(ric, version);
183     }
184
185     @Test
186     @DisplayName("tes create check types")
187     void create_check_types() throws ServiceException {
188         assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
189         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
190     }
191
192     @Test
193     @DisplayName("test create check types controllers")
194     void create_check_types_controllers() throws ServiceException {
195         this.ric = new Ric(ricConfig("anythingButEmpty"));
196
197         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient);
198
199         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient);
200     }
201
202     private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
203         for (A1Client clientMock : clientMocks) {
204             when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
205         }
206     }
207
208     private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
209         when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
210     }
211
212 }