Jian Li | 3defa84 | 2019-02-12 00:31:35 +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.DefaultK8sApiConfig; |
| 29 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 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.notNullValue; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 42 | import static org.onosproject.k8snode.api.K8sApiConfig.State.CONNECTED; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 43 | import static org.onosproject.k8snode.codec.K8sApiConfigJsonMatcher.matchesK8sApiConfig; |
| 44 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for kubernetes API config codec. |
| 48 | */ |
| 49 | public class K8sApiConfigCodecTest { |
| 50 | |
| 51 | MockCodecContext context; |
| 52 | |
| 53 | JsonCodec<K8sApiConfig> k8sApiConfigCodec; |
| 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 | k8sApiConfigCodec = new K8sApiConfigCodec(); |
| 65 | |
| 66 | assertThat(k8sApiConfigCodec, 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 API config encoding. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testK8sApiConfigEncode() { |
| 79 | K8sApiConfig config = DefaultK8sApiConfig.builder() |
| 80 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 81 | .ipAddress(IpAddress.valueOf("10.10.10.23")) |
| 82 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 83 | .state(CONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 84 | .token("token") |
| 85 | .caCertData("caCertData") |
| 86 | .clientCertData("clientCertData") |
| 87 | .clientKeyData("clientKeyData") |
| 88 | .build(); |
| 89 | |
| 90 | ObjectNode configJson = k8sApiConfigCodec.encode(config, context); |
| 91 | assertThat(configJson, matchesK8sApiConfig(config)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests the kubernetes API config decoding. |
| 96 | * |
| 97 | * @throws IOException IO exception |
| 98 | */ |
| 99 | @Test |
| 100 | public void testK8sApiConfigDecode() throws IOException { |
| 101 | K8sApiConfig config = getK8sApiConfig("K8sApiConfig.json"); |
| 102 | |
| 103 | assertEquals("HTTPS", config.scheme().name()); |
| 104 | assertEquals("10.134.34.223", config.ipAddress().toString()); |
| 105 | assertEquals(6443, config.port()); |
| 106 | assertEquals("token", config.token()); |
| 107 | assertEquals("caCertData", config.caCertData()); |
| 108 | assertEquals("clientCertData", config.clientCertData()); |
| 109 | assertEquals("clientKeyData", config.clientKeyData()); |
| 110 | } |
| 111 | |
| 112 | private K8sApiConfig getK8sApiConfig(String resourceName) throws IOException { |
| 113 | InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName); |
| 114 | JsonNode json = context.mapper().readTree(jsonStream); |
| 115 | assertThat(json, notNullValue()); |
| 116 | K8sApiConfig config = k8sApiConfigCodec.decode((ObjectNode) json, context); |
| 117 | assertThat(config, notNullValue()); |
| 118 | return config; |
| 119 | } |
| 120 | |
| 121 | private class MockCodecContext implements CodecContext { |
| 122 | private final ObjectMapper mapper = new ObjectMapper(); |
| 123 | private final CodecManager manager = new CodecManager(); |
| 124 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 125 | |
| 126 | /** |
| 127 | * Constructs a new mock codec context. |
| 128 | */ |
| 129 | public MockCodecContext() { |
| 130 | manager.activate(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public ObjectMapper mapper() { |
| 135 | return mapper; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | @SuppressWarnings("unchecked") |
| 140 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 141 | if (entityClass == K8sApiConfig.class) { |
| 142 | return (JsonCodec<T>) k8sApiConfigCodec; |
| 143 | } |
| 144 | return manager.getCodec(entityClass); |
| 145 | } |
| 146 | |
| 147 | @SuppressWarnings("unchecked") |
| 148 | @Override |
| 149 | public <T> T getService(Class<T> serviceClass) { |
| 150 | return (T) services.get(serviceClass); |
| 151 | } |
| 152 | |
| 153 | // for registering mock services |
| 154 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 155 | services.put(serviceClass, impl); |
| 156 | } |
| 157 | } |
| 158 | } |