blob: cf1a11016e4cb098379eb7ce8df49f84f86f4eb3 [file] [log] [blame]
Bharat saraswald6f12412016-03-28 15:50:13 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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 */
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;
26
27import static org.hamcrest.core.Is.is;
28import static org.junit.Assert.assertNotNull;
29import static org.junit.Assert.assertThat;
30import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
31import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
32import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuild;
33import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildForInterface;
34import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getCheckNotNull;
35import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
37import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
38import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
39import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForInterface;
40import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
41import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
42import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
43import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForInterface;
44import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
45import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
46import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
47import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
48import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
49import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
50import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
51import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
52import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
53import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
54import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
55import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
56import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
57import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
58import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
59import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
60import 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;
84
85/**
86 * Unit tests for generated methods from the file type.
87 */
88public final class MethodsGeneratorTest {
89
90 private static final String CLASS_NAME = "testname";
91 private static final String ATTRIBUTE_NAME = "testname";
92
93 /**
94 * Unit test for private constructor.
95 *
96 * @throws SecurityException if any security violation is observed
97 * @throws NoSuchMethodException if when the method is not found
98 * @throws IllegalArgumentException if there is illegal argument found
99 * @throws InstantiationException if instantiation is provoked for the private constructor
100 * @throws IllegalAccessException if instance is provoked or a method is provoked
101 * @throws InvocationTargetException when an exception occurs by the method or constructor
102 */
103 @Test
104 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
105 InstantiationException, IllegalAccessException, InvocationTargetException {
106
107 Class<?>[] classesToConstruct = {MethodsGenerator.class };
108 for (Class<?> clazz : classesToConstruct) {
109 Constructor<?> constructor = clazz.getDeclaredConstructor();
110 constructor.setAccessible(true);
111 assertNotNull(constructor.newInstance());
112 }
113 }
114
115 /**
116 * Unit test case for checking the parse builder and typedef constructor.
117 */
118 @Test
119 public void getTypeDefConstructorTest() {
120
121 JavaAttributeInfo testAttr = getTestAttribute();
122 String test = getTypeDefConstructor(testAttr, CLASS_NAME);
123 assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
124 }
125
126 /**
127 * Test for build method for class.
128 */
129 @Test
130 public void getBuildTest() {
131
132 String method = getBuild(CLASS_NAME);
133 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
134 + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
135 + RETURN + SPACE + NEW + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
136 + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
137
138 }
139
140 /**
141 * Test for build method of interface.
142 */
143 @Test
144 public void getBuildForInterfaceTest() {
145
146 String method = getBuildForInterface(CLASS_NAME);
147 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
148 OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
149 }
150
151 /**
152 * Test for check not null method.
153 */
154 @Test
155 public void getCheckNotNullTest() {
156
157 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() {
167
168 JavaAttributeInfo testAttr = getTestAttribute();
169 String method = getConstructor(CLASS_NAME, testAttr);
170 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() {
179
180 String method = getConstructorStart(CLASS_NAME);
181 assertThat(true, is(method.contains(PUBLIC + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + CLASS_NAME
182 + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
183 + OPEN_CURLY_BRACKET + NEW_LINE)));
184 }
185
186 /**
187 * Test case for quals method.
188 */
189 @Test
190 public void getEqualsMethodTest() {
191
192 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() {
203
204 JavaAttributeInfo testAttr = getTestAttribute();
205 String method = getToStringMethod(testAttr);
206 assertThat(true, is(method.equals(
207 TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
208 + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
209 }
210
211 /**
212 * Test for getter method of class.
213 */
214 @Test
215 public void getGetterForClassTest() {
216
217 JavaAttributeInfo testAttr = getTestAttribute();
218 String method = getGetterForClass(testAttr);
219 assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
220 }
221
222 /**
223 * Test for getter of interface.
224 */
225 @Test
226 public void getGetterForInterfaceTest() {
227
228 String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false);
229 assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
230 }
231
232 /**
233 * Test case for setter method of class.
234 */
235 @Test
236 public void getSetterForClassTest() {
237
238 JavaAttributeInfo testAttr = getTestAttribute();
239 String method = getSetterForClass(testAttr, CLASS_NAME);
240 assertThat(true, is(
241 method.contains(PUBLIC + SPACE + CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX
242 + getCaptialCase(ATTRIBUTE_NAME) + OPEN_PARENTHESIS + STRING_DATA_TYPE + SPACE
243 + ATTRIBUTE_NAME)));
244 }
245
246 /**
247 * Test for setter method of interface.
248 */
249 @Test
250 public void getSetterForInterfaceTest() {
251
252 String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false);
253 assertThat(true, is(method.contains(CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX + "Testname")));
254 }
255
256 /**
257 * Test case for of method.
258 */
259 @Test
260 public void getOfMethodest() {
261
262 JavaAttributeInfo testAttr = getTestAttribute();
263 String method = getOfMethod(CLASS_NAME, testAttr);
264 assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
265 + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
266 }
267
268 /**
269 * Test case for setter in type def class.
270 */
271 @Test
272 public void getSetterForTypeDefClassTest() {
273
274 JavaAttributeInfo testAttr = getTestAttribute();
275 String method = getSetterForTypeDefClass(testAttr);
276 assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
277 }
278
279 /**
280 * Test case for over ride string.
281 */
282 @Test
283 public void getOverRideStringTest() {
284
285 String method = getOverRideString();
286 assertThat(true, is(method.contains(OVERRIDE)));
287 }
288
289 /**
290 * Returns java attribute.
291 *
292 * @return java attribute
293 */
294 private JavaAttributeInfo getTestAttribute() {
295
296 JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
297 testAttr.setAttributeName(ATTRIBUTE_NAME);
298 testAttr.setAttributeType(getTestYangType());
299 testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
300 return testAttr;
301 }
302
303 /**
304 * Returns java qualified info.
305 *
306 * @return java qualified info
307 */
308 private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
309
310 JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
311 info.setPkgInfo(JAVA_LANG);
312 info.setClassInfo(STRING_DATA_TYPE);
313 return info;
314 }
315
316 /**
317 * Returns stub YANG type.
318 *
319 * @return test YANG type
320 */
321 private YangType<?> getTestYangType() {
322
323 YangType<?> attrType = new YangType<>();
324 attrType.setDataTypeName(STRING_DATA_TYPE);
325 attrType.setDataType(STRING);
326 return attrType;
327 }
328}