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 | |
| 20 | import java.time.Duration; |
| 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.DefaultRemoteMepEntry; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.RemoteMepEntry; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.RemoteMepEntry.InterfaceStatusTlvType; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.RemoteMepEntry.PortStatusTlvType; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.RemoteMepEntry.RemoteMepState; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.SenderIdTlv.SenderIdTlvType; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 36 | |
| 37 | import com.fasterxml.jackson.databind.JsonNode; |
| 38 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 39 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 40 | |
| 41 | public class RemoteMepEntryCodecTest { |
| 42 | ObjectMapper mapper; |
| 43 | CfmCodecContext context; |
| 44 | RemoteMepEntry remoteMep1; |
| 45 | |
| 46 | @Before |
| 47 | public void setUp() throws Exception, CfmConfigException { |
| 48 | mapper = new ObjectMapper(); |
| 49 | context = new CfmCodecContext(); |
| 50 | remoteMep1 = DefaultRemoteMepEntry |
| 51 | .builder(MepId.valueOf((short) 10), RemoteMepState.RMEP_OK) |
| 52 | .failedOrOkTime(Duration.ofMillis(546546546L)) |
| 53 | .interfaceStatusTlvType(InterfaceStatusTlvType.IS_LOWERLAYERDOWN) |
| 54 | .macAddress(MacAddress.IPV4_MULTICAST) |
| 55 | .portStatusTlvType(PortStatusTlvType.PS_NO_STATUS_TLV) |
| 56 | .rdi(true) |
| 57 | .senderIdTlvType(SenderIdTlvType.SI_NETWORK_ADDRESS) |
| 58 | .build(); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testEncodeRemoteMepEntryCodecContext() { |
| 63 | ObjectNode node = mapper.createObjectNode(); |
| 64 | node.set("remoteMep", context.codec(RemoteMepEntry.class) |
| 65 | .encode(remoteMep1, context)); |
| 66 | |
| 67 | assertEquals(10, node.get("remoteMep").get("remoteMepId").asInt()); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testEncodeIterableOfRemoteMepEntryCodecContext() |
| 72 | throws CfmConfigException { |
| 73 | RemoteMepEntry remoteMep2 = DefaultRemoteMepEntry |
| 74 | .builder(MepId.valueOf((short) 20), RemoteMepState.RMEP_IDLE) |
| 75 | .build(); |
| 76 | |
| 77 | ArrayList<RemoteMepEntry> remoteMeps = new ArrayList<>(); |
| 78 | remoteMeps.add(remoteMep1); |
| 79 | remoteMeps.add(remoteMep2); |
| 80 | |
| 81 | ObjectNode node = mapper.createObjectNode(); |
| 82 | node.set("remoteMep", context.codec(RemoteMepEntry.class) |
| 83 | .encode(remoteMeps, context)); |
| 84 | |
| 85 | Iterator<JsonNode> an = node.get("remoteMep").elements(); |
| 86 | while (an.hasNext()) { |
| 87 | JsonNode jn = an.next(); |
| 88 | assertEquals("RMEP_", jn.get("remoteMepState").asText().substring(0, 5)); |
| 89 | } |
| 90 | } |
| 91 | } |