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.cfm.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.net.InternetDomainName; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.cfm.CfmCodecContext; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 34 | |
| 35 | import java.io.ByteArrayInputStream; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.nio.charset.StandardCharsets; |
| 39 | |
| 40 | import static org.junit.Assert.assertEquals; |
| 41 | |
| 42 | /** |
| 43 | * Test that the MaintenanceDomainCodec can successfully parse Json in to a MaintenanceDomain. |
| 44 | */ |
| 45 | public class MaintenanceDomainCodecTest { |
| 46 | private static final MdId MDID1_CHAR = MdIdCharStr.asMdId("test-1"); |
| 47 | private static final MdId MDID2_DOMAIN = MdIdDomainName.asMdId( |
| 48 | InternetDomainName.from("test.opennetworking.org")); |
| 49 | private static final MdId MDID3_MACUINT = |
| 50 | MdIdMacUint.asMdId(MacAddress.valueOf("aa:bb:cc:dd:ee:ff"), 181); |
| 51 | private static final MdId MDID4_NONE = MdIdNone.asMdId(); |
| 52 | |
| 53 | private ObjectMapper mapper; |
| 54 | private CfmCodecContext context; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() throws Exception, CfmConfigException { |
| 58 | mapper = new ObjectMapper(); |
| 59 | context = new CfmCodecContext(); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testDecodeMd1() throws IOException { |
| 64 | String mdString = "{\"md\": { \"mdName\": \"test-1\"," + |
| 65 | "\"mdNameType\": \"CHARACTERSTRING\"," + |
| 66 | "\"mdLevel\": \"LEVEL1\", \"mdNumericId\": 1}}"; |
| 67 | |
| 68 | InputStream input = new ByteArrayInputStream( |
| 69 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 70 | JsonNode cfg = mapper.readTree(input); |
| 71 | MaintenanceDomain mdDecode1 = context |
| 72 | .codec(MaintenanceDomain.class).decode((ObjectNode) cfg, context); |
| 73 | assertEquals(MDID1_CHAR, mdDecode1.mdId()); |
| 74 | assertEquals(MaintenanceDomain.MdLevel.LEVEL1, mdDecode1.mdLevel()); |
| 75 | assertEquals(1, mdDecode1.mdNumericId()); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testDecodeMd1NoTypeGiven() throws IOException { |
| 80 | String mdString = "{\"md\": { \"mdName\": \"test-1\"," + |
| 81 | "\"mdLevel\": \"LEVEL1\", \"mdNumericId\": 1}}"; |
| 82 | |
| 83 | InputStream input = new ByteArrayInputStream( |
| 84 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 85 | JsonNode cfg = mapper.readTree(input); |
| 86 | MaintenanceDomain mdDecode1 = context |
| 87 | .codec(MaintenanceDomain.class).decode((ObjectNode) cfg, context); |
| 88 | assertEquals(MDID1_CHAR, mdDecode1.mdId()); |
| 89 | assertEquals(MaintenanceDomain.MdLevel.LEVEL1, mdDecode1.mdLevel()); |
| 90 | assertEquals(1, mdDecode1.mdNumericId()); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | @Test |
| 95 | public void testDecodeMd2() throws IOException { |
| 96 | String mdString = "{\"md\": { \"mdName\": \"test.opennetworking.org\"," + |
| 97 | "\"mdNameType\": \"DOMAINNAME\"}}"; |
| 98 | |
| 99 | InputStream input = new ByteArrayInputStream( |
| 100 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 101 | JsonNode cfg = mapper.readTree(input); |
| 102 | MaintenanceDomain mdDecode1 = context |
| 103 | .codec(MaintenanceDomain.class).decode((ObjectNode) cfg, context); |
| 104 | assertEquals(MDID2_DOMAIN, mdDecode1.mdId()); |
| 105 | assertEquals(MaintenanceDomain.MdLevel.LEVEL0, mdDecode1.mdLevel()); |
| 106 | assertEquals(0, mdDecode1.mdNumericId()); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testDecodeMd3() throws IOException { |
| 111 | String mdString = "{\"md\": { \"mdName\": \"aa:bb:cc:dd:ee:ff:181\"," + |
| 112 | "\"mdNameType\": \"MACANDUINT\"}}"; |
| 113 | |
| 114 | InputStream input = new ByteArrayInputStream( |
| 115 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 116 | JsonNode cfg = mapper.readTree(input); |
| 117 | MaintenanceDomain mdDecode1 = context |
| 118 | .codec(MaintenanceDomain.class).decode((ObjectNode) cfg, context); |
| 119 | assertEquals(MDID3_MACUINT, mdDecode1.mdId()); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testDecodeMd4() throws IOException { |
| 124 | String mdString = "{\"md\": { \"mdName\": \"\"," + |
| 125 | "\"mdNameType\": \"NONE\"}}"; |
| 126 | |
| 127 | InputStream input = new ByteArrayInputStream( |
| 128 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 129 | JsonNode cfg = mapper.readTree(input); |
| 130 | MaintenanceDomain mdDecode1 = context |
| 131 | .codec(MaintenanceDomain.class).decode((ObjectNode) cfg, context); |
| 132 | assertEquals(MDID4_NONE, mdDecode1.mdId()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testEncodeMd1() throws CfmConfigException { |
| 137 | MaintenanceDomain md1 = DefaultMaintenanceDomain.builder(MDID1_CHAR) |
| 138 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL1) |
| 139 | .mdNumericId((short) 1) |
| 140 | .build(); |
| 141 | |
| 142 | ObjectNode node = mapper.createObjectNode(); |
| 143 | node.set("md", context.codec(MaintenanceDomain.class).encode(md1, context)); |
| 144 | |
| 145 | assertEquals("{\"md\":{" + |
| 146 | "\"mdName\":\"test-1\"," + |
| 147 | "\"mdNameType\":\"CHARACTERSTRING\"," + |
| 148 | "\"mdLevel\":\"LEVEL1\"," + |
| 149 | "\"mdNumericId\":1," + |
| 150 | "\"maList\":[]}}", node.toString()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testEncodeMd2() throws CfmConfigException { |
| 155 | MaintenanceDomain md2 = DefaultMaintenanceDomain.builder(MDID2_DOMAIN) |
| 156 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build(); |
| 157 | |
| 158 | ObjectNode node = mapper.createObjectNode(); |
| 159 | node.set("md", context.codec(MaintenanceDomain.class).encode(md2, context)); |
| 160 | |
| 161 | assertEquals("{\"md\":{" + |
| 162 | "\"mdName\":\"test.opennetworking.org\"," + |
| 163 | "\"mdNameType\":\"DOMAINNAME\"," + |
| 164 | "\"mdLevel\":\"LEVEL2\"," + |
| 165 | "\"maList\":[]}}", node.toString()); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testEncodeMd3() throws CfmConfigException { |
| 170 | MaintenanceDomain md3 = DefaultMaintenanceDomain.builder(MDID3_MACUINT) |
| 171 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL3).build(); |
| 172 | |
| 173 | ObjectNode node = mapper.createObjectNode(); |
| 174 | node.set("md", context.codec(MaintenanceDomain.class).encode(md3, context)); |
| 175 | |
| 176 | assertEquals("{\"md\":{" + |
| 177 | "\"mdName\":\"AA:BB:CC:DD:EE:FF:181\"," + |
| 178 | "\"mdNameType\":\"MACANDUINT\"," + |
| 179 | "\"mdLevel\":\"LEVEL3\"," + |
| 180 | "\"maList\":[]}}", node.toString()); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testEncodeMd4() throws CfmConfigException { |
| 185 | MaintenanceDomain md4 = DefaultMaintenanceDomain.builder(MDID4_NONE) |
| 186 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL4).build(); |
| 187 | |
| 188 | ObjectNode node = mapper.createObjectNode(); |
| 189 | node.set("md", context.codec(MaintenanceDomain.class).encode(md4, context)); |
| 190 | |
| 191 | assertEquals("{\"md\":{" + |
| 192 | "\"mdName\":\"\"," + |
| 193 | "\"mdNameType\":\"NONE\"," + |
| 194 | "\"mdLevel\":\"LEVEL4\"," + |
| 195 | "\"maList\":[]}}", node.toString()); |
| 196 | } |
| 197 | } |