blob: b95f26d9a8c86debd82e2f30ef784a07f119555a [file] [log] [blame]
Palash Kalaa06a6162017-11-15 20:42:40 +09001/*
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.net.config.basics;
17
18import static org.hamcrest.Matchers.is;
19import static org.junit.Assert.*;
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.util.Map;
24
25import org.junit.AfterClass;
26import org.junit.Before;
27import org.junit.BeforeClass;
28import org.junit.Test;
29import org.onlab.junit.TestUtils;
30import org.onlab.junit.TestUtils.TestUtilsException;
31import org.onlab.osgi.ServiceDirectory;
32import org.onlab.osgi.TestServiceDirectory;
33import org.onosproject.codec.CodecService;
34import org.onosproject.codec.impl.CodecManager;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.config.BaseConfig;
37import org.onosproject.net.config.ConfigApplyDelegate;
38
39import com.fasterxml.jackson.core.JsonProcessingException;
40import com.fasterxml.jackson.databind.DeserializationFeature;
41import com.fasterxml.jackson.databind.JsonNode;
42import com.fasterxml.jackson.databind.ObjectMapper;
43import com.fasterxml.jackson.databind.node.JsonNodeFactory;
44import com.fasterxml.jackson.databind.node.NumericNode;
45import com.google.common.collect.ImmutableMap;
46
47public class DeviceAnnotationConfigTest {
48
49 private static final String SAMPLE_JSONFILE = "device_annotation_config.json";
50
51 private static TestServiceDirectory directory;
52 private static ServiceDirectory original;
53
54 private ObjectMapper mapper;
55
56 private final ConfigApplyDelegate noopDelegate = cfg -> { };
57
58 /**
59 * {@value #SAMPLE_JSONFILE} after parsing.
60 */
61 private JsonNode node;
62
63 // sample data
64 private final DeviceId deviceId = DeviceId.deviceId("of:0000000000000001");
65
66 private final String key = "foo";
67 private final String value = "bar";
68 private final String key1 = "foo1";
69 private final String value1 = "bar1";
70
71
72 // TODO consolidate code-clone in ProtectionConfigTest, and define constants for field name
73 @BeforeClass
74 public static void setUpClass() throws TestUtilsException {
75 directory = new TestServiceDirectory();
76
77 CodecManager codecService = new CodecManager();
78 codecService.activate();
79 directory.add(CodecService.class, codecService);
80
81 // replace service directory used by BaseConfig
82 original = TestUtils.getField(BaseConfig.class, "services");
83 TestUtils.setField(BaseConfig.class, "services", directory);
84 }
85
86 @AfterClass
87 public static void tearDownClass() throws TestUtilsException {
88 TestUtils.setField(BaseConfig.class, "services", original);
89 }
90
91 @Before
92 public void setUp() throws JsonProcessingException, IOException, TestUtilsException {
93
94 mapper = new ObjectMapper();
95 // Jackson configuration for ease of Numeric node comparison
96 // - treat integral number node as long node
97 mapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
98 mapper.setNodeFactory(new JsonNodeFactory(false) {
99 @Override
100 public NumericNode numberNode(int v) {
101 return super.numberNode((long) v);
102 }
103 @Override
104 public NumericNode numberNode(short v) {
105 return super.numberNode((long) v);
106 }
107 });
108
109 InputStream stream = DeviceAnnotationConfig.class
110 .getResourceAsStream(SAMPLE_JSONFILE);
111 JsonNode tree = mapper.readTree(stream);
112
113 node = tree.path("devices")
114 .path(deviceId.toString())
115 .path(DeviceAnnotationConfig.CONFIG_KEY);
116 assertTrue(node.isObject());
117 }
118
119 @Test
120 public void readTest() {
121 DeviceAnnotationConfig sut = new DeviceAnnotationConfig();
122 sut.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, node, mapper, noopDelegate);
123
124 assertThat(sut.subject(), is(deviceId));
125 Map<String, String> annotations = sut.annotations();
126 assertThat(annotations.size(), is(1));
127 assertThat(annotations.get(key), is(value));
128 }
129
130 @Test
131 public void writeEntryTest() throws JsonProcessingException, IOException {
132
133 DeviceAnnotationConfig w = new DeviceAnnotationConfig();
134 w.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate);
135
136 // write equivalent to sample
137 w.annotation(key, value);
138
139 // reparse JSON
140 JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node()));
141
142 DeviceAnnotationConfig sut = new DeviceAnnotationConfig();
143 sut.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
144
145 assertThat(sut.subject(), is(deviceId));
146 Map<String, String> annotations = sut.annotations();
147 assertThat(annotations.size(), is(1));
148 assertThat(annotations.get(key), is(value));
149 }
150
151 @Test
152 public void writeMapTest() throws JsonProcessingException, IOException {
153
154 DeviceAnnotationConfig w = new DeviceAnnotationConfig();
155 w.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate);
156
157 // write equivalent to sample
158 w.annotations(ImmutableMap.of(key, value));
159
160 // reparse JSON
161 JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node()));
162
163 DeviceAnnotationConfig sut = new DeviceAnnotationConfig();
164 sut.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
165
166 assertThat(sut.subject(), is(deviceId));
167 Map<String, String> annotations = sut.annotations();
168 assertThat(annotations.size(), is(1));
169 assertThat(annotations.get(key), is(value));
170 }
171
172 @Test
173 public void removeEntryTest() throws JsonProcessingException, IOException {
174 DeviceAnnotationConfig w = new DeviceAnnotationConfig();
175 w.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate);
176
177 //write equivalent to sample
178 w.annotation(key, value);
179 w.annotation(key1, value1);
180
181 //reparse JSON
182 JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node()));
183
184 DeviceAnnotationConfig sut = new DeviceAnnotationConfig();
185 sut.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
186
187 Map<String, String> annotations = sut.annotations();
188 assertThat(annotations.size(), is(2));
189
190
191 //remove entry
192 w.annotation(key);
193 r = mapper.readTree(mapper.writeValueAsString(w.node()));
194
195 sut = new DeviceAnnotationConfig();
196 sut.init(deviceId, DeviceAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate);
197 annotations = sut.annotations();
198 assertThat(annotations.size(), is(1));
199 assertThat(annotations.get(key1), is(value1));
200 }
201
202}