2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.device;
11 import java.util.Arrays;
12 import java.util.List;
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;
32 * org.opendaylight.openflowplugin.impl.device
34 * test of {@link DeviceStateImpl} - lightweight version, using basic ways (TDD)
36 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
38 * Created: Mar 29, 2015
40 @RunWith(MockitoJUnitRunner.class)
41 public class DeviceStateImplTest {
43 private NodeId nodeId;
45 private FeaturesReply featuresReply;
46 private DeviceStateImpl deviceState;
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());
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);
63 * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
65 @Test(expected=NullPointerException.class)
66 public void testDeviceStateImplNullNodeId(){
67 new DeviceStateImpl(featuresReply, null);
71 * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
73 @Test(expected=IllegalArgumentException.class)
74 public void testDeviceStateImplNullFeaturesReply(){
75 new DeviceStateImpl(null, nodeId);
79 * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}.
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);
89 * Test method for {@link DeviceStateImpl#getNodeId()}.
92 public void testGetNodeId(){
93 final NodeId getNodeId = deviceState.getNodeId();
94 Assert.assertNotNull(getNodeId);
95 Assert.assertEquals(nodeId, getNodeId);
99 * Test method for {@link DeviceStateImpl#getFeatures()}.
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());
111 * Test method for {@link DeviceStateImpl#getPhysicalPorts()}.
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)));
121 * Test method for {@link DeviceStateImpl#getPortsBandwidth()}.
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));
132 * Test method for {@link DeviceStateImpl#getPorts()}.
135 public void testGetPorts(){
136 final Set<Long> portNrs = deviceState.getPorts();
137 Assert.assertTrue(portNrs.contains(portNr));
141 * Test method for {@link DeviceStateImpl#getPhysicalPort(java.lang.Long)}.
144 public void testGetPhysicalPort(){
145 Assert.assertEquals(pPort.get(0), deviceState.getPhysicalPort(portNr));
149 * Test method for {@link DeviceStateImpl#getPortBandwidth(java.lang.Long)}.
152 public void testGetPortBandwidth(){
153 Assert.assertEquals(portBandwidth, deviceState.getPortBandwidth((portNr)));
157 * Test method for {@link DeviceStateImpl#isPortEnabled(long)}.
160 public void testIsPortEnabledLong(){
161 Assert.assertTrue(deviceState.isPortEnabled(portNr));
165 * Test method for {@link DeviceStateImpl#isPortEnabled(PortGrouping)}.
168 public void testIsPortEnabledPortGrouping(){
169 Assert.assertTrue(deviceState.isPortEnabled(pPort.get(0)));
173 * Test method for {@link DeviceStateImpl#getEnabledPorts()}.
176 public void testGetEnabledPorts(){
177 final List<PortGrouping> getEnabledPort = deviceState.getEnabledPorts();
178 Assert.assertNotNull(getEnabledPort);
179 Assert.assertTrue(getEnabledPort.contains(pPort.get(0)));