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