From 35757b1f7d26b06572ee43bdd37277970c55a2c7 Mon Sep 17 00:00:00 2001 From: Gilles Thouenon Date: Mon, 22 Mar 2021 14:27:00 +0100 Subject: [PATCH] Add new UT for deviceListeners - UT for each version of deviceListeners Signed-off-by: Gilles Thouenon Change-Id: Iff63e9e2634abc0e2ce174be132efac3004272f7 --- .../listeners/DeviceListener121.java | 5 +- .../listeners/DeviceListener221.java | 5 +- .../listeners/DeviceListener710.java | 5 +- .../listeners/DeviceListener121Test.java | 100 ++++++++++++++++++ .../listeners/DeviceListener221Test.java | 100 ++++++++++++++++++ .../listeners/DeviceListener710Test.java | 100 ++++++++++++++++++ 6 files changed, 309 insertions(+), 6 deletions(-) create mode 100644 networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121Test.java create mode 100644 networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221Test.java create mode 100644 networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710Test.java diff --git a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121.java b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121.java index d8aa5ed64..d577ef659 100644 --- a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121.java +++ b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121.java @@ -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 path = new LinkedList<>(); - path.addAll((Collection) edit.getTarget().getPathArguments()); + edit.getTarget().getPathArguments().forEach(p -> path.add(p)); InstanceIdentifier portIID = (InstanceIdentifier) InstanceIdentifier .create(path); String portName = InstanceIdentifier.keyOf(portIID).getPortName(); diff --git a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221.java b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221.java index 0b0213871..b7861d0b9 100644 --- a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221.java +++ b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221.java @@ -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 path = new LinkedList<>(); - path.addAll((Collection) edit.getTarget().getPathArguments()); + edit.getTarget().getPathArguments().forEach(p -> path.add(p)); InstanceIdentifier portIID = (InstanceIdentifier) InstanceIdentifier .create(path); String portName = InstanceIdentifier.keyOf(portIID).getPortName(); diff --git a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710.java b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710.java index fb3dbbb35..138bd27e3 100644 --- a/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710.java +++ b/networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710.java @@ -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 path = new LinkedList<>(); - path.addAll((Collection) edit.getTarget().getPathArguments()); + edit.getTarget().getPathArguments().forEach(p -> path.add(p)); InstanceIdentifier portIID = (InstanceIdentifier) 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 index 000000000..e0f88f4f9 --- /dev/null +++ b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener121Test.java @@ -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 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 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 createEditList() { + InstanceIdentifier 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 editList = ImmutableList.of(edit); + return editList; + } + + private ImmutableList createBadEditList() { + InstanceIdentifier cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class) + .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1")); + Edit edit = new EditBuilder() + .setOperation(EditOperationType.Merge) + .setTarget(cpId) + .build(); + ImmutableList 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 index 000000000..42a5c3968 --- /dev/null +++ b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener221Test.java @@ -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 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 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 createEditList() { + InstanceIdentifier 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 editList = ImmutableList.of(edit); + return editList; + } + + private ImmutableList createBadEditList() { + InstanceIdentifier cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class) + .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1")); + Edit edit = new EditBuilder() + .setOperation(EditOperationType.Merge) + .setTarget(cpId) + .build(); + ImmutableList 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 index 000000000..efb3dc7b3 --- /dev/null +++ b/networkmodel/src/test/java/org/opendaylight/transportpce/networkmodel/listeners/DeviceListener710Test.java @@ -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 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 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 createEditList() { + InstanceIdentifier 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 editList = ImmutableList.of(edit); + return editList; + } + + private ImmutableList createBadEditList() { + InstanceIdentifier cpId = InstanceIdentifier.create(OrgOpenroadmDevice.class) + .child(CircuitPacks.class, new CircuitPacksKey("circuit-pack1")); + Edit edit = new EditBuilder() + .setOperation(EditOperationType.Merge) + .setTarget(cpId) + .build(); + ImmutableList editList = ImmutableList.of(edit); + return editList; + } +} -- 2.36.6