code optimization for cluster environment
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / listeners / AclEventListenerTest.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.aclservice.listeners;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.clearStaticData;
16 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAcl;
17 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAclClusterUtil;
18 import static org.opendaylight.netvirt.aclservice.utils.AclServiceTestUtils.prepareAclDataUtil;
19
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
26 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
27 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 public class AclEventListenerTest {
33
34     private AclEventListener aclEventListener;
35     private AclServiceManager aclServiceManager;
36
37     private InstanceIdentifier<Acl> mockInstanceId;
38     private AclInterface aclInterfaceMock;
39
40     private ArgumentCaptor<AclInterface> aclInterfaceValueSaver;
41     private ArgumentCaptor<Action> actionValueSaver;
42     private ArgumentCaptor<Ace> aceValueSaver;
43     private String aclName;
44
45     @SuppressWarnings("unchecked")
46     @Before
47     public void setUp() {
48
49         mockInstanceId = mock(InstanceIdentifier.class);
50         aclInterfaceMock = mock(AclInterface.class);
51         aclServiceManager = mock(AclServiceManager.class);
52         aclEventListener = new AclEventListener(aclServiceManager, mock(DataBroker.class));
53
54         aclInterfaceValueSaver = ArgumentCaptor.forClass(AclInterface.class);
55         actionValueSaver = ArgumentCaptor.forClass(AclServiceManager.Action.class);
56         aceValueSaver = ArgumentCaptor.forClass(Ace.class);
57         prepareAclClusterUtil("netvirt-acl");
58
59
60         aclName = "00000000-0000-0000-0000-000000000001";
61     }
62
63     @After
64     public void tearDown() {
65         clearStaticData(aclInterfaceMock, aclName);
66     }
67
68     @Test
69     public void testUpdate_singleInterface_addNewAce() {
70         prepareAclDataUtil(aclInterfaceMock, aclName);
71
72         Acl previousAcl = prepareAcl(aclName, "AllowUDP");
73         Acl updatedAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
74
75         aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
76
77         verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
78                 aceValueSaver.capture());
79
80         assertEquals(Action.ADD, actionValueSaver.getValue());
81         assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName());
82     }
83
84     @Test
85     public void testUpdate_singleInterface_removeOldAce() {
86         prepareAclDataUtil(aclInterfaceMock, aclName);
87
88         Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
89         Acl updatedAcl = prepareAcl(aclName, "AllowUDP");
90
91         aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
92
93         verify(aclServiceManager).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
94                 aceValueSaver.capture());
95
96         assertEquals(Action.REMOVE, actionValueSaver.getValue());
97         assertEquals("AllowICMP", aceValueSaver.getValue().getRuleName());
98     }
99
100     @Test
101     public void testUpdate_singleInterface_addNewAceAndRemoveOldAce() {
102         prepareAclDataUtil(aclInterfaceMock, aclName);
103
104         Acl previousAcl = prepareAcl(aclName, "AllowICMP", "AllowUDP");
105         Acl updatedAcl = prepareAcl(aclName, "AllowTCP", "AllowUDP");
106
107         aclEventListener.update(mockInstanceId, previousAcl, updatedAcl);
108
109         verify(aclServiceManager, times(2)).notifyAce(aclInterfaceValueSaver.capture(), actionValueSaver.capture(),
110                 aceValueSaver.capture());
111
112         assertEquals(Action.ADD, actionValueSaver.getAllValues().get(0));
113         assertEquals("AllowTCP", aceValueSaver.getAllValues().get(0).getRuleName());
114
115         assertEquals(Action.REMOVE, actionValueSaver.getAllValues().get(1));
116         assertEquals("AllowICMP", aceValueSaver.getAllValues().get(1).getRuleName());
117     }
118 }