From: Tomer Date: Tue, 2 Aug 2016 11:46:29 +0000 (+0300) Subject: Add unit tests to AclEventListener X-Git-Tag: release/boron~161^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=2bf6df960830d69c6f42ad5a9c136ba25d1eb258;p=netvirt.git Add unit tests to AclEventListener Change-Id: I4fda31acd13ffd268f3daee0652c9b4fc3297b60 Signed-off-by: Tomer --- diff --git a/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/listeners/AclEventListenerTest.java b/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/listeners/AclEventListenerTest.java new file mode 100644 index 0000000000..9e86e505f9 --- /dev/null +++ b/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/listeners/AclEventListenerTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others. 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.netvirt.aclservice.listeners; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.clearStaticData; +import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAcl; +import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAclDataUtil; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.opendaylight.netvirt.aclservice.api.AclServiceManager; +import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action; +import org.opendaylight.netvirt.aclservice.api.utils.AclInterface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class AclEventListenerTest { + + private AclEventListener aclEventListener; + private AclServiceManager aclServiceManager; + + private InstanceIdentifier mockInstanceId; + private AclInterface aclInterfaceMock; + + private ArgumentCaptor aclInterfaceValueSaver; + private ArgumentCaptor actionValueSaver; + private ArgumentCaptor aceValueSaver; + private String aclName; + + @SuppressWarnings("unchecked") + @Before + public void setUp() { + + mockInstanceId = mock(InstanceIdentifier.class); + aclInterfaceMock = mock(AclInterface.class); + aclServiceManager = mock(AclServiceManager.class); + aclEventListener = new AclEventListener(aclServiceManager); + + aclInterfaceValueSaver = ArgumentCaptor.forClass(AclInterface.class); + actionValueSaver = ArgumentCaptor.forClass(AclServiceManager.Action.class); + aceValueSaver = ArgumentCaptor.forClass(Ace.class); + + aclName = "00000000-0000-0000-0000-000000000001"; + } + + @After + public void tearDown() { + clearStaticData(aclInterfaceMock, aclName); + } + + @Test + public void testUpdate_singleInterface_addNewAce() { + prepareAclDataUtil(aclInterfaceMock, aclName); + + Acl previousAcl = prepareAcl(aclName, "AllowUDP"); + Acl updatedAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP"); + + aclEventListener.update(mockInstanceId, previousAcl, updatedAcl); + + verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(), + aceValueSaver.capture()); + + assertEquals(Action.ADD, actionValueSaver.getValue()); + assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName()); + } + + @Test + public void testUpdate_singleInterface_removeOldAce() { + prepareAclDataUtil(aclInterfaceMock, aclName); + + Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP"); + Acl updatedAcl = prepareAcl(aclName, "AllowUDP"); + + aclEventListener.update(mockInstanceId, previousAcl, updatedAcl); + + verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(), + aceValueSaver.capture()); + + assertEquals(Action.REMOVE, actionValueSaver.getValue()); + assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName()); + } + + @Test + public void testUpdate_singleInterface_addNewAceAndRemoveOldAce() { + prepareAclDataUtil(aclInterfaceMock, aclName); + + Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP"); + Acl updatedAcl = prepareAcl(aclName, "AllowTCP", "AllowUDP"); + + aclEventListener.update(mockInstanceId, previousAcl, updatedAcl); + + verify(aclServiceManager, times(2)).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(), + aceValueSaver.capture()); + + assertEquals(Action.ADD, actionValueSaver.getAllValues().get(0)); + assertEquals("AllowTCP", aceValueSaver.getAllValues().get(0).getRuleName()); + + assertEquals(Action.REMOVE, actionValueSaver.getAllValues().get(1)); + assertEquals("AllowICMP", aceValueSaver.getAllValues().get(1).getRuleName()); + } +} diff --git a/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/utils/AclServiceTestUtils.java b/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/utils/AclServiceTestUtils.java index 471888649f..4cdbbba40f 100644 --- a/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/utils/AclServiceTestUtils.java +++ b/vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/utils/AclServiceTestUtils.java @@ -10,11 +10,16 @@ package org.opendaylight.netvirt.aclservice.utils; import static com.google.common.collect.Iterables.filter; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import com.google.common.collect.Iterables; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.Assert; import org.opendaylight.genius.mdsalutil.MatchFieldType; @@ -23,12 +28,17 @@ import org.opendaylight.genius.mdsalutil.MatchInfoBase; import org.opendaylight.genius.mdsalutil.NwConstants; import org.opendaylight.genius.mdsalutil.NxMatchFieldType; import org.opendaylight.genius.mdsalutil.NxMatchInfo; +import org.opendaylight.netvirt.aclservice.api.utils.AclInterface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; public class AclServiceTestUtils { @@ -125,4 +135,37 @@ public class AclServiceTestUtils { (item -> ((MatchInfo) item).getMatchField().equals(matchType))); Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches)); } + + public static void prepareAclDataUtil(AclInterface inter, String... updatedAclNames) { + AclDataUtil.addAclInterfaceMap(prapreaAclIds(updatedAclNames), inter); + } + + public static Acl prepareAcl(String aclName, String... aces) { + AccessListEntries aceEntries = mock(AccessListEntries.class); + List aceList = prepareAceList(aces); + when(aceEntries.getAce()).thenReturn(aceList); + + Acl acl = mock(Acl.class); + when(acl.getAccessListEntries()).thenReturn(aceEntries); + when(acl.getAclName()).thenReturn(aclName); + return acl; + } + + public static List prepareAceList(String... aces) { + List aceList = new ArrayList<>(); + for (String aceName : aces) { + Ace aceMock = mock(Ace.class); + when(aceMock.getRuleName()).thenReturn(aceName); + aceList.add(aceMock); + } + return aceList; + } + + public static List prapreaAclIds(String... names) { + return Stream.of(names).map(name -> new Uuid(name)).collect(Collectors.toList()); + } + + public static void clearStaticData(AclInterface inter, String... aclNames) { + AclDataUtil.removeAclInterfaceMap(prapreaAclIds(aclNames), inter); + } }