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