blob: 881a9065a16244b662f9bb9b13ef2cd32e923274 [file] [log] [blame]
Jian Lif9ec0832017-04-09 20:49:48 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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.drivers.lisp.extensions.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.IpPrefix;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
30import org.onosproject.drivers.lisp.extensions.LispNonceAddress;
31import org.onosproject.mapping.addresses.MappingAddresses;
32import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
33
34import java.io.IOException;
35import java.io.InputStream;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.is;
39import static org.hamcrest.Matchers.notNullValue;
40
41/**
42 * Unit tests for LispNonceAddressCodec.
43 */
44public class LispNonceAddressCodecTest {
45
46 private static final int NONCE = 1;
47 private static final IpPrefix ADDRESS = IpPrefix.valueOf("10.1.1.0/24");
48
49 private CodecContext context;
50 private JsonCodec<LispNonceAddress> nonceAddressCodec;
51 private LispMappingExtensionCodecRegistrator registrator;
52
53 /**
54 * Sets up for each test.
55 * Creates a context and fetches the LispNonceAddress codec.
56 */
57 @Before
58 public void setUp() {
59 CodecManager manager = new CodecManager();
60 registrator = new LispMappingExtensionCodecRegistrator();
61 registrator.codecService = manager;
62 registrator.activate();
63
64 context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
65 nonceAddressCodec = context.codec(LispNonceAddress.class);
66 assertThat("nonce address codec should not be null",
67 nonceAddressCodec, notNullValue());
68 }
69
70 /**
71 * Deactivates the codec registrator.
72 */
73 @After
74 public void tearDown() {
75 registrator.deactivate();
76 }
77
78 /**
79 * Tests encoding of a LispNonceAddress object.
80 */
81 @Test
82 public void testLispNonceAddressEncode() {
83 LispNonceAddress address = new LispNonceAddress.Builder()
84 .withNonce(NONCE)
85 .withAddress(MappingAddresses.ipv4MappingAddress(ADDRESS))
86 .build();
87 ObjectNode addressJson = nonceAddressCodec.encode(address, context);
88 assertThat("errors in encoding nonce address JSON",
89 addressJson, LispNonceAddressJsonMatcher.matchesNonceAddress(address));
90 }
91
92 /**
93 * Tests decoding of a LispNonceAddress JSON object.
94 */
95 @Test
96 public void testLispNonceAddressDecode() throws IOException {
97 LispNonceAddress address = getLispNonceAddress("LispNonceAddress.json");
98
99 assertThat("incorrect nonce", address.getNonce(), is(NONCE));
100 assertThat("incorrect address", address.getAddress(),
101 is(MappingAddresses.ipv4MappingAddress(ADDRESS)));
102 }
103
104 /**
105 * Hamcrest matcher for LispNonceAddress.
106 */
107 public static final class LispNonceAddressJsonMatcher
108 extends TypeSafeDiagnosingMatcher<JsonNode> {
109
110 private final LispNonceAddress address;
111
112 /**
113 * Default constructor.
114 *
115 * @param address LispNonceAddress object
116 */
117 private LispNonceAddressJsonMatcher(LispNonceAddress address) {
118 this.address = address;
119 }
120
121 @Override
122 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
123
124 // check nonce
125 int jsonNonce = jsonNode.get(LispNonceAddressCodec.NONCE).asInt();
126 int nonce = address.getNonce();
127 if (jsonNonce != nonce) {
128 description.appendText("Nonce was " + jsonNonce);
129 return false;
130 }
131
132 // check address
133 MappingAddressJsonMatcher addressMatcher =
134 MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress());
135
136 return addressMatcher.matches(jsonNode.get(LispNonceAddressCodec.ADDRESS));
137 }
138
139 @Override
140 public void describeTo(Description description) {
141 description.appendText(address.toString());
142 }
143
144 /**
145 * Factory to allocate a LispNonceAddress matcher.
146 *
147 * @param address LispNonceAddress object we are looking for
148 * @return matcher
149 */
150 public static LispNonceAddressJsonMatcher matchesNonceAddress(LispNonceAddress address) {
151 return new LispNonceAddressJsonMatcher(address);
152 }
153 }
154
155 /**
156 * Reads in a LispNonceAddress from the given resource and decodes it.
157 *
158 * @param resourceName resource to use to read the JSON for the rule
159 * @return decoded LispNonceAddress
160 * @throws IOException if processing the resource fails
161 */
162 private LispNonceAddress getLispNonceAddress(String resourceName) throws IOException {
163 InputStream jsonStream = LispNonceAddressCodecTest.class.getResourceAsStream(resourceName);
164 JsonNode json = context.mapper().readTree(jsonStream);
165 assertThat("JSON string should not be null", json, notNullValue());
166 LispNonceAddress nonceAddress = nonceAddressCodec.decode((ObjectNode) json, context);
167 assertThat("decoded address should not be null", nonceAddress, notNullValue());
168 return nonceAddress;
169 }
170}