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; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.k8snetworking.api.DefaultK8sIpam; |
| 30 | import org.onosproject.k8snetworking.api.K8sIpam; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.util.HashMap; |
| 35 | import java.util.Map; |
| 36 | |
| 37 | import static junit.framework.TestCase.assertEquals; |
| 38 | import static org.easymock.EasyMock.createMock; |
| 39 | import static org.easymock.EasyMock.expect; |
| 40 | import static org.easymock.EasyMock.replay; |
| 41 | import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | import static org.hamcrest.Matchers.notNullValue; |
| 43 | import static org.onosproject.k8snetworking.codec.K8sIpamJsonMatcher.matchesK8sIpam; |
| 44 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for kubernetes IPAM codec. |
| 48 | */ |
| 49 | public class K8sIpamCodecTest { |
| 50 | |
| 51 | MockCodecContext context; |
| 52 | |
| 53 | JsonCodec<K8sIpam> k8sIpamCodec; |
| 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 | k8sIpamCodec = new K8sIpamCodec(); |
| 65 | |
| 66 | assertThat(k8sIpamCodec, 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 IPAM encoding. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testK8sIpamEncode() { |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame^] | 79 | K8sIpam ipam = new DefaultK8sIpam("network-1-10.10.10.10", |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 80 | IpAddress.valueOf("10.10.10.10"), "network-1"); |
| 81 | |
| 82 | ObjectNode nodeJson = k8sIpamCodec.encode(ipam, context); |
| 83 | assertThat(nodeJson, matchesK8sIpam(ipam)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests the kubernetes IPAM decoding. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testK8sIpamDecode() throws IOException { |
| 91 | K8sIpam ipam = getK8sIpam("K8sIpam.json"); |
| 92 | |
| 93 | assertEquals("10.10.10.2", ipam.ipAddress().toString()); |
| 94 | assertEquals("network-1", ipam.networkId()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Reads in an kubernetes IPAM from the given resource and decodes it. |
| 99 | * |
| 100 | * @param resourceName resource to use to read the JSON for the rule |
| 101 | * @return decoded kubernetes node |
| 102 | * @throws IOException if processing the resource fails |
| 103 | */ |
| 104 | private K8sIpam getK8sIpam(String resourceName) throws IOException { |
| 105 | InputStream jsonStream = K8sIpamCodecTest.class.getResourceAsStream(resourceName); |
| 106 | JsonNode json = context.mapper().readTree(jsonStream); |
| 107 | MatcherAssert.assertThat(json, notNullValue()); |
| 108 | K8sIpam node = k8sIpamCodec.decode((ObjectNode) json, context); |
| 109 | assertThat(node, notNullValue()); |
| 110 | return node; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Mock codec context for use in codec unit tests. |
| 115 | */ |
| 116 | private class MockCodecContext implements CodecContext { |
| 117 | private final ObjectMapper mapper = new ObjectMapper(); |
| 118 | private final CodecManager manager = new CodecManager(); |
| 119 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 120 | |
| 121 | /** |
| 122 | * Constructs a new mock codec context. |
| 123 | */ |
| 124 | public MockCodecContext() { |
| 125 | manager.activate(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public ObjectMapper mapper() { |
| 130 | return mapper; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | @SuppressWarnings("unchecked") |
| 135 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 136 | if (entityClass == K8sIpam.class) { |
| 137 | return (JsonCodec<T>) k8sIpamCodec; |
| 138 | } |
| 139 | return manager.getCodec(entityClass); |
| 140 | } |
| 141 | |
| 142 | @SuppressWarnings("unchecked") |
| 143 | @Override |
| 144 | public <T> T getService(Class<T> serviceClass) { |
| 145 | return (T) services.get(serviceClass); |
| 146 | } |
| 147 | |
| 148 | // for registering mock services |
| 149 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 150 | services.put(serviceClass, impl); |
| 151 | } |
| 152 | } |
| 153 | } |