DeviceState implementation
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceStateImplTest.java
1 /**
2  * Copyright (c) 2015 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
9 package org.opendaylight.openflowplugin.impl.device;
10
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
29
30 /**
31  * openflowplugin-impl
32  * org.opendaylight.openflowplugin.impl.device
33  *
34  * test of {@link DeviceStateImpl} - lightweight version, using basic ways (TDD)
35  *
36  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
37  *
38  * Created: Mar 29, 2015
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class DeviceStateImplTest {
42
43     private NodeId nodeId;
44     @Mock
45     private FeaturesReply featuresReply;
46     private DeviceStateImpl deviceState;
47
48     private final short version = 13;
49     private final long portNr = 10L;
50     private final Long portBandwidth = 1024L;
51     private final List<PhyPort> pPort = Arrays.asList(new PhyPortBuilder()
52                     .setPortNo(portNr).setMaxSpeed(portBandwidth).build());
53
54     @Before
55     public void initialization() {
56         nodeId = new NodeId("test-node-id");
57         Mockito.when(featuresReply.getVersion()).thenReturn(version);
58         Mockito.when(featuresReply.getPhyPort()).thenReturn(pPort);
59         deviceState = new DeviceStateImpl(featuresReply, nodeId);
60     }
61
62     /**
63      * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
64      */
65     @Test(expected=NullPointerException.class)
66     public void testDeviceStateImplNullNodeId(){
67         new DeviceStateImpl(featuresReply, null);
68     }
69
70     /**
71      * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
72      */
73     @Test(expected=IllegalArgumentException.class)
74     public void testDeviceStateImplNullFeaturesReply(){
75         new DeviceStateImpl(null, nodeId);
76     }
77
78     /**
79      * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
80      */
81     @Test(expected=IllegalArgumentException.class)
82     public void testDeviceStateImplNullPhyPort(){
83         final FeaturesReply emptyFeaturesReply = Mockito.mock(FeaturesReply.class);
84         Mockito.when(emptyFeaturesReply.getPhyPort()).thenReturn(null);
85         new DeviceStateImpl(emptyFeaturesReply, nodeId);
86     }
87
88     /**
89      * Test method for {@link DeviceStateImpl#getNodeId()}.
90      */
91     @Test
92     public void testGetNodeId(){
93         final NodeId getNodeId = deviceState.getNodeId();
94         Assert.assertNotNull(getNodeId);
95         Assert.assertEquals(nodeId, getNodeId);
96     }
97
98     /**
99      * Test method for {@link DeviceStateImpl#getFeatures()}.
100      */
101     @Test
102     public void testGetFeatures(){
103         final GetFeaturesOutputBuilder expetedResult = new GetFeaturesOutputBuilder(featuresReply);
104         final GetFeaturesOutput getFeatures = deviceState.getFeatures();
105         Assert.assertNotNull(getFeatures);
106         Assert.assertEquals(expetedResult.getVersion(), getFeatures.getVersion());
107         Assert.assertEquals(expetedResult.getPhyPort(), getFeatures.getPhyPort());
108     }
109
110     /**
111      * Test method for {@link DeviceStateImpl#getPhysicalPorts()}.
112      */
113     @Test
114     public void testGetPhysicalPorts(){
115         final Map<Long, PortGrouping> getPhysPort = deviceState.getPhysicalPorts();
116         Assert.assertNotNull(getPhysPort);
117         Assert.assertTrue(getPhysPort.values().contains(pPort.get(0)));
118     }
119
120     /**
121      * Test method for {@link DeviceStateImpl#getPortsBandwidth()}.
122      */
123     @Test
124     public void testGetPortsBandwidth(){
125         final Map<Long, Long> portBandwidth = deviceState.getPortsBandwidth();
126         Assert.assertNotNull(portBandwidth);
127         Assert.assertTrue(portBandwidth.containsKey(portNr));
128         Assert.assertEquals(this.portBandwidth, portBandwidth.get(portNr));
129     }
130
131     /**
132      * Test method for {@link DeviceStateImpl#getPorts()}.
133      */
134     @Test
135     public void testGetPorts(){
136         final Set<Long> portNrs = deviceState.getPorts();
137         Assert.assertTrue(portNrs.contains(portNr));
138     }
139
140     /**
141      * Test method for {@link DeviceStateImpl#getPhysicalPort(java.lang.Long)}.
142      */
143     @Test
144     public void testGetPhysicalPort(){
145         Assert.assertEquals(pPort.get(0), deviceState.getPhysicalPort(portNr));
146     }
147
148     /**
149      * Test method for {@link DeviceStateImpl#getPortBandwidth(java.lang.Long)}.
150      */
151     @Test
152     public void testGetPortBandwidth(){
153         Assert.assertEquals(portBandwidth, deviceState.getPortBandwidth((portNr)));
154     }
155
156     /**
157      * Test method for {@link DeviceStateImpl#isPortEnabled(long)}.
158      */
159     @Test
160     public void testIsPortEnabledLong(){
161         Assert.assertTrue(deviceState.isPortEnabled(portNr));
162     }
163
164     /**
165      * Test method for {@link DeviceStateImpl#isPortEnabled(PortGrouping)}.
166      */
167     @Test
168     public void testIsPortEnabledPortGrouping(){
169         Assert.assertTrue(deviceState.isPortEnabled(pPort.get(0)));
170     }
171
172     /**
173      * Test method for {@link DeviceStateImpl#getEnabledPorts()}.
174      */
175     @Test
176     public void testGetEnabledPorts(){
177         final List<PortGrouping> getEnabledPort = deviceState.getEnabledPorts();
178         Assert.assertNotNull(getEnabledPort);
179         Assert.assertTrue(getEnabledPort.contains(pPort.get(0)));
180     }
181
182 }