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 org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.cfm.CfmCodecContext; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 33 | |
| 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.nio.charset.StandardCharsets; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | |
| 41 | /** |
| 42 | * Test that the MaintenanceAssociationCodec can successfully parse Json in to a MaintenanceAssociation. |
| 43 | */ |
| 44 | public class MaintenanceAssociationCodecTest { |
| 45 | private static final MaIdShort MAID1_CHAR = MaIdCharStr.asMaId("ma-1"); |
| 46 | private static final MaIdShort MAID2_VID = MaIdPrimaryVid.asMaId(1234); |
| 47 | private static final MaIdShort MAID3_OCTET = MaId2Octet.asMaId(12467); |
| 48 | private static final MaIdShort MAID4_RFC = MaIdRfc2685VpnId.asMaIdHex("aa:bb:cc:dd:ee:ff:99"); |
| 49 | private static final MaIdShort MAID5_Y1731 = MaIdIccY1731.asMaId("abc", "defghij"); |
| 50 | |
| 51 | |
| 52 | private ObjectMapper mapper; |
| 53 | private CfmCodecContext context; |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception, CfmConfigException { |
| 57 | mapper = new ObjectMapper(); |
| 58 | context = new CfmCodecContext(); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testDecodeMa1() throws IOException { |
| 63 | String mdString = "{\"ma\": { \"maName\": \"ma-1\"," + |
| 64 | "\"maNameType\": \"CHARACTERSTRING\"," + |
| 65 | "\"component-list\": [], " + |
| 66 | "\"rmep-list\": [], " + |
| 67 | "\"maNumericId\": 1}}"; |
| 68 | |
| 69 | InputStream input = new ByteArrayInputStream( |
| 70 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 71 | JsonNode cfg = mapper.readTree(input); |
| 72 | MaintenanceAssociation maDecode1 = ((MaintenanceAssociationCodec) context |
| 73 | .codec(MaintenanceAssociation.class)) |
| 74 | .decode((ObjectNode) cfg, context, 10); |
| 75 | assertEquals(MAID1_CHAR, maDecode1.maId()); |
| 76 | assertEquals(1, maDecode1.maNumericId()); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testDecodeMa1NoTypeGiven() throws IOException { |
| 81 | String mdString = "{\"ma\": { \"maName\": \"ma-1\"," + |
| 82 | "\"component-list\": [], " + |
| 83 | "\"rmep-list\": [], " + |
| 84 | "\"maNumericId\": 1}}"; |
| 85 | |
| 86 | InputStream input = new ByteArrayInputStream( |
| 87 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 88 | JsonNode cfg = mapper.readTree(input); |
| 89 | MaintenanceAssociation maDecode1 = ((MaintenanceAssociationCodec) context |
| 90 | .codec(MaintenanceAssociation.class)) |
| 91 | .decode((ObjectNode) cfg, context, 10); |
| 92 | assertEquals(MAID1_CHAR, maDecode1.maId()); |
| 93 | assertEquals(1, maDecode1.maNumericId()); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testDecodeMa2() throws IOException { |
| 98 | String mdString = "{\"ma\": { \"maName\": 1234," + |
| 99 | "\"maNameType\": \"PRIMARYVID\"," + |
| 100 | "\"component-list\": [], " + |
| 101 | "\"rmep-list\": [], " + |
| 102 | "\"maNumericId\": 2}}"; |
| 103 | |
| 104 | InputStream input = new ByteArrayInputStream( |
| 105 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 106 | JsonNode cfg = mapper.readTree(input); |
| 107 | MaintenanceAssociation maDecode2 = ((MaintenanceAssociationCodec) context |
| 108 | .codec(MaintenanceAssociation.class)) |
| 109 | .decode((ObjectNode) cfg, context, 10); |
| 110 | assertEquals(MAID2_VID, maDecode2.maId()); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testDecodeMa3() throws IOException { |
| 115 | String mdString = "{\"ma\": { \"maName\": 12467," + |
| 116 | "\"maNameType\": \"TWOOCTET\"," + |
| 117 | "\"component-list\": [], " + |
| 118 | "\"rmep-list\": [], " + |
| 119 | "\"maNumericId\": 3}}"; |
| 120 | |
| 121 | InputStream input = new ByteArrayInputStream( |
| 122 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 123 | JsonNode cfg = mapper.readTree(input); |
| 124 | MaintenanceAssociation maDecode3 = ((MaintenanceAssociationCodec) context |
| 125 | .codec(MaintenanceAssociation.class)) |
| 126 | .decode((ObjectNode) cfg, context, 10); |
| 127 | assertEquals(MAID3_OCTET, maDecode3.maId()); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testDecodeMa4() throws IOException { |
| 132 | String mdString = "{\"ma\": { \"maName\": \"aa:bb:cc:dd:ee:ff:99\"," + |
| 133 | "\"maNameType\": \"RFC2685VPNID\"," + |
| 134 | "\"component-list\": [], " + |
| 135 | "\"rmep-list\": [], " + |
| 136 | "\"maNumericId\": 4}}"; |
| 137 | |
| 138 | InputStream input = new ByteArrayInputStream( |
| 139 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 140 | JsonNode cfg = mapper.readTree(input); |
| 141 | MaintenanceAssociation maDecode4 = ((MaintenanceAssociationCodec) context |
| 142 | .codec(MaintenanceAssociation.class)) |
| 143 | .decode((ObjectNode) cfg, context, 10); |
| 144 | assertEquals(MAID4_RFC, maDecode4.maId()); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testDecodeMa5() throws IOException { |
| 149 | String mdString = "{\"ma\": { \"maName\": \"abc:defghij\"," + |
| 150 | "\"maNameType\": \"ICCY1731\"," + |
| 151 | "\"component-list\": [], " + |
| 152 | "\"rmep-list\": [], " + |
| 153 | "\"maNumericId\": 5}}"; |
| 154 | |
| 155 | InputStream input = new ByteArrayInputStream( |
| 156 | mdString.getBytes(StandardCharsets.UTF_8)); |
| 157 | JsonNode cfg = mapper.readTree(input); |
| 158 | MaintenanceAssociation maDecode5 = ((MaintenanceAssociationCodec) context |
| 159 | .codec(MaintenanceAssociation.class)) |
| 160 | .decode((ObjectNode) cfg, context, 10); |
| 161 | assertEquals(MAID5_Y1731, maDecode5.maId()); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testEncodeMa1() throws CfmConfigException { |
| 166 | MaintenanceAssociation ma1 = DefaultMaintenanceAssociation.builder(MAID1_CHAR, 10) |
| 167 | .maNumericId((short) 1) |
| 168 | .build(); |
| 169 | |
| 170 | ObjectNode node = mapper.createObjectNode(); |
| 171 | node.set("ma", context.codec(MaintenanceAssociation.class).encode(ma1, context)); |
| 172 | |
| 173 | assertEquals("{\"ma\":{" + |
| 174 | "\"maName\":\"ma-1\"," + |
| 175 | "\"maNameType\":\"CHARACTERSTRING\"," + |
| 176 | "\"maNumericId\":1," + |
| 177 | "\"component-list\":[]," + |
| 178 | "\"rmep-list\":[]}}", node.toString()); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testEncodeMa2() throws CfmConfigException { |
| 183 | MaintenanceAssociation ma1 = DefaultMaintenanceAssociation.builder(MAID2_VID, 10) |
| 184 | .maNumericId((short) 2) |
| 185 | .build(); |
| 186 | |
| 187 | ObjectNode node = mapper.createObjectNode(); |
| 188 | node.set("ma", context.codec(MaintenanceAssociation.class).encode(ma1, context)); |
| 189 | |
| 190 | assertEquals("{\"ma\":{" + |
| 191 | "\"maName\":\"1234\"," + |
| 192 | "\"maNameType\":\"PRIMARYVID\"," + |
| 193 | "\"maNumericId\":2," + |
| 194 | "\"component-list\":[]," + |
| 195 | "\"rmep-list\":[]}}", node.toString()); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testEncodeMa3() throws CfmConfigException { |
| 200 | MaintenanceAssociation ma1 = DefaultMaintenanceAssociation.builder(MAID3_OCTET, 10) |
| 201 | .maNumericId((short) 3) |
| 202 | .build(); |
| 203 | |
| 204 | ObjectNode node = mapper.createObjectNode(); |
| 205 | node.set("ma", context.codec(MaintenanceAssociation.class).encode(ma1, context)); |
| 206 | |
| 207 | assertEquals("{\"ma\":{" + |
| 208 | "\"maName\":\"12467\"," + |
| 209 | "\"maNameType\":\"TWOOCTET\"," + |
| 210 | "\"maNumericId\":3," + |
| 211 | "\"component-list\":[]," + |
| 212 | "\"rmep-list\":[]}}", node.toString()); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testEncodeMa4() throws CfmConfigException { |
| 217 | MaintenanceAssociation ma1 = DefaultMaintenanceAssociation.builder(MAID4_RFC, 10) |
| 218 | .maNumericId((short) 4) |
| 219 | .build(); |
| 220 | |
| 221 | ObjectNode node = mapper.createObjectNode(); |
| 222 | node.set("ma", context.codec(MaintenanceAssociation.class).encode(ma1, context)); |
| 223 | |
| 224 | assertEquals("{\"ma\":{" + |
| 225 | "\"maName\":\"aa:bb:cc:dd:ee:ff:99\"," + |
| 226 | "\"maNameType\":\"RFC2685VPNID\"," + |
| 227 | "\"maNumericId\":4," + |
| 228 | "\"component-list\":[]," + |
| 229 | "\"rmep-list\":[]}}", node.toString()); |
| 230 | } |
| 231 | |
| 232 | @Test |
| 233 | public void testEncodeMa5() throws CfmConfigException { |
| 234 | MaintenanceAssociation ma1 = DefaultMaintenanceAssociation.builder(MAID5_Y1731, 10) |
| 235 | .maNumericId((short) 5) |
| 236 | .build(); |
| 237 | |
| 238 | ObjectNode node = mapper.createObjectNode(); |
| 239 | node.set("ma", context.codec(MaintenanceAssociation.class).encode(ma1, context)); |
| 240 | |
| 241 | assertEquals("{\"ma\":{" + |
| 242 | "\"maName\":\"abc:defghij\"," + |
| 243 | "\"maNameType\":\"ICCY1731\"," + |
| 244 | "\"maNumericId\":5," + |
| 245 | "\"component-list\":[]," + |
| 246 | "\"rmep-list\":[]}}", node.toString()); |
| 247 | } |
| 248 | } |