Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.rest; |
| 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; |
| 20 | import com.sun.jersey.api.client.ClientResponse; |
| 21 | import com.sun.jersey.api.client.WebResource; |
| 22 | import org.hamcrest.Matchers; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.osgi.ServiceDirectory; |
| 27 | import org.onlab.osgi.TestServiceDirectory; |
| 28 | import org.onlab.rest.BaseResource; |
| 29 | import org.onosproject.codec.CodecService; |
| 30 | import org.onosproject.codec.impl.CodecManager; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.net.NetTestTools; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 34 | import org.onosproject.rest.resources.CoreWebApplication; |
| 35 | |
| 36 | import javax.ws.rs.core.MediaType; |
| 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 | |
| 61 | public FlowObjectiveResourceTest() { |
| 62 | super(CoreWebApplication.class); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets up the global values for all the tests. |
| 67 | */ |
| 68 | @Before |
| 69 | public void setUpTest() { |
| 70 | // Mock Core Service |
| 71 | expect(mockCoreService.getAppId(anyShort())) |
| 72 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
| 73 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 74 | .andReturn(APP_ID).anyTimes(); |
| 75 | replay(mockCoreService); |
| 76 | |
| 77 | // Register the services needed for the test |
| 78 | final CodecManager codecService = new CodecManager(); |
| 79 | codecService.activate(); |
| 80 | ServiceDirectory testDirectory = |
| 81 | new TestServiceDirectory() |
| 82 | .add(FlowObjectiveService.class, mockFlowObjectiveService) |
| 83 | .add(CodecService.class, codecService) |
| 84 | .add(CoreService.class, mockCoreService); |
| 85 | |
| 86 | BaseResource.setServiceDirectory(testDirectory); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Cleans up and verifies the mocks. |
| 91 | */ |
| 92 | @After |
| 93 | public void tearDownTest() { |
| 94 | verify(mockFlowObjectiveService); |
| 95 | verify(mockCoreService); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests creating a filtering objective with POST. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testFilteringObjectivePost() { |
| 103 | mockFlowObjectiveService.filter(anyObject(), anyObject()); |
| 104 | prepareService(); |
| 105 | testObjectiveCreation("post-filter-objective.json", "of:0000000000000001", "filter"); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests creating a forwarding objective with POST. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testForwardingObjectivePost() { |
| 113 | mockFlowObjectiveService.forward(anyObject(), anyObject()); |
| 114 | prepareService(); |
| 115 | testObjectiveCreation("post-forward-objective.json", "of:0000000000000001", "forward"); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests creating a next objective with POST. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testNextObjectivePost() { |
| 123 | mockFlowObjectiveService.next(anyObject(), anyObject()); |
| 124 | prepareService(); |
| 125 | testObjectiveCreation("post-next-objective.json", "of:0000000000000001", "next"); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Tests obtaining a global unique nextId with GET. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testNextId() { |
| 133 | expect(mockFlowObjectiveService.allocateNextId()).andReturn(10).anyTimes(); |
| 134 | prepareService(); |
| 135 | |
| 136 | WebResource rs = resource(); |
| 137 | final String response = rs.path("flowobjectives/next").get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 138 | final JsonObject result = Json.parse(response).asObject(); |
Jian Li | e34f110 | 2016-01-14 10:33:54 -0800 | [diff] [blame] | 139 | assertThat(result, notNullValue()); |
| 140 | |
| 141 | assertThat(result.names(), hasSize(1)); |
| 142 | assertThat(result.names().get(0), is("nextId")); |
| 143 | final int jsonNextId = result.get("nextId").asInt(); |
| 144 | assertThat(jsonNextId, is(10)); |
| 145 | } |
| 146 | |
| 147 | private void prepareService() { |
| 148 | expectLastCall(); |
| 149 | replay(mockFlowObjectiveService); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * A base class for testing various objective creation. |
| 154 | * |
| 155 | * @param jsonFile json file path |
| 156 | * @param deviceId device id in string format |
| 157 | * @param method objective method |
| 158 | */ |
| 159 | private void testObjectiveCreation(String jsonFile, String deviceId, String method) { |
| 160 | WebResource rs = resource(); |
| 161 | InputStream jsonStream = FlowsResourceTest.class |
| 162 | .getResourceAsStream(jsonFile); |
| 163 | |
| 164 | StringBuilder sb = new StringBuilder(); |
| 165 | sb.append("flowobjectives"); |
| 166 | sb.append("/"); |
| 167 | sb.append(deviceId); |
| 168 | sb.append("/"); |
| 169 | sb.append(method); |
| 170 | |
| 171 | ClientResponse response = rs.path(sb.toString()) |
| 172 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 173 | .post(ClientResponse.class, jsonStream); |
| 174 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 175 | String location = response.getLocation().getPath(); |
| 176 | assertThat(location, Matchers.startsWith("/" + sb.toString())); |
| 177 | } |
| 178 | } |