Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [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.net.config.basics; |
| 17 | |
| 18 | import static org.hamcrest.Matchers.is; |
| 19 | import static org.junit.Assert.*; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.junit.AfterClass; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.BeforeClass; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.junit.TestUtils; |
| 30 | import org.onlab.junit.TestUtils.TestUtilsException; |
| 31 | import org.onlab.osgi.ServiceDirectory; |
| 32 | import org.onlab.osgi.TestServiceDirectory; |
| 33 | import org.onosproject.codec.CodecService; |
| 34 | import org.onosproject.codec.impl.CodecManager; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.config.BaseConfig; |
| 37 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 38 | |
| 39 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 40 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 41 | import com.fasterxml.jackson.databind.JsonNode; |
| 42 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 43 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 44 | import com.fasterxml.jackson.databind.node.NumericNode; |
| 45 | import com.google.common.collect.ImmutableMap; |
| 46 | |
| 47 | public 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 | } |