Merge "Fix bug in A1 clients"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / A1ClientFactoryTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.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.oransc.policyagent.clients.A1Client.A1ProtocolType;
38 import org.oransc.policyagent.configuration.ApplicationConfig;
39 import org.oransc.policyagent.configuration.ControllerConfig;
40 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
41 import org.oransc.policyagent.configuration.ImmutableRicConfig;
42 import org.oransc.policyagent.exceptions.ServiceException;
43 import org.oransc.policyagent.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             .name(RIC_NAME) //
74             .baseUrl("baseUrl") //
75             .managedElementIds(new Vector<>()) //
76             .controllerName(controllerName) //
77             .build();
78     }
79
80     @BeforeEach
81     void createFactoryUnderTest() {
82         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
83         this.ric = new Ric(ricConfig(""));
84
85     }
86
87     @Test
88     void getProtocolVersion_ok() throws ServiceException {
89         whenGetProtocolVersionThrowException(clientMock1);
90         whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
91         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
92
93         A1Client client = factoryUnderTest.createA1Client(ric).block();
94
95         assertEquals(clientMock2, client, "Not correct client returned");
96         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
97     }
98
99     @Test
100     void getProtocolVersion_ok_Last() throws ServiceException {
101         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
102         whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
103         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
104
105         A1Client client = factoryUnderTest.createA1Client(ric).block();
106
107         assertEquals(clientMock4, client, "Not correct client returned");
108         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
109     }
110
111     @Test
112     void getProtocolVersion_error() throws ServiceException {
113         whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
114         doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
115
116         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
117             .expectSubscription() //
118             .expectError() //
119             .verify();
120
121         assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.name());
122     }
123
124     private A1Client createClient(A1ProtocolType version) throws ServiceException {
125         return factoryUnderTest.createClient(ric, version);
126     }
127
128     @Test
129     void create_check_types() throws ServiceException {
130         assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
131         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
132     }
133
134     @Test
135     void create_check_types_controllers() throws ServiceException {
136         this.ric = new Ric(ricConfig("anythingButEmpty"));
137         whenGetGetControllerConfigReturn();
138         assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client);
139
140         whenGetGetControllerConfigReturn();
141         assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client);
142
143         whenGetGetControllerConfigReturn();
144         assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client);
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 }