Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.soam.web; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.nio.charset.StandardCharsets; |
| 25 | import java.time.Duration; |
| 26 | |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onosproject.cfm.CfmCodecContext; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 32 | import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException; |
| 33 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DefaultDelayMeasurementCreate; |
| 34 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate; |
| 35 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.DmCreateBuilder; |
| 36 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.DmType; |
| 37 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.MeasurementOption; |
| 38 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.Version; |
| 39 | |
| 40 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 41 | import com.fasterxml.jackson.databind.JsonNode; |
| 42 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 43 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 44 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 45 | |
| 46 | public class DmCreateCodecTest { |
| 47 | ObjectMapper mapper; |
| 48 | CfmCodecContext context; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() throws Exception { |
| 52 | mapper = new ObjectMapper(); |
| 53 | context = new CfmCodecContext(); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testDecodeObjectNodeCodecContext1() |
| 58 | throws JsonProcessingException, IOException { |
| 59 | String moStr = "{\"dm\": {}}"; |
| 60 | |
| 61 | InputStream input = new ByteArrayInputStream( |
| 62 | moStr.getBytes(StandardCharsets.UTF_8)); |
| 63 | JsonNode cfg = mapper.readTree(input); |
| 64 | |
| 65 | try { |
| 66 | context.codec(DelayMeasurementCreate.class) |
| 67 | .decode((ObjectNode) cfg, context); |
| 68 | fail("Expecting an exception"); |
| 69 | } catch (IllegalArgumentException e) { |
| 70 | assertEquals("remoteMepId is required", e.getMessage()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testDecodeObjectNodeCodecContext2() |
| 76 | throws JsonProcessingException, IOException { |
| 77 | String moStr = "{\"dm\": {" + |
| 78 | "\"version\":\"Y17312008\"," + |
| 79 | "\"dmType\":\"DMDMM\"," + |
| 80 | "\"remoteMepId\":12," + |
| 81 | "\"priority\":\"PRIO6\"," + |
| 82 | "\"measurementsEnabled\" :" + |
| 83 | "[\"FRAME_DELAY_RANGE_BACKWARD_AVERAGE\", " + |
| 84 | "\"INTER_FRAME_DELAY_VARIATION_FORWARD_AVERAGE\"]" + |
| 85 | "}}"; |
| 86 | |
| 87 | InputStream input = new ByteArrayInputStream( |
| 88 | moStr.getBytes(StandardCharsets.UTF_8)); |
| 89 | JsonNode cfg = mapper.readTree(input); |
| 90 | |
| 91 | DelayMeasurementCreate dmCreate = context |
| 92 | .codec(DelayMeasurementCreate.class) |
| 93 | .decode((ObjectNode) cfg, context); |
| 94 | |
| 95 | assertEquals(Version.Y17312008, dmCreate.version()); |
| 96 | assertEquals(DmType.DMDMM, dmCreate.dmCfgType()); |
| 97 | assertEquals(12, dmCreate.remoteMepId().id().shortValue()); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testEncodeDelayMeasurementCreateCodecContext() |
| 102 | throws SoamConfigException { |
| 103 | DmCreateBuilder builder = DefaultDelayMeasurementCreate |
| 104 | .builder(DmType.DM1DMRX, Version.Y17312011, |
| 105 | MepId.valueOf((short) 16), Priority.PRIO5); |
| 106 | builder.addToMeasurementsEnabled( |
| 107 | MeasurementOption.FRAME_DELAY_BACKWARD_MAX); |
| 108 | builder.addToMeasurementsEnabled( |
| 109 | MeasurementOption.FRAME_DELAY_TWO_WAY_MAX); |
| 110 | builder.addToMeasurementsEnabled( |
| 111 | MeasurementOption.INTER_FRAME_DELAY_VARIATION_BACKWARD_BINS); |
| 112 | builder = (DmCreateBuilder) builder.messagePeriod(Duration.ofMillis(100)); |
| 113 | builder = (DmCreateBuilder) builder.frameSize((short) 1200); |
| 114 | |
| 115 | ObjectNode node = mapper.createObjectNode(); |
| 116 | node.set("dm", context.codec(DelayMeasurementCreate.class) |
| 117 | .encode(builder.build(), context)); |
| 118 | |
| 119 | assertEquals(DmType.DM1DMRX.name(), node.get("dm").get("dmCfgType").asText()); |
| 120 | assertEquals(Version.Y17312011.name(), node.get("dm").get("version").asText()); |
| 121 | assertEquals(16, node.get("dm").get("remoteMepId").asInt()); |
| 122 | assertEquals(Priority.PRIO5.name(), node.get("dm").get("priority").asText()); |
| 123 | assertEquals(100, node.get("dm").get("messagePeriodMs").asInt()); |
| 124 | assertEquals(1200, node.get("dm").get("frameSize").asInt()); |
| 125 | |
| 126 | assertEquals(3, ((ArrayNode) node.get("dm").get("measurementsEnabled")).size()); |
| 127 | } |
| 128 | |
| 129 | } |