Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snode.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.k8snode.api.K8sNode; |
| 25 | import org.onosproject.k8snode.codec.K8sNodeCodec; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertNull; |
| 32 | |
| 33 | /** |
| 34 | * Unit tests for kubernetes node codec register. |
| 35 | */ |
| 36 | public final class K8sNodeCodecRegisterTest { |
| 37 | |
| 38 | private K8sNodeCodecRegister register; |
| 39 | |
| 40 | /** |
| 41 | * Tests codec register activation and deactivation. |
| 42 | */ |
| 43 | @Test |
| 44 | public void testActivateDeactivate() { |
| 45 | register = new K8sNodeCodecRegister(); |
| 46 | CodecService codecService = new TestCodecService(); |
| 47 | |
| 48 | TestUtils.setField(register, "codecService", codecService); |
| 49 | register.activate(); |
| 50 | |
| 51 | assertEquals(K8sNodeCodec.class.getName(), |
| 52 | codecService.getCodec(K8sNode.class).getClass().getName()); |
| 53 | |
| 54 | register.deactivate(); |
| 55 | |
| 56 | assertNull(codecService.getCodec(K8sNode.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 | } |