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.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.cfm.CfmCodecContext; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLtCreate; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 28 | |
| 29 | import java.io.ByteArrayInputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.nio.charset.StandardCharsets; |
| 33 | import java.util.BitSet; |
| 34 | |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertNull; |
| 37 | |
| 38 | public class MepLtCreateCodecTest { |
| 39 | ObjectMapper mapper; |
| 40 | CfmCodecContext context; |
| 41 | |
| 42 | @Before |
| 43 | public void setUp() throws Exception { |
| 44 | mapper = new ObjectMapper(); |
| 45 | context = new CfmCodecContext(); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void testDecodeMepLtCreateMepId() throws JsonProcessingException, IOException { |
| 50 | String linktraceString = "{\"linktrace\": { " + |
| 51 | "\"remoteMepId\": 20," + |
| 52 | "\"defaultTtl\": 21," + |
| 53 | "\"transmitLtmFlags\": \"use-fdb-only\"}}"; |
| 54 | |
| 55 | InputStream input = new ByteArrayInputStream( |
| 56 | linktraceString.getBytes(StandardCharsets.UTF_8)); |
| 57 | JsonNode cfg = mapper.readTree(input); |
| 58 | MepLtCreate mepLtCreate = context |
| 59 | .codec(MepLtCreate.class).decode((ObjectNode) cfg, context); |
| 60 | |
| 61 | assertNull(mepLtCreate.remoteMepAddress()); |
| 62 | assertEquals(20, mepLtCreate.remoteMepId().id().shortValue()); |
| 63 | assertEquals(21, mepLtCreate.defaultTtl().intValue()); |
| 64 | assertEquals(BitSet.valueOf(new byte[]{1}), mepLtCreate.transmitLtmFlags()); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testDecodeMepLtCreateInvalidTransmitLtmFlags() |
| 69 | throws JsonProcessingException, IOException { |
| 70 | String linktraceString = "{\"linktrace\": { " + |
| 71 | "\"remoteMepId\": 20," + |
| 72 | "\"transmitLtmFlags\": \"1\"}}"; |
| 73 | |
| 74 | InputStream input = new ByteArrayInputStream( |
| 75 | linktraceString.getBytes(StandardCharsets.UTF_8)); |
| 76 | JsonNode cfg = mapper.readTree(input); |
| 77 | try { |
| 78 | context.codec(MepLtCreate.class).decode((ObjectNode) cfg, context); |
| 79 | } catch (IllegalArgumentException e) { |
| 80 | assertEquals("Expecting value 'use-fdb-only' or '' " + |
| 81 | "for transmitLtmFlags", e.getMessage()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testEncodeMepLtCreate() { |
| 87 | MepId mepId1 = MepId.valueOf((short) 1); |
| 88 | MepLtCreate mepLtCreate1 = DefaultMepLtCreate |
| 89 | .builder(mepId1) |
| 90 | .defaultTtl((short) 20) |
| 91 | .transmitLtmFlags(BitSet.valueOf(new byte[]{1})) |
| 92 | .build(); |
| 93 | |
| 94 | ObjectMapper mapper = new ObjectMapper(); |
| 95 | CfmCodecContext context = new CfmCodecContext(); |
| 96 | ObjectNode node = mapper.createObjectNode(); |
| 97 | node.set("linktrace", context.codec(MepLtCreate.class).encode(mepLtCreate1, context)); |
| 98 | |
| 99 | assertEquals("{\"linktrace\":{" + |
| 100 | "\"remoteMepId\":1," + |
| 101 | "\"defaultTtl\":20," + |
| 102 | "\"transmitLtmFlags\":\"use-fdb-only\"}}", node.toString()); |
| 103 | } |
| 104 | } |