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