Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.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.kubevirtnode.api.DefaultKubevirtApiConfig; |
| 29 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 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; |
| 42 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS; |
| 43 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.CONNECTED; |
| 44 | import static org.onosproject.kubevirtnode.codec.KubevirtApiConfigJsonMatcher.matchesKubevirtApiConfig; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | |
| 47 | /** |
| 48 | * Unit tests for KubeVirt API config codec. |
| 49 | */ |
| 50 | public class KubevirtApiConfigCodecTest { |
| 51 | |
| 52 | MockCodecContext context; |
| 53 | |
| 54 | JsonCodec<KubevirtApiConfig> kubevirtApiConfigCodec; |
| 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 | kubevirtApiConfigCodec = new KubevirtApiConfigCodec(); |
| 66 | |
| 67 | assertThat(kubevirtApiConfigCodec, 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 KubeVirt API config encoding. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testKubevirtApiConfigEncode() { |
| 80 | KubevirtApiConfig config = DefaultKubevirtApiConfig.builder() |
| 81 | .scheme(HTTPS) |
| 82 | .ipAddress(IpAddress.valueOf("10.10.10.23")) |
| 83 | .port(6443) |
| 84 | .state(CONNECTED) |
| 85 | .token("token") |
| 86 | .caCertData("caCertData") |
| 87 | .clientCertData("clientCertData") |
| 88 | .clientKeyData("clientKeyData") |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 89 | .serviceFqdn("kubevirt.edgestack.svc.cluster.local") |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 90 | .apiServerFqdn("kubernetes.default.svc.cluster.local") |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 91 | .controllerIp(IpAddress.valueOf("127.0.0.1")) |
Daniel Park | 2ba66e2 | 2022-04-27 12:33:45 +0900 | [diff] [blame] | 92 | .datacenterId("BD") |
| 93 | .clusterId("BD-MEH-CT01") |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 94 | .build(); |
Daniel Park | 2ba66e2 | 2022-04-27 12:33:45 +0900 | [diff] [blame] | 95 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 96 | ObjectNode configJson = kubevirtApiConfigCodec.encode(config, context); |
| 97 | assertThat(configJson, matchesKubevirtApiConfig(config)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the KubeVirt API config decoding. |
| 102 | * |
| 103 | * @throws IOException IO exception |
| 104 | */ |
| 105 | @Test |
| 106 | public void testKubevirtApiConfigDecode() throws IOException { |
| 107 | KubevirtApiConfig config = getKubevirtApiConfig("KubevirtApiConfig.json"); |
| 108 | |
| 109 | assertEquals("HTTPS", config.scheme().name()); |
| 110 | assertEquals("10.134.34.223", config.ipAddress().toString()); |
| 111 | assertEquals(6443, config.port()); |
| 112 | assertEquals("token", config.token()); |
| 113 | assertEquals("caCertData", config.caCertData()); |
| 114 | assertEquals("clientCertData", config.clientCertData()); |
| 115 | assertEquals("clientKeyData", config.clientKeyData()); |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 116 | assertEquals("kubevirt.edgestack.svc.cluster.local", config.serviceFqdn()); |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame] | 117 | assertEquals("kubernetes.default.svc.cluster.local", config.apiServerFqdn()); |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 118 | assertEquals("127.0.0.1", config.controllerIp().toString()); |
Daniel Park | 2ba66e2 | 2022-04-27 12:33:45 +0900 | [diff] [blame] | 119 | assertEquals("BD", config.datacenterId()); |
| 120 | assertEquals("BD-MEH-CT01", config.clusterId()); |
| 121 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private KubevirtApiConfig getKubevirtApiConfig(String resourceName) throws IOException { |
| 125 | InputStream jsonStream = KubevirtNodeCodecTest.class.getResourceAsStream(resourceName); |
| 126 | JsonNode json = context.mapper().readTree(jsonStream); |
| 127 | assertThat(json, notNullValue()); |
| 128 | KubevirtApiConfig config = kubevirtApiConfigCodec.decode((ObjectNode) json, context); |
| 129 | assertThat(config, notNullValue()); |
| 130 | return config; |
| 131 | } |
| 132 | |
| 133 | private class MockCodecContext implements CodecContext { |
| 134 | private final ObjectMapper mapper = new ObjectMapper(); |
| 135 | private final CodecManager manager = new CodecManager(); |
| 136 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 137 | |
| 138 | /** |
| 139 | * Constructs a new mock codec context. |
| 140 | */ |
| 141 | public MockCodecContext() { |
| 142 | manager.activate(); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public ObjectMapper mapper() { |
| 147 | return mapper; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | @SuppressWarnings("unchecked") |
| 152 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 153 | if (entityClass == KubevirtApiConfig.class) { |
| 154 | return (JsonCodec<T>) kubevirtApiConfigCodec; |
| 155 | } |
| 156 | return manager.getCodec(entityClass); |
| 157 | } |
| 158 | |
| 159 | @SuppressWarnings("unchecked") |
| 160 | @Override |
| 161 | public <T> T getService(Class<T> serviceClass) { |
| 162 | return (T) services.get(serviceClass); |
| 163 | } |
| 164 | |
| 165 | // for registering mock services |
| 166 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 167 | services.put(serviceClass, impl); |
| 168 | } |
| 169 | } |
| 170 | } |