Jian Li | 49109b5 | 2019-01-22 00:17:28 +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.k8snode.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.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 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.k8snode.api.DefaultK8sNode; |
| 29 | import org.onosproject.k8snode.api.K8sNode; |
| 30 | import org.onosproject.k8snode.api.K8sNodeState; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.Map; |
| 37 | |
| 38 | import static junit.framework.TestCase.assertEquals; |
| 39 | import static org.easymock.EasyMock.createMock; |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.replay; |
| 42 | import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | import static org.hamcrest.Matchers.notNullValue; |
| 44 | import static org.onosproject.k8snode.codec.K8sNodeJsonMatcher.matchesK8sNode; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | |
| 47 | /** |
| 48 | * Unit tests for Kubernetes Node codec. |
| 49 | */ |
| 50 | public class K8sNodeCodecTest { |
| 51 | |
| 52 | MockCodecContext context; |
| 53 | |
| 54 | JsonCodec<K8sNode> k8sNodeCodec; |
| 55 | |
| 56 | final CoreService mockCoreService = createMock(CoreService.class); |
| 57 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 58 | |
| 59 | /** |
| 60 | * Initial setup for this unit test. |
| 61 | */ |
| 62 | @Before |
| 63 | public void setUp() { |
| 64 | context = new MockCodecContext(); |
| 65 | k8sNodeCodec = new K8sNodeCodec(); |
| 66 | |
| 67 | assertThat(k8sNodeCodec, notNullValue()); |
| 68 | |
| 69 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 70 | .andReturn(APP_ID).anyTimes(); |
| 71 | replay(mockCoreService); |
| 72 | context.registerService(CoreService.class, mockCoreService); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests the kubernetes minion node encoding. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testK8sMinionNodeEncode() { |
| 80 | K8sNode node = DefaultK8sNode.builder() |
| 81 | .hostname("minion") |
| 82 | .type(K8sNode.Type.MINION) |
| 83 | .state(K8sNodeState.INIT) |
| 84 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 85 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
| 86 | .intgBridge(DeviceId.deviceId("kbr-int")) |
| 87 | .build(); |
| 88 | |
| 89 | ObjectNode nodeJson = k8sNodeCodec.encode(node, context); |
| 90 | assertThat(nodeJson, matchesK8sNode(node)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests the kubernetes minion node decoding. |
| 95 | * |
| 96 | * @throws IOException IO exception |
| 97 | */ |
| 98 | @Test |
| 99 | public void testK8sMinionNodeDecode() throws IOException { |
| 100 | K8sNode node = getK8sNode("K8sMinionNode.json"); |
| 101 | |
| 102 | assertEquals("minion", node.hostname()); |
| 103 | assertEquals("MINION", node.type().name()); |
| 104 | assertEquals("172.16.130.4", node.managementIp().toString()); |
| 105 | assertEquals("172.16.130.4", node.dataIp().toString()); |
| 106 | assertEquals("of:00000000000000a1", node.intgBridge().toString()); |
| 107 | } |
| 108 | |
| 109 | private K8sNode getK8sNode(String resourceName) throws IOException { |
| 110 | InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName); |
| 111 | JsonNode json = context.mapper().readTree(jsonStream); |
| 112 | assertThat(json, notNullValue()); |
| 113 | K8sNode node = k8sNodeCodec.decode((ObjectNode) json, context); |
| 114 | assertThat(node, notNullValue()); |
| 115 | return node; |
| 116 | } |
| 117 | |
| 118 | private class MockCodecContext implements CodecContext { |
| 119 | private final ObjectMapper mapper = new ObjectMapper(); |
| 120 | private final CodecManager manager = new CodecManager(); |
| 121 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 122 | |
| 123 | /** |
| 124 | * Constructs a new mock codec context. |
| 125 | */ |
| 126 | public MockCodecContext() { |
| 127 | manager.activate(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ObjectMapper mapper() { |
| 132 | return mapper; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | @SuppressWarnings("unchecked") |
| 137 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 138 | if (entityClass == K8sNode.class) { |
| 139 | return (JsonCodec<T>) k8sNodeCodec; |
| 140 | } |
| 141 | return manager.getCodec(entityClass); |
| 142 | } |
| 143 | |
| 144 | @SuppressWarnings("unchecked") |
| 145 | @Override |
| 146 | public <T> T getService(Class<T> serviceClass) { |
| 147 | return (T) services.get(serviceClass); |
| 148 | } |
| 149 | |
| 150 | // for registering mock services |
| 151 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 152 | services.put(serviceClass, impl); |
| 153 | } |
| 154 | } |
| 155 | } |