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.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.rev170206.ChangeNotification;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.Edit;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.EditBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.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;
37 @RunWith(MockitoJUnitRunner.StrictStubs.class)
38 public class DeviceListener121Test {
40 private PortMapping portMapping;
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);
50 DeviceListener121 listener = new DeviceListener121("node1", portMapping);
51 listener.onChangeNotification(notification);
52 verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
54 verify(portMapping, times(1)).updateMapping("node1", oldMapping);
58 public void testOnChangeNotificationWhenNoEditList() {
59 ChangeNotification notification = mock(ChangeNotification.class);
60 when(notification.getEdit()).thenReturn(null);
61 DeviceListener121 listener = new DeviceListener121("node1", portMapping);
62 listener.onChangeNotification(notification);
63 verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
64 verify(portMapping, never()).updateMapping(anyString(), any());
68 public void testOnChangeNotificationWhenOtherthingUpdated() {
69 ChangeNotification notification = mock(ChangeNotification.class);
70 ImmutableList<Edit> editList = createBadEditList();
71 when(notification.getEdit()).thenReturn(editList);
72 DeviceListener121 listener = new DeviceListener121("node1", portMapping);
73 listener.onChangeNotification(notification);
74 verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
75 verify(portMapping, never()).updateMapping(anyString(), any());
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)
86 ImmutableList<Edit> editList = ImmutableList.of(edit);
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)
97 ImmutableList<Edit> editList = ImmutableList.of(edit);