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.openstacknode.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.net.behaviour.ControllerInfo; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 25 | import org.onosproject.openstacknode.api.DpdkConfig; |
| 26 | import org.onosproject.openstacknode.api.DpdkInterface; |
Jian Li | 086ad70 | 2018-07-30 10:50:02 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknode.api.OpenstackAuth; |
| 28 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
| 30 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 31 | import org.onosproject.openstacknode.codec.DpdkConfigCodec; |
| 32 | import org.onosproject.openstacknode.codec.DpdkInterfaceCodec; |
Jian Li | 086ad70 | 2018-07-30 10:50:02 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknode.codec.OpenstackAuthCodec; |
| 34 | import org.onosproject.openstacknode.codec.OpenstackControllerCodec; |
| 35 | import org.onosproject.openstacknode.codec.OpenstackNodeCodec; |
| 36 | import org.onosproject.openstacknode.codec.OpenstackPhyInterfaceCodec; |
| 37 | import org.onosproject.openstacknode.codec.OpenstackSshAuthCodec; |
| 38 | |
| 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | import static org.junit.Assert.assertEquals; |
| 43 | import static org.junit.Assert.assertNull; |
| 44 | |
| 45 | /** |
| 46 | * Unit test for openstack node codec register. |
| 47 | */ |
| 48 | public final class OpenstackNodeCodecRegisterTest { |
| 49 | |
| 50 | private OpenstackNodeCodecRegister register; |
| 51 | |
| 52 | /** |
| 53 | * Tests codec register activation and deactivation. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testActivateDeactivate() { |
| 57 | register = new OpenstackNodeCodecRegister(); |
| 58 | CodecService codecService = new TestCodecService(); |
| 59 | |
| 60 | TestUtils.setField(register, "codecService", codecService); |
| 61 | register.activate(); |
| 62 | |
| 63 | assertEquals(OpenstackNodeCodec.class.getName(), |
| 64 | codecService.getCodec(OpenstackNode.class).getClass().getName()); |
| 65 | assertEquals(OpenstackAuthCodec.class.getName(), |
| 66 | codecService.getCodec(OpenstackAuth.class).getClass().getName()); |
| 67 | assertEquals(OpenstackPhyInterfaceCodec.class.getName(), |
| 68 | codecService.getCodec(OpenstackPhyInterface.class).getClass().getName()); |
| 69 | assertEquals(OpenstackControllerCodec.class.getName(), |
| 70 | codecService.getCodec(ControllerInfo.class).getClass().getName()); |
| 71 | assertEquals(OpenstackSshAuthCodec.class.getName(), |
| 72 | codecService.getCodec(OpenstackSshAuth.class).getClass().getName()); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 73 | assertEquals(DpdkConfigCodec.class.getName(), |
| 74 | codecService.getCodec(DpdkConfig.class).getClass().getName()); |
| 75 | assertEquals(DpdkInterfaceCodec.class.getName(), |
| 76 | codecService.getCodec(DpdkInterface.class).getClass().getName()); |
Jian Li | 086ad70 | 2018-07-30 10:50:02 +0900 | [diff] [blame] | 77 | |
| 78 | register.deactivate(); |
| 79 | |
| 80 | assertNull(codecService.getCodec(OpenstackNode.class)); |
| 81 | assertNull(codecService.getCodec(OpenstackAuth.class)); |
| 82 | assertNull(codecService.getCodec(OpenstackPhyInterface.class)); |
| 83 | assertNull(codecService.getCodec(ControllerInfo.class)); |
| 84 | assertNull(codecService.getCodec(OpenstackSshAuth.class)); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 85 | assertNull(codecService.getCodec(DpdkConfig.class)); |
| 86 | assertNull(codecService.getCodec(DpdkInterface.class)); |
Jian Li | 086ad70 | 2018-07-30 10:50:02 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private static class TestCodecService implements CodecService { |
| 90 | |
| 91 | private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap(); |
| 92 | |
| 93 | @Override |
| 94 | public Set<Class<?>> getCodecs() { |
| 95 | return ImmutableSet.of(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public <T> JsonCodec<T> getCodec(Class<T> entityClass) { |
| 100 | return codecMap.get(entityClass.getName()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
| 105 | codecMap.put(entityClass.getName(), codec); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void unregisterCodec(Class<?> entityClass) { |
| 110 | codecMap.remove(entityClass.getName()); |
| 111 | } |
| 112 | } |
| 113 | } |