Open Kilda Java Documentation
SpeakerBoltTest.java
Go to the documentation of this file.
1 package org.openkilda.simulator.bolts;
2 
3 import static org.hamcrest.CoreMatchers.instanceOf;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertFalse;
6 import static org.junit.Assert.assertThat;
7 import static org.junit.Assert.assertTrue;
8 
18 
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import org.apache.storm.tuple.Values;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 public class SpeakerBoltTest {
31  SpeakerBolt speakerBolt;
32  SwitchId dpid = new SwitchId("00:00:00:00:00:01");
33  int numOfPorts = 10;
34  int linkLatency = 10;
35  int localLinkPort = 1;
36  List<LinkMessage> links = new ArrayList<>();
37  String peerSwitch = "00:00:00:00:00:05";
38  int peerPort = 1;
39  LinkMessage link;
40  SwitchMessage switchMessage;
41  ObjectMapper mapper;
42 
43  @Rule
44  public ExpectedException thrown = ExpectedException.none();
45 
46 
47  @Before
48  public void setUp() throws Exception {
49  mapper = new ObjectMapper();
50  speakerBolt = new SpeakerBolt();
51  speakerBolt.prepare(null, null, null);
52 
53  link = new LinkMessage(linkLatency, localLinkPort, peerSwitch, peerPort);
54  links.add(link);
55 
56  switchMessage = new SwitchMessage(dpid, numOfPorts, links);
57  }
58 
59  @After
60  public void tearDown() throws Exception {
61  }
62 
63  @Test
64  public void addSwitch() throws Exception {
65  speakerBolt.addSwitch(switchMessage);
66  assertEquals(1, speakerBolt.switches.size());
67 
68  ISwitchImpl sw = speakerBolt.switches.get(dpid);
69  assertTrue(sw.isActive());
70 
71  List<IPortImpl> ports = sw.getPorts();
72  assertEquals(numOfPorts, ports.size());
73  for (IPortImpl port : ports) {
74  if (port.getNumber() != localLinkPort) {
75  assertFalse(port.isActive());
76  assertFalse(port.isActiveIsl());
77  } else {
78  assertTrue(port.isActive());
79  assertTrue(port.isActiveIsl());
80  }
81  }
82  }
83 
84  @Test
85  public void testAddSwitchValues() throws Exception {
86  List<Values> values = speakerBolt.addSwitch(switchMessage);
87 
88  assertEquals(3, values.size());
89  int count = 0;
90  for (Values value : values) {
91  InfoMessage infoMessage = mapper.readValue((String) value.get(1), InfoMessage.class);
92  if (count < 2) {
93  assertThat(infoMessage.getData(), instanceOf(SwitchInfoData.class));
94  SwitchInfoData sw = (SwitchInfoData) infoMessage.getData();
95  assertEquals(dpid, sw.getSwitchId());
96  } else {
97  assertThat(infoMessage.getData(), instanceOf(PortInfoData.class));
98  PortInfoData port = (PortInfoData) infoMessage.getData();
99  assertEquals(dpid, port.getSwitchId());
100  if (port.getPortNo() == localLinkPort) {
101  assertEquals(PortChangeType.UP, port.getState());
102  } else {
103  assertEquals(PortChangeType.DOWN, port.getState());
104  }
105  }
106  count++;
107  }
108  }
109 }
List< Values > addSwitch(AddSwitchCommand data)
value
Definition: nodes.py:62
Map< SwitchId, ISwitchImpl > switches
void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector)
int count
Definition: generator.py:19