blob: 3b974a55a7e80c1e107892478f1e62790a92f28a [file] [log] [blame]
Zafer Kabanb0a16682018-12-04 11:16:24 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM Core
4 * ================================================================================
5 * Copyright (C) 2018 Netsia
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
21package org.onap.osam.core;
22
23import junitparams.JUnitParamsRunner;
24import junitparams.Parameters;
25import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.mockito.ArgumentCaptor;
29import org.mockito.Captor;
30import org.mockito.InjectMocks;
31import org.mockito.Mock;
32import org.mockito.MockitoAnnotations;
33import org.onap.osam.api.service.AccessPodService;
34import org.onap.osam.api.service.DeviceService;
35import org.onap.osam.common.exception.AbstractOLTException;
36import org.onap.osam.common.exception.InvalidOperationException;
37import org.onap.osam.common.exception.NotFoundException;
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +020038import org.onap.osam.common.exception.ServerException;
Zafer Kabanb0a16682018-12-04 11:16:24 +030039import org.onap.osam.external.grpc.AbstractOLTClient;
40import org.onap.osam.model.dao.*;
41import org.onap.osam.model.repository.ChassisRepository;
42import org.onap.osam.model.repository.OLTPortRepository;
43import org.onap.osam.model.repository.OLTSlotRepository;
44import org.onap.osam.model.repository.ONTDeviceRepository;
45
46import java.util.ArrayList;
47import java.util.HashSet;
48import java.util.List;
49import java.util.Optional;
50import java.util.Set;
51
52import static org.assertj.core.api.Assertions.assertThat;
53import static org.assertj.core.api.Assertions.assertThatThrownBy;
54import static org.mockito.ArgumentMatchers.any;
55import static org.mockito.Mockito.*;
56
57/**
58 * Created by Zafer Kaban on 26.11.2018.
59 */
60
61@RunWith(JUnitParamsRunner.class)
62public class DeviceServiceImplTest {
63
64 private static String TEST_PNF_ID = "TEST_PNF_ID";
65 private static String TEST_CLLI = "TEST_CLLI";
66 private static String TEST_SERIAL = "SERIAL_NUMBER";
67
68 @Mock
69 private ChassisRepository chassisRepository;
70 @Mock
71 private OLTPortRepository oltPortRepository;
72 @Mock
73 private OLTSlotRepository oltSlotRepository;
74 @Mock
75 private ONTDeviceRepository ontDeviceRepository;
76
77 @Mock
78 private AbstractOLTClient abstractOLTClient;
79
80 @Mock
81 private AccessPodService accessPodService;
82
83 @Captor
84 private ArgumentCaptor<ONTDevice> ontDeviceCaptor;
85
86 @Captor
87 private ArgumentCaptor<OLTPort> portCaptor;
88
89
90 private Chassis chassis;
91
92 private AccessPod accessPod;
93
94 private OLTSlot oltSlot;
95
96 private OLTPort oltPort;
97
98 private ONTDevice ontDevice;
99
100 @InjectMocks
101 private DeviceServiceImpl deviceService;
102
103 @Before
104 public void initMocks() {
105 MockitoAnnotations.initMocks(this);
106 accessPod = new AccessPod();
107 accessPod.setPnfId(TEST_PNF_ID);
108 chassis = new Chassis();
109 chassis.setClli(TEST_CLLI);
110 chassis.setAccessPod(accessPod);
111 chassis.setId(1L);
112 oltSlot = new OLTSlot();
113 oltSlot.setId(1L);
114 oltSlot.setNumber(1);
115 oltPort = new OLTPort();
116 oltPort.setId(1L);
117 oltPort.setPortNumber(1);
118 ontDevice = new ONTDevice();
119 ontDevice.setId(1L);
120 ontDevice.setOLTPort(oltPort);
121 oltPort.setOltSlot(oltSlot);
122 oltSlot.setChassis(chassis);
123
124 when(chassisRepository.findByClli(TEST_CLLI)).thenReturn(Optional.ofNullable(chassis));
125 when(chassisRepository.findById(1L)).thenReturn(Optional.ofNullable(chassis));
126 }
127
128 @Test
129 public void whenAddChassis_sunnyFlow(){
130
131 // TEST Sunshine scenario
132 when(accessPodService.findByPnfId(TEST_PNF_ID)).thenReturn(accessPod);
133 when(chassisRepository.save(chassis)).thenReturn(chassis);
134
135 Chassis chassisResult = deviceService.addChassis(chassis);
136 assertThat(chassisResult).isSameAs(chassis);
137 }
138
139
140 @Test
141 public void whenAddChassisPnfNotFound_shouldThrowException() {
142 // TEST when PNF registration does not exist so that Access POD does not exist in OSAM DB
143
144 when(accessPodService.findByPnfId(TEST_PNF_ID)).thenThrow(NotFoundException.class);
145 assertThatThrownBy(() -> deviceService.addChassis(chassis)).isInstanceOf(NotFoundException.class);
146 //verify we save nothing to DB
147 verifyZeroInteractions(chassisRepository);
148 }
149
150 @Test
151 public void whenAddChassisAbstractOltReturnsNull_shouldThrowException() {
152 // TEST grpc failure case
153
154 when(accessPodService.findByPnfId(TEST_PNF_ID)).thenReturn(accessPod);
155 doThrow(AbstractOLTException.class).when(abstractOLTClient).createChassis(chassis);
156 assertThatThrownBy(() -> deviceService.addChassis(chassis)).isInstanceOf(AbstractOLTException.class);
157 //verify we save nothing to DB
158 verifyZeroInteractions(chassisRepository);
159 }
160
161
162 @Test
163 public void whenDeleteChassisById_sunnyFlow() {
164 deviceService.deleteChassis(1L);
165 verify(chassisRepository, times(1)).deleteById(1L);
166 }
167
168 @Test
169 public void whenDeleteChassisByClli_sunnyFlow () {
170 deviceService.deleteChassisByClli(TEST_CLLI);
171 //Test chassis has clli TEST_CLLI and id 1L, thus the verify
172 verify(chassisRepository, times(1)).deleteById(1L);
173 }
174
175
176
177 @Test
178 public void whenGetChassisById_sunnyFlow(){
179 Chassis testChassis = deviceService.getChassisById(1L);
180 assertThat(testChassis).isSameAs(chassis);
181 }
182
183 @Test
184 public void whenGetChassisByNotExistingId_throwNotFoundException(){
185 assertThatThrownBy(() -> deviceService.getChassisById(100L)).isInstanceOf(NotFoundException.class);
186 }
187
188 @Test
189 public void whenGetChassisByTestClli_sunnyFlow(){
190 Chassis testChassis = deviceService.getChassisByClli(TEST_CLLI);
191 assertThat(testChassis).isSameAs(chassis);
192 }
193
194 @Test
195 public void whenGetChassisByNotExistingClli_throwNotFoundException(){
196 assertThatThrownBy(() -> deviceService.getChassisByClli("SOME_FAKE_CLLI")).isInstanceOf(NotFoundException.class);
197 }
198
199 @Test
200 public void whenCountChassis_sunnyFlow(){
201 when(chassisRepository.count()).thenReturn(1L);
202 long count = deviceService.getChassisCount();
203 assertThat(count).isEqualTo(1L);
204 }
205
206 @Test
207 public void whenGetByPnfId_sunnyFlow(){
208 when(chassisRepository.findByAccessPodPnfId(TEST_PNF_ID)).thenReturn(Optional.of(new ArrayList<Chassis>()));
209 ArrayList<Chassis> chassisResult = (ArrayList<Chassis>) deviceService.getByPnfId(TEST_PNF_ID);
210 assertThat(chassisResult).isNotNull();
211 }
212
213
214 @Test
215 public void whenGetByPnfIdNotExisting_shouldThrowException(){
216 assertThatThrownBy(() -> deviceService.getByPnfId("SOME_FAKE_PNFID")).isInstanceOf(NotFoundException.class);
217 }
218
219
220 @Test
221 public void whenGetAllChassis_sunnyFlow() {
222 when(chassisRepository.findAll()).thenReturn(new ArrayList<Chassis>());
223 ArrayList<Chassis> chassisResult = (ArrayList<Chassis>) deviceService.getAllChassis();
224 assertThat(chassisResult).isNotNull();
225 }
226
227
228 @Test
229 public void whenAddOLTSlot_sunnyFlow() {
230
231 Set<OLTSlot> oltSlots = new HashSet<OLTSlot>();
232 chassis.setOltSlots(oltSlots);
233 when(oltSlotRepository.save(oltSlot)).thenReturn(oltSlot);
234 when(abstractOLTClient.createOLTChassis(oltSlot)).thenReturn(TEST_CLLI);
235
236 OLTSlot oltResult = deviceService.addOLTSlot(oltSlot, chassis);
237
238 //verify creation of 16 ports
239 verify(oltPortRepository, times(16)).save(portCaptor.capture());
240 final List<OLTPort> allOltPortsValues = portCaptor.getAllValues();
241 allOltPortsValues.forEach(x -> {
242 assertThat(x.getOltSlot()).isSameAs(oltSlot);
243 assertThat(x.getAdminState()).isEqualTo(AdminState.ENABLED);
244 assertThat(x.getPortAuthState()).isEqualTo(ActivityState.ACTIVE);
245 });
246
247 //verify added to chassis
248 assertThat(chassis.getOltSlots()).hasSize(1);
249
250 //verify oltSlot logic
251 assertThat(oltResult).isSameAs(oltSlot);
252 assertThat(oltResult.getAdminState()).isEqualTo(AdminState.ENABLED);
253 assertThat(oltResult.getOperationalState()).isEqualTo(ActivityState.ACTIVE);
254 assertThat(oltResult.getPortAuthState()).isEqualTo(ActivityState.ACTIVE);
255
256 }
257
258 public void whenAddOLTSlotTooManySlotsOnChassis_shouldThrowException() {
259 //already add 16 slots, cannot add another one
260 Set<OLTSlot> oltSlots = new HashSet<OLTSlot>();
261 for (int i = 0; i < 16; i++) {
262 oltSlots.add(new OLTSlot());
263 }
264 chassis.setOltSlots(oltSlots);
265 assertThatThrownBy(()-> deviceService.addOLTSlot(oltSlot, chassis)).isInstanceOf(InvalidOperationException.class);
266 //verify no DB interactions
267 verifyZeroInteractions(oltSlotRepository, oltPortRepository, chassisRepository);
268 }
269
270 public void whenAddOLTSlotAbstractOLTReturnsNull_shouldThrowException() {
271 when(abstractOLTClient.createOLTChassis(oltSlot)).thenReturn(null);
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +0200272 assertThatThrownBy(()-> deviceService.addOLTSlot(oltSlot, chassis)).isInstanceOf(ServerException.class);
Zafer Kabanb0a16682018-12-04 11:16:24 +0300273 //verify no DB interactions
274 verifyZeroInteractions(oltSlotRepository, oltPortRepository, chassisRepository);
275 }
276
277 @Test
278 public void whenDeleteOLTSlot_repositoryMethodCalledOnce() {
279 deviceService.deleteOLTSlot(1L);
280 verify(oltSlotRepository, times(1)).deleteById(1L);
281 }
282
283
284 @Test
285 public void whenGetOLTSlotById_sunnyFlow(){
286 when(oltSlotRepository.findById(1L)).thenReturn(Optional.of(oltSlot));
287 OLTSlot oltActualSlot = deviceService.getOLTSlotById(1L);
288 assertThat(oltActualSlot).isSameAs(oltSlot);
289 }
290
291 @Test
292 public void whenGetOLTSlotByIdNotExisting_shouldThrowException(){
293 assertThatThrownBy(()-> deviceService.getOLTSlotById(100L)).isInstanceOf(NotFoundException.class);
294 }
295
296 @Test
297 public void whenGetOLTSlotBySerialId_sunnyFlow() {
298 when(oltSlotRepository.findBySerialNumber(TEST_SERIAL)).thenReturn(Optional.of(oltSlot));
299 OLTSlot oltActualSlot = deviceService.getOLTSlotBySerialNumber(TEST_SERIAL);
300 assertThat(oltActualSlot).isSameAs(oltSlot);
301 }
302
303 @Test
304 public void whenGetOLTSlotBySerialIdNotExisting_shouldThrowException(){
305 assertThatThrownBy(()-> deviceService.getOLTSlotBySerialNumber("SOME_FAKE_SERIAL")).isInstanceOf(NotFoundException.class);
306 }
307
308 @Test
309 public void whenGetAllOLTSlot_sunnyFlow() {
310 final ArrayList<OLTSlot> slotArrayList = new ArrayList<>();
311 when(oltSlotRepository.findAll()).thenReturn(slotArrayList);
312 ArrayList<OLTSlot> oltSlots = (ArrayList<OLTSlot>) deviceService.getAllOLTSlots();
313 assertThat(oltSlots).isEqualTo(slotArrayList);
314 }
315
316 @Test
317 public void whenDeleteOLTPort_sunnyFlow() {
318 deviceService.deleteOLTPort(1L);
319 verify(oltPortRepository, times(1)).deleteById(1L);
320 }
321
322 @Test
323 public void whenGetOLTPort_sunnyFlow() {
324 when(oltPortRepository.findById(1L)).thenReturn(Optional.of(oltPort));
325 final OLTPort actualPort = deviceService.getOLTPortById(1L);
326 assertThat(actualPort).isSameAs(oltPort);
327 }
328
329 @Test
330 public void whenGetOLTPortByIdNotExisting_shouldThrowException(){
331 assertThatThrownBy(()-> deviceService.getOLTPortById(100L)).isInstanceOf(NotFoundException.class);
332 }
333
334 @Test
335 @Parameters(method = "provTypeToTestForOnt")
336 public void whenProvisionONTDevice_sunnyFlow(DeviceService.OntProvisioningType provType) {
337
338
339 //setting up ontDevice
340 ontDevice.setSerialNumber("SOME_SERIAL_NUMBER");
341 ontDevice.setNumber(23);
342 ontDevice.setCTag(111);
343 ontDevice.setSTag(222);
344 ontDevice.setCircuitId("CIRCUIT_ID");
345 ontDevice.setNasPortId("NAS_PORT_ID");
346 ontDevice.setAdminState(AdminState.ENABLED);
347 ontDevice.setOperationalState(ActivityState.ACTIVE);
348 ontDevice.setPortAuthState(ActivityState.ACTIVE);
349
350 when(oltPortRepository.findByPortNumberAndOltSlot_NumberAndOltSlot_ChassisClli(1,1,TEST_CLLI)).thenReturn(Optional.ofNullable(oltPort));
351 when(abstractOLTClient.provisionONT(ontDevice)).thenReturn(true);
352 when(abstractOLTClient.preProvisionOnt(ontDevice)).thenReturn(true);
353 when(abstractOLTClient.provisionOntFull(ontDevice)).thenReturn(true);
354
355 //This is because in order to be inserted to hashset of oltSlots at the next command of tested function,
356 //the ONTDevice has to have ID
357 when(ontDeviceRepository.save(any())).thenReturn(ontDevice);
358
359 deviceService.provisionONTDevice(ontDevice, provType);
360
361 //checking that the copy to persisted object was as expected
362 verify(ontDeviceRepository, times(1)).save(ontDeviceCaptor.capture());
363 final ONTDevice capturedONTDevice = ontDeviceCaptor.getValue();
364
365 //TODO Pavel fix after discussion with Netsia regarding the returned object.
366 //Currently the assert will fail because internal object has no ID.
367 //I didn't want to use isEqualUsingSpecificFields, to catch set operations we might miss in the future
368 //assertThat(capturedONTDevice).isEqualToComparingFieldByField(ontDevice);
369
370 verify(oltPortRepository, times(1)).save(oltPort);
371 }
372
373 private Object[] provTypeToTestForOnt() {
374 return new Object[] {
375 new Object[] { DeviceService.OntProvisioningType.PROVISION },
376 new Object[] { DeviceService.OntProvisioningType.PREPROVISION },
377 new Object[] { DeviceService.OntProvisioningType.FULL }
378 };
379 }
380
381 @Test
382 @Parameters(method = "provTypeToTestForOnt")
383 public void whenAddONTDeviceNoPortFound_shouldThrowException(DeviceService.OntProvisioningType provType) {
384 when(oltPortRepository.findByPortNumberAndOltSlot_NumberAndOltSlot_ChassisClli(1,1,TEST_CLLI)).thenReturn(Optional.ofNullable(null));
385 assertThatThrownBy(()-> deviceService.provisionONTDevice(ontDevice, provType)).isInstanceOf(NotFoundException.class);
386
387 }
388
389 @Test
390 @Parameters(method = "provTypeToTestForOnt")
391 public void whenAddONTDeviceAbstractOLTError_shouldThrowException(DeviceService.OntProvisioningType provType) {
392 when(oltPortRepository.findByPortNumberAndOltSlot_NumberAndOltSlot_ChassisClli(1,1,TEST_CLLI)).thenReturn(Optional.ofNullable(oltPort));
393 when(abstractOLTClient.provisionONT(ontDevice)).thenReturn(false);
394 when(abstractOLTClient.preProvisionOnt(ontDevice)).thenReturn(false);
395 when(abstractOLTClient.provisionOntFull(ontDevice)).thenReturn(false);
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +0200396 assertThatThrownBy(()-> deviceService.provisionONTDevice(ontDevice, provType)).isInstanceOf(ServerException.class);
Zafer Kabanb0a16682018-12-04 11:16:24 +0300397
398 }
399
400 @Test
401 public void whenDeleteONTDevice_sunnyFlow() {
402 deviceService.deleteONTDevice(1L);
403 verify(ontDeviceRepository, times(1)).deleteById(1L);
404 }
405
406 @Test
407 public void whenGetONTDeviceById_sunnyFlow() {
408 when(ontDeviceRepository.findById(1L)).thenReturn(Optional.of(ontDevice));
409 ONTDevice actualOntDevice = deviceService.getONTDeviceById(1L);
410 assertThat(actualOntDevice).isSameAs(ontDevice);
411 }
412
413 @Test
414 public void whenGetONTDeviceNoId_shouldThrowException() {
415 assertThatThrownBy(()-> deviceService.getONTDeviceById(100L)).isInstanceOf(NotFoundException.class);
416
417 }
418
419 @Test
420 public void whenGetONTDeviceBySerialNumber_sunnyFlow() {
421 when(ontDeviceRepository.findBySerialNumber(TEST_SERIAL)).thenReturn(Optional.of(ontDevice));
422 ONTDevice actualOntDevice = deviceService.getONTDeviceBySerialNumber(TEST_SERIAL);
423 assertThat(actualOntDevice).isSameAs(ontDevice);
424 }
425
426 @Test
427 public void whenGetONTDeviceNoSerialNumber_shouldThrowException() {
428 assertThatThrownBy(()-> deviceService.getONTDeviceBySerialNumber(TEST_SERIAL)).isInstanceOf(NotFoundException.class);
429
430 }
431
432 @Test
433 public void whenGetAllONTDevices_sunnyFlow() {
434 final ArrayList<ONTDevice> devices = new ArrayList<>();
435 devices.add(ontDevice);
436 when(ontDeviceRepository.findAll()).thenReturn(devices);
437 ArrayList<ONTDevice> ontDevices = (ArrayList<ONTDevice>) deviceService.getAllONTDevices();
438 assertThat(ontDevices).isEqualTo(devices);
439 }
440}