blob: 51f0b24520609bf03eb5eeb50db63f40ad6e4d1e [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.openstacknetworking.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.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.codec.InstancePortCodec;
26
27import java.util.Map;
28import java.util.Set;
29
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertNull;
32
33/**
34 * Unit test for openstack networking codec register.
35 */
36public final class OpenstackNetworkingCodecRegisterTest {
37
38 private OpenstackNetworkingCodecRegister register;
39
40 /**
41 * Tests codec register activation and deactivation.
42 */
43 @Test
44 public void testActivateDeactivate() {
45 register = new OpenstackNetworkingCodecRegister();
46 CodecService codecService = new TestCodecService();
47
48 TestUtils.setField(register, "codecService", codecService);
49 register.activate();
50
51 assertEquals(InstancePortCodec.class.getName(),
52 codecService.getCodec(InstancePort.class).getClass().getName());
53
54 register.deactivate();
55
56 assertNull(codecService.getCodec(InstancePort.class));
57 }
58
59 private static class TestCodecService implements CodecService {
60
61 private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
62
63 @Override
64 public Set<Class<?>> getCodecs() {
65 return ImmutableSet.of();
66 }
67
68 @Override
69 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
70 return codecMap.get(entityClass.getName());
71 }
72
73 @Override
74 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
75 codecMap.put(entityClass.getName(), codec);
76 }
77
78 @Override
79 public void unregisterCodec(Class<?> entityClass) {
80 codecMap.remove(entityClass.getName());
81 }
82 }
83}