Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 1 | /* |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +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 | */ |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 16 | package org.onosproject.pcerest; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.notNullValue; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 27 | |
| 28 | import org.onlab.util.DataRateUnit; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 29 | import org.onosproject.codec.JsonCodec; |
| 30 | import org.onosproject.pce.pceservice.PcePath; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 31 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 32 | import org.onosproject.net.intent.Constraint; |
| 33 | import org.onosproject.pce.pceservice.constraint.CostConstraint; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 34 | |
| 35 | import com.fasterxml.jackson.databind.JsonNode; |
| 36 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 37 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | |
| 39 | /** |
| 40 | * PCE path codec unit tests. |
| 41 | */ |
| 42 | public class PcePathCodecTest { |
| 43 | |
| 44 | MockPceCodecContext context; |
| 45 | JsonCodec<PcePath> pcePathCodec; |
| 46 | /** |
| 47 | * Sets up for each test. Creates a context and fetches the PCE path codec. |
| 48 | */ |
| 49 | @Before |
| 50 | public void setUp() { |
| 51 | context = new MockPceCodecContext(); |
| 52 | pcePathCodec = context.codec(PcePath.class); |
| 53 | assertThat(pcePathCodec, notNullValue()); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Reads in a pce-path from the given resource and decodes it. |
| 58 | * |
| 59 | * @param resourceName resource to use to read the json for the pce-path |
| 60 | * @return decoded pce-path |
| 61 | * @throws IOException if processing the resource fails |
| 62 | */ |
| 63 | private PcePath getPcePath(String resourceName) throws IOException { |
| 64 | InputStream jsonStream = PcePathCodecTest.class |
| 65 | .getResourceAsStream(resourceName); |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | JsonNode json = mapper.readTree(jsonStream); |
| 68 | assertThat(json, notNullValue()); |
| 69 | PcePath pcePath = pcePathCodec.decode((ObjectNode) json, context); |
| 70 | assertThat(pcePath, notNullValue()); |
| 71 | return pcePath; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks that a simple pce-path is decoded properly. |
| 76 | * |
| 77 | * @throws IOException if the resource cannot be processed |
| 78 | */ |
| 79 | @Test |
| 80 | public void codecPcePathTest() throws IOException { |
| 81 | |
| 82 | PcePath pcePath = getPcePath("pcePath.json"); |
| 83 | |
| 84 | assertThat(pcePath, notNullValue()); |
| 85 | |
| 86 | assertThat(pcePath.source().toString(), is("11.0.0.1")); |
| 87 | assertThat(pcePath.destination(), is("11.0.0.2")); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 88 | assertThat(pcePath.lspType().toString(), is("WITHOUT_SIGNALLING_AND_WITHOUT_SR")); |
| 89 | // testing cost type |
| 90 | String cost = "2"; |
| 91 | Constraint costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]); |
| 92 | assertThat(pcePath.costConstraint(), is(costConstraint)); |
| 93 | // testing bandwidth |
| 94 | String bandwidth = "200"; |
| 95 | Constraint bandwidthConstraint = BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit |
| 96 | .valueOf("BPS")); |
| 97 | assertThat(pcePath.bandwidthConstraint(), is(bandwidthConstraint)); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 98 | } |
| 99 | } |