Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / natservice / natservice-impl / src / test / java / org / opendaylight / vpnservice / natservice / internal / test / ExternalNetworksChangeListenerTest.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.natservice.internal.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.when;
14 import static org.mockito.Mockito.mock;
15
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
29 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
30 import org.opendaylight.vpnservice.mdsalutil.ActionType;
31 import org.opendaylight.vpnservice.mdsalutil.BucketInfo;
32 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
33 import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
34 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
35 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
36 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
37 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
38 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
39 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
40 import org.opendaylight.vpnservice.natservice.internal.ExternalNetworksChangeListener;
41 import org.opendaylight.vpnservice.natservice.internal.NatUtil;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest(MDSALUtil.class)
52 public class ExternalNetworksChangeListenerTest {
53
54     @Mock DataBroker dataBroker;
55     @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
56     @Mock IMdsalApiManager mdsalManager;
57     @Mock FlowEntity flowMock;
58     @Mock GroupEntity groupMock;
59     InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.external.networks.Networks> id = null;
60     org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.external.networks.Networks networks = null;
61     private ExternalNetworksChangeListener extNetworks;
62
63     @Before
64     public void setUp() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         when(dataBroker.registerDataChangeListener(
67                 any(LogicalDatastoreType.class),
68                 any(InstanceIdentifier.class),
69                 any(DataChangeListener.class),
70                 any(DataChangeScope.class)))
71                 .thenReturn(dataChangeListenerRegistration);
72         extNetworks = new ExternalNetworksChangeListener(dataBroker);
73
74         PowerMockito.mockStatic(MDSALUtil.class);
75     }
76
77
78     @Test
79     public void testSnatFlowEntity() {
80         FlowEntity flowMock = mock(FlowEntity.class);
81         final short SNAT_TABLE = 40;
82         final int DEFAULT_SNAT_FLOW_PRIORITY = 0;
83         final String FLOWID_SEPARATOR = ".";
84         String SNAT_FLOWID_PREFIX = "SNAT.";
85
86
87         BigInteger dpnId = new BigInteger("100");
88         String routerName = new String("200");
89         long routerId = 200;
90         long groupId = 300;
91         List<BucketInfo> bucketInfo = new ArrayList<BucketInfo>();
92         List<ActionInfo> listActionInfoPrimary = new ArrayList<ActionInfo>();
93         listActionInfoPrimary.add(new ActionInfo(ActionType.output,
94                 new String[] {"3"}));
95         BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
96         List<ActionInfo> listActionInfoSecondary = new ArrayList<ActionInfo>();
97         listActionInfoSecondary.add(new ActionInfo(ActionType.output,
98                 new String[] {"4"}));
99         BucketInfo bucketSecondary = new BucketInfo(listActionInfoPrimary);
100         bucketInfo.add(0, bucketPrimary);
101         bucketInfo.add(1, bucketSecondary);
102
103         List<MatchInfo> matches = new ArrayList<MatchInfo>();
104         matches.add(new MatchInfo(MatchFieldType.eth_type,
105                 new long[] { 0x0800L }));
106
107         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
108         List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
109         actionsInfos.add(new ActionInfo(ActionType.group, new String[] {String.valueOf(groupId)}));
110         instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
111
112
113         String flowRef =  new StringBuffer().append(SNAT_FLOWID_PREFIX).append(dpnId).append(FLOWID_SEPARATOR).
114                 append(SNAT_TABLE).append(FLOWID_SEPARATOR).append(routerId).toString();
115
116         BigInteger cookieSnat = NatUtil.getCookieSnatFlow(routerId);
117         try {
118             PowerMockito.when(MDSALUtil.class, "buildFlowEntity", dpnId, SNAT_TABLE, flowRef,
119                     DEFAULT_SNAT_FLOW_PRIORITY, flowRef, 0, 0,
120                     cookieSnat, matches, instructions ).thenReturn(flowMock);
121         } catch (Exception e) {
122             // Test failed anyways
123             assertEquals("true", "false");
124         }
125         /* TODO : Fix this to mock it properly when it reads DS
126         extNetworks.buildSnatFlowEntity(dpnId, routerName, groupId);
127         PowerMockito.verifyStatic(); */
128
129     }
130
131 }