Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.flowmanager.web; |
| 2 | |
| 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | import static org.junit.Assert.assertTrue; |
| 8 | |
| 9 | import java.io.IOException; |
| 10 | import java.io.StringWriter; |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Collections; |
| 13 | import java.util.HashMap; |
| 14 | import java.util.HashSet; |
| 15 | import java.util.List; |
| 16 | import java.util.Map; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import net.floodlightcontroller.util.MACAddress; |
| 20 | import net.onrc.onos.api.flowmanager.Flow; |
| 21 | import net.onrc.onos.api.flowmanager.FlowId; |
| 22 | import net.onrc.onos.api.flowmanager.FlowLink; |
| 23 | import net.onrc.onos.api.flowmanager.FlowManagerService; |
| 24 | import net.onrc.onos.api.flowmanager.OpticalPathFlow; |
| 25 | import net.onrc.onos.api.flowmanager.PacketPathFlow; |
| 26 | import net.onrc.onos.api.flowmanager.Path; |
| 27 | import net.onrc.onos.core.matchaction.action.Action; |
| 28 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 29 | import net.onrc.onos.core.matchaction.action.ModifyLambdaAction; |
| 30 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 31 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 32 | import net.onrc.onos.core.util.PortNumber; |
| 33 | import net.onrc.onos.core.util.SwitchPort; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.restlet.Context; |
| 38 | import org.restlet.Request; |
| 39 | import org.restlet.representation.Representation; |
| 40 | |
| 41 | /** |
| 42 | * Tests for the {@link FlowResource} REST handler. |
| 43 | */ |
| 44 | public 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 | } |