blob: ad64489b98fa9409c3c652d30506636b54477ffa [file] [log] [blame]
Jian Li086ad702018-07-30 10:50:02 +09001/*
2 * Copyright 2018-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.openstacknode.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.net.behaviour.ControllerInfo;
Daniel Parkd02d7bd2018-08-23 23:04:31 +090025import org.onosproject.openstacknode.api.DpdkConfig;
26import org.onosproject.openstacknode.api.DpdkInterface;
Jian Li086ad702018-07-30 10:50:02 +090027import org.onosproject.openstacknode.api.OpenstackAuth;
28import org.onosproject.openstacknode.api.OpenstackNode;
29import org.onosproject.openstacknode.api.OpenstackPhyInterface;
30import org.onosproject.openstacknode.api.OpenstackSshAuth;
Daniel Parkd02d7bd2018-08-23 23:04:31 +090031import org.onosproject.openstacknode.codec.DpdkConfigCodec;
32import org.onosproject.openstacknode.codec.DpdkInterfaceCodec;
Jian Li086ad702018-07-30 10:50:02 +090033import org.onosproject.openstacknode.codec.OpenstackAuthCodec;
34import org.onosproject.openstacknode.codec.OpenstackControllerCodec;
35import org.onosproject.openstacknode.codec.OpenstackNodeCodec;
36import org.onosproject.openstacknode.codec.OpenstackPhyInterfaceCodec;
37import org.onosproject.openstacknode.codec.OpenstackSshAuthCodec;
38
39import java.util.Map;
40import java.util.Set;
41
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertNull;
44
45/**
46 * Unit test for openstack node codec register.
47 */
48public final class OpenstackNodeCodecRegisterTest {
49
50 private OpenstackNodeCodecRegister register;
51
52 /**
53 * Tests codec register activation and deactivation.
54 */
55 @Test
56 public void testActivateDeactivate() {
57 register = new OpenstackNodeCodecRegister();
58 CodecService codecService = new TestCodecService();
59
60 TestUtils.setField(register, "codecService", codecService);
61 register.activate();
62
63 assertEquals(OpenstackNodeCodec.class.getName(),
64 codecService.getCodec(OpenstackNode.class).getClass().getName());
65 assertEquals(OpenstackAuthCodec.class.getName(),
66 codecService.getCodec(OpenstackAuth.class).getClass().getName());
67 assertEquals(OpenstackPhyInterfaceCodec.class.getName(),
68 codecService.getCodec(OpenstackPhyInterface.class).getClass().getName());
69 assertEquals(OpenstackControllerCodec.class.getName(),
70 codecService.getCodec(ControllerInfo.class).getClass().getName());
71 assertEquals(OpenstackSshAuthCodec.class.getName(),
72 codecService.getCodec(OpenstackSshAuth.class).getClass().getName());
Daniel Parkd02d7bd2018-08-23 23:04:31 +090073 assertEquals(DpdkConfigCodec.class.getName(),
74 codecService.getCodec(DpdkConfig.class).getClass().getName());
75 assertEquals(DpdkInterfaceCodec.class.getName(),
76 codecService.getCodec(DpdkInterface.class).getClass().getName());
Jian Li086ad702018-07-30 10:50:02 +090077
78 register.deactivate();
79
80 assertNull(codecService.getCodec(OpenstackNode.class));
81 assertNull(codecService.getCodec(OpenstackAuth.class));
82 assertNull(codecService.getCodec(OpenstackPhyInterface.class));
83 assertNull(codecService.getCodec(ControllerInfo.class));
84 assertNull(codecService.getCodec(OpenstackSshAuth.class));
Daniel Parkd02d7bd2018-08-23 23:04:31 +090085 assertNull(codecService.getCodec(DpdkConfig.class));
86 assertNull(codecService.getCodec(DpdkInterface.class));
Jian Li086ad702018-07-30 10:50:02 +090087 }
88
89 private static class TestCodecService implements CodecService {
90
91 private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
92
93 @Override
94 public Set<Class<?>> getCodecs() {
95 return ImmutableSet.of();
96 }
97
98 @Override
99 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
100 return codecMap.get(entityClass.getName());
101 }
102
103 @Override
104 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
105 codecMap.put(entityClass.getName(), codec);
106 }
107
108 @Override
109 public void unregisterCodec(Class<?> entityClass) {
110 codecMap.remove(entityClass.getName());
111 }
112 }
113}