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.soam.web; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | |
| 20 | import java.io.ByteArrayInputStream; |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.nio.charset.StandardCharsets; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.onosproject.cfm.CfmCodecContext; |
| 31 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.MeasurementOption; |
| 32 | |
| 33 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 34 | import com.fasterxml.jackson.databind.JsonNode; |
| 35 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 36 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 37 | |
| 38 | public class DmMeasurementOptionCodecTest { |
| 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 testEncodeIterableOfMeasurementOptionCodecContext() { |
| 50 | List<MeasurementOption> moList = new ArrayList<>(); |
| 51 | moList.add(MeasurementOption.FRAME_DELAY_BACKWARD_MAX); |
| 52 | moList.add(MeasurementOption.FRAME_DELAY_FORWARD_BINS); |
| 53 | |
| 54 | ArrayNode an = |
| 55 | context.codec(MeasurementOption.class).encode(moList, context); |
| 56 | |
| 57 | assertEquals(MeasurementOption.FRAME_DELAY_BACKWARD_MAX.toString(), |
| 58 | an.get(0).asText()); |
| 59 | assertEquals(MeasurementOption.FRAME_DELAY_FORWARD_BINS.toString(), |
| 60 | an.get(1).asText()); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testDecodeArrayNodeCodecContext() |
| 65 | throws JsonProcessingException, IOException { |
| 66 | String moStr = "{\"measurementsEnabled\": " + |
| 67 | "[\"FRAME_DELAY_RANGE_BACKWARD_AVERAGE\", " + |
| 68 | "\"INTER_FRAME_DELAY_VARIATION_FORWARD_AVERAGE\"]}"; |
| 69 | InputStream input = new ByteArrayInputStream( |
| 70 | moStr.getBytes(StandardCharsets.UTF_8)); |
| 71 | JsonNode cfg = mapper.readTree(input); |
| 72 | Iterable<MeasurementOption> moIter = context |
| 73 | .codec(MeasurementOption.class) |
| 74 | .decode((ArrayNode) cfg.get("measurementsEnabled"), context); |
| 75 | |
| 76 | Iterator<MeasurementOption> source = moIter.iterator(); |
| 77 | List<MeasurementOption> moList = new ArrayList<>(); |
| 78 | source.forEachRemaining(moList::add); |
| 79 | |
| 80 | assertEquals(MeasurementOption.FRAME_DELAY_RANGE_BACKWARD_AVERAGE.toString(), |
| 81 | moList.get(0).name()); |
| 82 | assertEquals(MeasurementOption.INTER_FRAME_DELAY_VARIATION_FORWARD_AVERAGE.toString(), |
| 83 | moList.get(1).name()); |
| 84 | } |
| 85 | |
| 86 | } |