Ai Hamano | f59360f | 2018-12-12 15:39:56 +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.odtn.utils.openconfig; |
| 17 | |
| 18 | import java.lang.reflect.Field; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | import org.junit.Assert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.yang.model.ResourceId; |
| 25 | import org.onosproject.yang.runtime.AnnotatedNodeInfo; |
| 26 | |
| 27 | import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanneltop.logicalchannels.channel.DefaultConfig; |
| 28 | |
| 29 | import static org.junit.Assert.assertEquals; |
| 30 | |
| 31 | /** |
| 32 | * UnitTest class for OpenConfigConfigOfChannelHandler. |
| 33 | */ |
| 34 | public class OpenConfigConfigOfChannelHandlerTest { |
| 35 | // parent Handler |
| 36 | OpenConfigChannelHandler parent; |
| 37 | |
| 38 | // expected ResourceId |
| 39 | ResourceId rid; |
| 40 | |
| 41 | /** |
| 42 | * Set up method for UnitTest. |
| 43 | */ |
| 44 | @Before |
| 45 | public void setUp() { |
| 46 | // parent Handler set up |
| 47 | // create <terminal-device xmlns="http://openconfig.net/yang/terminal-device"> |
| 48 | // </terminal-device> |
| 49 | OpenConfigTerminalDeviceHandler terminalDevice = new OpenConfigTerminalDeviceHandler(); |
| 50 | |
| 51 | // add <logical-channels></logical-channels> |
| 52 | OpenConfigLogicalChannelsHandler logicalChannels = |
| 53 | new OpenConfigLogicalChannelsHandler(terminalDevice); |
| 54 | |
| 55 | // add <channel><index>1</index></channel> |
| 56 | parent = new OpenConfigChannelHandler(1, logicalChannels); |
| 57 | |
| 58 | // expected ResourceId set up |
| 59 | rid = ResourceId.builder() |
| 60 | .addBranchPointSchema("terminal-device", "http://openconfig.net/yang/terminal-device") |
| 61 | .addBranchPointSchema("logical-channels", "http://openconfig.net/yang/terminal-device") |
| 62 | .addBranchPointSchema("channel", "http://openconfig.net/yang/terminal-device") |
| 63 | .addKeyLeaf("index", "http://openconfig.net/yang/terminal-device", 1) |
| 64 | .addBranchPointSchema("config", "http://openconfig.net/yang/terminal-device") |
| 65 | .build(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * UnitTest method for getModelObject. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testGetModelObject() { |
| 73 | // test Handler |
| 74 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 75 | |
| 76 | // expected ModelObject |
| 77 | DefaultConfig modelObject = new DefaultConfig(); |
| 78 | |
| 79 | assertEquals("[NG]getModelObject:Return is not an expected ModelObject.\n", |
| 80 | modelObject, config.getModelObject()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * UnitTest method for setResourceId. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testSetResourceId() { |
| 88 | // call setResourceId |
| 89 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 90 | |
| 91 | // get resourceId |
| 92 | ResourceId resourceId = null; |
| 93 | try { |
| 94 | Field field = OpenConfigObjectHandler.class.getDeclaredField("resourceId"); |
| 95 | field.setAccessible(true); |
| 96 | resourceId = (ResourceId) field.get(config); |
| 97 | } catch (NoSuchFieldException e) { |
| 98 | Assert.fail("[NG]setResourceId:ResourceId does not exist.\n" + e); |
| 99 | } catch (IllegalAccessException e) { |
| 100 | Assert.fail("[NG]setResourceId:Access to ResourceId is illegal.\n" + e); |
| 101 | } |
| 102 | |
| 103 | assertEquals("[NG]setResourceId:Set ResourceId is not an expected one.\n", |
| 104 | rid, resourceId); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * UnitTest method for getResourceId. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testGetResourceId() { |
| 112 | // test Handler |
| 113 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 114 | |
| 115 | assertEquals("[NG]getResourceId:Return is not an expected ResourceId.\n", |
| 116 | rid, config.getResourceId()); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * UnitTest method for getResourceIdBuilder. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testGetResourceIdBuilder() { |
| 124 | // test Handler |
| 125 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 126 | |
| 127 | assertEquals("[NG]getResourceIdBuilder:Return is not an expected ResourceIdBuilder.\n", |
| 128 | rid, config.getResourceIdBuilder().build()); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * UnitTest method for addAnnotation. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testAddAnnotation() { |
| 136 | // test Handler |
| 137 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 138 | |
| 139 | // call addAnnotation |
| 140 | config.addAnnotation("name", "value"); |
| 141 | |
| 142 | // get annotatedNodeInfos |
| 143 | List<AnnotatedNodeInfo> annotatedNodeInfos = new ArrayList<AnnotatedNodeInfo>(); |
| 144 | try { |
| 145 | Field field = OpenConfigObjectHandler.class.getDeclaredField("annotatedNodeInfos"); |
| 146 | field.setAccessible(true); |
| 147 | annotatedNodeInfos = (List<AnnotatedNodeInfo>) field.get(config); |
| 148 | } catch (NoSuchFieldException e) { |
| 149 | Assert.fail("[NG]addAnnotation:List of AnnotatedNodeInfo does not exist.\n" + e); |
| 150 | } catch (IllegalAccessException e) { |
| 151 | Assert.fail("[NG]addAnnotation:Access to list of AnnotatedNodeInfo is illegal.\n" + e); |
| 152 | } |
| 153 | |
| 154 | assertEquals("[NG]addAnnotation:List size of AnnotatedNodeInfo is invalid.\n", |
| 155 | 1, annotatedNodeInfos.size()); |
| 156 | assertEquals("[NG]addAnnotation:ResourceId of AnnotatedNodeInfo is not an expected one.\n", |
| 157 | rid, annotatedNodeInfos.get(0).resourceId()); |
| 158 | assertEquals("[NG]addAnnotation:List size of Annotation is invalid.\n", |
| 159 | 1, annotatedNodeInfos.get(0).annotations().size()); |
| 160 | assertEquals("[NG]addAnnotation:Name of Annotation is not an expected one.\n", |
| 161 | "name", annotatedNodeInfos.get(0).annotations().get(0).name()); |
| 162 | assertEquals("[NG]addAnnotation:Value of Annotation is not an expected one.\n", |
| 163 | "value", annotatedNodeInfos.get(0).annotations().get(0).value()); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * UnitTest method for getAnnotatedNodeInfoList. |
| 168 | */ |
| 169 | @Test |
| 170 | public void testGetAnnotatedNodeInfoList() { |
| 171 | // test Handler |
| 172 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 173 | |
| 174 | // set list of AnnotatedNodeInfo |
| 175 | config.addAnnotation("name", "value"); |
| 176 | |
| 177 | assertEquals("[NG]getAnnotatedNodeInfoList:List size of AnnotatedNodeInfo is invalid.\n", |
| 178 | 1, config.getAnnotatedNodeInfoList().size()); |
| 179 | assertEquals("[NG]getAnnotatedNodeInfoList:ResourceId of AnnotatedNodeInfo is not an expected one.\n", |
| 180 | rid, config.getAnnotatedNodeInfoList().get(0).resourceId()); |
| 181 | assertEquals("[NG]getAnnotatedNodeInfoList:List size of Annotation is invalid.\n", |
| 182 | 1, config.getAnnotatedNodeInfoList().get(0).annotations().size()); |
| 183 | assertEquals("[NG]getAnnotatedNodeInfoList:Name of Annotation is not an expected one.\n", |
| 184 | "name", config.getAnnotatedNodeInfoList().get(0).annotations().get(0).name()); |
| 185 | assertEquals("[NG]getAnnotatedNodeInfoList:Value of Annotation is not an expected one.\n", |
| 186 | "value", config.getAnnotatedNodeInfoList().get(0).annotations().get(0).value()); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * UnitTest method for addIndex. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testAddIndex() { |
| 194 | // test Handler |
| 195 | OpenConfigConfigOfChannelHandler config = new OpenConfigConfigOfChannelHandler(parent); |
| 196 | |
| 197 | // call addIndex |
| 198 | config.addIndex(2); |
| 199 | |
| 200 | // expected ModelObject |
| 201 | DefaultConfig modelObject = new DefaultConfig(); |
| 202 | modelObject.index(2); |
| 203 | |
| 204 | assertEquals("[NG]addIndex:ModelObject(Index added) is not an expected one.\n", |
| 205 | modelObject, config.getModelObject()); |
| 206 | } |
| 207 | } |