Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 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 | import static org.onosproject.net.ConnectPoint.deviceConnectPoint; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.junit.AfterClass; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.BeforeClass; |
| 29 | import org.junit.Test; |
| 30 | import org.onlab.junit.TestUtils; |
| 31 | import org.onlab.junit.TestUtils.TestUtilsException; |
| 32 | import org.onlab.osgi.ServiceDirectory; |
| 33 | import org.onlab.osgi.TestServiceDirectory; |
| 34 | import org.onosproject.codec.CodecService; |
| 35 | import org.onosproject.codec.impl.CodecManager; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.config.BaseConfig; |
| 38 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 39 | |
| 40 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 41 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 42 | import com.fasterxml.jackson.databind.JsonNode; |
| 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 45 | import com.fasterxml.jackson.databind.node.NumericNode; |
| 46 | import com.google.common.collect.ImmutableMap; |
| 47 | |
| 48 | public 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 | |
| 129 | public void writeEntryTest() throws JsonProcessingException, IOException { |
| 130 | |
| 131 | PortAnnotationConfig w = new PortAnnotationConfig(); |
| 132 | w.init(cp, PortAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate); |
| 133 | |
| 134 | // write equivalent to sample |
| 135 | w.annotation(key, value); |
| 136 | |
| 137 | // reparse JSON |
| 138 | JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node())); |
| 139 | |
| 140 | PortAnnotationConfig sut = new PortAnnotationConfig(); |
| 141 | sut.init(cp, PortAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate); |
| 142 | |
| 143 | assertThat(sut.subject(), is(cp)); |
| 144 | Map<String, String> annotations = sut.annotations(); |
| 145 | assertThat(annotations.size(), is(1)); |
| 146 | assertThat(annotations.get(key), is(value)); |
| 147 | } |
| 148 | |
| 149 | public void writeMapTest() throws JsonProcessingException, IOException { |
| 150 | |
| 151 | PortAnnotationConfig w = new PortAnnotationConfig(); |
| 152 | w.init(cp, PortAnnotationConfig.CONFIG_KEY, mapper.createObjectNode(), mapper, noopDelegate); |
| 153 | |
| 154 | // write equivalent to sample |
| 155 | w.annotations(ImmutableMap.of(key, value)); |
| 156 | |
| 157 | // reparse JSON |
| 158 | JsonNode r = mapper.readTree(mapper.writeValueAsString(w.node())); |
| 159 | |
| 160 | PortAnnotationConfig sut = new PortAnnotationConfig(); |
| 161 | sut.init(cp, PortAnnotationConfig.CONFIG_KEY, r, mapper, noopDelegate); |
| 162 | |
| 163 | assertThat(sut.subject(), is(cp)); |
| 164 | Map<String, String> annotations = sut.annotations(); |
| 165 | assertThat(annotations.size(), is(1)); |
| 166 | assertThat(annotations.get(key), is(value)); |
| 167 | } |
| 168 | |
| 169 | } |