Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +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.onlab.packet.VlanId; |
| 27 | import org.onosproject.codec.CodecContext; |
| 28 | import org.onosproject.codec.JsonCodec; |
| 29 | import org.onosproject.codec.impl.CodecManager; |
| 30 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
| 31 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 32 | import org.onosproject.openstacknetworking.impl.DefaultExternalPeerRouter; |
| 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.ExternalPeerRouterJsonMatcher.matchesExternalPeerRouter; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for ExternalPeerRouter codec. |
| 46 | */ |
| 47 | public class ExternalPeerRouterCodecTest { |
| 48 | |
| 49 | MockCodecContext context; |
| 50 | JsonCodec<ExternalPeerRouter> externalPeerRouterCodec; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | context = new MockCodecContext(); |
| 55 | externalPeerRouterCodec = new ExternalPeerRouterCodec(); |
| 56 | |
| 57 | assertThat(externalPeerRouterCodec, notNullValue()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests the external peer router encoding. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testExternalPeerRouterEncode() { |
| 65 | ExternalPeerRouter router = DefaultExternalPeerRouter.builder() |
| 66 | .ipAddress(IpAddress.valueOf("10.10.10.1")) |
| 67 | .macAddress(MacAddress.valueOf("11:22:33:44:55:66")) |
| 68 | .vlanId(VlanId.vlanId("1")) |
| 69 | .build(); |
| 70 | |
| 71 | ObjectNode routerJson = externalPeerRouterCodec.encode(router, context); |
| 72 | assertThat(routerJson, matchesExternalPeerRouter(router)); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests the external peer router decoding. |
| 77 | * @throws IOException |
| 78 | */ |
| 79 | @Test |
| 80 | public void testExternalPeerRouterDecode() throws IOException { |
| 81 | ExternalPeerRouter router = getPeerRouter("ExternalPeerRouter.json"); |
| 82 | |
| 83 | assertThat(router.ipAddress(), is(IpAddress.valueOf("10.10.10.1"))); |
| 84 | assertThat(router.macAddress(), is(MacAddress.valueOf("11:22:33:44:55:66"))); |
| 85 | assertThat(router.vlanId(), is(VlanId.vlanId("1"))); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Reads in an external peer router from the given resource and decodes it. |
| 90 | * |
| 91 | * @param resourceName resource to use to read the JSON for the rule |
| 92 | * @return decoded external peer router |
| 93 | * @throws IOException if processing the resource fails |
| 94 | */ |
| 95 | private ExternalPeerRouter getPeerRouter(String resourceName) throws IOException { |
| 96 | InputStream jsonStream = InstancePortCodecTest.class.getResourceAsStream(resourceName); |
| 97 | JsonNode json = context.mapper().readTree(jsonStream); |
| 98 | MatcherAssert.assertThat(json, notNullValue()); |
| 99 | ExternalPeerRouter router = externalPeerRouterCodec.decode((ObjectNode) json, context); |
| 100 | assertThat(router, notNullValue()); |
| 101 | return router; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Mock codec context for use in codec unit tests. |
| 106 | */ |
| 107 | private class MockCodecContext implements CodecContext { |
| 108 | private final ObjectMapper mapper = new ObjectMapper(); |
| 109 | private final CodecManager manager = new CodecManager(); |
| 110 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 111 | |
| 112 | /** |
| 113 | * Constructs a new mock codec context. |
| 114 | */ |
| 115 | public MockCodecContext() { |
| 116 | manager.activate(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public ObjectMapper mapper() { |
| 121 | return mapper; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | @SuppressWarnings("unchecked") |
| 126 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 127 | if (entityClass == InstancePort.class) { |
| 128 | return (JsonCodec<T>) externalPeerRouterCodec; |
| 129 | } |
| 130 | return manager.getCodec(entityClass); |
| 131 | } |
| 132 | |
| 133 | @SuppressWarnings("unchecked") |
| 134 | @Override |
| 135 | public <T> T getService(Class<T> serviceClass) { |
| 136 | return (T) services.get(serviceClass); |
| 137 | } |
| 138 | |
| 139 | // for registering mock services |
| 140 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 141 | services.put(serviceClass, impl); |
| 142 | } |
| 143 | } |
| 144 | } |