blob: e56831dd3cb30d237639f9be1487621adecf7890 [file] [log] [blame]
Jian Li45083522017-03-20 18:46:07 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li45083522017-03-20 18:46: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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Li45083522017-03-20 18:46:07 +090017
Jian Li124ecf02017-04-05 16:38:34 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Li45083522017-03-20 18:46:07 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onosproject.codec.CodecContext;
Jian Li45083522017-03-20 18:46:07 +090026import org.onosproject.codec.JsonCodec;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.mapping.addresses.MappingAddress;
29import org.onosproject.mapping.addresses.MappingAddresses;
Jian Li2e818b02017-04-12 19:28:30 +090030import org.onosproject.mapping.MappingCodecRegistrator;
Jian Li45083522017-03-20 18:46:07 +090031
Jian Li124ecf02017-04-05 16:38:34 +090032import java.io.IOException;
33import java.io.InputStream;
34
Jian Li45083522017-03-20 18:46:07 +090035import static org.hamcrest.MatcherAssert.assertThat;
Jian Li124ecf02017-04-05 16:38:34 +090036import static org.hamcrest.Matchers.is;
Jian Li45083522017-03-20 18:46:07 +090037import static org.hamcrest.Matchers.notNullValue;
Jian Li2e818b02017-04-12 19:28:30 +090038import static org.onosproject.mapping.codec.MappingAddressJsonMatcher.matchesMappingAddress;
Jian Li45083522017-03-20 18:46:07 +090039
40/**
41 * Unit tests for MappingAddressCodec.
42 */
43public class MappingAddressCodecTest {
44
45 private CodecContext context;
46 private JsonCodec<MappingAddress> addressCodec;
Jian Lib54d14b2017-03-28 21:34:34 +090047 private MappingCodecRegistrator registrator;
Jian Li124ecf02017-04-05 16:38:34 +090048 private static final String IPV4_STRING = "1.2.3.4";
49 private static final String PORT_STRING = "32";
50 private static final IpPrefix IPV4_PREFIX =
51 IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING);
Jian Li45083522017-03-20 18:46:07 +090052 private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
53 private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
54 private static final String DN = "onos";
55 private static final String AS = "AS1000";
56
57 /**
58 * Sets up for each test.
59 * Creates a context and fetches the mapping address codec.
60 */
61 @Before
62 public void setUp() {
63 CodecManager manager = new CodecManager();
64 registrator = new MappingCodecRegistrator();
65 registrator.codecService = manager;
66 registrator.activate();
67
Jian Lifa69be62017-04-05 16:00:10 +090068 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Li45083522017-03-20 18:46:07 +090069
70 addressCodec = context.codec(MappingAddress.class);
71 assertThat(addressCodec, notNullValue());
72 }
73
74 @After
75 public void tearDown() {
76 registrator.deactivate();
77 }
78
79 /**
80 * Tests AS mapping address.
81 */
82 @Test
83 public void asMappingAddressTest() {
84 MappingAddress address = MappingAddresses.asMappingAddress(AS);
85 ObjectNode result = addressCodec.encode(address, context);
86 assertThat(result, matchesMappingAddress(address));
87 }
88
89 /**
90 * Tests DN mapping address.
91 */
92 @Test
93 public void dnMappingAddressTest() {
94 MappingAddress address = MappingAddresses.dnMappingAddress(DN);
95 ObjectNode result = addressCodec.encode(address, context);
96 assertThat(result, matchesMappingAddress(address));
97 }
98
99 /**
100 * Tests IPv4 mapping address.
101 */
102 @Test
103 public void ipv4MappingAddressTest() {
104 MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
105 ObjectNode result = addressCodec.encode(address, context);
106 assertThat(result, matchesMappingAddress(address));
107 }
108
109 /**
110 * Tests IPv6 mapping address.
111 */
112 @Test
113 public void ipv6MappingAddressTest() {
114 MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
115 ObjectNode result = addressCodec.encode(address, context);
116 assertThat(result, matchesMappingAddress(address));
117 }
118
119 /**
120 * Tests Ethernet mapping address.
121 */
122 @Test
123 public void ethMappingAddressTest() {
124 MappingAddress address = MappingAddresses.ethMappingAddress(MAC);
125 ObjectNode result = addressCodec.encode(address, context);
126 assertThat(result, matchesMappingAddress(address));
127 }
Jian Li124ecf02017-04-05 16:38:34 +0900128
129 /**
130 * Tests the decoding of mapping address from JSON object.
131 *
132 * @throws IOException if processing the resource fails
133 */
134 @Test
135 public void testMappingAddressDecode() throws IOException {
136 MappingAddress address = getAddress("MappingAddress.json");
137 assertThat(address.toString(),
138 is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
139 }
140
141 /**
142 * Reads in a mapping address from the given resource and decodes it.
143 *
144 * @param resourceName resource to use to read the JSON for the rule
145 * @return decoded mappingAddress
146 * @throws IOException if processing the resource fails
147 */
148 private MappingAddress getAddress(String resourceName) throws IOException {
149 InputStream jsonStream = MappingAddressCodecTest.class.getResourceAsStream(resourceName);
150 JsonNode json = context.mapper().readTree(jsonStream);
151 assertThat(json, notNullValue());
152 MappingAddress address = addressCodec.decode((ObjectNode) json, context);
153 assertThat(address, notNullValue());
154 return address;
155 }
Jian Li45083522017-03-20 18:46:07 +0900156}