blob: d941cdc1f6d75951608186264e677a5a35a313c5 [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 java.math.BigDecimal;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.yang.model.ResourceId;
26import org.onosproject.yang.runtime.AnnotatedNodeInfo;
27
28import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmenttop.logicalchannelassignments.assignment.DefaultConfig;
29import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmentconfig.AssignmentTypeEnum;
30
31import static org.junit.Assert.assertEquals;
32
33/**
34 * UnitTest class for OpenConfigConfigOfAssignmentHandler.
35 */
36public class OpenConfigConfigOfAssignmentHandlerTest {
37 // parent Handler
38 OpenConfigAssignmentHandler 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 OpenConfigLogicalChannelsHandler logicalChannels =
55 new OpenConfigLogicalChannelsHandler(terminalDevice);
56
57 // add <channel><index>1</index></channel>
58 OpenConfigChannelHandler channel =
59 new OpenConfigChannelHandler(1, logicalChannels);
60
61 // add <logical-channel-assignments></logical-channel-assignments>
62 OpenConfigLogicalChannelAssignmentsHandler logicalChannelAssignments =
63 new OpenConfigLogicalChannelAssignmentsHandler(channel);
64
65 // add <assignment><index>2</index></assignment>
66 parent = new OpenConfigAssignmentHandler(2, logicalChannelAssignments);
67
68 // expected ResourceId set up
69 rid = ResourceId.builder()
70 .addBranchPointSchema("terminal-device", "http://openconfig.net/yang/terminal-device")
71 .addBranchPointSchema("logical-channels", "http://openconfig.net/yang/terminal-device")
72 .addBranchPointSchema("channel", "http://openconfig.net/yang/terminal-device")
73 .addKeyLeaf("index", "http://openconfig.net/yang/terminal-device", 1)
74 .addBranchPointSchema("logical-channel-assignments", "http://openconfig.net/yang/terminal-device")
75 .addBranchPointSchema("assignment", "http://openconfig.net/yang/terminal-device")
76 .addKeyLeaf("index", "http://openconfig.net/yang/terminal-device", 2)
77 .addBranchPointSchema("config", "http://openconfig.net/yang/terminal-device")
78 .build();
79 }
80
81 /**
82 * UnitTest method for getModelObject.
83 */
84 @Test
85 public void testGetModelObject() {
86 // test Handler
87 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
88
89 // expected ModelObject
90 DefaultConfig modelObject = new DefaultConfig();
91
92 assertEquals("[NG]getModelObject:Return is not an expected ModelObject.\n",
93 modelObject, config.getModelObject());
94 }
95
96 /**
97 * UnitTest method for setResourceId.
98 */
99 @Test
100 public void testSetResourceId() {
101 // call setResourceId
102 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
103
104 // get resourceId
105 ResourceId resourceId = null;
106 try {
107 Field field = OpenConfigObjectHandler.class.getDeclaredField("resourceId");
108 field.setAccessible(true);
109 resourceId = (ResourceId) field.get(config);
110 } catch (NoSuchFieldException e) {
111 Assert.fail("[NG]setResourceId:ResourceId does not exist.\n" + e);
112 } catch (IllegalAccessException e) {
113 Assert.fail("[NG]setResourceId:Access to ResourceId is illegal.\n" + e);
114 }
115
116 assertEquals("[NG]setResourceId:Set ResourceId is not an expected one.\n",
117 rid, resourceId);
118 }
119
120 /**
121 * UnitTest method for getResourceId.
122 */
123 @Test
124 public void testGetResourceId() {
125 // test Handler
126 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
127
128 assertEquals("[NG]getResourceId:Return is not an expected ResourceId.\n",
129 rid, config.getResourceId());
130 }
131
132 /**
133 * UnitTest method for getResourceIdBuilder.
134 */
135 @Test
136 public void testGetResourceIdBuilder() {
137 // test Handler
138 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
139
140 assertEquals("[NG]getResourceIdBuilder:Return is not an expected ResourceIdBuilder.\n",
141 rid, config.getResourceIdBuilder().build());
142 }
143
144 /**
145 * UnitTest method for addAnnotation.
146 */
147 @Test
148 public void testAddAnnotation() {
149 // test Handler
150 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
151
152 // call addAnnotation
153 config.addAnnotation("name", "value");
154
155 // get annotatedNodeInfos
156 List<AnnotatedNodeInfo> annotatedNodeInfos = new ArrayList<AnnotatedNodeInfo>();
157 try {
158 Field field = OpenConfigObjectHandler.class.getDeclaredField("annotatedNodeInfos");
159 field.setAccessible(true);
160 annotatedNodeInfos = (List<AnnotatedNodeInfo>) field.get(config);
161 } catch (NoSuchFieldException e) {
162 Assert.fail("[NG]addAnnotation:List of AnnotatedNodeInfo does not exist.\n" + e);
163 } catch (IllegalAccessException e) {
164 Assert.fail("[NG]addAnnotation:Access to list of AnnotatedNodeInfo is illegal.\n" + e);
165 }
166
167 assertEquals("[NG]addAnnotation:List size of AnnotatedNodeInfo is invalid.\n",
168 1, annotatedNodeInfos.size());
169 assertEquals("[NG]addAnnotation:ResourceId of AnnotatedNodeInfo is not an expected one.\n",
170 rid, annotatedNodeInfos.get(0).resourceId());
171 assertEquals("[NG]addAnnotation:List size of Annotation is invalid.\n",
172 1, annotatedNodeInfos.get(0).annotations().size());
173 assertEquals("[NG]addAnnotation:Name of Annotation is not an expected one.\n",
174 "name", annotatedNodeInfos.get(0).annotations().get(0).name());
175 assertEquals("[NG]addAnnotation:Value of Annotation is not an expected one.\n",
176 "value", annotatedNodeInfos.get(0).annotations().get(0).value());
177 }
178
179 /**
180 * UnitTest method for getAnnotatedNodeInfoList.
181 */
182 @Test
183 public void testGetAnnotatedNodeInfoList() {
184 // test Handler
185 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
186
187 // set list of AnnotatedNodeInfo
188 config.addAnnotation("name", "value");
189
190 assertEquals("[NG]getAnnotatedNodeInfoList:List size of AnnotatedNodeInfo is invalid.\n",
191 1, config.getAnnotatedNodeInfoList().size());
192 assertEquals("[NG]getAnnotatedNodeInfoList:ResourceId of AnnotatedNodeInfo is not an expected one.\n",
193 rid, config.getAnnotatedNodeInfoList().get(0).resourceId());
194 assertEquals("[NG]getAnnotatedNodeInfoList:List size of Annotation is invalid.\n",
195 1, config.getAnnotatedNodeInfoList().get(0).annotations().size());
196 assertEquals("[NG]getAnnotatedNodeInfoList:Name of Annotation is not an expected one.\n",
197 "name", config.getAnnotatedNodeInfoList().get(0).annotations().get(0).name());
198 assertEquals("[NG]getAnnotatedNodeInfoList:Value of Annotation is not an expected one.\n",
199 "value", config.getAnnotatedNodeInfoList().get(0).annotations().get(0).value());
200 }
201
202 /**
203 * UnitTest method for addIndex.
204 */
205 @Test
206 public void testAddIndex() {
207 // test Handler
208 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
209
210 // call addIndex
211 config.addIndex(Long.valueOf(3));
212
213 // expected ModelObject
214 DefaultConfig modelObject = new DefaultConfig();
215 modelObject.index(3);
216
217 assertEquals("[NG]addIndex:ModelObject(Index added) is not an expected one.\n",
218 modelObject, config.getModelObject());
219 }
220
221 /**
222 * UnitTest method for addAssignmentType.
223 */
224 @Test
225 public void testAddAssignmentType() {
226 // test Handler
227 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
228
229 // call addAssignmentType
230 config.addAssignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL);
231
232 // expected ModelObject
233 DefaultConfig modelObject = new DefaultConfig();
234 modelObject.assignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL);
235
236 assertEquals("[NG]addAssignmentType:ModelObject(AssignmentType added) is not an expected one.\n",
237 modelObject, config.getModelObject());
238 }
239
240 /**
241 * UnitTest method for addLogicalChannel.
242 */
243 @Test
244 public void testAddLogicalChannel() {
245 // test Handler
246 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
247
248 // call addLogicalChannel
249 config.addLogicalChannel("name");
250
251 // expected ModelObject
252 DefaultConfig modelObject = new DefaultConfig();
253 modelObject.logicalChannel("name");
254
255 assertEquals("[NG]addLogicalChannel:ModelObject(LogicalChannel added) is not an expected one.\n",
256 modelObject, config.getModelObject());
257 }
258
259 /**
260 * UnitTest method for addAllocation.
261 */
262 @Test
263 public void testAddAllocation() {
264 // test Handler
265 OpenConfigConfigOfAssignmentHandler config = new OpenConfigConfigOfAssignmentHandler(parent);
266
267 // call addAllocation
268 config.addAllocation(BigDecimal.valueOf(4));
269
270 // expected ModelObject
271 DefaultConfig modelObject = new DefaultConfig();
272 modelObject.allocation(BigDecimal.valueOf(4));
273
274 assertEquals("[NG]addAllocation:ModelObject(Allocation added) is not an expected one.\n",
275 modelObject, config.getModelObject());
276 }
277}