blob: 4986d5ae618840e36b996071032772628720e860 [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.soam.web;
17
18import static org.junit.Assert.assertEquals;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.nio.charset.StandardCharsets;
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.List;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.onosproject.cfm.CfmCodecContext;
31import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.MeasurementOption;
32
33import com.fasterxml.jackson.core.JsonProcessingException;
34import com.fasterxml.jackson.databind.JsonNode;
35import com.fasterxml.jackson.databind.ObjectMapper;
36import com.fasterxml.jackson.databind.node.ArrayNode;
37
38public 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}