blob: d441c030e52910d53bf0558a2ea24d71d65fa360 [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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053033import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswald6f12412016-03-28 15:50:13 +053034import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuild;
35import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildForInterface;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getCheckNotNull;
37import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
38import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
39import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
40import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
41import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForInterface;
42import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
43import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
44import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
45import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForInterface;
46import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
47import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053048import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswald6f12412016-03-28 15:50:13 +053049import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
50import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
51import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
52import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
53import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
54import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
55import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
56import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
57import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
58import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
59import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
60import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
61import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
62import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
63import static org.onosproject.yangutils.utils.UtilConstants.NEW;
64import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
65import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
66import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
67import static org.onosproject.yangutils.utils.UtilConstants.OF;
68import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
69import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
70import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
71import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
72import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
73import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
74import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
75import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
76import static org.onosproject.yangutils.utils.UtilConstants.SET_METHOD_PREFIX;
77import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
78import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
79import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
80import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
81import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
82import static org.onosproject.yangutils.utils.UtilConstants.THIS;
83import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
84import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
85import static org.onosproject.yangutils.utils.UtilConstants.VOID;
86
87/**
88 * Unit tests for generated methods from the file type.
89 */
90public final class MethodsGeneratorTest {
91
92 private static final String CLASS_NAME = "testname";
93 private static final String ATTRIBUTE_NAME = "testname";
94
95 /**
96 * Unit test for private constructor.
97 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053098 * @throws SecurityException if any security violation is observed
99 * @throws NoSuchMethodException if when the method is not found
100 * @throws IllegalArgumentException if there is illegal argument found
101 * @throws InstantiationException if instantiation is provoked for the private constructor
102 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswald6f12412016-03-28 15:50:13 +0530103 * @throws InvocationTargetException when an exception occurs by the method or constructor
104 */
105 @Test
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530106 public void callPrivateConstructors()
107 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswald6f12412016-03-28 15:50:13 +0530108 InstantiationException, IllegalAccessException, InvocationTargetException {
109
Bharat saraswal33dfa012016-05-17 19:59:16 +0530110 Class<?>[] classesToConstruct = {MethodsGenerator.class };
Bharat saraswald6f12412016-03-28 15:50:13 +0530111 for (Class<?> clazz : classesToConstruct) {
112 Constructor<?> constructor = clazz.getDeclaredConstructor();
113 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530114 assertThat(null, not(constructor.newInstance()));
Bharat saraswald6f12412016-03-28 15:50:13 +0530115 }
116 }
117
118 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530119 * Unit test case for checking the parse builder and type constructor.
Bharat saraswald6f12412016-03-28 15:50:13 +0530120 */
121 @Test
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530122 public void getTypeConstructorTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530123
124 YangPluginConfig pluginConfig = new YangPluginConfig();
Bharat saraswald6f12412016-03-28 15:50:13 +0530125 JavaAttributeInfo testAttr = getTestAttribute();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530126 String test = getTypeConstructorStringAndJavaDoc(testAttr, CLASS_NAME, pluginConfig);
Bharat saraswald6f12412016-03-28 15:50:13 +0530127 assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
128 }
129
130 /**
131 * Test for build method for class.
132 */
133 @Test
134 public void getBuildTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530135 String method = getBuild(CLASS_NAME);
136 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
137 + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
138 + RETURN + SPACE + NEW + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
139 + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
140
141 }
142
143 /**
144 * Test for build method of interface.
145 */
146 @Test
147 public void getBuildForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530148 String method = getBuildForInterface(CLASS_NAME);
149 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
150 OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
151 }
152
153 /**
154 * Test for check not null method.
155 */
156 @Test
157 public void getCheckNotNullTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530158 String method = getCheckNotNull(CLASS_NAME);
159 assertThat(true, is(method.equals(EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS
160 + CLASS_NAME + COMMA + SPACE + CLASS_NAME + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
161 }
162
163 /**
164 * Test case for constructor.
165 */
166 @Test
167 public void getConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530168 JavaAttributeInfo testAttr = getTestAttribute();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530169 YangPluginConfig pluginConfig = new YangPluginConfig();
170 String method = getConstructor(CLASS_NAME, testAttr, GENERATE_SERVICE_AND_MANAGER, pluginConfig);
Bharat saraswald6f12412016-03-28 15:50:13 +0530171 assertThat(true, is(method.contains(THIS + PERIOD + CLASS_NAME + SPACE + EQUAL + SPACE + "builder" + OBJECT
172 + PERIOD + GET_METHOD_PREFIX + "Testname" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN)));
173 }
174
175 /**
176 * Test for constrcutor start method.
177 */
178 @Test
179 public void getConstructorStartTest() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530180 YangPluginConfig pluginConfig = new YangPluginConfig();
181 String method = getConstructorStart(CLASS_NAME, pluginConfig);
Bharat saraswald6f12412016-03-28 15:50:13 +0530182 assertThat(true, is(method.contains(PUBLIC + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + CLASS_NAME
183 + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
184 + OPEN_CURLY_BRACKET + NEW_LINE)));
185 }
186
187 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530188 * Test case for equals method.
Bharat saraswald6f12412016-03-28 15:50:13 +0530189 */
190 @Test
191 public void getEqualsMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530192 JavaAttributeInfo testAttr = getTestAttribute();
193 String method = getEqualsMethod(testAttr);
194 assertThat(true, is(method.contains(SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD
195 + EQUALS_STRING + OPEN_PARENTHESIS)));
196 }
197
198 /**
199 * Test for to string method.
200 */
201 @Test
202 public void getToStringMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530203 JavaAttributeInfo testAttr = getTestAttribute();
204 String method = getToStringMethod(testAttr);
205 assertThat(true, is(method.equals(
206 TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
207 + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
208 }
209
210 /**
211 * Test for getter method of class.
212 */
213 @Test
214 public void getGetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530215 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216 String method = getGetterForClass(testAttr, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530217 assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
218 }
219
220 /**
221 * Test for getter of interface.
222 */
223 @Test
224 public void getGetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530225 String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530226 assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
227 }
228
229 /**
230 * Test case for setter method of class.
231 */
232 @Test
233 public void getSetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530234 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530235 String method = getSetterForClass(testAttr, CLASS_NAME, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530236 assertThat(true, is(
237 method.contains(PUBLIC + SPACE + VOID + SPACE +
238 SET_METHOD_PREFIX + getCapitalCase(CLASS_NAME) + OPEN_PARENTHESIS +
239 STRING_DATA_TYPE + SPACE + ATTRIBUTE_NAME)));
Bharat saraswald6f12412016-03-28 15:50:13 +0530240 }
241
242 /**
243 * Test for setter method of interface.
244 */
245 @Test
246 public void getSetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530247 String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false,
248 GENERATE_SERVICE_AND_MANAGER);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530249 assertThat(true, is(method.contains(VOID + SPACE +
250 SET_METHOD_PREFIX + "Testname")));
Bharat saraswald6f12412016-03-28 15:50:13 +0530251 }
252
253 /**
254 * Test case for of method.
255 */
256 @Test
257 public void getOfMethodest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530258 JavaAttributeInfo testAttr = getTestAttribute();
259 String method = getOfMethod(CLASS_NAME, testAttr);
260 assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
261 + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
262 }
263
264 /**
265 * Test case for setter in type def class.
266 */
267 @Test
268 public void getSetterForTypeDefClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530269 JavaAttributeInfo testAttr = getTestAttribute();
270 String method = getSetterForTypeDefClass(testAttr);
271 assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
272 }
273
274 /**
275 * Test case for over ride string.
276 */
277 @Test
278 public void getOverRideStringTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530279 String method = getOverRideString();
280 assertThat(true, is(method.contains(OVERRIDE)));
281 }
282
283 /**
284 * Returns java attribute.
285 *
286 * @return java attribute
287 */
288 private JavaAttributeInfo getTestAttribute() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530289 JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
290 testAttr.setAttributeName(ATTRIBUTE_NAME);
291 testAttr.setAttributeType(getTestYangType());
292 testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
293 return testAttr;
294 }
295
296 /**
297 * Returns java qualified info.
298 *
299 * @return java qualified info
300 */
301 private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530302 JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
303 info.setPkgInfo(JAVA_LANG);
304 info.setClassInfo(STRING_DATA_TYPE);
305 return info;
306 }
307
308 /**
309 * Returns stub YANG type.
310 *
311 * @return test YANG type
312 */
313 private YangType<?> getTestYangType() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530314 YangType<?> attrType = new YangType<>();
315 attrType.setDataTypeName(STRING_DATA_TYPE);
316 attrType.setDataType(STRING);
317 return attrType;
318 }
319}