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