Add unit tests to AclEventListener 64/42964/3
authorTomer <tomer.pearl@hpe.com>
Tue, 2 Aug 2016 11:46:29 +0000 (14:46 +0300)
committerTomer <tomer.pearl@hpe.com>
Thu, 4 Aug 2016 19:47:05 +0000 (22:47 +0300)
Change-Id: I4fda31acd13ffd268f3daee0652c9b4fc3297b60
Signed-off-by: Tomer <tomer.pearl@hpe.com>
vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/listeners/AclEventListenerTest.java [new file with mode: 0644]
vpnservice/aclservice/impl/src/test/java/org/opendaylight/netvirt/aclservice/utils/AclServiceTestUtils.java

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 (file)
index 0000000..9e86e50
--- /dev/null
@@ -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<Acl> mockInstanceId;
+    private AclInterface aclInterfaceMock;
+
+    private ArgumentCaptor<AclInterface> aclInterfaceValueSaver;
+    private ArgumentCaptor<Action> actionValueSaver;
+    private ArgumentCaptor<Ace> 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());
+    }
+}
index 471888649f651ac30a402d8402b7960a955c3600..4cdbbba40f40f2df1c76465aeedbb75679e36abe 100644 (file)
@@ -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<Ace> 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<Ace> prepareAceList(String... aces) {
+        List<Ace> 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<Uuid> 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);
+    }
 }