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.assertNull; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.nio.charset.StandardCharsets; |
| 25 | |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onosproject.cfm.CfmCodecContext; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate; |
| 30 | |
| 31 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 32 | import com.fasterxml.jackson.databind.JsonNode; |
| 33 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 35 | |
| 36 | public class MepLbCreateCodecTest { |
| 37 | ObjectMapper mapper; |
| 38 | CfmCodecContext context; |
| 39 | |
| 40 | @Before |
| 41 | public void setUp() throws Exception { |
| 42 | mapper = new ObjectMapper(); |
| 43 | context = new CfmCodecContext(); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testDecodeMepLbCreateMepId() throws JsonProcessingException, IOException { |
| 48 | String loopbackString = "{\"loopback\": { \"remoteMepId\": 20," + |
| 49 | "\"numberMessages\": 10, \"vlanDropEligible\": true," + |
| 50 | "\"vlanPriority\": 6, \"dataTlvHex\": \"0A:BB:CC\" }}"; |
| 51 | |
| 52 | InputStream input = new ByteArrayInputStream( |
| 53 | loopbackString.getBytes(StandardCharsets.UTF_8)); |
| 54 | JsonNode cfg = mapper.readTree(input); |
| 55 | MepLbCreate mepLbCreate = context |
| 56 | .codec(MepLbCreate.class).decode((ObjectNode) cfg, context); |
| 57 | |
| 58 | assertNull(mepLbCreate.remoteMepAddress()); |
| 59 | assertEquals(20, mepLbCreate.remoteMepId().id().shortValue()); |
| 60 | assertEquals(10, mepLbCreate.numberMessages().intValue()); |
| 61 | assertEquals(6, mepLbCreate.vlanPriority().ordinal()); |
| 62 | assertEquals(true, mepLbCreate.vlanDropEligible()); |
| 63 | assertEquals("0A:BB:CC".toLowerCase(), mepLbCreate.dataTlvHex()); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testDecodeMepLbCreateMepMac() throws JsonProcessingException, IOException { |
| 68 | String loopbackString = "{\"loopback\": { " + |
| 69 | "\"remoteMepMac\": \"AA:BB:CC:DD:EE:FF\" }}"; |
| 70 | InputStream input = new ByteArrayInputStream( |
| 71 | loopbackString.getBytes(StandardCharsets.UTF_8)); |
| 72 | JsonNode cfg = mapper.readTree(input); |
| 73 | MepLbCreate mepLbCreate = context |
| 74 | .codec(MepLbCreate.class).decode((ObjectNode) cfg, context); |
| 75 | |
| 76 | assertNull(mepLbCreate.remoteMepId()); |
| 77 | assertEquals("AA:BB:CC:DD:EE:FF", mepLbCreate.remoteMepAddress().toString()); |
| 78 | assertNull(mepLbCreate.dataTlvHex()); |
| 79 | } |
| 80 | } |