ef4ad1c83cf2c812fff34db218f0c66dc4dfa7e4
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
22
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;
29
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;
43
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
46
47 @ExtendWith(MockitoExtension.class)
48 class A1ClientFactoryTest {
49     private static final String RIC_NAME = "Name";
50     private static final String EXCEPTION_MESSAGE = "Error";
51
52     @Mock
53     private ApplicationConfig applicationConfigMock;
54
55     @Mock
56     A1Client clientMock1;
57
58     @Mock
59     A1Client clientMock2;
60
61     @Mock
62     A1Client clientMock3;
63
64     @Mock
65     A1Client clientMock4;
66
67     private Ric ric;
68     private A1ClientFactory factoryUnderTest;
69
70     private static RicConfig ricConfig(String controllerName, String customAdapter) {
71         return RicConfig.builder() //
72                 .ricId(RIC_NAME) //
73                 .baseUrl("baseUrl") //
74                 .controllerName(controllerName) //
75                 .customAdapterClass(customAdapter) //
76                 .build();
77     }
78
79     private static RicConfig ricConfig(String controllerName) {
80         return ricConfig(controllerName, "");
81     }
82
83     @BeforeEach
84     void createFactoryUnderTest() {
85         SecurityContext sec = new SecurityContext("");
86         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock, sec));
87         this.ric = new Ric(ricConfig(""));
88     }
89
90     @Test
91     @DisplayName("test get Protocol Version ok")
92     void getProtocolVersion_ok() throws ServiceException {
93         whenGetProtocolVersionThrowException(clientMock1);
94         whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
95         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
96
97         A1Client client = factoryUnderTest.createA1Client(ric).block();
98
99         assertEquals(clientMock2, client, "Not correct client returned");
100         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
101     }
102
103     @Test
104     @DisplayName("test get Protocol Version ok Last")
105     void getProtocolVersion_ok_Last() throws ServiceException {
106         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
107         whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
108         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
109
110         A1Client client = factoryUnderTest.createA1Client(ric).block();
111
112         assertEquals(clientMock4, client, "Not correct client returned");
113         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
114     }
115
116     public static class CustomA1AdapterFactory implements A1Client.Factory {
117         @Override
118         public A1Client create(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
119             return new StdA1ClientVersion2(ricConfig, restClientFactory);
120         }
121     }
122
123     @Test
124     @DisplayName("test Custom Adapter Creation")
125     void testCustomAdapterCreation() {
126
127         Ric ric = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName()));
128         A1Client client = factoryUnderTest.createA1Client(ric).block();
129
130         assertEquals(client.getClass(), StdA1ClientVersion2.class);
131
132         ric = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2"));
133         client = factoryUnderTest.createA1Client(ric).block();
134
135         assertEquals(client.getClass(), StdA1ClientVersion2.class);
136
137         ric = new Ric(
138                 ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2$Factory"));
139         client = factoryUnderTest.createA1Client(ric).block();
140
141         assertEquals(client.getClass(), StdA1ClientVersion2.class);
142
143         Exception e = Assertions.assertThrows(Exception.class, () -> {
144             factoryUnderTest.createClient(new Ric(ricConfig("", "junk")), A1ProtocolType.CUSTOM_PROTOCOL);
145         });
146         assertEquals("Could not find class: junk", e.getMessage());
147     }
148
149     @Test
150     @DisplayName("test get Protocol Version error")
151     void getProtocolVersion_error() throws ServiceException {
152         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
153         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
154
155         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
156                 .expectSubscription() //
157                 .expectError() //
158                 .verify();
159
160         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.id());
161     }
162
163     private A1Client createClient(A1ProtocolType version) throws ServiceException {
164         return factoryUnderTest.createClient(ric, version);
165     }
166
167     @Test
168     @DisplayName("tes create check types")
169     void create_check_types() throws ServiceException {
170         assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
171         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
172     }
173
174     @Test
175     @DisplayName("test create check types controllers")
176     void create_check_types_controllers() throws ServiceException {
177         this.ric = new Ric(ricConfig("anythingButEmpty"));
178         whenGetGetControllerConfigReturn();
179
180         whenGetGetControllerConfigReturn();
181         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient);
182
183         whenGetGetControllerConfigReturn();
184         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient);
185     }
186
187     private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
188         for (A1Client clientMock : clientMocks) {
189             when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
190         }
191     }
192
193     private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
194         when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
195     }
196
197     private void whenGetGetControllerConfigReturn() throws ServiceException {
198         ControllerConfig controllerCfg = ControllerConfig.builder() //
199                 .name("name") //
200                 .baseUrl("baseUrl") //
201                 .password("pass") //
202                 .userName("user") //
203                 .build();
204         when(applicationConfigMock.getControllerConfig(any())).thenReturn(controllerCfg);
205     }
206
207 }