Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonObject; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 20 | import org.hamcrest.Matchers; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.osgi.ServiceDirectory; |
| 25 | import org.onlab.osgi.TestServiceDirectory; |
| 26 | import org.onlab.rest.BaseResource; |
| 27 | import org.onosproject.codec.CodecService; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.net.NetTestTools; |
| 31 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 32 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 33 | import javax.ws.rs.client.Entity; |
| 34 | import javax.ws.rs.client.WebTarget; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 35 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 36 | import javax.ws.rs.core.Response; |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 37 | import java.io.InputStream; |
| 38 | import java.net.HttpURLConnection; |
| 39 | |
| 40 | import static org.easymock.EasyMock.anyObject; |
| 41 | import static org.easymock.EasyMock.anyShort; |
| 42 | import static org.easymock.EasyMock.createMock; |
| 43 | import static org.easymock.EasyMock.expect; |
| 44 | import static org.easymock.EasyMock.expectLastCall; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.easymock.EasyMock.verify; |
| 47 | import static org.hamcrest.Matchers.hasSize; |
| 48 | import static org.hamcrest.Matchers.is; |
| 49 | import static org.hamcrest.Matchers.notNullValue; |
| 50 | import static org.junit.Assert.assertThat; |
| 51 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for flow objectives REST APIs. |
| 55 | */ |
| 56 | public 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 Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 61 | /** |
| 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 132 | WebTarget wt = target(); |
| 133 | final String response = wt.path("flowobjectives/next").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 134 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 135 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 156 | WebTarget wt = target(); |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 157 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 167 | Response response = wt.path(sb.toString()) |
| 168 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 169 | .post(Entity.json(jsonStream)); |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 170 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 171 | String location = response.getLocation().getPath(); |
| 172 | assertThat(location, Matchers.startsWith("/" + sb.toString())); |
| 173 | } |
| 174 | } |