blob: c3784bab72970e5c2a09dcb3db8c1afada811c34 [file] [log] [blame]
Jian Lif97a07e2021-01-13 18:05:00 +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.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.kubevirtnetworking.api.KubevirtHostRoute;
25import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
26import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
27import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec;
28import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec;
29import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec;
30
31import java.util.Map;
32import java.util.Set;
33
34import static org.junit.Assert.assertEquals;
35
36/**
37 * Unit tests for kubevirt networking codec register.
38 */
39public class KubevirtNetworkingCodecRegisterTest {
40
41 /**
42 * Tests codec register activation and deactivation.
43 */
44 @Test
45 public void testActivateDeactivate() {
46 KubevirtNetworkingCodecRegister register = new KubevirtNetworkingCodecRegister();
47 CodecService codecService = new TestCodecService();
48
49 TestUtils.setField(register, "codecService", codecService);
50 register.activate();
51
52 assertEquals(KubevirtNetworkCodec.class.getName(),
53 codecService.getCodec(KubevirtNetwork.class).getClass().getName());
54 assertEquals(KubevirtHostRouteCodec.class.getName(),
55 codecService.getCodec(KubevirtHostRoute.class).getClass().getName());
56 assertEquals(KubevirtIpPoolCodec.class.getName(),
57 codecService.getCodec(KubevirtIpPool.class).getClass().getName());
58
59 register.deactivate();
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}