blob: 370df8d4823d42e1d31402d4f034ed54d01c0b67 [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;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053028import static org.hamcrest.core.IsNot.not;
Bharat saraswald6f12412016-03-28 15:50:13 +053029import 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);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111 assertThat(null, not(constructor.newInstance()));
Bharat saraswald6f12412016-03-28 15:50:13 +0530112 }
113 }
114
115 /**
116 * Unit test case for checking the parse builder and typedef constructor.
117 */
118 @Test
119 public void getTypeDefConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530120 JavaAttributeInfo testAttr = getTestAttribute();
121 String test = getTypeDefConstructor(testAttr, CLASS_NAME);
122 assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
123 }
124
125 /**
126 * Test for build method for class.
127 */
128 @Test
129 public void getBuildTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530130 String method = getBuild(CLASS_NAME);
131 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
132 + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
133 + RETURN + SPACE + NEW + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
134 + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
135
136 }
137
138 /**
139 * Test for build method of interface.
140 */
141 @Test
142 public void getBuildForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530143 String method = getBuildForInterface(CLASS_NAME);
144 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
145 OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
146 }
147
148 /**
149 * Test for check not null method.
150 */
151 @Test
152 public void getCheckNotNullTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530153 String method = getCheckNotNull(CLASS_NAME);
154 assertThat(true, is(method.equals(EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS
155 + CLASS_NAME + COMMA + SPACE + CLASS_NAME + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
156 }
157
158 /**
159 * Test case for constructor.
160 */
161 @Test
162 public void getConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530163 JavaAttributeInfo testAttr = getTestAttribute();
164 String method = getConstructor(CLASS_NAME, testAttr);
165 assertThat(true, is(method.contains(THIS + PERIOD + CLASS_NAME + SPACE + EQUAL + SPACE + "builder" + OBJECT
166 + PERIOD + GET_METHOD_PREFIX + "Testname" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN)));
167 }
168
169 /**
170 * Test for constrcutor start method.
171 */
172 @Test
173 public void getConstructorStartTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530174 String method = getConstructorStart(CLASS_NAME);
175 assertThat(true, is(method.contains(PUBLIC + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + CLASS_NAME
176 + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
177 + OPEN_CURLY_BRACKET + NEW_LINE)));
178 }
179
180 /**
181 * Test case for quals method.
182 */
183 @Test
184 public void getEqualsMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530185 JavaAttributeInfo testAttr = getTestAttribute();
186 String method = getEqualsMethod(testAttr);
187 assertThat(true, is(method.contains(SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD
188 + EQUALS_STRING + OPEN_PARENTHESIS)));
189 }
190
191 /**
192 * Test for to string method.
193 */
194 @Test
195 public void getToStringMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530196 JavaAttributeInfo testAttr = getTestAttribute();
197 String method = getToStringMethod(testAttr);
198 assertThat(true, is(method.equals(
199 TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
200 + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
201 }
202
203 /**
204 * Test for getter method of class.
205 */
206 @Test
207 public void getGetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530208 JavaAttributeInfo testAttr = getTestAttribute();
209 String method = getGetterForClass(testAttr);
210 assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
211 }
212
213 /**
214 * Test for getter of interface.
215 */
216 @Test
217 public void getGetterForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530218 String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false);
219 assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
220 }
221
222 /**
223 * Test case for setter method of class.
224 */
225 @Test
226 public void getSetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530227 JavaAttributeInfo testAttr = getTestAttribute();
228 String method = getSetterForClass(testAttr, CLASS_NAME);
229 assertThat(true, is(
230 method.contains(PUBLIC + SPACE + CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX
231 + getCaptialCase(ATTRIBUTE_NAME) + OPEN_PARENTHESIS + STRING_DATA_TYPE + SPACE
232 + ATTRIBUTE_NAME)));
233 }
234
235 /**
236 * Test for setter method of interface.
237 */
238 @Test
239 public void getSetterForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530240 String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false);
241 assertThat(true, is(method.contains(CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX + "Testname")));
242 }
243
244 /**
245 * Test case for of method.
246 */
247 @Test
248 public void getOfMethodest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530249 JavaAttributeInfo testAttr = getTestAttribute();
250 String method = getOfMethod(CLASS_NAME, testAttr);
251 assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
252 + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
253 }
254
255 /**
256 * Test case for setter in type def class.
257 */
258 @Test
259 public void getSetterForTypeDefClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530260 JavaAttributeInfo testAttr = getTestAttribute();
261 String method = getSetterForTypeDefClass(testAttr);
262 assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
263 }
264
265 /**
266 * Test case for over ride string.
267 */
268 @Test
269 public void getOverRideStringTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530270 String method = getOverRideString();
271 assertThat(true, is(method.contains(OVERRIDE)));
272 }
273
274 /**
275 * Returns java attribute.
276 *
277 * @return java attribute
278 */
279 private JavaAttributeInfo getTestAttribute() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530280 JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
281 testAttr.setAttributeName(ATTRIBUTE_NAME);
282 testAttr.setAttributeType(getTestYangType());
283 testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
284 return testAttr;
285 }
286
287 /**
288 * Returns java qualified info.
289 *
290 * @return java qualified info
291 */
292 private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530293 JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
294 info.setPkgInfo(JAVA_LANG);
295 info.setClassInfo(STRING_DATA_TYPE);
296 return info;
297 }
298
299 /**
300 * Returns stub YANG type.
301 *
302 * @return test YANG type
303 */
304 private YangType<?> getTestYangType() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530305 YangType<?> attrType = new YangType<>();
306 attrType.setDataTypeName(STRING_DATA_TYPE);
307 attrType.setDataType(STRING);
308 return attrType;
309 }
310}