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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
| 19 | import com.eclipsesource.json.JsonObject; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Lists; |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.osgi.ServiceDirectory; |
| 26 | import org.onlab.osgi.TestServiceDirectory; |
| 27 | import org.onlab.rest.BaseResource; |
| 28 | import org.onosproject.codec.CodecService; |
| 29 | import org.onosproject.vtnrsc.PortPairGroup; |
| 30 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 31 | import org.onosproject.vtnrsc.PortPairId; |
| 32 | import org.onosproject.vtnrsc.TenantId; |
| 33 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
| 34 | import org.onosproject.vtnweb.web.SfcCodecContext; |
| 35 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 36 | import javax.ws.rs.NotFoundException; |
| 37 | import javax.ws.rs.client.Entity; |
| 38 | import javax.ws.rs.client.WebTarget; |
| 39 | import javax.ws.rs.core.MediaType; |
| 40 | import javax.ws.rs.core.Response; |
| 41 | import java.io.InputStream; |
| 42 | import java.net.HttpURLConnection; |
| 43 | import java.util.HashSet; |
| 44 | import java.util.List; |
| 45 | import java.util.Objects; |
| 46 | import java.util.Set; |
| 47 | |
| 48 | import static org.easymock.EasyMock.anyObject; |
| 49 | import static org.easymock.EasyMock.createMock; |
| 50 | import static org.easymock.EasyMock.expect; |
| 51 | import static org.easymock.EasyMock.replay; |
| 52 | import static org.hamcrest.Matchers.containsString; |
| 53 | import static org.hamcrest.Matchers.is; |
| 54 | import static org.hamcrest.Matchers.notNullValue; |
| 55 | import static org.junit.Assert.assertThat; |
| 56 | import static org.junit.Assert.fail; |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 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 | } |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 124 | |
| 125 | @Override |
| 126 | public void addLoad(PortPairId portPairId) { |
| 127 | // TODO Auto-generated method stub |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int getLoad(PortPairId portPairId) { |
| 132 | // TODO Auto-generated method stub |
| 133 | return 0; |
| 134 | } |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets up the global values for all the tests. |
| 139 | */ |
| 140 | @Before |
| 141 | public void setUpTest() { |
| 142 | SfcCodecContext context = new SfcCodecContext(); |
| 143 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 144 | .add(PortPairGroupService.class, portPairGroupService) |
| 145 | .add(CodecService.class, context.codecManager()); |
| 146 | BaseResource.setServiceDirectory(testDirectory); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Cleans up. |
| 152 | */ |
| 153 | @After |
| 154 | public void tearDownTest() { |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Tests the result of the rest api GET when there are no port pair groups. |
| 159 | */ |
| 160 | @Test |
| 161 | public void testPortPairGroupsEmpty() { |
| 162 | |
| 163 | expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes(); |
| 164 | replay(portPairGroupService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 165 | final WebTarget wt = target(); |
| 166 | final String response = wt.path("port_pair_groups").request().get(String.class); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 167 | assertThat(response, is("{\"port_pair_groups\":[]}")); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests the result of a rest api GET for port pair group id. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testGetPortPairGroupId() { |
| 175 | |
| 176 | final Set<PortPairGroup> portPairGroups = new HashSet<>(); |
| 177 | portPairGroups.add(portPairGroup1); |
| 178 | |
| 179 | expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes(); |
| 180 | expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes(); |
| 181 | replay(portPairGroupService); |
| 182 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 183 | final WebTarget wt = target(); |
| 184 | final String response = wt.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1") |
| 185 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 186 | final JsonObject result = Json.parse(response).asObject(); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 187 | assertThat(result, notNullValue()); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Tests that a fetch of a non-existent port pair group object throws an exception. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testBadGet() { |
| 195 | expect(portPairGroupService.getPortPairGroup(anyObject())) |
| 196 | .andReturn(null).anyTimes(); |
| 197 | replay(portPairGroupService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 198 | WebTarget wt = target(); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 199 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 200 | wt.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae") |
| 201 | .request().get(String.class); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 202 | fail("Fetch of non-existent port pair group did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 203 | } catch (NotFoundException ex) { |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 204 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 205 | containsString("HTTP 404 Not Found")); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests creating a port pair group with POST. |
| 211 | */ |
| 212 | @Test |
| 213 | public void testPost() { |
| 214 | |
| 215 | expect(portPairGroupService.createPortPairGroup(anyObject())) |
| 216 | .andReturn(true).anyTimes(); |
| 217 | replay(portPairGroupService); |
| 218 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 219 | WebTarget wt = target(); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 220 | InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json"); |
| 221 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 222 | Response response = wt.path("port_pair_groups") |
| 223 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 224 | .post(Entity.json(jsonStream)); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 225 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests deleting a port pair group. |
| 230 | */ |
| 231 | @Test |
| 232 | public void testDelete() { |
| 233 | expect(portPairGroupService.removePortPairGroup(anyObject())) |
| 234 | .andReturn(true).anyTimes(); |
| 235 | replay(portPairGroupService); |
| 236 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 237 | WebTarget wt = target(); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 238 | |
| 239 | String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1"; |
| 240 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 241 | Response deleteResponse = wt.path(location) |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame^] | 242 | .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 243 | .delete(); |
Phaneendra Manda | d44649e8 | 2015-12-01 21:11:57 +0530 | [diff] [blame] | 244 | assertThat(deleteResponse.getStatus(), |
| 245 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 246 | } |
| 247 | } |