blob: 5a8f82275f77362578f0d32959651ec6f4ace435 [file] [log] [blame]
Bharat saraswald6f12412016-03-28 15:50:13 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswald6f12412016-03-28 15:50:13 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
19import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
22import org.junit.Test;
23import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
25import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswald6f12412016-03-28 15:50:13 +053027
28import static org.hamcrest.core.Is.is;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053029import static org.hamcrest.core.IsNot.not;
Bharat saraswald6f12412016-03-28 15:50:13 +053030import static org.junit.Assert.assertThat;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053031import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswald6f12412016-03-28 15:50:13 +053033import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuild;
34import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildForInterface;
35import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getCheckNotNull;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
37import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
38import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
39import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
40import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForInterface;
41import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
42import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
43import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
44import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForInterface;
45import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
46import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053047import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswald6f12412016-03-28 15:50:13 +053048import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
49import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
50import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
51import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
52import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
53import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
54import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
55import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
56import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
57import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
58import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
59import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
Bharat saraswald6f12412016-03-28 15:50:13 +053060import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
61import static org.onosproject.yangutils.utils.UtilConstants.NEW;
62import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
63import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
64import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
65import static org.onosproject.yangutils.utils.UtilConstants.OF;
66import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
67import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
68import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
69import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
70import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
71import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
72import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
73import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
74import static org.onosproject.yangutils.utils.UtilConstants.SET_METHOD_PREFIX;
75import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
76import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
77import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
78import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
79import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
80import static org.onosproject.yangutils.utils.UtilConstants.THIS;
81import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
82import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
83import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053084import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswald6f12412016-03-28 15:50:13 +053085
86/**
87 * Unit tests for generated methods from the file type.
88 */
89public final class MethodsGeneratorTest {
90
91 private static final String CLASS_NAME = "testname";
92 private static final String ATTRIBUTE_NAME = "testname";
93
94 /**
95 * Unit test for private constructor.
96 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053097 * @throws SecurityException if any security violation is observed
98 * @throws NoSuchMethodException if when the method is not found
99 * @throws IllegalArgumentException if there is illegal argument found
100 * @throws InstantiationException if instantiation is provoked for the private constructor
101 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswald6f12412016-03-28 15:50:13 +0530102 * @throws InvocationTargetException when an exception occurs by the method or constructor
103 */
104 @Test
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530105 public void callPrivateConstructors()
106 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswald6f12412016-03-28 15:50:13 +0530107 InstantiationException, IllegalAccessException, InvocationTargetException {
108
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530109 Class<?>[] classesToConstruct = {MethodsGenerator.class};
Bharat saraswald6f12412016-03-28 15:50:13 +0530110 for (Class<?> clazz : classesToConstruct) {
111 Constructor<?> constructor = clazz.getDeclaredConstructor();
112 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530113 assertThat(null, not(constructor.newInstance()));
Bharat saraswald6f12412016-03-28 15:50:13 +0530114 }
115 }
116
117 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530118 * Unit test case for checking the parse builder and type constructor.
Bharat saraswald6f12412016-03-28 15:50:13 +0530119 */
120 @Test
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530121 public void getTypeConstructorTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530122
123 YangPluginConfig pluginConfig = new YangPluginConfig();
Bharat saraswald6f12412016-03-28 15:50:13 +0530124 JavaAttributeInfo testAttr = getTestAttribute();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530125 String test = getTypeConstructorStringAndJavaDoc(testAttr, CLASS_NAME, pluginConfig);
Bharat saraswald6f12412016-03-28 15:50:13 +0530126 assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
127 }
128
129 /**
130 * Test for build method for class.
131 */
132 @Test
133 public void getBuildTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530134 String method = getBuild(CLASS_NAME);
135 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
136 + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530137 + RETURN + SPACE + NEW + SPACE + "Default" + CLASS_NAME + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
Bharat saraswald6f12412016-03-28 15:50:13 +0530138 + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
139
140 }
141
142 /**
143 * Test for build method of interface.
144 */
145 @Test
146 public void getBuildForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530147 String method = getBuildForInterface(CLASS_NAME);
148 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
149 OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
150 }
151
152 /**
153 * Test for check not null method.
154 */
155 @Test
156 public void getCheckNotNullTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530157 String method = getCheckNotNull(CLASS_NAME);
158 assertThat(true, is(method.equals(EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS
159 + CLASS_NAME + COMMA + SPACE + CLASS_NAME + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
160 }
161
162 /**
163 * Test case for constructor.
164 */
165 @Test
166 public void getConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530167 JavaAttributeInfo testAttr = getTestAttribute();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530168 YangPluginConfig pluginConfig = new YangPluginConfig();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530169 String method = getConstructor(testAttr, GENERATE_SERVICE_AND_MANAGER, pluginConfig);
Bharat saraswald6f12412016-03-28 15:50:13 +0530170 assertThat(true, is(method.contains(THIS + PERIOD + CLASS_NAME + SPACE + EQUAL + SPACE + "builder" + OBJECT
171 + PERIOD + GET_METHOD_PREFIX + "Testname" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN)));
172 }
173
174 /**
175 * Test for constrcutor start method.
176 */
177 @Test
178 public void getConstructorStartTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530179 YangPluginConfig pluginConfig = new YangPluginConfig();
180 String method = getConstructorStart(CLASS_NAME, pluginConfig);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530181 assertThat(true, is(method.contains(PUBLIC + SPACE + "Default" + CLASS_NAME + OPEN_PARENTHESIS + CLASS_NAME
Bharat saraswald6f12412016-03-28 15:50:13 +0530182 + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
183 + OPEN_CURLY_BRACKET + NEW_LINE)));
184 }
185
186 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530187 * Test case for equals method.
Bharat saraswald6f12412016-03-28 15:50:13 +0530188 */
189 @Test
190 public void getEqualsMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530191 JavaAttributeInfo testAttr = getTestAttribute();
192 String method = getEqualsMethod(testAttr);
193 assertThat(true, is(method.contains(SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD
194 + EQUALS_STRING + OPEN_PARENTHESIS)));
195 }
196
197 /**
198 * Test for to string method.
199 */
200 @Test
201 public void getToStringMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530202 JavaAttributeInfo testAttr = getTestAttribute();
203 String method = getToStringMethod(testAttr);
204 assertThat(true, is(method.equals(
205 TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
206 + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
207 }
208
209 /**
210 * Test for getter method of class.
211 */
212 @Test
213 public void getGetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530214 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215 String method = getGetterForClass(testAttr, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530216 assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
217 }
218
219 /**
220 * Test for getter of interface.
221 */
222 @Test
223 public void getGetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530224 String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530225 assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
226 }
227
228 /**
229 * Test case for setter method of class.
230 */
231 @Test
232 public void getSetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530233 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530234 String method = getSetterForClass(testAttr, CLASS_NAME, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530235 assertThat(true, is(
236 method.contains(PUBLIC + SPACE + VOID + SPACE +
237 SET_METHOD_PREFIX + getCapitalCase(CLASS_NAME) + OPEN_PARENTHESIS +
238 STRING_DATA_TYPE + SPACE + ATTRIBUTE_NAME)));
Bharat saraswald6f12412016-03-28 15:50:13 +0530239 }
240
241 /**
242 * Test for setter method of interface.
243 */
244 @Test
245 public void getSetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246 String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false,
247 GENERATE_SERVICE_AND_MANAGER);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 assertThat(true, is(method.contains(VOID + SPACE +
249 SET_METHOD_PREFIX + "Testname")));
Bharat saraswald6f12412016-03-28 15:50:13 +0530250 }
251
252 /**
253 * Test case for of method.
254 */
255 @Test
256 public void getOfMethodest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530257 JavaAttributeInfo testAttr = getTestAttribute();
258 String method = getOfMethod(CLASS_NAME, testAttr);
259 assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
260 + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
261 }
262
263 /**
264 * Test case for setter in type def class.
265 */
266 @Test
267 public void getSetterForTypeDefClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530268 JavaAttributeInfo testAttr = getTestAttribute();
269 String method = getSetterForTypeDefClass(testAttr);
270 assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
271 }
272
273 /**
274 * Test case for over ride string.
275 */
276 @Test
277 public void getOverRideStringTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530278 String method = getOverRideString();
279 assertThat(true, is(method.contains(OVERRIDE)));
280 }
281
282 /**
283 * Returns java attribute.
284 *
285 * @return java attribute
286 */
287 private JavaAttributeInfo getTestAttribute() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530288 JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
289 testAttr.setAttributeName(ATTRIBUTE_NAME);
290 testAttr.setAttributeType(getTestYangType());
291 testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
292 return testAttr;
293 }
294
295 /**
296 * Returns java qualified info.
297 *
298 * @return java qualified info
299 */
300 private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530301 JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
302 info.setPkgInfo(JAVA_LANG);
303 info.setClassInfo(STRING_DATA_TYPE);
304 return info;
305 }
306
307 /**
308 * Returns stub YANG type.
309 *
310 * @return test YANG type
311 */
312 private YangType<?> getTestYangType() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530313 YangType<?> attrType = new YangType<>();
314 attrType.setDataTypeName(STRING_DATA_TYPE);
315 attrType.setDataType(STRING);
316 return attrType;
317 }
318}