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