Migrate networkmodel module to JUnit5
[transportpce.git] / networkmodel / src / test / java / org / opendaylight / transportpce / networkmodel / listeners / DeviceListener121Test.java
1 /*
2  * Copyright © 2021 Orange.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.networkmodel.listeners;
10
11 import static org.mockito.Mockito.any;
12 import static org.mockito.Mockito.anyString;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.collect.ImmutableList;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.transportpce.common.mapping.PortMapping;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mapping.Mapping;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.ChangeNotification;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceData;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.Edit;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.EditBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditOperationType;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37
38 @ExtendWith(MockitoExtension.class)
39 public class DeviceListener121Test {
40     @Mock
41     private PortMapping portMapping;
42
43     @Test
44     void testOnChangeNotificationWhenPortUpdated() throws InterruptedException {
45         ChangeNotification notification = mock(ChangeNotification.class);
46         Mapping oldMapping = mock(Mapping.class);
47         ImmutableList<Edit> editList = createEditList();
48         when(notification.getEdit()).thenReturn(editList);
49         when(portMapping.getMapping("node1", "circuit-pack1", "port1")).thenReturn(oldMapping);
50
51         DeviceListener121 listener = new DeviceListener121("node1", portMapping);
52         listener.onChangeNotification(notification);
53         verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
54         Thread.sleep(3000);
55         verify(portMapping, times(1)).updateMapping("node1", oldMapping);
56     }
57
58     @Test
59     void testOnChangeNotificationWhenNoEditList() {
60         ChangeNotification notification = mock(ChangeNotification.class);
61         when(notification.getEdit()).thenReturn(null);
62         DeviceListener121 listener = new DeviceListener121("node1", portMapping);
63         listener.onChangeNotification(notification);
64         verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
65         verify(portMapping, never()).updateMapping(anyString(), any());
66     }
67
68     @Test
69     void testOnChangeNotificationWhenOtherthingUpdated() {
70         ChangeNotification notification = mock(ChangeNotification.class);
71         ImmutableList<Edit> editList = createBadEditList();
72         when(notification.getEdit()).thenReturn(editList);
73         DeviceListener121 listener = new DeviceListener121("node1", portMapping);
74         listener.onChangeNotification(notification);
75         verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
76         verify(portMapping, never()).updateMapping(anyString(), any());
77     }
78
79     private ImmutableList<Edit> createEditList() {
80         InstanceIdentifier<Ports> portId = InstanceIdentifier
81             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
82             .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
83             .child(Ports.class, new PortsKey("port1"))
84             .build();
85         Edit edit = new EditBuilder()
86             .setOperation(EditOperationType.Merge)
87             .setTarget(portId)
88             .build();
89         ImmutableList<Edit> editList = ImmutableList.of(edit);
90         return editList;
91     }
92
93     private ImmutableList<Edit> createBadEditList() {
94         InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier
95             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
96             .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
97             .build();
98         Edit edit = new EditBuilder()
99             .setOperation(EditOperationType.Merge)
100             .setTarget(cpId)
101             .build();
102         ImmutableList<Edit> editList = ImmutableList.of(edit);
103         return editList;
104     }
105 }