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