blob: c536a3c9d3268557ef0e7ea57c1746d473c8d0d4 [file] [log] [blame]
Jian Lia80b1582019-01-25 12:47:42 +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.web;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
20import org.junit.Test;
21import org.onlab.junit.TestUtils;
22import org.onosproject.codec.CodecService;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.k8snetworking.api.K8sNetwork;
25import org.onosproject.k8snetworking.api.K8sPort;
26import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
27import org.onosproject.k8snetworking.codec.K8sPortCodec;
28import org.onosproject.k8snode.api.K8sNode;
29
30import java.util.Map;
31import java.util.Set;
32
33import static org.junit.Assert.assertEquals;
34import static org.junit.Assert.assertNull;
35
36/**
37 * Unit tests for kubernetes networking codec register.
38 */
39public class K8sNetworkingCodecRegisterTest {
40
41 /**
42 * Tests codec register activation and deactivation.
43 */
44 @Test
45 public void testActivateDeactivate() {
46 K8sNetworkingCodecRegister register = new K8sNetworkingCodecRegister();
47 CodecService codecService = new TestCodecService();
48
49 TestUtils.setField(register, "codecService", codecService);
50 register.activate();
51
52 assertEquals(K8sNetworkCodec.class.getName(),
53 codecService.getCodec(K8sNetwork.class).getClass().getName());
54 assertEquals(K8sPortCodec.class.getName(),
55 codecService.getCodec(K8sPort.class).getClass().getName());
56
57 register.deactivate();
58
59 assertNull(codecService.getCodec(K8sNode.class));
60 }
61
62 private static class TestCodecService implements CodecService {
63
64 private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
65
66 @Override
67 public Set<Class<?>> getCodecs() {
68 return ImmutableSet.of();
69 }
70
71 @Override
72 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
73 return codecMap.get(entityClass.getName());
74 }
75
76 @Override
77 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
78 codecMap.put(entityClass.getName(), codec);
79 }
80
81 @Override
82 public void unregisterCodec(Class<?> entityClass) {
83 codecMap.remove(entityClass.getName());
84 }
85 }
86}