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