blob: f5eb825f01943b0da04abaca980ea8590083eb78 [file] [log] [blame]
Jian Li45083522017-03-20 18:46:07 +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.mapping.web.codec;
17
Jian Li45083522017-03-20 18:46:07 +090018import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onosproject.codec.CodecContext;
Jian Li45083522017-03-20 18:46:07 +090025import org.onosproject.codec.JsonCodec;
26import org.onosproject.codec.impl.CodecManager;
27import org.onosproject.mapping.addresses.MappingAddress;
28import org.onosproject.mapping.addresses.MappingAddresses;
29import org.onosproject.mapping.web.MappingCodecRegistrator;
30
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.notNullValue;
33import static org.onosproject.mapping.web.codec.MappingAddressJsonMatcher.matchesMappingAddress;
34
35/**
36 * Unit tests for MappingAddressCodec.
37 */
38public class MappingAddressCodecTest {
39
40 private CodecContext context;
41 private JsonCodec<MappingAddress> addressCodec;
Jian Lib54d14b2017-03-28 21:34:34 +090042 private MappingCodecRegistrator registrator;
Jian Li45083522017-03-20 18:46:07 +090043 private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
44 private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
45 private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
46 private static final String DN = "onos";
47 private static final String AS = "AS1000";
48
49 /**
50 * Sets up for each test.
51 * Creates a context and fetches the mapping address codec.
52 */
53 @Before
54 public void setUp() {
55 CodecManager manager = new CodecManager();
56 registrator = new MappingCodecRegistrator();
57 registrator.codecService = manager;
58 registrator.activate();
59
Jian Lifa69be62017-04-05 16:00:10 +090060 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Li45083522017-03-20 18:46:07 +090061
62 addressCodec = context.codec(MappingAddress.class);
63 assertThat(addressCodec, notNullValue());
64 }
65
66 @After
67 public void tearDown() {
68 registrator.deactivate();
69 }
70
71 /**
72 * Tests AS mapping address.
73 */
74 @Test
75 public void asMappingAddressTest() {
76 MappingAddress address = MappingAddresses.asMappingAddress(AS);
77 ObjectNode result = addressCodec.encode(address, context);
78 assertThat(result, matchesMappingAddress(address));
79 }
80
81 /**
82 * Tests DN mapping address.
83 */
84 @Test
85 public void dnMappingAddressTest() {
86 MappingAddress address = MappingAddresses.dnMappingAddress(DN);
87 ObjectNode result = addressCodec.encode(address, context);
88 assertThat(result, matchesMappingAddress(address));
89 }
90
91 /**
92 * Tests IPv4 mapping address.
93 */
94 @Test
95 public void ipv4MappingAddressTest() {
96 MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
97 ObjectNode result = addressCodec.encode(address, context);
98 assertThat(result, matchesMappingAddress(address));
99 }
100
101 /**
102 * Tests IPv6 mapping address.
103 */
104 @Test
105 public void ipv6MappingAddressTest() {
106 MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
107 ObjectNode result = addressCodec.encode(address, context);
108 assertThat(result, matchesMappingAddress(address));
109 }
110
111 /**
112 * Tests Ethernet mapping address.
113 */
114 @Test
115 public void ethMappingAddressTest() {
116 MappingAddress address = MappingAddresses.ethMappingAddress(MAC);
117 ObjectNode result = addressCodec.encode(address, context);
118 assertThat(result, matchesMappingAddress(address));
119 }
Jian Li45083522017-03-20 18:46:07 +0900120}