b3c77355c18d5f571ab5d2bd366ed99038eb45cd
[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 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45
46 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.TestPropertySource;
49
50 import reactor.core.publisher.Mono;
51 import reactor.test.StepVerifier;
52
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @TestPropertySource(properties = { //
55     "management.tracing.enabled=false",
56     "server.ssl.key-store=./config/keystore.jks", //
57     "app.webclient.trust-store=./config/truststore.jks", //
58     "app.webclient.trust-store-used=true", //
59     "app.vardata-directory=/tmp/pmstest", //
60     "app.filepath=", //
61     "app.s3.bucket=" // If this is set, S3 will be used to store data.
62 })
63 @ExtendWith(MockitoExtension.class)
64 class A1ClientFactoryTest {
65     private static final String RIC_NAME = "Name";
66     private static final String EXCEPTION_MESSAGE = "Error";
67
68     @Mock
69     private ApplicationConfig applicationConfigMock;
70
71     @Mock
72     A1Client clientMock1;
73
74     @Mock
75     A1Client clientMock2;
76
77     @Mock
78     A1Client clientMock3;
79
80     @Mock
81     A1Client clientMock4;
82
83     private Ric ric;
84     private A1ClientFactory factoryUnderTest;
85
86     private static RicConfig ricConfig(String controllerName, String customAdapter) {
87         ControllerConfig controllerConfig = null;
88         if (!controllerName.isEmpty()) {
89             controllerConfig = ControllerConfig.builder().baseUrl("baseUrl").name(controllerName).build();
90         }
91         return RicConfig.builder() //
92                 .ricId(RIC_NAME) //
93                 .baseUrl("baseUrl") //
94                 .controllerConfig(controllerConfig) //
95                 .customAdapterClass(customAdapter) //
96                 .build();
97     }
98
99     private static RicConfig ricConfig(String controllerName) {
100         return ricConfig(controllerName, "");
101     }
102
103     @BeforeEach
104     void createFactoryUnderTest() {
105         SecurityContext sec = new SecurityContext("");
106         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock, sec));
107         this.ric = new Ric(ricConfig(""));
108     }
109
110     @Test
111     @DisplayName("test get Protocol Version ok")
112     void getProtocolVersion_ok() throws ServiceException {
113         whenGetProtocolVersionThrowException(clientMock1);
114         whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
115         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
116
117         A1Client client = factoryUnderTest.createA1Client(ric).block();
118
119         assertEquals(clientMock2, client, "Not correct client returned");
120         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
121     }
122
123     @Test
124     @DisplayName("test get Protocol Version ok Last")
125     void getProtocolVersion_ok_Last() throws ServiceException {
126         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
127         whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
128         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
129
130         A1Client client = factoryUnderTest.createA1Client(ric).block();
131
132         assertEquals(clientMock4, client, "Not correct client returned");
133         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
134     }
135
136     public static class CustomA1AdapterFactory implements A1Client.Factory {
137         @Override
138         public A1Client create(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
139             return new StdA1ClientVersion2(ricConfig, restClientFactory);
140         }
141     }
142
143     @Test
144     @DisplayName("test Custom Adapter Creation")
145     void testCustomAdapterCreation() {
146
147         Ric ric = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName()));
148         A1Client client = factoryUnderTest.createA1Client(ric).block();
149
150         assertEquals(client.getClass(), StdA1ClientVersion2.class);
151
152         ric = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2"));
153         client = factoryUnderTest.createA1Client(ric).block();
154
155         assertEquals(client.getClass(), StdA1ClientVersion2.class);
156
157         ric = new Ric(
158                 ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2$Factory"));
159         client = factoryUnderTest.createA1Client(ric).block();
160
161         assertEquals(client.getClass(), StdA1ClientVersion2.class);
162
163         Exception e = Assertions.assertThrows(Exception.class, () -> {
164             factoryUnderTest.createClient(new Ric(ricConfig("", "junk")), A1ProtocolType.CUSTOM_PROTOCOL);
165         });
166         assertEquals("Could not find class: junk", e.getMessage());
167
168         Exception exceptionNoSuchMethod = Assertions.assertThrows(Exception.class, () -> {
169             factoryUnderTest.createClient(new Ric(ricConfig("", MockA1Client.class.getName())),
170                     A1ProtocolType.CUSTOM_PROTOCOL);
171         });
172         assertEquals("Could not find the required constructor in class " + MockA1Client.class.getName(),
173                 exceptionNoSuchMethod.getMessage());
174
175         Exception exceptionNullCustomAdaptor = Assertions.assertThrows(Exception.class, () -> {
176             factoryUnderTest.createClient(new Ric(ricConfig("", null)), A1ProtocolType.CUSTOM_PROTOCOL);
177         });
178         assertEquals("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL", exceptionNullCustomAdaptor.getMessage());
179     }
180
181     @Test
182     @DisplayName("test get Protocol Version error")
183     void getProtocolVersion_error() throws ServiceException {
184         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
185         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
186
187         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
188                 .expectSubscription() //
189                 .expectError() //
190                 .verify();
191
192         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.id());
193     }
194
195     private A1Client createClient(A1ProtocolType version) throws ServiceException {
196         return factoryUnderTest.createClient(ric, version);
197     }
198
199     @Test
200     @DisplayName("tes create check types")
201     void create_check_types() throws ServiceException {
202         assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
203         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
204     }
205
206     @Test
207     @DisplayName("test create check types controllers")
208     void create_check_types_controllers() throws ServiceException {
209         this.ric = new Ric(ricConfig("anythingButEmpty"));
210
211         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient);
212
213         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient);
214     }
215
216     private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
217         for (A1Client clientMock : clientMocks) {
218             when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
219         }
220     }
221
222     private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
223         when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
224     }
225
226 }