2 * Copyright © 2021 Orange. All rights reserved.
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
9 package org.opendaylight.transportpce.networkmodel.listeners;
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;
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.rev220922.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.OrgOpenroadmDeviceData;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.Edit;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.EditBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.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;
38 @ExtendWith(MockitoExtension.class)
39 public class DeviceListener710Test {
41 private PortMapping portMapping;
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);
51 DeviceListener710 listener = new DeviceListener710("node1", portMapping);
52 listener.onChangeNotification(notification);
53 verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
55 verify(portMapping, times(1)).updateMapping("node1", oldMapping);
59 void testOnChangeNotificationWhenNoEditList() {
60 ChangeNotification notification = mock(ChangeNotification.class);
61 when(notification.getEdit()).thenReturn(null);
62 DeviceListener710 listener = new DeviceListener710("node1", portMapping);
63 listener.onChangeNotification(notification);
64 verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
65 verify(portMapping, never()).updateMapping(anyString(), any());
69 void testOnChangeNotificationWhenOtherthingUpdated() {
70 ChangeNotification notification = mock(ChangeNotification.class);
71 ImmutableList<Edit> editList = createBadEditList();
72 when(notification.getEdit()).thenReturn(editList);
73 DeviceListener710 listener = new DeviceListener710("node1", portMapping);
74 listener.onChangeNotification(notification);
75 verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
76 verify(portMapping, never()).updateMapping(anyString(), any());
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"))
85 Edit edit = new EditBuilder()
86 .setOperation(EditOperationType.Merge)
89 ImmutableList<Edit> editList = ImmutableList.of(edit);
93 private ImmutableList<Edit> createBadEditList() {
94 InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier
95 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
96 .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
98 Edit edit = new EditBuilder()
99 .setOperation(EditOperationType.Merge)
102 ImmutableList<Edit> editList = ImmutableList.of(edit);