Jian Li | 2c63bd2 | 2018-07-15 23:35:34 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.hamcrest.MatcherAssert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 32 | import org.onosproject.openstacknetworking.impl.DefaultInstancePort; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
| 42 | import static org.onosproject.openstacknetworking.codec.InstancePortJsonMatcher.matchesInstancePort; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for InstancePort codec. |
| 46 | */ |
| 47 | public 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 | } |