blob: eb99cff3290e49dac469c2db437c9ded1feedaeb [file] [log] [blame]
Jian Lie34f1102016-01-14 10:33:54 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Lie34f1102016-01-14 10:33:54 -08003 *
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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Jian Lie34f1102016-01-14 10:33:54 -080017
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;
Jian Lie34f1102016-01-14 10:33:54 -080026import org.onosproject.codec.CodecService;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.NetTestTools;
30import org.onosproject.net.flowobjective.FlowObjectiveService;
Jian Lie34f1102016-01-14 10:33:54 -080031
Jian Li9d616492016-03-09 10:52:49 -080032import javax.ws.rs.client.Entity;
33import javax.ws.rs.client.WebTarget;
Jian Lie34f1102016-01-14 10:33:54 -080034import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.core.Response;
Jian Lie34f1102016-01-14 10:33:54 -080036import 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
Jian Lie34f1102016-01-14 10:33:54 -080060 /**
61 * Sets up the global values for all the tests.
62 */
63 @Before
64 public void setUpTest() {
65 // Mock Core Service
66 expect(mockCoreService.getAppId(anyShort()))
67 .andReturn(NetTestTools.APP_ID).anyTimes();
68 expect(mockCoreService.registerApplication(REST_APP_ID))
69 .andReturn(APP_ID).anyTimes();
70 replay(mockCoreService);
71
72 // Register the services needed for the test
73 final CodecManager codecService = new CodecManager();
74 codecService.activate();
75 ServiceDirectory testDirectory =
76 new TestServiceDirectory()
77 .add(FlowObjectiveService.class, mockFlowObjectiveService)
78 .add(CodecService.class, codecService)
79 .add(CoreService.class, mockCoreService);
80
Ray Milkey094a1352018-01-22 14:03:54 -080081 setServiceDirectory(testDirectory);
Jian Lie34f1102016-01-14 10:33:54 -080082 }
83
84 /**
85 * Cleans up and verifies the mocks.
86 */
87 @After
88 public void tearDownTest() {
89 verify(mockFlowObjectiveService);
90 verify(mockCoreService);
91 }
92
93 /**
94 * Tests creating a filtering objective with POST.
95 */
96 @Test
97 public void testFilteringObjectivePost() {
98 mockFlowObjectiveService.filter(anyObject(), anyObject());
99 prepareService();
100 testObjectiveCreation("post-filter-objective.json", "of:0000000000000001", "filter");
101 }
102
103 /**
104 * Tests creating a forwarding objective with POST.
105 */
106 @Test
107 public void testForwardingObjectivePost() {
108 mockFlowObjectiveService.forward(anyObject(), anyObject());
109 prepareService();
110 testObjectiveCreation("post-forward-objective.json", "of:0000000000000001", "forward");
111 }
112
113 /**
114 * Tests creating a next objective with POST.
115 */
116 @Test
117 public void testNextObjectivePost() {
118 mockFlowObjectiveService.next(anyObject(), anyObject());
119 prepareService();
120 testObjectiveCreation("post-next-objective.json", "of:0000000000000001", "next");
121 }
122
123 /**
124 * Tests obtaining a global unique nextId with GET.
125 */
126 @Test
127 public void testNextId() {
128 expect(mockFlowObjectiveService.allocateNextId()).andReturn(10).anyTimes();
129 prepareService();
130
Jian Li9d616492016-03-09 10:52:49 -0800131 WebTarget wt = target();
132 final String response = wt.path("flowobjectives/next").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800133 final JsonObject result = Json.parse(response).asObject();
Jian Lie34f1102016-01-14 10:33:54 -0800134 assertThat(result, notNullValue());
135
136 assertThat(result.names(), hasSize(1));
137 assertThat(result.names().get(0), is("nextId"));
138 final int jsonNextId = result.get("nextId").asInt();
139 assertThat(jsonNextId, is(10));
140 }
141
142 private void prepareService() {
143 expectLastCall();
144 replay(mockFlowObjectiveService);
145 }
146
147 /**
148 * A base class for testing various objective creation.
149 *
150 * @param jsonFile json file path
151 * @param deviceId device id in string format
152 * @param method objective method
153 */
154 private void testObjectiveCreation(String jsonFile, String deviceId, String method) {
Jian Li9d616492016-03-09 10:52:49 -0800155 WebTarget wt = target();
Jian Lie34f1102016-01-14 10:33:54 -0800156 InputStream jsonStream = FlowsResourceTest.class
157 .getResourceAsStream(jsonFile);
158
159 StringBuilder sb = new StringBuilder();
160 sb.append("flowobjectives");
161 sb.append("/");
162 sb.append(deviceId);
163 sb.append("/");
164 sb.append(method);
165
Jian Li9d616492016-03-09 10:52:49 -0800166 Response response = wt.path(sb.toString())
167 .request(MediaType.APPLICATION_JSON_TYPE)
168 .post(Entity.json(jsonStream));
Jian Lie34f1102016-01-14 10:33:54 -0800169 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
170 String location = response.getLocation().getPath();
171 assertThat(location, Matchers.startsWith("/" + sb.toString()));
172 }
173}