blob: 41913c343b9650b097edcabba807559c08d83f55 [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
18import com.fasterxml.jackson.databind.ObjectMapper;
19import 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;
26import org.onosproject.codec.CodecService;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.mapping.addresses.MappingAddress;
30import org.onosproject.mapping.addresses.MappingAddresses;
31import org.onosproject.mapping.web.MappingCodecRegistrator;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.notNullValue;
35import static org.onosproject.mapping.web.codec.MappingAddressJsonMatcher.matchesMappingAddress;
36
37/**
38 * Unit tests for MappingAddressCodec.
39 */
40public class MappingAddressCodecTest {
41
42 private CodecContext context;
43 private JsonCodec<MappingAddress> addressCodec;
Jian Lib54d14b2017-03-28 21:34:34 +090044 private MappingCodecRegistrator registrator;
Jian Li45083522017-03-20 18:46:07 +090045 private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
46 private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
47 private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
48 private static final String DN = "onos";
49 private static final String AS = "AS1000";
50
51 /**
52 * Sets up for each test.
53 * Creates a context and fetches the mapping address codec.
54 */
55 @Before
56 public void setUp() {
57 CodecManager manager = new CodecManager();
58 registrator = new MappingCodecRegistrator();
59 registrator.codecService = manager;
60 registrator.activate();
61
62 context = new MappingTestContext(registrator.codecService);
63
64 addressCodec = context.codec(MappingAddress.class);
65 assertThat(addressCodec, notNullValue());
66 }
67
68 @After
69 public void tearDown() {
70 registrator.deactivate();
71 }
72
73 /**
74 * Tests AS mapping address.
75 */
76 @Test
77 public void asMappingAddressTest() {
78 MappingAddress address = MappingAddresses.asMappingAddress(AS);
79 ObjectNode result = addressCodec.encode(address, context);
80 assertThat(result, matchesMappingAddress(address));
81 }
82
83 /**
84 * Tests DN mapping address.
85 */
86 @Test
87 public void dnMappingAddressTest() {
88 MappingAddress address = MappingAddresses.dnMappingAddress(DN);
89 ObjectNode result = addressCodec.encode(address, context);
90 assertThat(result, matchesMappingAddress(address));
91 }
92
93 /**
94 * Tests IPv4 mapping address.
95 */
96 @Test
97 public void ipv4MappingAddressTest() {
98 MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
99 ObjectNode result = addressCodec.encode(address, context);
100 assertThat(result, matchesMappingAddress(address));
101 }
102
103 /**
104 * Tests IPv6 mapping address.
105 */
106 @Test
107 public void ipv6MappingAddressTest() {
108 MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
109 ObjectNode result = addressCodec.encode(address, context);
110 assertThat(result, matchesMappingAddress(address));
111 }
112
113 /**
114 * Tests Ethernet mapping address.
115 */
116 @Test
117 public void ethMappingAddressTest() {
118 MappingAddress address = MappingAddresses.ethMappingAddress(MAC);
119 ObjectNode result = addressCodec.encode(address, context);
120 assertThat(result, matchesMappingAddress(address));
121 }
122
123 /**
124 * Test mapping codec context.
125 */
126 private class MappingTestContext implements CodecContext {
127 private final ObjectMapper mapper = new ObjectMapper();
128 private final CodecService manager;
129
130 /**
131 * Constructs a new mock codec context.
132 */
133 public MappingTestContext(CodecService manager) {
134 this.manager = manager;
135 }
136
137 @Override
138 public ObjectMapper mapper() {
139 return mapper;
140 }
141
142 @Override
143 public <T> JsonCodec<T> codec(Class<T> entityClass) {
144 return manager.getCodec(entityClass);
145 }
146
147 @Override
148 public <T> T getService(Class<T> serviceClass) {
149 return null;
150 }
151 }
152}