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