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 static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Iterator; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.cfm.CfmCodecContext; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepDirection; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry.MepEntryBuilder; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | |
| 40 | import com.fasterxml.jackson.databind.JsonNode; |
| 41 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 42 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 43 | |
| 44 | /** |
| 45 | * Test that the MepEntryCodec can successfully parse Json in to a Mep. |
| 46 | */ |
| 47 | public class MepEntryCodecTest { |
| 48 | ObjectMapper mapper; |
| 49 | CfmCodecContext context; |
| 50 | MepEntry mepEntry1; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception, CfmConfigException { |
| 54 | mapper = new ObjectMapper(); |
| 55 | context = new CfmCodecContext(); |
| 56 | MepEntryBuilder builder = DefaultMepEntry.builder( |
| 57 | MepId.valueOf((short) 22), |
| 58 | DeviceId.deviceId("netconf:1234:830"), |
| 59 | PortNumber.portNumber(2), |
| 60 | MepDirection.UP_MEP, |
| 61 | MdIdCharStr.asMdId("md-1"), |
| 62 | MaIdCharStr.asMaId("ma-1-1")) |
| 63 | .macAddress(MacAddress.valueOf("aa:bb:cc:dd:ee:ff")); |
| 64 | builder = (MepEntryBuilder) builder |
| 65 | .administrativeState(true) |
| 66 | .cciEnabled(true) |
| 67 | .ccmLtmPriority(Priority.PRIO1); |
| 68 | mepEntry1 = builder.buildEntry(); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testEncodeMepEntryCodecContext() { |
| 74 | ObjectNode node = mapper.createObjectNode(); |
| 75 | node.set("mep", context.codec(MepEntry.class).encode(mepEntry1, context)); |
| 76 | |
| 77 | assertEquals(22, node.get("mep").get("mepId").asInt()); |
| 78 | assertEquals("aa:bb:cc:dd:ee:ff".toUpperCase(), |
| 79 | node.get("mep").get("macAddress").asText()); |
| 80 | assertTrue(node.get("mep").get("administrative-state").asBoolean()); |
| 81 | assertTrue(node.get("mep").get("cci-enabled").asBoolean()); |
| 82 | assertEquals(Priority.PRIO1.ordinal(), |
| 83 | node.get("mep").get("ccm-ltm-priority").asInt()); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testEncodeIterableOfMepEntryCodecContext() throws CfmConfigException { |
| 88 | MepEntry mepEntry2 = DefaultMepEntry.builder( |
| 89 | MepId.valueOf((short) 33), |
| 90 | DeviceId.deviceId("netconf:4321:830"), |
| 91 | PortNumber.portNumber(1), |
| 92 | MepDirection.DOWN_MEP, |
| 93 | MdIdCharStr.asMdId("md-2"), |
| 94 | MaIdCharStr.asMaId("ma-2-2")) |
| 95 | .buildEntry(); |
| 96 | |
| 97 | ArrayList<MepEntry> meps = new ArrayList<>(); |
| 98 | meps.add(mepEntry1); |
| 99 | meps.add(mepEntry2); |
| 100 | |
| 101 | ObjectNode node = mapper.createObjectNode(); |
| 102 | node.set("mep", context.codec(MepEntry.class) |
| 103 | .encode(meps, context)); |
| 104 | |
| 105 | Iterator<JsonNode> an = node.get("mep").elements(); |
| 106 | while (an.hasNext()) { |
| 107 | JsonNode jn = an.next(); |
| 108 | assertEquals("md-", jn.get("mdName").asText().substring(0, 3)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |