Merge "Bug 639, Bug 641, Bug 642: This is MD-SAL based sample implementation of a...
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / test / java / org / opendaylight / controller / sample / l2switch / md / flow / FlowWriterServiceImplTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.sample.l2switch.md.flow;
9
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService;
13 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 import static junit.framework.Assert.assertEquals;
27 import static junit.framework.Assert.fail;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 /**
35  */
36 public class FlowWriterServiceImplTest {
37   private DataBrokerService dataBrokerService;
38   private NodeConnectorRef srcNodeConnectorRef;
39   private NodeConnectorRef destNodeConnectorRef;
40   private MacAddress destMacAddress;
41   private MacAddress srcMacAddress;
42   private DataModificationTransaction dataModificationTransaction;
43   private NetworkGraphService networkGraphService;
44
45   @Before
46   public void init() {
47     dataBrokerService = mock(DataBrokerService.class);
48     networkGraphService = mock(NetworkGraphService.class);
49     //build source node connector ref
50     InstanceIdentifier<Nodes> srcNodesInstanceIdentifier
51         = InstanceIdentifier.builder(Nodes.class)
52         .build();
53     InstanceIdentifier<Node> srcNodeInstanceIdentifier
54         = InstanceIdentifier.builder(srcNodesInstanceIdentifier)
55         .child(Node.class, new NodeKey(new NodeId("openflow:1")))
56         .build();
57     InstanceIdentifier<NodeConnector> srcNodeConnectorInstanceIdentifier
58         = InstanceIdentifier.builder(srcNodeInstanceIdentifier)
59         .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:1:2")))
60         .build();
61     srcNodeConnectorRef = new NodeConnectorRef(srcNodeConnectorInstanceIdentifier);
62
63     //build dest node connector ref
64     InstanceIdentifier<Nodes> nodesInstanceIdentifier
65         = InstanceIdentifier.builder(Nodes.class)
66         .build();
67     InstanceIdentifier<Node> nodeInstanceIdentifier
68         = InstanceIdentifier.builder(nodesInstanceIdentifier)
69         .child(Node.class, new NodeKey(new NodeId("openflow:2")))
70         .build();
71     InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier
72         = InstanceIdentifier.builder(nodeInstanceIdentifier)
73         .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:2:2")))
74         .build();
75     destNodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
76     destMacAddress = new MacAddress("00:0a:95:9d:68:16");
77     srcMacAddress = new MacAddress("00:0a:95:8c:97:24");
78     dataModificationTransaction = mock(DataModificationTransaction.class);
79     when(dataBrokerService.beginTransaction()).thenReturn(dataModificationTransaction);
80   }
81
82   @Test
83   public void testFlowWriterServiceImpl_NPEWhenDataBrokerServiceIsNull() throws Exception {
84     try {
85       new FlowWriterServiceImpl(null, networkGraphService);
86       fail("Expected null pointer exception.");
87     } catch(NullPointerException npe) {
88       assertEquals("dataBrokerService should not be null.", npe.getMessage());
89     }
90   }
91
92   @Test
93   public void testAddMacToMacFlow_NPEWhenNullSourceMacDestMacAndNodeConnectorRef() throws Exception {
94     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
95     try {
96       flowWriterService.addMacToMacFlow(null, null, null);
97       fail("Expected null pointer exception.");
98     } catch(NullPointerException npe) {
99       assertEquals("Destination mac address should not be null.", npe.getMessage());
100     }
101   }
102
103   @Test
104   public void testAddMacToMacFlow_NPEWhenSourceMacNullMac() throws Exception {
105     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
106     try {
107       flowWriterService.addMacToMacFlow(null, null, destNodeConnectorRef);
108       fail("Expected null pointer exception.");
109     } catch(NullPointerException npe) {
110       assertEquals("Destination mac address should not be null.", npe.getMessage());
111     }
112   }
113
114   @Test
115   public void testAddMacToMacFlow_NPEWhenNullSourceMacNodeConnectorRef() throws Exception {
116     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
117     try {
118       flowWriterService.addMacToMacFlow(null, destMacAddress, null);
119       fail("Expected null pointer exception.");
120     } catch(NullPointerException npe) {
121       assertEquals("Destination port should not be null.", npe.getMessage());
122     }
123   }
124
125   @Test
126   public void testAddMacToMacFlow_WhenNullSourceMac() throws Exception {
127     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
128     flowWriterService.addMacToMacFlow(null, destMacAddress, destNodeConnectorRef);
129     verify(dataBrokerService, times(1)).beginTransaction();
130     verify(dataModificationTransaction, times(1)).commit();
131   }
132
133   @Test
134   public void testAddMacToMacFlow_WhenSrcAndDestMacAreSame() throws Exception {
135     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
136     flowWriterService.addMacToMacFlow(new MacAddress(destMacAddress.getValue()), destMacAddress, destNodeConnectorRef);
137     verify(dataBrokerService, never()).beginTransaction();
138     verify(dataModificationTransaction, never()).commit();
139
140   }
141
142   @Test
143   public void testAddMacToMacFlow_SunnyDay() throws Exception {
144     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
145     flowWriterService.addMacToMacFlow(srcMacAddress, destMacAddress, destNodeConnectorRef);
146     verify(dataBrokerService, times(1)).beginTransaction();
147     verify(dataModificationTransaction, times(1)).commit();
148   }
149
150 }