Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.pce.web; |
| 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 javax.ws.rs.NotFoundException; |
| 29 | //import javax.ws.rs.client.Entity; |
| 30 | //import javax.ws.rs.client.WebTarget; |
| 31 | //import javax.ws.rs.core.MediaType; |
| 32 | //import javax.ws.rs.core.Response; |
| 33 | //import java.io.InputStream; |
| 34 | //import java.net.HttpURLConnection; |
| 35 | //import java.util.HashSet; |
| 36 | //import java.util.List; |
| 37 | //import java.util.Objects; |
| 38 | //import java.util.Optional; |
| 39 | //import java.util.Set; |
| 40 | |
| 41 | //import com.eclipsesource.json.Json; |
| 42 | //import com.eclipsesource.json.JsonObject; |
| 43 | //import com.google.common.collect.ImmutableList; |
| 44 | //import com.google.common.collect.Lists; |
| 45 | |
| 46 | import org.junit.After; |
| 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.onlab.osgi.ServiceDirectory; |
| 50 | import org.onlab.osgi.TestServiceDirectory; |
| 51 | import org.onlab.rest.BaseResource; |
| 52 | import org.onosproject.codec.CodecService; |
| 53 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 54 | import org.onosproject.net.intent.Constraint; |
| 55 | import org.onosproject.pce.pceservice.PcePath; |
| 56 | import org.onosproject.pce.pceservice.LspType; |
| 57 | import org.onosproject.pce.pceservice.api.PceService; |
| 58 | |
| 59 | /** |
| 60 | * Unit tests for pce path REST APIs. |
| 61 | */ |
| 62 | public class PcePathResourceTest extends PceResourceTest { |
| 63 | final PceService pceService = createMock(PceService.class); |
| 64 | final TunnelId pcePathId1 = TunnelId.valueOf("1"); |
| 65 | //TODO: will be uncommented below lines once CostConstraint and LocalBandwidthConstraint classes are ready |
| 66 | final Constraint costConstraint = null; //CostConstraint.of("2"); |
| 67 | final Constraint bandwidthConstraint = null; //LocalBandwidthConstraint.of("200.0"); |
| 68 | final LspType lspType = LspType.WITH_SIGNALLING; |
| 69 | final MockPcePath pcePath1 = new MockPcePath(pcePathId1, "11.0.0.1", "11.0.0.2", lspType, "pcc2", |
| 70 | costConstraint, bandwidthConstraint); |
| 71 | |
| 72 | /** |
| 73 | * Mock class for a pce path. |
| 74 | */ |
| 75 | private static class MockPcePath implements PcePath { |
| 76 | private TunnelId id; |
| 77 | private String source; |
| 78 | private String destination; |
| 79 | private LspType lspType; |
| 80 | private String name; |
| 81 | private Constraint costConstraint; |
| 82 | private Constraint bandwidthConstraint; |
| 83 | |
| 84 | /** |
| 85 | * Constructor to initialize member variables. |
| 86 | * |
| 87 | * @param id pce path id |
| 88 | * @param src source device |
| 89 | * @param dst destination device |
| 90 | * @param type lsp type |
| 91 | * @param name symbolic path name |
| 92 | * @param constrnt pce constraint |
| 93 | */ |
| 94 | public MockPcePath(TunnelId id, String src, String dst, LspType type, String name, |
| 95 | Constraint costConstrnt, Constraint bandwidthConstrnt) { |
| 96 | this.id = id; |
| 97 | this.source = src; |
| 98 | this.destination = dst; |
| 99 | this.name = name; |
| 100 | this.lspType = type; |
| 101 | this.costConstraint = costConstrnt; |
| 102 | this.bandwidthConstraint = bandwidthConstrnt; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public TunnelId id() { |
| 107 | return id; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void id(TunnelId id) { |
| 112 | this.id = id; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String source() { |
| 117 | return source; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void source(String src) { |
| 122 | this.source = src; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String destination() { |
| 127 | return destination; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void destination(String dst) { |
| 132 | this.destination = dst; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public LspType lspType() { |
| 137 | return lspType; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String name() { |
| 142 | return name; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Constraint costConstraint() { |
| 147 | return costConstraint; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Constraint bandwidthConstraint() { |
| 152 | return bandwidthConstraint; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public PcePath copy(PcePath path) { |
| 157 | if (null != path.source()) { |
| 158 | this.source = path.source(); |
| 159 | } |
| 160 | if (null != path.destination()) { |
| 161 | this.destination = path.destination(); |
| 162 | } |
| 163 | if (this.lspType != path.lspType()) { |
| 164 | this.lspType = path.lspType(); |
| 165 | } |
| 166 | if (null != path.name()) { |
| 167 | this.name = path.name(); |
| 168 | } |
| 169 | if (null != path.costConstraint()) { |
| 170 | this.costConstraint = path.costConstraint(); |
| 171 | } |
| 172 | if (null != path.bandwidthConstraint()) { |
| 173 | this.bandwidthConstraint = path.bandwidthConstraint(); |
| 174 | } |
| 175 | return this; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets up the global values for all the tests. |
| 181 | */ |
| 182 | @Before |
| 183 | public void setUpTest() { |
| 184 | MockPceCodecContext context = new MockPceCodecContext(); |
| 185 | ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService) |
| 186 | .add(CodecService.class, context.codecManager()); |
| 187 | BaseResource.setServiceDirectory(testDirectory); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Cleans up. |
| 192 | */ |
| 193 | @After |
| 194 | public void tearDownTest() { |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests the result of the rest api GET when there are no pce paths. |
| 199 | */ |
| 200 | @Test |
| 201 | public void testPcePathsEmpty() { |
| 202 | //TODO: will be uncommented below code once PceService is ready |
| 203 | //expect(pceService.queryAllPath()).andReturn(null).anyTimes(); |
| 204 | //replay(pceService); |
| 205 | //final WebTarget wt = target(); |
| 206 | //final String response = wt.path("path").request().get(String.class); |
| 207 | //assertThat(response, is("{\"paths\":[]}")); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Tests the result of a rest api GET for pce path id. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testGetTunnelId() { |
| 215 | //TODO: will be uncommented below code once PceService is ready |
| 216 | //final Set<PcePath> pcePaths = new HashSet<>(); |
| 217 | //pcePaths.add(pcePath1); |
| 218 | |
| 219 | //expect(pceService.queryPath(anyObject())).andReturn(pcePath1).anyTimes(); |
| 220 | //replay(pceService); |
| 221 | |
| 222 | //final WebTarget wt = target(); |
| 223 | //final String response = wt.path("path/1").request().get(String.class); |
| 224 | //final JsonObject result = Json.parse(response).asObject(); |
| 225 | //assertThat(result, notNullValue()); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests that a fetch of a non-existent pce path object throws an exception. |
| 230 | */ |
| 231 | @Test |
| 232 | public void testBadGet() { |
| 233 | //TODO: will be uncommented below code once PceService is ready |
| 234 | //expect(pceService.queryPath(anyObject())) |
| 235 | // .andReturn(null).anyTimes(); |
| 236 | //replay(pceService); |
| 237 | |
| 238 | //WebTarget wt = target(); |
| 239 | //try { |
| 240 | // wt.path("path/1").request().get(String.class); |
| 241 | // fail("Fetch of non-existent pce path did not throw an exception"); |
| 242 | //} catch (NotFoundException ex) { |
| 243 | // assertThat(ex.getMessage(), |
| 244 | // containsString("HTTP 404 Not Found")); |
| 245 | //} |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests creating a pce path with POST. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testPost() { |
| 253 | //TODO: will be uncommented below code once PceService is ready |
| 254 | //expect(pceService.setupPath(anyObject())) |
| 255 | // .andReturn(true).anyTimes(); |
| 256 | //replay(pceService); |
| 257 | |
| 258 | //WebTarget wt = target(); |
| 259 | //InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json"); |
| 260 | |
| 261 | //Response response = wt.path("path") |
| 262 | // .request(MediaType.APPLICATION_JSON_TYPE) |
| 263 | // .post(Entity.json(jsonStream)); |
| 264 | //assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Tests creating a pce path with PUT. |
| 269 | */ |
| 270 | @Test |
| 271 | public void testPut() { |
| 272 | //TODO: will be uncommented below code once PceService is ready |
| 273 | //expect(pceService.updatePath(anyObject())) |
| 274 | // .andReturn(true).anyTimes(); |
| 275 | //replay(pceService); |
| 276 | |
| 277 | //WebTarget wt = target(); |
| 278 | //InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json"); |
| 279 | |
| 280 | //Response response = wt.path("path/1") |
| 281 | // .request(MediaType.APPLICATION_JSON_TYPE) |
| 282 | // .put(Entity.json(jsonStream)); |
| 283 | //assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Tests deleting a pce path. |
| 288 | */ |
| 289 | @Test |
| 290 | public void testDelete() { |
| 291 | //TODO: will be uncommented below code once PceService is ready |
| 292 | //expect(pceService.releasePath(anyObject())) |
| 293 | // .andReturn(true).anyTimes(); |
| 294 | //replay(pceService); |
| 295 | |
| 296 | //WebTarget wt = target(); |
| 297 | |
| 298 | //String location = "path/1"; |
| 299 | |
| 300 | //Response deleteResponse = wt.path(location) |
| 301 | // .request(MediaType.APPLICATION_JSON_TYPE) |
| 302 | // .delete(); |
| 303 | //assertThat(deleteResponse.getStatus(), |
| 304 | // is(HttpURLConnection.HTTP_OK)); |
| 305 | } |
| 306 | } |