blob: 7c5e61c0e51f6b23f543b09f4b31823d50c7b145 [file] [log] [blame]
Jian Li2c63bd22018-07-15 23:35:34 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacknetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.hamcrest.MatcherAssert;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
31import org.onosproject.openstacknetworking.api.InstancePort;
32import org.onosproject.openstacknetworking.impl.DefaultInstancePort;
33
34import java.io.IOException;
35import java.io.InputStream;
36import java.util.HashMap;
37import java.util.Map;
38
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.is;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.onosproject.openstacknetworking.codec.InstancePortJsonMatcher.matchesInstancePort;
43
44/**
45 * Unit tests for InstancePort codec.
46 */
47public class InstancePortCodecTest {
48
49 MockCodecContext context;
50 JsonCodec<InstancePort> instancePortCodec;
51
52
53 @Before
54 public void setUp() {
55 context = new MockCodecContext();
56 instancePortCodec = new InstancePortCodec();
57
58 assertThat(instancePortCodec, notNullValue());
59 }
60
61 /**
62 * Tests the instance port encoding.
63 */
64 @Test
65 public void testInstancePortEncode() {
66 InstancePort port = DefaultInstancePort.builder()
67 .networkId("net-id-1")
68 .portId("port-id-1")
69 .deviceId(DeviceId.deviceId("of:000000000000000a"))
70 .portNumber(PortNumber.portNumber(1, "tap-1"))
71 .ipAddress(IpAddress.valueOf("10.10.10.1"))
72 .macAddress(MacAddress.valueOf("11:22:33:44:55:66"))
73 .state(InstancePort.State.valueOf("ACTIVE"))
74 .build();
75
76 ObjectNode portJson = instancePortCodec.encode(port, context);
77 assertThat(portJson, matchesInstancePort(port));
78 }
79
80 /**
81 * Tests the instance port decoding.
82 */
83 @Test
84 public void testInstancePortDecode() throws IOException {
85 InstancePort port = getInstancePort("InstancePort.json");
86
87 assertThat(port.networkId(), is("net-id-1"));
88 assertThat(port.portId(), is("port-id-1"));
89 assertThat(port.deviceId(), is(DeviceId.deviceId("of:000000000000000a")));
90 assertThat(port.portNumber(), is(PortNumber.portNumber(1, "tap-1")));
91 assertThat(port.ipAddress(), is(IpAddress.valueOf("10.10.10.1")));
92 assertThat(port.macAddress(), is(MacAddress.valueOf("11:22:33:44:55:66")));
93 assertThat(port.state().name(), is("ACTIVE"));
94 }
95
96 /**
97 * Reads in an instance port from the given resource and decodes it.
98 *
99 * @param resourceName resource to use to read the JSON for the rule
100 * @return decoded instance port
101 * @throws IOException if processing the resource fails
102 */
103 private InstancePort getInstancePort(String resourceName) throws IOException {
104 InputStream jsonStream = InstancePortCodecTest.class.getResourceAsStream(resourceName);
105 JsonNode json = context.mapper().readTree(jsonStream);
106 MatcherAssert.assertThat(json, notNullValue());
107 InstancePort port = instancePortCodec.decode((ObjectNode) json, context);
108 assertThat(port, notNullValue());
109 return port;
110 }
111
112 /**
113 * Mock codec context for use in codec unit tests.
114 */
115 private class MockCodecContext implements CodecContext {
116 private final ObjectMapper mapper = new ObjectMapper();
117 private final CodecManager manager = new CodecManager();
118 private final Map<Class<?>, Object> services = new HashMap<>();
119
120 /**
121 * Constructs a new mock codec context.
122 */
123 public MockCodecContext() {
124 manager.activate();
125 }
126
127 @Override
128 public ObjectMapper mapper() {
129 return mapper;
130 }
131
132 @Override
133 @SuppressWarnings("unchecked")
134 public <T> JsonCodec<T> codec(Class<T> entityClass) {
135 if (entityClass == InstancePort.class) {
136 return (JsonCodec<T>) instancePortCodec;
137 }
138 return manager.getCodec(entityClass);
139 }
140
141 @SuppressWarnings("unchecked")
142 @Override
143 public <T> T getService(Class<T> serviceClass) {
144 return (T) services.get(serviceClass);
145 }
146
147 // for registering mock services
148 public <T> void registerService(Class<T> serviceClass, T impl) {
149 services.put(serviceClass, impl);
150 }
151 }
152}