blob: 32a6b598b52b4c1ea43703544dc372da69c4135d [file] [log] [blame]
Jian Lid1445012017-04-09 02:12:07 +09001/*
Brian O'Connorce2a03d2017-08-03 19:21:03 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lid1445012017-04-09 02:12:07 +09003 *
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.LispListAddress;
30import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
31import org.onosproject.mapping.addresses.MappingAddresses;
Jian Li2e818b02017-04-12 19:28:30 +090032import org.onosproject.mapping.codec.MappingAddressJsonMatcher;
Jian Lid1445012017-04-09 02:12:07 +090033
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 LispListAddressCodec.
43 */
44public class LispListAddressCodecTest {
45
46 private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
47 private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
48
49 private CodecContext context;
50 private JsonCodec<LispListAddress> listAddressCodec;
51 private LispMappingExtensionCodecRegistrator registrator;
52
53 /**
54 * Sets up for each test.
55 * Creates a context and fetches the LispListAddress 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 listAddressCodec = context.codec(LispListAddress.class);
66 assertThat("List address codec should not be null",
67 listAddressCodec, 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 LispListAddress object.
80 */
81 @Test
82 public void testLispListAddressEncode() {
83 LispListAddress address = new LispListAddress.Builder()
84 .withIpv4(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX))
85 .withIpv6(MappingAddresses.ipv6MappingAddress(IPV6_PREFIX))
86 .build();
87 ObjectNode addressJson = listAddressCodec.encode(address, context);
88 assertThat("errors in encoding List address JSON",
89 addressJson, LispListAddressJsonMatcher.matchesListAddress(address));
90 }
91
92 /**
93 * Tests decoding of a LispListAddress JSON object.
94 */
95 @Test
96 public void testLispListAddressDecode() throws IOException {
97 LispListAddress address = getLispListAddress("LispListAddress.json");
98
99 assertThat("incorrect IPv4 address", address.getIpv4(),
100 is(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX)));
101 assertThat("incorrect IPv6 address", address.getIpv6(),
102 is(MappingAddresses.ipv6MappingAddress(IPV6_PREFIX)));
103 }
104
105 /**
106 * Hamcrest matcher for LispListAddress.
107 */
108 public static final class LispListAddressJsonMatcher
109 extends TypeSafeDiagnosingMatcher<JsonNode> {
110
111 private final LispListAddress address;
112
113 /**
114 * Default constructor.
115 *
116 * @param address LispListAddress object
117 */
118 private LispListAddressJsonMatcher(LispListAddress address) {
119 this.address = address;
120 }
121
122 @Override
123 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
124
125 // check ipv4
126 MappingAddressJsonMatcher ipv4Matcher =
127 MappingAddressJsonMatcher.matchesMappingAddress(address.getIpv4());
128
129 // check ipv6
130 MappingAddressJsonMatcher ipv6Matcher =
131 MappingAddressJsonMatcher.matchesMappingAddress(address.getIpv6());
132
133 return ipv4Matcher.matches(jsonNode.get(LispListAddressCodec.IPV4)) ||
134 ipv6Matcher.matches(jsonNode.get(LispListAddressCodec.IPV6));
135
136 }
137
138 @Override
139 public void describeTo(Description description) {
140 description.appendText(address.toString());
141 }
142
143 /**
144 * Factory to allocate a LispListAddress matcher.
145 *
146 * @param address LispListAddress object we are looking for
147 * @return matcher
148 */
149 public static LispListAddressJsonMatcher matchesListAddress(LispListAddress address) {
150 return new LispListAddressJsonMatcher(address);
151 }
152 }
153
154 /**
155 * Reads in a LispListAddress from the given resource and decodes it.
156 *
157 * @param resourceName resource to use to read the JSON for the rule
158 * @return decoded LispListAddress
159 * @throws IOException if processing the resource fails
160 */
161 private LispListAddress getLispListAddress(String resourceName) throws IOException {
162 InputStream jsonStream = LispListAddressCodecTest.class.getResourceAsStream(resourceName);
163 JsonNode json = context.mapper().readTree(jsonStream);
164 assertThat("JSON string should not be null", json, notNullValue());
165 LispListAddress listAddress = listAddressCodec.decode((ObjectNode) json, context);
166 assertThat("decoded address should not be null", listAddress, notNullValue());
167 return listAddress;
168 }
169}