Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.hamcrest.MatcherAssert; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.codec.impl.MockCodecContext; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | import static junit.framework.TestCase.assertEquals; |
| 34 | import static org.easymock.EasyMock.createMock; |
| 35 | import static org.easymock.EasyMock.expect; |
| 36 | import static org.easymock.EasyMock.replay; |
| 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | import static org.hamcrest.Matchers.notNullValue; |
| 39 | import static org.onosproject.kubevirtnetworking.codec.KubevirtFloatingIpJsonMatcher.matchesKubevirtFloatingIp; |
| 40 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for KubevirtFloatingIp codec. |
| 44 | */ |
| 45 | public final class KubevirtFloatingIpCodecTest { |
| 46 | |
| 47 | MockCodecContext context; |
| 48 | |
| 49 | JsonCodec<KubevirtFloatingIp> kubevirtFloatingIpCodec; |
| 50 | |
| 51 | final CoreService mockCoreService = createMock(CoreService.class); |
| 52 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 53 | |
| 54 | @Before |
| 55 | public void setUp() { |
| 56 | context = new MockCodecContext(); |
| 57 | kubevirtFloatingIpCodec = new KubevirtFloatingIpCodec(); |
| 58 | |
| 59 | assertThat(kubevirtFloatingIpCodec, notNullValue()); |
| 60 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 61 | .andReturn(APP_ID).anyTimes(); |
| 62 | replay(mockCoreService); |
| 63 | context.registerService(CoreService.class, mockCoreService); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests the kubevirt floating IP encoding. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testKubevirtFloatingIpEncode() { |
| 71 | KubevirtFloatingIp floatingIp = DefaultKubevirtFloatingIp.builder() |
| 72 | .id("fip-id") |
| 73 | .routerName("router-1") |
| 74 | .floatingIp(IpAddress.valueOf("10.10.10.10")) |
| 75 | .podName("pod-1") |
| 76 | .fixedIp(IpAddress.valueOf("20.20.20.20")) |
| 77 | .build(); |
| 78 | |
| 79 | ObjectNode floatingIpJson = kubevirtFloatingIpCodec.encode(floatingIp, context); |
| 80 | assertThat(floatingIpJson, matchesKubevirtFloatingIp(floatingIp)); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testKubevirtFloatingIpDecode() throws IOException { |
| 85 | KubevirtFloatingIp floatingIp = getKubevirtFloatingIp("KubevirtFloatingIp.json"); |
| 86 | |
| 87 | assertEquals("fip-1", floatingIp.id()); |
| 88 | assertEquals("router-1", floatingIp.routerName()); |
| 89 | assertEquals("10.10.10.10", floatingIp.floatingIp().toString()); |
| 90 | assertEquals("pod-1", floatingIp.podName()); |
| 91 | assertEquals("20.20.20.20", floatingIp.fixedIp().toString()); |
| 92 | } |
| 93 | |
| 94 | private KubevirtFloatingIp getKubevirtFloatingIp(String resourceName) throws IOException { |
| 95 | InputStream jsonStream = KubevirtFloatingIpCodecTest.class.getResourceAsStream(resourceName); |
| 96 | JsonNode json = context.mapper().readTree(jsonStream); |
| 97 | MatcherAssert.assertThat(json, notNullValue()); |
| 98 | KubevirtFloatingIp fip = kubevirtFloatingIpCodec.decode((ObjectNode) json, context); |
| 99 | assertThat(fip, notNullValue()); |
| 100 | return fip; |
| 101 | } |
| 102 | } |