Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +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.Objects; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | |
| 36 | import org.junit.After; |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.onlab.osgi.ServiceDirectory; |
| 40 | import org.onlab.osgi.TestServiceDirectory; |
| 41 | import org.onlab.rest.BaseResource; |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 42 | import org.onosproject.codec.CodecService; |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 43 | import org.onosproject.vtnrsc.PortPair; |
| 44 | import org.onosproject.vtnrsc.PortPairId; |
| 45 | import org.onosproject.vtnrsc.TenantId; |
| 46 | import org.onosproject.vtnrsc.portpair.PortPairService; |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 47 | import org.onosproject.vtnweb.web.SfcCodecContext; |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 48 | |
| 49 | import com.eclipsesource.json.JsonObject; |
| 50 | import com.sun.jersey.api.client.ClientResponse; |
| 51 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 52 | import com.sun.jersey.api.client.WebResource; |
| 53 | /** |
| 54 | * Unit tests for port pair REST APIs. |
| 55 | */ |
| 56 | public class PortPairResourceTest extends VtnResourceTest { |
| 57 | |
| 58 | final PortPairService portPairService = createMock(PortPairService.class); |
| 59 | |
| 60 | PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 61 | TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); |
| 62 | |
| 63 | final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1", |
| 64 | "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1", |
| 65 | "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); |
| 66 | |
| 67 | /** |
| 68 | * Mock class for a port pair. |
| 69 | */ |
| 70 | private static class MockPortPair implements PortPair { |
| 71 | |
| 72 | private final PortPairId portPairId; |
| 73 | private final TenantId tenantId; |
| 74 | private final String name; |
| 75 | private final String description; |
| 76 | private final String ingress; |
| 77 | private final String egress; |
| 78 | |
| 79 | public MockPortPair(PortPairId portPairId, TenantId tenantId, |
| 80 | String name, String description, |
| 81 | String ingress, String egress) { |
| 82 | |
| 83 | this.portPairId = portPairId; |
| 84 | this.tenantId = tenantId; |
| 85 | this.name = name; |
| 86 | this.description = description; |
| 87 | this.ingress = ingress; |
| 88 | this.egress = egress; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public PortPairId portPairId() { |
| 93 | return portPairId; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public TenantId tenantId() { |
| 98 | return tenantId; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String name() { |
| 103 | return name; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String description() { |
| 108 | return description; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String ingress() { |
| 113 | return ingress; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String egress() { |
| 118 | return egress; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean exactMatch(PortPair portPair) { |
| 123 | return this.equals(portPair) && |
| 124 | Objects.equals(this.portPairId, portPair.portPairId()) && |
| 125 | Objects.equals(this.tenantId, portPair.tenantId()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sets up the global values for all the tests. |
| 131 | */ |
| 132 | @Before |
| 133 | public void setUpTest() { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 134 | |
| 135 | SfcCodecContext context = new SfcCodecContext(); |
| 136 | ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService) |
| 137 | .add(CodecService.class, context.codecManager()); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 138 | BaseResource.setServiceDirectory(testDirectory); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Cleans up. |
| 144 | */ |
| 145 | @After |
| 146 | public void tearDownTest() { |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Tests the result of the rest api GET when there are no port pairs. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testPortPairsEmpty() { |
| 154 | |
| 155 | expect(portPairService.getPortPairs()).andReturn(null).anyTimes(); |
| 156 | replay(portPairService); |
| 157 | final WebResource rs = resource(); |
| 158 | final String response = rs.path("port_pairs").get(String.class); |
| 159 | assertThat(response, is("{\"port_pairs\":[]}")); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests the result of a rest api GET for port pair id. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testGetPortPairId() { |
| 167 | |
| 168 | final Set<PortPair> portPairs = new HashSet<>(); |
| 169 | portPairs.add(portPair1); |
| 170 | |
| 171 | expect(portPairService.exists(anyObject())).andReturn(true).anyTimes(); |
| 172 | expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes(); |
| 173 | replay(portPairService); |
| 174 | |
| 175 | final WebResource rs = resource(); |
| 176 | final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class); |
| 177 | final JsonObject result = JsonObject.readFrom(response); |
| 178 | assertThat(result, notNullValue()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tests that a fetch of a non-existent port pair object throws an exception. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testBadGet() { |
| 186 | expect(portPairService.getPortPair(anyObject())) |
| 187 | .andReturn(null).anyTimes(); |
| 188 | replay(portPairService); |
| 189 | WebResource rs = resource(); |
| 190 | try { |
| 191 | rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); |
| 192 | fail("Fetch of non-existent port pair did not throw an exception"); |
| 193 | } catch (UniformInterfaceException ex) { |
| 194 | assertThat(ex.getMessage(), |
| 195 | containsString("returned a response status of")); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Tests creating a port pair with POST. |
| 201 | */ |
| 202 | @Test |
| 203 | public void testPost() { |
| 204 | |
| 205 | expect(portPairService.createPortPair(anyObject())) |
| 206 | .andReturn(true).anyTimes(); |
| 207 | replay(portPairService); |
| 208 | |
| 209 | WebResource rs = resource(); |
| 210 | InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json"); |
| 211 | |
| 212 | ClientResponse response = rs.path("port_pairs") |
| 213 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 214 | .post(ClientResponse.class, jsonStream); |
| 215 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Tests deleting a port pair. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testDelete() { |
| 223 | expect(portPairService.removePortPair(anyObject())) |
| 224 | .andReturn(true).anyTimes(); |
| 225 | replay(portPairService); |
| 226 | |
| 227 | WebResource rs = resource(); |
| 228 | |
| 229 | String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"; |
| 230 | |
| 231 | ClientResponse deleteResponse = rs.path(location) |
| 232 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 233 | .delete(ClientResponse.class); |
| 234 | assertThat(deleteResponse.getStatus(), |
| 235 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 236 | } |
| 237 | } |