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