blob: ea07b8ea6cf493b579baf5a644ecb77a4416e139 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.cfm.web;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertTrue;
20
21import java.util.ArrayList;
22import java.util.Iterator;
23
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.MacAddress;
27import org.onosproject.cfm.CfmCodecContext;
28import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry;
29import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepDirection;
30import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority;
31import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
32import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry.MepEntryBuilder;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
35import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
36import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.PortNumber;
39
40import com.fasterxml.jackson.databind.JsonNode;
41import com.fasterxml.jackson.databind.ObjectMapper;
42import com.fasterxml.jackson.databind.node.ObjectNode;
43
44/**
45 * Test that the MepEntryCodec can successfully parse Json in to a Mep.
46 */
47public 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}