BUG 2090 : Clustering : Bring akka-raft unit test coverage upto 80%
[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 static junit.framework.Assert.assertEquals;
11 import static junit.framework.Assert.fail;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
21 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
22 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 = InstanceIdentifier.create(Nodes.class);
51         InstanceIdentifier<Node> srcNodeInstanceIdentifier = srcNodesInstanceIdentifier
52                 .child(Node.class, new NodeKey(new NodeId("openflow:1")));
53         InstanceIdentifier<NodeConnector> srcNodeConnectorInstanceIdentifier = srcNodeInstanceIdentifier
54                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:1:2")));
55         srcNodeConnectorRef = new NodeConnectorRef(srcNodeConnectorInstanceIdentifier);
56
57         //build dest node connector ref
58         InstanceIdentifier<Nodes> nodesInstanceIdentifier
59         = InstanceIdentifier.builder(Nodes.class)
60         .build();
61         InstanceIdentifier<Node> nodeInstanceIdentifier =
62                 nodesInstanceIdentifier.child(Node.class, new NodeKey(new NodeId("openflow:2")));
63         InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier =
64                 nodeInstanceIdentifier.child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:2:2")));
65         destNodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
66         destMacAddress = new MacAddress("00:0a:95:9d:68:16");
67         srcMacAddress = new MacAddress("00:0a:95:8c:97:24");
68         dataModificationTransaction = mock(DataModificationTransaction.class);
69         when(dataBrokerService.beginTransaction()).thenReturn(dataModificationTransaction);
70     }
71
72     @Test
73     public void testFlowWriterServiceImpl_NPEWhenDataBrokerServiceIsNull() throws Exception {
74         try {
75             new FlowWriterServiceImpl(null, networkGraphService);
76             fail("Expected null pointer exception.");
77         } catch(NullPointerException npe) {
78             assertEquals("dataBrokerService should not be null.", npe.getMessage());
79         }
80     }
81
82     @Test
83     public void testAddMacToMacFlow_NPEWhenNullSourceMacDestMacAndNodeConnectorRef() throws Exception {
84         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
85         try {
86             flowWriterService.addMacToMacFlow(null, null, null);
87             fail("Expected null pointer exception.");
88         } catch(NullPointerException npe) {
89             assertEquals("Destination mac address should not be null.", npe.getMessage());
90         }
91     }
92
93     @Test
94     public void testAddMacToMacFlow_NPEWhenSourceMacNullMac() throws Exception {
95         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
96         try {
97             flowWriterService.addMacToMacFlow(null, null, destNodeConnectorRef);
98             fail("Expected null pointer exception.");
99         } catch(NullPointerException npe) {
100             assertEquals("Destination mac address should not be null.", npe.getMessage());
101         }
102     }
103
104     @Test
105     public void testAddMacToMacFlow_NPEWhenNullSourceMacNodeConnectorRef() throws Exception {
106         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
107         try {
108             flowWriterService.addMacToMacFlow(null, destMacAddress, null);
109             fail("Expected null pointer exception.");
110         } catch(NullPointerException npe) {
111             assertEquals("Destination port should not be null.", npe.getMessage());
112         }
113     }
114
115     @Test
116     public void testAddMacToMacFlow_WhenNullSourceMac() throws Exception {
117         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
118         flowWriterService.addMacToMacFlow(null, destMacAddress, destNodeConnectorRef);
119         verify(dataBrokerService, times(1)).beginTransaction();
120         verify(dataModificationTransaction, times(1)).commit();
121     }
122
123     @Test
124     public void testAddMacToMacFlow_WhenSrcAndDestMacAreSame() throws Exception {
125         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
126         flowWriterService.addMacToMacFlow(new MacAddress(destMacAddress.getValue()), destMacAddress, destNodeConnectorRef);
127         verify(dataBrokerService, never()).beginTransaction();
128         verify(dataModificationTransaction, never()).commit();
129
130     }
131
132     @Test
133     public void testAddMacToMacFlow_SunnyDay() throws Exception {
134         FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
135         flowWriterService.addMacToMacFlow(srcMacAddress, destMacAddress, destNodeConnectorRef);
136         verify(dataBrokerService, times(1)).beginTransaction();
137         verify(dataModificationTransaction, times(1)).commit();
138     }
139
140 }