blob: df6ce7b92240d2d96474bf7535a8ee2a3ea432b4 [file] [log] [blame]
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08003 *
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.net.config.basics;
17
18import static org.hamcrest.Matchers.is;
19import static org.junit.Assert.*;
20import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.util.Map;
25
26import org.junit.AfterClass;
27import org.junit.Before;
28import org.junit.BeforeClass;
29import org.junit.Test;
30import org.onlab.junit.TestUtils;
31import org.onlab.junit.TestUtils.TestUtilsException;
32import org.onlab.osgi.ServiceDirectory;
33import org.onlab.osgi.TestServiceDirectory;
34import org.onosproject.codec.CodecService;
35import org.onosproject.codec.impl.CodecManager;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.config.BaseConfig;
38import org.onosproject.net.config.ConfigApplyDelegate;
39
40import com.fasterxml.jackson.core.JsonProcessingException;
41import com.fasterxml.jackson.databind.DeserializationFeature;
42import com.fasterxml.jackson.databind.JsonNode;
43import com.fasterxml.jackson.databind.ObjectMapper;
44import com.fasterxml.jackson.databind.node.JsonNodeFactory;
45import com.fasterxml.jackson.databind.node.NumericNode;
46import com.google.common.collect.ImmutableMap;
47
48public class PortAnnotationConfigTest {
49
50 private static final String SAMPLE_JSONFILE = "port_annotation_config.json";
51
52 private static TestServiceDirectory directory;
53 private static ServiceDirectory original;
54
55 private ObjectMapper mapper;
56
57 private final ConfigApplyDelegate noopDelegate = cfg -> { };
58
59 /**
60 * {@value #SAMPLE_JSONFILE} after parsing.
61 */
62 private JsonNode node;
63
64 // sample data
65 private final ConnectPoint cp = deviceConnectPoint("of:0000000000000001/2");
66
67 private final String key = "foo";
68 private final String value = "bar";
69
70
71 // TODO consolidate code-clone in ProtectionConfigTest, and define constants for field name
72 @BeforeClass
73 public static void setUpClass() throws TestUtilsException {
74 directory = new TestServiceDirectory();
75
76 CodecManager codecService = new CodecManager();
77 codecService.activate();
78 directory.add(CodecService.class, codecService);
79
80 // replace service directory used by BaseConfig
81 original = TestUtils.getField(BaseConfig.class, "services");
82 TestUtils.setField(BaseConfig.class, "services", directory);
83 }
84
85 @AfterClass
86 public static void tearDownClass() throws TestUtilsException {
87 TestUtils.setField(BaseConfig.class, "services", original);
88 }
89
90 @Before
91 public void setUp() throws JsonProcessingException, IOException, TestUtilsException {
92
93 mapper = new ObjectMapper();
94 // Jackson configuration for ease of Numeric node comparison
95 // - treat integral number node as long node
96 mapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
97 mapper.setNodeFactory(new JsonNodeFactory(false) {
98 @Override
99 public NumericNode numberNode(int v) {
100 return super.numberNode((long) v);
101 }
102 @Override
103 public NumericNode numberNode(short v) {
104 return super.numberNode((long) v);
105 }
106 });
107
108 InputStream stream = PortAnnotationConfig.class
109 .getResourceAsStream(SAMPLE_JSONFILE);
110 JsonNode tree = mapper.readTree(stream);
111
112 node = tree.path("ports")
113 .path(cp.toString())
114 .path(PortAnnotationConfig.CONFIG_KEY);
115 assertTrue(node.isObject());
116 }
117
118 @Test
119 public void readTest() {
120 PortAnnotationConfig sut = new PortAnnotationConfig();
121 sut.init(cp, PortAnnotationConfig.CONFIG_KEY, node, mapper, noopDelegate);
122
123 assertThat(sut.subject(), is(cp));
124 Map<String, String> annotations = sut.annotations();
125 assertThat(annotations.size(), is(1));
126 assertThat(annotations.get(key), is(value));
127 }
128
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800129 @Test
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800130 public void writeEntryTest() throws JsonProcessingException, IOException {
131
132 PortAnnotationConfig w = new PortAnnotationConfig();
133 w.init(cp, PortAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate);
134
135 // write equivalent to sample
136 w.annotation(key, value);
137
138 // reparse JSON
139 JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node()));
140
141 PortAnnotationConfig sut = new PortAnnotationConfig();
142 sut.init(cp, PortAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
143
144 assertThat(sut.subject(), is(cp));
145 Map<String, String> annotations = sut.annotations();
146 assertThat(annotations.size(), is(1));
147 assertThat(annotations.get(key), is(value));
148 }
149
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800150 @Test
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -0800151 public void writeMapTest() throws JsonProcessingException, IOException {
152
153 PortAnnotationConfig w = new PortAnnotationConfig();
154 w.init(cp, PortAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate);
155
156 // write equivalent to sample
157 w.annotations(ImmutableMap.of(key, value));
158
159 // reparse JSON
160 JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node()));
161
162 PortAnnotationConfig sut = new PortAnnotationConfig();
163 sut.init(cp, PortAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
164
165 assertThat(sut.subject(), is(cp));
166 Map<String, String> annotations = sut.annotations();
167 assertThat(annotations.size(), is(1));
168 assertThat(annotations.get(key), is(value));
169 }
170
171}