Jian Li | 58b3398 | 2020-07-01 19:07:02 +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.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 com.google.common.collect.ImmutableSet; |
| 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.k8snode.api.DefaultK8sHost; |
| 30 | import org.onosproject.k8snode.api.K8sHost; |
| 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.k8snode.api.K8sHostState.INIT; |
| 44 | import static org.onosproject.k8snode.codec.K8sHostJsonMatcher.matchesK8sHost; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | |
| 47 | public class K8sHostCodecTest { |
| 48 | |
| 49 | MockCodecContext context; |
| 50 | |
| 51 | JsonCodec<K8sHost> k8sHostCodec; |
| 52 | |
| 53 | final CoreService mockCoreService = createMock(CoreService.class); |
| 54 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 55 | |
| 56 | /** |
| 57 | * Initial setup for this unit test. |
| 58 | */ |
| 59 | @Before |
| 60 | public void setUp() { |
| 61 | context = new MockCodecContext(); |
| 62 | k8sHostCodec = new K8sHostCodec(); |
| 63 | |
| 64 | assertThat(k8sHostCodec, notNullValue()); |
| 65 | |
| 66 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 67 | .andReturn(APP_ID).anyTimes(); |
| 68 | replay(mockCoreService); |
| 69 | context.registerService(CoreService.class, mockCoreService); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests the kubernetes host encoding. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testK8sHostEncode() { |
| 77 | K8sHost host = DefaultK8sHost.builder() |
| 78 | .hostIp(IpAddress.valueOf("192.168.200.10")) |
| 79 | .state(INIT) |
| 80 | .nodeNames(ImmutableSet.of("1", "2")) |
| 81 | .build(); |
| 82 | |
| 83 | ObjectNode hostJson = k8sHostCodec.encode(host, context); |
| 84 | assertThat(hostJson, matchesK8sHost(host)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests the kubernetes host decoding. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testK8sHostDecode() throws IOException { |
| 92 | K8sHost host = getK8sHost("K8sHost.json"); |
| 93 | |
| 94 | assertEquals("192.168.200.10", host.hostIp().toString()); |
| 95 | assertEquals("INIT", host.state().name()); |
| 96 | } |
| 97 | |
| 98 | private K8sHost getK8sHost(String resourceName) throws IOException { |
| 99 | InputStream jsonStream = K8sHostCodecTest.class.getResourceAsStream(resourceName); |
| 100 | JsonNode json = context.mapper().readTree(jsonStream); |
| 101 | assertThat(json, notNullValue()); |
| 102 | K8sHost host = k8sHostCodec.decode((ObjectNode) json, context); |
| 103 | assertThat(host, notNullValue()); |
| 104 | return host; |
| 105 | } |
| 106 | |
| 107 | private class MockCodecContext implements CodecContext { |
| 108 | private final ObjectMapper mapper = new ObjectMapper(); |
| 109 | private final CodecManager manager = new CodecManager(); |
| 110 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 111 | |
| 112 | /** |
| 113 | * Constructs a new mock codec context. |
| 114 | */ |
| 115 | public MockCodecContext() { |
| 116 | manager.activate(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public ObjectMapper mapper() { |
| 121 | return mapper; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | @SuppressWarnings("unchecked") |
| 126 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 127 | if (entityClass == K8sHost.class) { |
| 128 | return (JsonCodec<T>) k8sHostCodec; |
| 129 | } |
| 130 | return manager.getCodec(entityClass); |
| 131 | } |
| 132 | |
| 133 | @SuppressWarnings("unchecked") |
| 134 | @Override |
| 135 | public <T> T getService(Class<T> serviceClass) { |
| 136 | return (T) services.get(serviceClass); |
| 137 | } |
| 138 | |
| 139 | // for registering mock services |
| 140 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 141 | services.put(serviceClass, impl); |
| 142 | } |
| 143 | } |
| 144 | } |