Jian Li | 086ad70 | 2018-07-30 10:50:02 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.web; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.junit.TestUtils; |
| 22 | import org.onosproject.codec.CodecService; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 25 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 26 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
| 27 | import org.onosproject.openstacktelemetry.codec.FlowInfoJsonCodec; |
| 28 | import org.onosproject.openstacktelemetry.codec.StatsFlowRuleJsonCodec; |
| 29 | import org.onosproject.openstacktelemetry.codec.StatsInfoJsonCodec; |
| 30 | |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | |
| 37 | /** |
| 38 | * Unit test for openstack telemetry codec register. |
| 39 | */ |
| 40 | public 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 | } |