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