Add new UT for deviceListeners 47/95547/15
authorGilles Thouenon <gilles.thouenon@orange.com>
Mon, 22 Mar 2021 13:27:00 +0000 (14:27 +0100)
committerguillaume.lambert <guillaume.lambert@orange.com>
Tue, 13 Apr 2021 09:11:52 +0000 (11:11 +0200)
- UT for each version of deviceListeners

Signed-off-by: Gilles Thouenon <gilles.thouenon@orange.com>
Change-Id: Iff63e9e2634abc0e2ce174be132efac3004272f7

networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121.java
networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221.java
networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710.java
networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121Test.java [new file with mode: 0644]
networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221Test.java [new file with mode: 0644]
networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710Test.java [new file with mode: 0644]

index d8aa5ed64678754bde11a1f55e8b5de501774aa0..d577ef659b5eb6f7077996766c28bcb79146b000 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.transportpce.networkmodel.listeners;
 
-import java.util.Collection;
 import java.util.LinkedList;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
@@ -30,6 +29,7 @@ public class DeviceListener121 implements OrgOpenroadmDeviceListener {
     private final PortMapping portMapping;
 
     public DeviceListener121(String nodeId, PortMapping portMapping) {
+        super();
         this.nodeId = nodeId;
         this.portMapping = portMapping;
     }
@@ -40,6 +40,7 @@ public class DeviceListener121 implements OrgOpenroadmDeviceListener {
      * @param notification ChangeNotification object
      */
     @Override
+    @SuppressWarnings("unchecked")
     public void onChangeNotification(ChangeNotification notification) {
         if (notification.getEdit() == null) {
             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
@@ -50,7 +51,7 @@ public class DeviceListener121 implements OrgOpenroadmDeviceListener {
             switch (edit.getTarget().getTargetType().getSimpleName()) {
                 case "Ports":
                     LinkedList<PathArgument> path = new LinkedList<>();
-                    path.addAll((Collection<? extends PathArgument>) edit.getTarget().getPathArguments());
+                    edit.getTarget().getPathArguments().forEach(p -> path.add(p));
                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
                         .create(path);
                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
index 0b021387124c7f9bdf872bdb59689a374b785562..b7861d0b92647cf6fcb02e045596a03b45eeeae3 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.transportpce.networkmodel.listeners;
 
-import java.util.Collection;
 import java.util.LinkedList;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
@@ -31,6 +30,7 @@ public class DeviceListener221 implements OrgOpenroadmDeviceListener {
     private final PortMapping portMapping;
 
     public DeviceListener221(String nodeId, PortMapping portMapping) {
+        super();
         this.nodeId = nodeId;
         this.portMapping = portMapping;
     }
@@ -42,6 +42,7 @@ public class DeviceListener221 implements OrgOpenroadmDeviceListener {
      *            ChangeNotification object
      */
     @Override
+    @SuppressWarnings("unchecked")
     public void onChangeNotification(ChangeNotification notification) {
         if (notification.getEdit() == null) {
             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
@@ -52,7 +53,7 @@ public class DeviceListener221 implements OrgOpenroadmDeviceListener {
             switch (edit.getTarget().getTargetType().getSimpleName()) {
                 case "Ports":
                     LinkedList<PathArgument> path = new LinkedList<>();
-                    path.addAll((Collection<? extends PathArgument>) edit.getTarget().getPathArguments());
+                    edit.getTarget().getPathArguments().forEach(p -> path.add(p));
                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
                         .create(path);
                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
index fb3dbbb352b67d4f2b94ea98887717324897fec9..138bd27e3affa99bbe4e9cba7086d558c815d9d5 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.transportpce.networkmodel.listeners;
 
-import java.util.Collection;
 import java.util.LinkedList;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
@@ -31,6 +30,7 @@ public class DeviceListener710 implements OrgOpenroadmDeviceListener {
     private final PortMapping portMapping;
 
     public DeviceListener710(String nodeId, PortMapping portMapping) {
+        super();
         this.nodeId = nodeId;
         this.portMapping = portMapping;
     }
@@ -42,6 +42,7 @@ public class DeviceListener710 implements OrgOpenroadmDeviceListener {
      *            ChangeNotification object
      */
     @Override
+    @SuppressWarnings("unchecked")
     public void onChangeNotification(ChangeNotification notification) {
         if (notification.getEdit() == null) {
             LOG.warn("unable to handle {} notificatin received - list of edit is null", ChangeNotification.QNAME);
@@ -52,7 +53,7 @@ public class DeviceListener710 implements OrgOpenroadmDeviceListener {
             switch (edit.getTarget().getTargetType().getSimpleName()) {
                 case "Ports":
                     LinkedList<PathArgument> path = new LinkedList<>();
-                    path.addAll((Collection<? extends PathArgument>) edit.getTarget().getPathArguments());
+                    edit.getTarget().getPathArguments().forEach(p -> path.add(p));
                     InstanceIdentifier<Ports> portIID = (InstanceIdentifier<Ports>) InstanceIdentifier
                         .create(path);
                     String portName = InstanceIdentifier.keyOf(portIID).getPortName();
diff --git a/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121Test.java b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121Test.java
new file mode 100644 (file)
index 0000000..e0f88f4
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2021 Orange.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.transportpce.networkmodel.listeners;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.ChangeNotification;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.Edit;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.change.notification.EditBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditOperationType;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class DeviceListener121Test {
+    @Mock
+    private PortMapping portMapping;
+
+    @Test
+    public void testOnChangeNotificationWhenPortUpdated() throws InterruptedException {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        Mapping oldMapping = mock(Mapping.class);
+        ImmutableList<Edit> editList = createEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        when(portMapping.getMapping("node1", "circuit-pack1", "port1")).thenReturn(oldMapping);
+
+        DeviceListener121 listener = new DeviceListener121("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
+        Thread.sleep(3000);
+        verify(portMapping, times(1)).updateMapping("node1", oldMapping);
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenNoEditList() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        when(notification.getEdit()).thenReturn(null);
+        DeviceListener121 listener = new DeviceListener121("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenOtherthingUpdated() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        ImmutableList<Edit> editList = createBadEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        DeviceListener121 listener = new DeviceListener121("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    private ImmutableList<Edit> createEditList() {
+        InstanceIdentifier<Ports> portId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
+            .child(Ports.class, new PortsKey("port1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(portId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+
+    private ImmutableList<Edit> createBadEditList() {
+        InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(cpId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+}
diff --git a/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221Test.java b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221Test.java
new file mode 100644 (file)
index 0000000..42a5c39
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2021 Orange.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.transportpce.networkmodel.listeners;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.ChangeNotification;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.change.notification.Edit;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.change.notification.EditBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditOperationType;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class DeviceListener221Test {
+    @Mock
+    private PortMapping portMapping;
+
+    @Test
+    public void testOnChangeNotificationWhenPortUpdated() throws InterruptedException {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        Mapping oldMapping = mock(Mapping.class);
+        ImmutableList<Edit> editList = createEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        when(portMapping.getMapping("node1", "circuit-pack1", "port1")).thenReturn(oldMapping);
+
+        DeviceListener221 listener = new DeviceListener221("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
+        Thread.sleep(3000);
+        verify(portMapping, times(1)).updateMapping("node1", oldMapping);
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenNoEditList() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        when(notification.getEdit()).thenReturn(null);
+        DeviceListener221 listener = new DeviceListener221("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenOtherthingUpdated() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        ImmutableList<Edit> editList = createBadEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        DeviceListener221 listener = new DeviceListener221("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    private ImmutableList<Edit> createEditList() {
+        InstanceIdentifier<Ports> portId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
+            .child(Ports.class, new PortsKey("port1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(portId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+
+    private ImmutableList<Edit> createBadEditList() {
+        InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(cpId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+}
diff --git a/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710Test.java b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710Test.java
new file mode 100644 (file)
index 0000000..efb3dc7
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2021 Orange.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.transportpce.networkmodel.listeners;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.ChangeNotification;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.Edit;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.change.notification.EditBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditOperationType;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class DeviceListener710Test {
+    @Mock
+    private PortMapping portMapping;
+
+    @Test
+    public void testOnChangeNotificationWhenPortUpdated() throws InterruptedException {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        Mapping oldMapping = mock(Mapping.class);
+        ImmutableList<Edit> editList = createEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        when(portMapping.getMapping("node1", "circuit-pack1", "port1")).thenReturn(oldMapping);
+
+        DeviceListener710 listener = new DeviceListener710("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, times(1)).getMapping("node1", "circuit-pack1", "port1");
+        Thread.sleep(3000);
+        verify(portMapping, times(1)).updateMapping("node1", oldMapping);
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenNoEditList() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        when(notification.getEdit()).thenReturn(null);
+        DeviceListener710 listener = new DeviceListener710("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    @Test
+    public void testOnChangeNotificationWhenOtherthingUpdated() {
+        ChangeNotification notification = mock(ChangeNotification.class);
+        ImmutableList<Edit> editList = createBadEditList();
+        when(notification.getEdit()).thenReturn(editList);
+        DeviceListener710 listener = new DeviceListener710("node1", portMapping);
+        listener.onChangeNotification(notification);
+        verify(portMapping, never()).getMapping(anyString(), anyString(), anyString());
+        verify(portMapping, never()).updateMapping(anyString(), any());
+    }
+
+    private ImmutableList<Edit> createEditList() {
+        InstanceIdentifier<Ports> portId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"))
+            .child(Ports.class, new PortsKey("port1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(portId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+
+    private ImmutableList<Edit> createBadEditList() {
+        InstanceIdentifier<CircuitPacks> cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1"));
+        Edit edit = new EditBuilder()
+            .setOperation(EditOperationType.Merge)
+            .setTarget(cpId)
+            .build();
+        ImmutableList<Edit> editList = ImmutableList.of(edit);
+        return editList;
+    }
+}