blob: 2dd00013ba0f7ea623da21db4f4fc431a7beb7f6 [file] [log] [blame]
Jian Lie34f1102016-01-14 10:33:54 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.rest;
17
18import com.eclipsesource.json.JsonObject;
19import com.sun.jersey.api.client.ClientResponse;
20import com.sun.jersey.api.client.WebResource;
21import org.hamcrest.Matchers;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
27import org.onlab.rest.BaseResource;
28import org.onosproject.codec.CodecService;
29import org.onosproject.codec.impl.CodecManager;
30import org.onosproject.core.CoreService;
31import org.onosproject.net.NetTestTools;
32import org.onosproject.net.flowobjective.FlowObjectiveService;
33import org.onosproject.rest.resources.CoreWebApplication;
34
35import javax.ws.rs.core.MediaType;
36import java.io.InputStream;
37import java.net.HttpURLConnection;
38
39import static org.easymock.EasyMock.anyObject;
40import static org.easymock.EasyMock.anyShort;
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.expectLastCall;
44import static org.easymock.EasyMock.replay;
45import static org.easymock.EasyMock.verify;
46import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
48import static org.hamcrest.Matchers.notNullValue;
49import static org.junit.Assert.assertThat;
50import static org.onosproject.net.NetTestTools.APP_ID;
51
52/**
53 * Unit tests for flow objectives REST APIs.
54 */
55public class FlowObjectiveResourceTest extends ResourceTest {
56 final FlowObjectiveService mockFlowObjectiveService = createMock(FlowObjectiveService.class);
57 CoreService mockCoreService = createMock(CoreService.class);
58 public static final String REST_APP_ID = "org.onosproject.rest";
59
60 public FlowObjectiveResourceTest() {
61 super(CoreWebApplication.class);
62 }
63
64 /**
65 * Sets up the global values for all the tests.
66 */
67 @Before
68 public void setUpTest() {
69 // Mock Core Service
70 expect(mockCoreService.getAppId(anyShort()))
71 .andReturn(NetTestTools.APP_ID).anyTimes();
72 expect(mockCoreService.registerApplication(REST_APP_ID))
73 .andReturn(APP_ID).anyTimes();
74 replay(mockCoreService);
75
76 // Register the services needed for the test
77 final CodecManager codecService = new CodecManager();
78 codecService.activate();
79 ServiceDirectory testDirectory =
80 new TestServiceDirectory()
81 .add(FlowObjectiveService.class, mockFlowObjectiveService)
82 .add(CodecService.class, codecService)
83 .add(CoreService.class, mockCoreService);
84
85 BaseResource.setServiceDirectory(testDirectory);
86 }
87
88 /**
89 * Cleans up and verifies the mocks.
90 */
91 @After
92 public void tearDownTest() {
93 verify(mockFlowObjectiveService);
94 verify(mockCoreService);
95 }
96
97 /**
98 * Tests creating a filtering objective with POST.
99 */
100 @Test
101 public void testFilteringObjectivePost() {
102 mockFlowObjectiveService.filter(anyObject(), anyObject());
103 prepareService();
104 testObjectiveCreation("post-filter-objective.json", "of:0000000000000001", "filter");
105 }
106
107 /**
108 * Tests creating a forwarding objective with POST.
109 */
110 @Test
111 public void testForwardingObjectivePost() {
112 mockFlowObjectiveService.forward(anyObject(), anyObject());
113 prepareService();
114 testObjectiveCreation("post-forward-objective.json", "of:0000000000000001", "forward");
115 }
116
117 /**
118 * Tests creating a next objective with POST.
119 */
120 @Test
121 public void testNextObjectivePost() {
122 mockFlowObjectiveService.next(anyObject(), anyObject());
123 prepareService();
124 testObjectiveCreation("post-next-objective.json", "of:0000000000000001", "next");
125 }
126
127 /**
128 * Tests obtaining a global unique nextId with GET.
129 */
130 @Test
131 public void testNextId() {
132 expect(mockFlowObjectiveService.allocateNextId()).andReturn(10).anyTimes();
133 prepareService();
134
135 WebResource rs = resource();
136 final String response = rs.path("flowobjectives/next").get(String.class);
137 final JsonObject result = JsonObject.readFrom(response);
138 assertThat(result, notNullValue());
139
140 assertThat(result.names(), hasSize(1));
141 assertThat(result.names().get(0), is("nextId"));
142 final int jsonNextId = result.get("nextId").asInt();
143 assertThat(jsonNextId, is(10));
144 }
145
146 private void prepareService() {
147 expectLastCall();
148 replay(mockFlowObjectiveService);
149 }
150
151 /**
152 * A base class for testing various objective creation.
153 *
154 * @param jsonFile json file path
155 * @param deviceId device id in string format
156 * @param method objective method
157 */
158 private void testObjectiveCreation(String jsonFile, String deviceId, String method) {
159 WebResource rs = resource();
160 InputStream jsonStream = FlowsResourceTest.class
161 .getResourceAsStream(jsonFile);
162
163 StringBuilder sb = new StringBuilder();
164 sb.append("flowobjectives");
165 sb.append("/");
166 sb.append(deviceId);
167 sb.append("/");
168 sb.append(method);
169
170 ClientResponse response = rs.path(sb.toString())
171 .type(MediaType.APPLICATION_JSON_TYPE)
172 .post(ClientResponse.class, jsonStream);
173 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
174 String location = response.getLocation().getPath();
175 assertThat(location, Matchers.startsWith("/" + sb.toString()));
176 }
177}