blob: 37f6f5a0979e8350736555ddfc75ca3e77fe300c [file] [log] [blame]
Jonathan Hart77803d32014-08-19 08:53:00 -07001package net.onrc.onos.core.flowmanager.web;
2
3import static org.easymock.EasyMock.createMock;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.replay;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertTrue;
8
9import java.io.IOException;
10import java.io.StringWriter;
11import java.util.ArrayList;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.List;
16import java.util.Map;
17import java.util.Set;
18
19import net.floodlightcontroller.util.MACAddress;
20import net.onrc.onos.api.flowmanager.Flow;
21import net.onrc.onos.api.flowmanager.FlowId;
22import net.onrc.onos.api.flowmanager.FlowLink;
23import net.onrc.onos.api.flowmanager.FlowManagerService;
24import net.onrc.onos.api.flowmanager.OpticalPathFlow;
25import net.onrc.onos.api.flowmanager.PacketPathFlow;
26import net.onrc.onos.api.flowmanager.Path;
27import net.onrc.onos.core.matchaction.action.Action;
28import net.onrc.onos.core.matchaction.action.ModifyDstMacAction;
29import net.onrc.onos.core.matchaction.action.ModifyLambdaAction;
30import net.onrc.onos.core.matchaction.match.PacketMatch;
31import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
32import net.onrc.onos.core.util.PortNumber;
33import net.onrc.onos.core.util.SwitchPort;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.restlet.Context;
38import org.restlet.Request;
39import org.restlet.representation.Representation;
40
41/**
42 * Tests for the {@link FlowResource} REST handler.
43 */
44public class FlowResourceTest {
45 FlowResource flowResource;
46
47 /**
48 * Set up the FlowResource for the test.
49 */
50 @Before
51 public void setUp() {
52 // Create some flow data
53 Set<Flow> flowSet = createFlows();
54
55 // Create a mock flow manager service that will return the flows
56 FlowManagerService flowManager = createMock(FlowManagerService.class);
57 expect(flowManager.getFlows()).andReturn(flowSet);
58 replay(flowManager);
59
60 // Inject the flow manager service into a Restlet context
61 Map<String, Object> attributes = new HashMap<>();
62 attributes.put(FlowManagerService.class.getCanonicalName(), flowManager);
63 Context context = new Context();
64 context.setAttributes(attributes);
65
66 // Create a FlowResource and initialize with the context
67 flowResource = new FlowResource();
68 flowResource.init(context, new Request(), null);
69 }
70
71 /**
72 * Creates some flow data that the REST handler can retrieve.
73 * The data is arbitrary because it is never verified during the test.
74 *
75 * @return a set of dummy Flow objects for the test
76 */
77 private Set<Flow> createFlows() {
78 Set<Flow> flowSet = new HashSet<>();
79
80 PacketMatch match = new PacketMatchBuilder().setDstTcpPort((short) 1).build();
81
82 List<FlowLink> links = new ArrayList<>();
83 links.add(new FlowLink(new SwitchPort(1L, 2L), new SwitchPort(2L, 1L)));
84 links.add(new FlowLink(new SwitchPort(2L, 2L), new SwitchPort(3L, 1L)));
85
86 Path path = new Path(links);
87
88 PacketPathFlow packetFlow = new PacketPathFlow(new FlowId(1L),
89 match, PortNumber.uint32(1), path,
90 Collections.<Action>singletonList(new ModifyDstMacAction(MACAddress.valueOf(4L))),
91 0, 0);
92
93 OpticalPathFlow opticalFlow = new OpticalPathFlow(new FlowId(2L),
94 PortNumber.uint32(3), path,
95 Collections.<Action>singletonList(new ModifyLambdaAction(2)), 4);
96
97 flowSet.add(packetFlow);
98 flowSet.add(opticalFlow);
99
100 return flowSet;
101 }
102
103 /**
104 * Tests the handler method that retrieves all flow resources.
105 *
106 * @throws IOException if there's an error serializing the representation
107 */
108 @Test
109 public void testRetrieve() throws IOException {
110 Representation rep = flowResource.retrieve();
111
112 StringWriter writer = new StringWriter();
113
114 rep.write(writer);
115 String output = writer.toString();
116
117 System.out.println(writer);
118
119 assertNotNull(output);
120 // Output should be a JSON array of JSON objects
121 assertTrue(output.startsWith("[{"));
122 }
123
124}