blob: 4ea3cd39ca3a56b5ac628bba0ba3497a543c1b64 [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
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Lie34f1102016-01-14 10:33:54 -080019import com.eclipsesource.json.JsonObject;
Jian Lie34f1102016-01-14 10:33:54 -080020import org.hamcrest.Matchers;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.osgi.TestServiceDirectory;
26import org.onlab.rest.BaseResource;
27import org.onosproject.codec.CodecService;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.core.CoreService;
30import org.onosproject.net.NetTestTools;
31import org.onosproject.net.flowobjective.FlowObjectiveService;
Jian Lie34f1102016-01-14 10:33:54 -080032
Jian Li9d616492016-03-09 10:52:49 -080033import javax.ws.rs.client.Entity;
34import javax.ws.rs.client.WebTarget;
Jian Lie34f1102016-01-14 10:33:54 -080035import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080036import javax.ws.rs.core.Response;
Jian Lie34f1102016-01-14 10:33:54 -080037import java.io.InputStream;
38import java.net.HttpURLConnection;
39
40import static org.easymock.EasyMock.anyObject;
41import static org.easymock.EasyMock.anyShort;
42import static org.easymock.EasyMock.createMock;
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.expectLastCall;
45import static org.easymock.EasyMock.replay;
46import static org.easymock.EasyMock.verify;
47import static org.hamcrest.Matchers.hasSize;
48import static org.hamcrest.Matchers.is;
49import static org.hamcrest.Matchers.notNullValue;
50import static org.junit.Assert.assertThat;
51import static org.onosproject.net.NetTestTools.APP_ID;
52
53/**
54 * Unit tests for flow objectives REST APIs.
55 */
56public class FlowObjectiveResourceTest extends ResourceTest {
57 final FlowObjectiveService mockFlowObjectiveService = createMock(FlowObjectiveService.class);
58 CoreService mockCoreService = createMock(CoreService.class);
59 public static final String REST_APP_ID = "org.onosproject.rest";
60
Jian Lie34f1102016-01-14 10:33:54 -080061 /**
62 * Sets up the global values for all the tests.
63 */
64 @Before
65 public void setUpTest() {
66 // Mock Core Service
67 expect(mockCoreService.getAppId(anyShort()))
68 .andReturn(NetTestTools.APP_ID).anyTimes();
69 expect(mockCoreService.registerApplication(REST_APP_ID))
70 .andReturn(APP_ID).anyTimes();
71 replay(mockCoreService);
72
73 // Register the services needed for the test
74 final CodecManager codecService = new CodecManager();
75 codecService.activate();
76 ServiceDirectory testDirectory =
77 new TestServiceDirectory()
78 .add(FlowObjectiveService.class, mockFlowObjectiveService)
79 .add(CodecService.class, codecService)
80 .add(CoreService.class, mockCoreService);
81
82 BaseResource.setServiceDirectory(testDirectory);
83 }
84
85 /**
86 * Cleans up and verifies the mocks.
87 */
88 @After
89 public void tearDownTest() {
90 verify(mockFlowObjectiveService);
91 verify(mockCoreService);
92 }
93
94 /**
95 * Tests creating a filtering objective with POST.
96 */
97 @Test
98 public void testFilteringObjectivePost() {
99 mockFlowObjectiveService.filter(anyObject(), anyObject());
100 prepareService();
101 testObjectiveCreation("post-filter-objective.json", "of:0000000000000001", "filter");
102 }
103
104 /**
105 * Tests creating a forwarding objective with POST.
106 */
107 @Test
108 public void testForwardingObjectivePost() {
109 mockFlowObjectiveService.forward(anyObject(), anyObject());
110 prepareService();
111 testObjectiveCreation("post-forward-objective.json", "of:0000000000000001", "forward");
112 }
113
114 /**
115 * Tests creating a next objective with POST.
116 */
117 @Test
118 public void testNextObjectivePost() {
119 mockFlowObjectiveService.next(anyObject(), anyObject());
120 prepareService();
121 testObjectiveCreation("post-next-objective.json", "of:0000000000000001", "next");
122 }
123
124 /**
125 * Tests obtaining a global unique nextId with GET.
126 */
127 @Test
128 public void testNextId() {
129 expect(mockFlowObjectiveService.allocateNextId()).andReturn(10).anyTimes();
130 prepareService();
131
Jian Li9d616492016-03-09 10:52:49 -0800132 WebTarget wt = target();
133 final String response = wt.path("flowobjectives/next").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800134 final JsonObject result = Json.parse(response).asObject();
Jian Lie34f1102016-01-14 10:33:54 -0800135 assertThat(result, notNullValue());
136
137 assertThat(result.names(), hasSize(1));
138 assertThat(result.names().get(0), is("nextId"));
139 final int jsonNextId = result.get("nextId").asInt();
140 assertThat(jsonNextId, is(10));
141 }
142
143 private void prepareService() {
144 expectLastCall();
145 replay(mockFlowObjectiveService);
146 }
147
148 /**
149 * A base class for testing various objective creation.
150 *
151 * @param jsonFile json file path
152 * @param deviceId device id in string format
153 * @param method objective method
154 */
155 private void testObjectiveCreation(String jsonFile, String deviceId, String method) {
Jian Li9d616492016-03-09 10:52:49 -0800156 WebTarget wt = target();
Jian Lie34f1102016-01-14 10:33:54 -0800157 InputStream jsonStream = FlowsResourceTest.class
158 .getResourceAsStream(jsonFile);
159
160 StringBuilder sb = new StringBuilder();
161 sb.append("flowobjectives");
162 sb.append("/");
163 sb.append(deviceId);
164 sb.append("/");
165 sb.append(method);
166
Jian Li9d616492016-03-09 10:52:49 -0800167 Response response = wt.path(sb.toString())
168 .request(MediaType.APPLICATION_JSON_TYPE)
169 .post(Entity.json(jsonStream));
Jian Lie34f1102016-01-14 10:33:54 -0800170 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
171 String location = response.getLocation().getPath();
172 assertThat(location, Matchers.startsWith("/" + sb.toString()));
173 }
174}