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