blob: 25d6ff435eff7dd9101ebe92f9a9d06ad78a038d [file] [log] [blame]
Jian Li5a577a32017-04-04 12:37:53 +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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Li5a577a32017-04-04 12:37:53 +090017
18import com.fasterxml.jackson.databind.JsonNode;
Jian Li5a577a32017-04-04 12:37:53 +090019import 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;
Jian Li5a577a32017-04-04 12:37:53 +090027import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.mapping.DefaultMappingKey;
30import org.onosproject.mapping.MappingKey;
31import org.onosproject.mapping.addresses.MappingAddress;
32import org.onosproject.mapping.addresses.MappingAddresses;
Jian Li2e818b02017-04-12 19:28:30 +090033import org.onosproject.mapping.MappingCodecRegistrator;
Jian Li5a577a32017-04-04 12:37:53 +090034
35import java.io.IOException;
36import java.io.InputStream;
37
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.is;
40import static org.hamcrest.Matchers.notNullValue;
41
42/**
43 * Unit tests for MappingKeyCodec.
44 */
45public class MappingKeyCodecTest {
46
47 private static final String IPV4_STRING = "1.2.3.4";
48 private static final String PORT_STRING = "32";
49 private static final IpPrefix IPV4_PREFIX =
50 IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING);
51
52 private CodecContext context;
53 private JsonCodec<MappingKey> keyCodec;
54 private MappingCodecRegistrator registrator;
55
56 /**
57 * Sets up for each test.
58 * Creates a context and fetches the mapping key codec.
59 */
60 @Before
61 public void setUp() {
62 CodecManager manager = new CodecManager();
63 registrator = new MappingCodecRegistrator();
64 registrator.codecService = manager;
65 registrator.activate();
66
Jian Lifa69be62017-04-05 16:00:10 +090067 context = new MappingCodecContextAdapter(registrator.codecService);
Jian Li5a577a32017-04-04 12:37:53 +090068 keyCodec = context.codec(MappingKey.class);
69 assertThat(keyCodec, notNullValue());
70 }
71
72 /**
73 * Deactivates the codec registrator.
74 */
75 @After
76 public void tearDown() {
77 registrator.deactivate();
78 }
79
80 /**
81 * Tests encoding of a mapping key object.
82 */
83 @Test
84 public void testMappingKeyEncode() {
85
86 MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
87
88 MappingKey key = DefaultMappingKey.builder()
89 .withAddress(address)
90 .build();
91
92 ObjectNode keyJson = keyCodec.encode(key, context);
93 assertThat(keyJson, MappingKeyJsonMatcher.matchesMappingKey(key));
94 }
95
96 /**
97 * Tests decoding of a mapping key JSON object.
98 */
99 @Test
100 public void testMappingKeyDecode() throws IOException {
101 MappingKey key = getKey("MappingKey.json");
102 assertThat(key.address().toString(),
103 is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
104 }
105
106 /**
107 * Hamcrest matcher for mapping key.
108 */
109 public static final class MappingKeyJsonMatcher
110 extends TypeSafeDiagnosingMatcher<JsonNode> {
111
112 private final MappingKey mappingKey;
113
114 /**
115 * A default constructor.
116 *
117 * @param mappingKey mapping key
118 */
119 private MappingKeyJsonMatcher(MappingKey mappingKey) {
120 this.mappingKey = mappingKey;
121 }
122
123 @Override
124 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
125
126 // check address
127 final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS);
128
129 assertThat(jsonAddressNode,
130 MappingAddressJsonMatcher.matchesMappingAddress(mappingKey.address()));
131
132 return true;
133 }
134
135 @Override
136 public void describeTo(Description description) {
137 description.appendText(mappingKey.toString());
138 }
139
140 /**
141 * Factory to allocate a mapping treatment.
142 *
143 * @param mappingKey mapping treatment object we are looking for
144 * @return matcher
145 */
146 static MappingKeyJsonMatcher matchesMappingKey(MappingKey mappingKey) {
147 return new MappingKeyJsonMatcher(mappingKey);
148 }
149 }
150
151 /**
Jian Li5a577a32017-04-04 12:37:53 +0900152 * Reads in a mapping key from the given resource and decodes it.
153 *
154 * @param resourceName resource to use to read the JSON for the rule
155 * @return decoded mappingKey
156 * @throws IOException if processing the resource fails
157 */
158 private MappingKey getKey(String resourceName) throws IOException {
159 InputStream jsonStream = MappingKeyCodecTest.class.getResourceAsStream(resourceName);
160 JsonNode json = context.mapper().readTree(jsonStream);
161 assertThat(json, notNullValue());
162 MappingKey key = keyCodec.decode((ObjectNode) json, context);
163 assertThat(key, notNullValue());
164 return key;
165 }
166}