Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.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; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.codec.impl.CodecManager; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.k8snetworking.api.DefaultK8sNetwork; |
| 29 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
| 33 | import java.util.HashMap; |
| 34 | import java.util.Map; |
| 35 | |
| 36 | import static junit.framework.TestCase.assertEquals; |
| 37 | import static org.easymock.EasyMock.createMock; |
| 38 | import static org.easymock.EasyMock.expect; |
| 39 | import static org.easymock.EasyMock.replay; |
| 40 | import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | import static org.hamcrest.Matchers.is; |
| 42 | import static org.hamcrest.Matchers.notNullValue; |
| 43 | import static org.onosproject.k8snetworking.codec.K8sNetworkJsonMatcher.matchesK8sNetwork; |
| 44 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for kubernetes network codec. |
| 48 | */ |
| 49 | public class K8sNetworkCodecTest { |
| 50 | |
| 51 | MockCodecContext context; |
| 52 | |
| 53 | JsonCodec<K8sNetwork> k8sNetworkCodec; |
| 54 | |
| 55 | final CoreService mockCoreService = createMock(CoreService.class); |
| 56 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 57 | |
| 58 | /** |
| 59 | * Initial setup for this unit test. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | context = new MockCodecContext(); |
| 64 | k8sNetworkCodec = new K8sNetworkCodec(); |
| 65 | |
| 66 | assertThat(k8sNetworkCodec, notNullValue()); |
| 67 | |
| 68 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 69 | .andReturn(APP_ID).anyTimes(); |
| 70 | replay(mockCoreService); |
| 71 | context.registerService(CoreService.class, mockCoreService); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests the kubernetes network encoding. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testK8sNetworkEncode() { |
| 79 | K8sNetwork network = DefaultK8sNetwork.builder() |
| 80 | .networkId("network-1") |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 81 | .name("network-1") |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 82 | .segmentId("1") |
| 83 | .type(K8sNetwork.Type.VXLAN) |
Jian Li | 5a9c2bb | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 84 | .cidr("10.10.0.0/24") |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 85 | .mtu(1500) |
| 86 | .build(); |
| 87 | |
| 88 | ObjectNode nodeJson = k8sNetworkCodec.encode(network, context); |
| 89 | assertThat(nodeJson, matchesK8sNetwork(network)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests the kubernetes network decoding. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testK8sNetworkDecode() throws IOException { |
| 97 | K8sNetwork network = getK8sNetwork("K8sNetwork.json"); |
| 98 | |
| 99 | assertEquals("network-1", network.networkId()); |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 100 | assertEquals("network-1", network.name()); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 101 | assertEquals("1", network.segmentId()); |
| 102 | assertEquals("VXLAN", network.type().name()); |
Jian Li | 5a9c2bb | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 103 | assertEquals("10.10.0.1", network.gatewayIp().toString()); |
| 104 | assertEquals("10.10.0.0/24", network.cidr()); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 105 | assertThat(network.mtu(), is(1500)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Reads in an kubernetes network from the given resource and decodes it. |
| 110 | * |
| 111 | * @param resourceName resource to use to read the JSON for the rule |
| 112 | * @return decoded kubernetes network |
| 113 | * @throws IOException if processing the resource fails |
| 114 | */ |
| 115 | private K8sNetwork getK8sNetwork(String resourceName) throws IOException { |
| 116 | InputStream jsonStream = K8sNetworkCodecTest.class.getResourceAsStream(resourceName); |
| 117 | JsonNode json = context.mapper().readTree(jsonStream); |
| 118 | MatcherAssert.assertThat(json, notNullValue()); |
| 119 | K8sNetwork network = k8sNetworkCodec.decode((ObjectNode) json, context); |
| 120 | assertThat(network, notNullValue()); |
| 121 | return network; |
| 122 | } |
| 123 | |
| 124 | private class MockCodecContext implements CodecContext { |
| 125 | private final ObjectMapper mapper = new ObjectMapper(); |
| 126 | private final CodecManager manager = new CodecManager(); |
| 127 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 128 | |
| 129 | /** |
| 130 | * Constructs a new mock codec context. |
| 131 | */ |
| 132 | public MockCodecContext() { |
| 133 | manager.activate(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public ObjectMapper mapper() { |
| 138 | return mapper; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | @SuppressWarnings("unchecked") |
| 143 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 144 | if (entityClass == K8sNetwork.class) { |
| 145 | return (JsonCodec<T>) k8sNetworkCodec; |
| 146 | } |
| 147 | return manager.getCodec(entityClass); |
| 148 | } |
| 149 | |
| 150 | @SuppressWarnings("unchecked") |
| 151 | @Override |
| 152 | public <T> T getService(Class<T> serviceClass) { |
| 153 | return (T) services.get(serviceClass); |
| 154 | } |
| 155 | |
| 156 | // for registering mock services |
| 157 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 158 | services.put(serviceClass, impl); |
| 159 | } |
| 160 | } |
| 161 | } |