blob: 93351531ed5e41d051626e76a6ea8f4c44c9cd14 [file] [log] [blame]
Jian Li171582e2020-12-21 01:44:05 +09001/*
2 * Copyright 2020-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.kubevirtnode.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.kubevirtnode.api.KubevirtNode;
25import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
26import org.onosproject.kubevirtnode.codec.KubevirtNodeCodec;
27import org.onosproject.kubevirtnode.codec.KubevirtPhyInterfaceCodec;
28
29import java.util.Map;
30import java.util.Set;
31
32import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertNull;
34
35/**
36 * Unit test for kubevirt node codec register.
37 */
38public final class KubevirtNodeCodecRegisterTest {
39
40 private KubevirtNodeCodecRegister register;
41
42 /**
43 * Tests codec register activation and deactivation.
44 */
45 @Test
46 public void testActivateDeactivate() {
47 register = new KubevirtNodeCodecRegister();
48 CodecService codecService = new TestCodecService();
49
50 TestUtils.setField(register, "codecService", codecService);
51 register.activate();
52
53 assertEquals(KubevirtNodeCodec.class.getName(),
54 codecService.getCodec(KubevirtNode.class).getClass().getName());
55 assertEquals(KubevirtPhyInterfaceCodec.class.getName(),
56 codecService.getCodec(KubevirtPhyInterface.class).getClass().getName());
57
58 register.deactivate();
59
60 assertNull(codecService.getCodec(KubevirtNode.class));
61 assertNull(codecService.getCodec(KubevirtPhyInterface.class));
62 }
63
64 private static class TestCodecService implements CodecService {
65
66 private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
67
68 @Override
69 public Set<Class<?>> getCodecs() {
70 return ImmutableSet.of();
71 }
72
73 @Override
74 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
75 return codecMap.get(entityClass.getName());
76 }
77
78 @Override
79 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
80 codecMap.put(entityClass.getName(), codec);
81 }
82
83 @Override
84 public void unregisterCodec(Class<?> entityClass) {
85 codecMap.remove(entityClass.getName());
86 }
87 }
88}