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