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