SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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.vtnweb.resources; |
| 17 | |
| 18 | import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.hamcrest.Matchers.containsString; |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.hamcrest.Matchers.notNullValue; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.junit.Assert.fail; |
| 27 | |
| 28 | import java.io.InputStream; |
| 29 | import java.net.HttpURLConnection; |
| 30 | import java.util.HashSet; |
| 31 | import java.util.List; |
| 32 | import java.util.Objects; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame^] | 37 | import com.eclipsesource.json.Json; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 38 | import org.junit.After; |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.onlab.osgi.ServiceDirectory; |
| 42 | import org.onlab.osgi.TestServiceDirectory; |
| 43 | import org.onlab.rest.BaseResource; |
| 44 | import org.onosproject.codec.CodecService; |
| 45 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 46 | import org.onosproject.vtnrsc.PortChain; |
| 47 | import org.onosproject.vtnrsc.PortChainId; |
| 48 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 49 | import org.onosproject.vtnrsc.TenantId; |
| 50 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 51 | import org.onosproject.vtnweb.web.SfcCodecContext; |
| 52 | |
| 53 | import com.eclipsesource.json.JsonObject; |
| 54 | import com.google.common.collect.ImmutableList; |
| 55 | import com.google.common.collect.Lists; |
| 56 | import com.sun.jersey.api.client.ClientResponse; |
| 57 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 58 | import com.sun.jersey.api.client.WebResource; |
| 59 | |
| 60 | /** |
| 61 | * Unit tests for port chain REST APIs. |
| 62 | */ |
| 63 | public class PortChainResourceTest extends VtnResourceTest { |
| 64 | |
| 65 | final PortChainService portChainService = createMock(PortChainService.class); |
| 66 | |
| 67 | PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 68 | TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); |
| 69 | private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList(); |
| 70 | private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList(); |
| 71 | |
| 72 | |
| 73 | final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1", |
| 74 | "Mock port chain", portPairGroupList1, |
| 75 | flowClassifierList1); |
| 76 | |
| 77 | /** |
| 78 | * Mock class for a port chain. |
| 79 | */ |
| 80 | private static class MockPortChain implements PortChain { |
| 81 | |
| 82 | private final PortChainId portChainId; |
| 83 | private final TenantId tenantId; |
| 84 | private final String name; |
| 85 | private final String description; |
| 86 | private final List<PortPairGroupId> portPairGroupList; |
| 87 | private final List<FlowClassifierId> flowClassifierList; |
| 88 | |
| 89 | public MockPortChain(PortChainId portChainId, TenantId tenantId, |
| 90 | String name, String description, |
| 91 | List<PortPairGroupId> portPairGroupList, |
| 92 | List<FlowClassifierId> flowClassifierList) { |
| 93 | |
| 94 | this.portChainId = portChainId; |
| 95 | this.tenantId = tenantId; |
| 96 | this.name = name; |
| 97 | this.description = description; |
| 98 | this.portPairGroupList = portPairGroupList; |
| 99 | this.flowClassifierList = flowClassifierList; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public PortChainId portChainId() { |
| 104 | return portChainId; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public TenantId tenantId() { |
| 109 | return tenantId; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String name() { |
| 114 | return name; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String description() { |
| 119 | return description; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public List<PortPairGroupId> portPairGroups() { |
| 124 | return ImmutableList.copyOf(portPairGroupList); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public List<FlowClassifierId> flowClassifiers() { |
| 129 | return ImmutableList.copyOf(flowClassifierList); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean exactMatch(PortChain portChain) { |
| 134 | return this.equals(portChain) && |
| 135 | Objects.equals(this.portChainId, portChain.portChainId()) && |
| 136 | Objects.equals(this.tenantId, portChain.tenantId()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets up the global values for all the tests. |
| 142 | */ |
| 143 | @Before |
| 144 | public void setUpTest() { |
| 145 | SfcCodecContext context = new SfcCodecContext(); |
| 146 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 147 | .add(PortChainService.class, portChainService) |
| 148 | .add(CodecService.class, context.codecManager()); |
| 149 | BaseResource.setServiceDirectory(testDirectory); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Cleans up. |
| 155 | */ |
| 156 | @After |
| 157 | public void tearDownTest() { |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests the result of the rest api GET when there are no port chains. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testPortChainsEmpty() { |
| 165 | |
| 166 | expect(portChainService.getPortChains()).andReturn(null).anyTimes(); |
| 167 | replay(portChainService); |
| 168 | final WebResource rs = resource(); |
| 169 | final String response = rs.path("port_chains").get(String.class); |
| 170 | assertThat(response, is("{\"port_chains\":[]}")); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Tests the result of a rest api GET for port chain id. |
| 175 | */ |
| 176 | @Test |
| 177 | public void testGetPortChainId() { |
| 178 | |
| 179 | final Set<PortChain> portChains = new HashSet<>(); |
| 180 | portChains.add(portChain1); |
| 181 | |
| 182 | expect(portChainService.exists(anyObject())).andReturn(true).anyTimes(); |
| 183 | expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes(); |
| 184 | replay(portChainService); |
| 185 | |
| 186 | final WebResource rs = resource(); |
| 187 | final String response = rs.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751").get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame^] | 188 | final JsonObject result = Json.parse(response).asObject(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 189 | assertThat(result, notNullValue()); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Tests that a fetch of a non-existent port chain object throws an exception. |
| 194 | */ |
| 195 | @Test |
| 196 | public void testBadGet() { |
| 197 | expect(portChainService.getPortChain(anyObject())) |
| 198 | .andReturn(null).anyTimes(); |
| 199 | replay(portChainService); |
| 200 | WebResource rs = resource(); |
| 201 | try { |
| 202 | rs.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); |
| 203 | fail("Fetch of non-existent port chain did not throw an exception"); |
| 204 | } catch (UniformInterfaceException ex) { |
| 205 | assertThat(ex.getMessage(), |
| 206 | containsString("returned a response status of")); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Tests creating a port chain with POST. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testPost() { |
| 215 | |
| 216 | expect(portChainService.createPortChain(anyObject())) |
| 217 | .andReturn(true).anyTimes(); |
| 218 | replay(portChainService); |
| 219 | |
| 220 | WebResource rs = resource(); |
| 221 | InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json"); |
| 222 | |
| 223 | ClientResponse response = rs.path("port_chains") |
| 224 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 225 | .post(ClientResponse.class, jsonStream); |
| 226 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Tests deleting a port chain. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testDelete() { |
| 234 | expect(portChainService.removePortChain(anyObject())) |
| 235 | .andReturn(true).anyTimes(); |
| 236 | replay(portChainService); |
| 237 | |
| 238 | WebResource rs = resource(); |
| 239 | |
| 240 | String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751"; |
| 241 | |
| 242 | ClientResponse deleteResponse = rs.path(location) |
| 243 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 244 | .delete(ClientResponse.class); |
| 245 | assertThat(deleteResponse.getStatus(), |
| 246 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 247 | } |
| 248 | } |