blob: 95914eb01a2762a4d1159cb6b6775bc1da75a52e [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
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 */
16package org.onosproject.k8snetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.hamcrest.MatcherAssert;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.IpAddress;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.core.CoreService;
29import org.onosproject.k8snetworking.api.DefaultK8sIpam;
30import org.onosproject.k8snetworking.api.K8sIpam;
31
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.HashMap;
35import java.util.Map;
36
37import static junit.framework.TestCase.assertEquals;
38import static org.easymock.EasyMock.createMock;
39import static org.easymock.EasyMock.expect;
40import static org.easymock.EasyMock.replay;
41import static org.hamcrest.MatcherAssert.assertThat;
42import static org.hamcrest.Matchers.notNullValue;
43import static org.onosproject.k8snetworking.codec.K8sIpamJsonMatcher.matchesK8sIpam;
44import static org.onosproject.net.NetTestTools.APP_ID;
45
46/**
47 * Unit tests for kubernetes IPAM codec.
48 */
49public class K8sIpamCodecTest {
50
51 MockCodecContext context;
52
53 JsonCodec<K8sIpam> k8sIpamCodec;
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 k8sIpamCodec = new K8sIpamCodec();
65
66 assertThat(k8sIpamCodec, 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 IPAM encoding.
76 */
77 @Test
78 public void testK8sIpamEncode() {
Jian Li9b199162019-02-10 18:00:35 +090079 K8sIpam ipam = new DefaultK8sIpam("network-1-10.10.10.10",
Jian Li9ee9c8b2019-01-24 11:48:12 +090080 IpAddress.valueOf("10.10.10.10"), "network-1");
81
82 ObjectNode nodeJson = k8sIpamCodec.encode(ipam, context);
83 assertThat(nodeJson, matchesK8sIpam(ipam));
84 }
85
86 /**
87 * Tests the kubernetes IPAM decoding.
88 */
89 @Test
90 public void testK8sIpamDecode() throws IOException {
91 K8sIpam ipam = getK8sIpam("K8sIpam.json");
92
93 assertEquals("10.10.10.2", ipam.ipAddress().toString());
94 assertEquals("network-1", ipam.networkId());
95 }
96
97 /**
98 * Reads in an kubernetes IPAM from the given resource and decodes it.
99 *
100 * @param resourceName resource to use to read the JSON for the rule
101 * @return decoded kubernetes node
102 * @throws IOException if processing the resource fails
103 */
104 private K8sIpam getK8sIpam(String resourceName) throws IOException {
105 InputStream jsonStream = K8sIpamCodecTest.class.getResourceAsStream(resourceName);
106 JsonNode json = context.mapper().readTree(jsonStream);
107 MatcherAssert.assertThat(json, notNullValue());
108 K8sIpam node = k8sIpamCodec.decode((ObjectNode) json, context);
109 assertThat(node, notNullValue());
110 return node;
111 }
112
113 /**
114 * Mock codec context for use in codec unit tests.
115 */
116 private class MockCodecContext implements CodecContext {
117 private final ObjectMapper mapper = new ObjectMapper();
118 private final CodecManager manager = new CodecManager();
119 private final Map<Class<?>, Object> services = new HashMap<>();
120
121 /**
122 * Constructs a new mock codec context.
123 */
124 public MockCodecContext() {
125 manager.activate();
126 }
127
128 @Override
129 public ObjectMapper mapper() {
130 return mapper;
131 }
132
133 @Override
134 @SuppressWarnings("unchecked")
135 public <T> JsonCodec<T> codec(Class<T> entityClass) {
136 if (entityClass == K8sIpam.class) {
137 return (JsonCodec<T>) k8sIpamCodec;
138 }
139 return manager.getCodec(entityClass);
140 }
141
142 @SuppressWarnings("unchecked")
143 @Override
144 public <T> T getService(Class<T> serviceClass) {
145 return (T) services.get(serviceClass);
146 }
147
148 // for registering mock services
149 public <T> void registerService(Class<T> serviceClass, T impl) {
150 services.put(serviceClass, impl);
151 }
152 }
153}