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