blob: 0886a4a1f70e4ae39cebb588fb68823ec9b2ff1a [file] [log] [blame]
Hyunsun Moonf5b21532016-10-25 15:45:41 -07001/*
2 * Copyright 2016-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.opencord.cordvtn.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.NullNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.junit.Before;
23import org.junit.Rule;
24import org.junit.Test;
25import org.junit.rules.ExpectedException;
26import org.onosproject.codec.JsonCodec;
27import org.opencord.cordvtn.api.net.NetworkId;
28import org.opencord.cordvtn.api.net.ProviderNetwork;
29import org.opencord.cordvtn.api.net.ServiceNetwork;
30
31import java.io.IOException;
32import java.io.InputStream;
33
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.is;
36import static org.hamcrest.Matchers.notNullValue;
37import static org.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL;
38import static org.opencord.cordvtn.api.net.ServiceNetwork.ServiceNetworkType.MANAGEMENT_LOCAL;
39import static org.opencord.cordvtn.api.net.ServiceNetwork.ServiceNetworkType.PRIVATE;
40import static org.opencord.cordvtn.codec.ServiceNetworkJsonMatcher.matchesServiceNetwork;
41
42/**
43 * Unit tests for ServiceNetwork codec.
44 */
45public final class ServiceNetworkCodecTest {
46 private static final String SERVICE_NETWORK = "serviceNetwork";
47 private static final String ID = "id";
48 private static final String TYPE = "type";
49 private static final String PROVIDER_NETWORKS = "providerNetworks";
50
51 private final ProviderNetwork providerA =
52 ProviderNetwork.of(NetworkId.of("A"), BIDIRECTIONAL);
53
54 private final ServiceNetwork networkB = new ServiceNetwork(
55 NetworkId.of("A"),
56 MANAGEMENT_LOCAL,
57 ImmutableSet.of());
58
59 private final ServiceNetwork networkA = new ServiceNetwork(
60 NetworkId.of("B"),
61 PRIVATE,
62 ImmutableSet.of(providerA));
63
64 @Rule
65 public ExpectedException exception = ExpectedException.none();
66
67 private JsonCodec<ServiceNetwork> codec;
68 private MockCodecContext context;
69
70 /**
71 * Sets up for each test.
72 * Creates a context and fetches the ServiceNetwork codec.
73 */
74 @Before
75 public void setUp() {
76 context = new MockCodecContext();
77 codec = context.codec(ServiceNetwork.class);
78 assertThat(codec, notNullValue());
79 }
80
81 @Test
82 public void testServiceNetworkEncode() {
83 ObjectNode networkJson = codec.encode(networkA, context);
84 assertThat(networkJson, notNullValue());
85 assertThat(networkJson, matchesServiceNetwork(networkA));
86
87 networkJson = codec.encode(networkB, context);
88 assertThat(networkJson, notNullValue());
89 assertThat(networkJson, matchesServiceNetwork(networkB));
90 }
91
92 @Test
93 public void testServiceNetworkDecode() throws IOException {
94 ServiceNetwork snet = getServiceNetwork("service-network.json");
95 assertThat(snet.id(), is(NetworkId.of("A")));
96 assertThat(snet.type(), is(MANAGEMENT_LOCAL));
97 assertThat(snet.providers(), is(ImmutableSet.of()));
98
99 snet = getServiceNetwork("service-network-with-provider.json");
100 assertThat(snet.id(), is(NetworkId.of("B")));
101 assertThat(snet.type(), is(PRIVATE));
102 assertThat(snet.providers(), is(ImmutableSet.of(providerA)));
103 }
104
105 @Test
106 public void testServiceNetworkDecodeMissingId() throws IllegalArgumentException {
107 final JsonNode jsonMissingId = context.mapper().createObjectNode()
108 .put(TYPE, PRIVATE.name())
109 .put(PROVIDER_NETWORKS, "");
110 exception.expect(IllegalArgumentException.class);
111 codec.decode((ObjectNode) jsonMissingId, context);
112 }
113
114 @Test
115 public void testServiceNetworkDecodeEmptyId() throws IllegalArgumentException {
116 final JsonNode jsonEmptyId = context.mapper().createObjectNode()
117 .put(ID, "")
118 .put(TYPE, PRIVATE.name())
119 .put(PROVIDER_NETWORKS, "");
120 exception.expect(IllegalArgumentException.class);
121 codec.decode((ObjectNode) jsonEmptyId, context);
122 }
123
124 @Test
125 public void testServiceNetworkDecodeNullId() throws NullPointerException {
126 final JsonNode jsonNullId = context.mapper().createObjectNode()
127 .put(TYPE, PRIVATE.name())
128 .put(PROVIDER_NETWORKS, "")
129 .set(ID, NullNode.getInstance());
130 exception.expect(IllegalArgumentException.class);
131 codec.decode((ObjectNode) jsonNullId, context);
132 }
133
134 @Test
135 public void testServiceNetworkDecodeMissingType() throws IllegalArgumentException {
136 final JsonNode jsonMissingType = context.mapper().createObjectNode()
137 .put(ID, "A")
138 .put(PROVIDER_NETWORKS, "");
139 exception.expect(IllegalArgumentException.class);
140 codec.decode((ObjectNode) jsonMissingType, context);
141 }
142
143 @Test
144 public void testServiceNetworkDecodeEmptyType() throws IllegalArgumentException {
145 final JsonNode jsonEmptyType = context.mapper().createObjectNode()
146 .put(ID, "A")
147 .put(TYPE, "")
148 .put(PROVIDER_NETWORKS, "");
149 exception.expect(IllegalArgumentException.class);
150 codec.decode((ObjectNode) jsonEmptyType, context);
151 }
152
153 @Test
154 public void testServiceNetworkDecodeNullType() throws IllegalArgumentException {
155 final JsonNode jsonNullType = context.mapper().createObjectNode()
156 .put(ID, "A")
157 .put(PROVIDER_NETWORKS, "")
158 .set(TYPE, NullNode.getInstance());
159 exception.expect(IllegalArgumentException.class);
160 codec.decode((ObjectNode) jsonNullType, context);
161 }
162
163 @Test
164 public void testServiceNetworkDecodeWrongType() throws IllegalArgumentException {
165 final JsonNode jsonNoneType = context.mapper().createObjectNode()
166 .put(ID, "A")
167 .put(TYPE, "none")
168 .put(PROVIDER_NETWORKS, "");
169 exception.expect(IllegalArgumentException.class);
170 codec.decode((ObjectNode) jsonNoneType, context);
171 }
172
173 @Test
174 public void testServiceNetworkDecodeWithMissingProviderId() throws NullPointerException {
175 final JsonNode jsonMissingProviderId = context.mapper().createObjectNode()
176 .put(TYPE, "B");
177 final JsonNode jsonWithProvider = context.mapper().createObjectNode()
178 .put(ID, "A")
179 .put(TYPE, PRIVATE.name())
180 .set(PROVIDER_NETWORKS, jsonMissingProviderId);
181 exception.expect(NullPointerException.class);
182 codec.decode((ObjectNode) jsonWithProvider, context);
183 }
184
185 @Test
186 public void testServiceNetworkDecodeWithWrongProviderType() throws NullPointerException {
187 final JsonNode jsonWrongProviderType = context.mapper().createObjectNode()
188 .put(ID, "B")
189 .put(TYPE, "none");
190 final JsonNode jsonWithProvider = context.mapper().createObjectNode()
191 .put(ID, "A")
192 .put(TYPE, PRIVATE.name())
193 .set(PROVIDER_NETWORKS, jsonWrongProviderType);
194 exception.expect(NullPointerException.class);
195 codec.decode((ObjectNode) jsonWithProvider, context);
196 }
197
198 private ServiceNetwork getServiceNetwork(String resource) throws IOException {
199 InputStream jsonStream = ServiceNetworkCodecTest.class.getResourceAsStream(resource);
200 JsonNode jsonNode = context.mapper().readTree(jsonStream).get(SERVICE_NETWORK);
201 assertThat(jsonNode, notNullValue());
202
203 ServiceNetwork snet = codec.decode((ObjectNode) jsonNode, context);
204 assertThat(snet, notNullValue());
205 return snet;
206 }
207}