645f235334a024ae60bd13243769cd3cd900b91a
[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.BeforeEach;
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.ImmutableControllerConfig;
41 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
42 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
43 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
44
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
47
48 @ExtendWith(MockitoExtension.class)
49 class A1ClientFactoryTest {
50     private static final String RIC_NAME = "Name";
51     private static final String EXCEPTION_MESSAGE = "Error";
52
53     @Mock
54     private ApplicationConfig applicationConfigMock;
55
56     @Mock
57     A1Client clientMock1;
58
59     @Mock
60     A1Client clientMock2;
61
62     @Mock
63     A1Client clientMock3;
64
65     @Mock
66     A1Client clientMock4;
67
68     private Ric ric;
69     private A1ClientFactory factoryUnderTest;
70
71     private static ImmutableRicConfig ricConfig(String controllerName) {
72         return ImmutableRicConfig.builder() //
73                 .ricId(RIC_NAME) //
74                 .baseUrl("baseUrl") //
75                 .managedElementIds(new Vector<>()) //
76                 .controllerName(controllerName) //
77                 .build();
78     }
79
80     @BeforeEach
81     void createFactoryUnderTest() {
82         SecurityContext sec = new SecurityContext("");
83         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock, sec));
84         this.ric = new Ric(ricConfig(""));
85
86     }
87
88     @Test
89     void getProtocolVersion_ok() throws ServiceException {
90         whenGetProtocolVersionThrowException(clientMock1);
91         whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
92         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
93
94         A1Client client = factoryUnderTest.createA1Client(ric).block();
95
96         assertEquals(clientMock2, client, "Not correct client returned");
97         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
98     }
99
100     @Test
101     void getProtocolVersion_ok_Last() throws ServiceException {
102         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
103         whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
104         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
105
106         A1Client client = factoryUnderTest.createA1Client(ric).block();
107
108         assertEquals(clientMock4, client, "Not correct client returned");
109         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
110     }
111
112     @Test
113     void getProtocolVersion_error() throws ServiceException {
114         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
115         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
116
117         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
118                 .expectSubscription() //
119                 .expectError() //
120                 .verify();
121
122         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.id());
123     }
124
125     private A1Client createClient(A1ProtocolType version) throws ServiceException {
126         return factoryUnderTest.createClient(ric, version);
127     }
128
129     @Test
130     void create_check_types() throws ServiceException {
131         assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
132         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
133     }
134
135     @Test
136     void create_check_types_controllers() throws ServiceException {
137         this.ric = new Ric(ricConfig("anythingButEmpty"));
138         whenGetGetControllerConfigReturn();
139
140         whenGetGetControllerConfigReturn();
141         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) instanceof CcsdkA1AdapterClient);
142
143         whenGetGetControllerConfigReturn();
144         assertTrue(createClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) instanceof CcsdkA1AdapterClient);
145     }
146
147     private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
148         for (A1Client clientMock : clientMocks) {
149             when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
150         }
151     }
152
153     private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
154         when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
155     }
156
157     private void whenGetGetControllerConfigReturn() throws ServiceException {
158         ControllerConfig controllerCfg = ImmutableControllerConfig.builder() //
159                 .name("name") //
160                 .baseUrl("baseUrl") //
161                 .password("pass") //
162                 .userName("user") //
163                 .build();
164         when(applicationConfigMock.getControllerConfig(any())).thenReturn(controllerCfg);
165     }
166
167 }