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