blob: 79182dae94cd31af43322bc364ef6470b534d709 [file] [log] [blame]
Jian Li073f1ba2021-02-28 03:50:15 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.hamcrest.MatcherAssert;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.codec.impl.MockCodecContext;
26import org.onosproject.core.CoreService;
27import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp;
28import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp;
29
30import java.io.IOException;
31import java.io.InputStream;
32
33import static junit.framework.TestCase.assertEquals;
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expect;
36import static org.easymock.EasyMock.replay;
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.onosproject.kubevirtnetworking.codec.KubevirtFloatingIpJsonMatcher.matchesKubevirtFloatingIp;
40import static org.onosproject.net.NetTestTools.APP_ID;
41
42/**
43 * Unit tests for KubevirtFloatingIp codec.
44 */
45public 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}