Refactor SupportedIfCapability usage
[transportpce.git] / networkmodel / src / test / java / org / opendaylight / transportpce / networkmodel / listeners / DeviceListener710Test.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.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
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.rev200529.ChangeNotification;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.Edit;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.EditBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditOperationType;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 @RunWith(MockitoJUnitRunner.StrictStubs.class)
38 public class DeviceListener710Test {
39     @Mock
40     private PortMapping portMapping;
41
42     @Test
43     public void testOnChangeNotificationWhenPortUpdated() throws InterruptedException {
44         ChangeNotification notification = mock(ChangeNotification.class);
45         Mapping oldMapping = mock(Mapping.class);
46         ImmutableList<Edit> editList = createEditList();
47         when(notification.getEdit()).thenReturn(editList);
48         when(portMapping.getMapping("node1", "circuit-pack1", "port1")).thenReturn(oldMapping);
49
50         DeviceListener710 listener = new DeviceListener710("node1", portMapping);
51         listener.onChangeNotification(notification);
52         verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
53         Thread.sleep(3000);
54         verify(portMapping, times(1)).updateMapping("node1", oldMapping);
55     }
56
57     @Test
58     public void testOnChangeNotificationWhenNoEditList() {
59         ChangeNotification notification = mock(ChangeNotification.class);
60         when(notification.getEdit()).thenReturn(null);
61         DeviceListener710 listener = new DeviceListener710("node1", portMapping);
62         listener.onChangeNotification(notification);
63         verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
64         verify(portMapping, never()).updateMapping(anyString(), any());
65     }
66
67     @Test
68     public void testOnChangeNotificationWhenOtherthingUpdated() {
69         ChangeNotification notification = mock(ChangeNotification.class);
70         ImmutableList<Edit> editList = createBadEditList();
71         when(notification.getEdit()).thenReturn(editList);
72         DeviceListener710 listener = new DeviceListener710("node1", portMapping);
73         listener.onChangeNotification(notification);
74         verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
75         verify(portMapping, never()).updateMapping(anyString(), any());
76     }
77
78     private ImmutableList<Edit> createEditList() {
79         InstanceIdentifier<Ports> portId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
80             .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
81             .child(Ports.class, new PortsKey("port1"));
82         Edit edit = new EditBuilder()
83             .setOperation(EditOperationType.Merge)
84             .setTarget(portId)
85             .build();
86         ImmutableList<Edit> editList = ImmutableList.of(edit);
87         return editList;
88     }
89
90     private ImmutableList<Edit> createBadEditList() {
91         InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
92             .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"));
93         Edit edit = new EditBuilder()
94             .setOperation(EditOperationType.Merge)
95             .setTarget(cpId)
96             .build();
97         ImmutableList<Edit> editList = ImmutableList.of(edit);
98         return editList;
99     }
100 }