blob: 020ba44738beaae12d7586b83e47421b83d21047 [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;
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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswald6f12412016-03-28 15:50:13 +053032import 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;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053046import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswald6f12412016-03-28 15:50:13 +053047import 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 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053096 * @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
Bharat saraswald6f12412016-03-28 15:50:13 +0530101 * @throws InvocationTargetException when an exception occurs by the method or constructor
102 */
103 @Test
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530104 public void callPrivateConstructors()
105 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswald6f12412016-03-28 15:50:13 +0530106 InstantiationException, IllegalAccessException, InvocationTargetException {
107
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530108 Class<?>[] classesToConstruct = {MethodsGenerator.class};
Bharat saraswald6f12412016-03-28 15:50:13 +0530109 for (Class<?> clazz : classesToConstruct) {
110 Constructor<?> constructor = clazz.getDeclaredConstructor();
111 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530112 assertThat(null, not(constructor.newInstance()));
Bharat saraswald6f12412016-03-28 15:50:13 +0530113 }
114 }
115
116 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530117 * Unit test case for checking the parse builder and type constructor.
Bharat saraswald6f12412016-03-28 15:50:13 +0530118 */
119 @Test
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530120 public void getTypeConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530121 JavaAttributeInfo testAttr = getTestAttribute();
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530122 String test = getTypeConstructorStringAndJavaDoc(testAttr, CLASS_NAME);
Bharat saraswald6f12412016-03-28 15:50:13 +0530123 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() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530131 String method = getBuild(CLASS_NAME);
132 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
133 + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
134 + RETURN + SPACE + NEW + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
135 + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
136
137 }
138
139 /**
140 * Test for build method of interface.
141 */
142 @Test
143 public void getBuildForInterfaceTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530144 String method = getBuildForInterface(CLASS_NAME);
145 assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
146 OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
147 }
148
149 /**
150 * Test for check not null method.
151 */
152 @Test
153 public void getCheckNotNullTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530154 String method = getCheckNotNull(CLASS_NAME);
155 assertThat(true, is(method.equals(EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS
156 + CLASS_NAME + COMMA + SPACE + CLASS_NAME + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
157 }
158
159 /**
160 * Test case for constructor.
161 */
162 @Test
163 public void getConstructorTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530164 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530165 String method = getConstructor(CLASS_NAME, testAttr, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530166 assertThat(true, is(method.contains(THIS + PERIOD + CLASS_NAME + SPACE + EQUAL + SPACE + "builder" + OBJECT
167 + PERIOD + GET_METHOD_PREFIX + "Testname" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN)));
168 }
169
170 /**
171 * Test for constrcutor start method.
172 */
173 @Test
174 public void getConstructorStartTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530175 String method = getConstructorStart(CLASS_NAME);
176 assertThat(true, is(method.contains(PUBLIC + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + CLASS_NAME
177 + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
178 + OPEN_CURLY_BRACKET + NEW_LINE)));
179 }
180
181 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530182 * Test case for equals method.
Bharat saraswald6f12412016-03-28 15:50:13 +0530183 */
184 @Test
185 public void getEqualsMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530186 JavaAttributeInfo testAttr = getTestAttribute();
187 String method = getEqualsMethod(testAttr);
188 assertThat(true, is(method.contains(SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD
189 + EQUALS_STRING + OPEN_PARENTHESIS)));
190 }
191
192 /**
193 * Test for to string method.
194 */
195 @Test
196 public void getToStringMethodTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530197 JavaAttributeInfo testAttr = getTestAttribute();
198 String method = getToStringMethod(testAttr);
199 assertThat(true, is(method.equals(
200 TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
201 + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
202 }
203
204 /**
205 * Test for getter method of class.
206 */
207 @Test
208 public void getGetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530209 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530210 String method = getGetterForClass(testAttr, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530211 assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
212 }
213
214 /**
215 * Test for getter of interface.
216 */
217 @Test
218 public void getGetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530219 String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false, GENERATE_SERVICE_AND_MANAGER);
Bharat saraswald6f12412016-03-28 15:50:13 +0530220 assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
221 }
222
223 /**
224 * Test case for setter method of class.
225 */
226 @Test
227 public void getSetterForClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530228 JavaAttributeInfo testAttr = getTestAttribute();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530229 String method = getSetterForClass(testAttr, CLASS_NAME, GENERATE_SERVICE_AND_MANAGER);
230// assertThat(true, is(
231// method.contains(PUBLIC + SPACE + CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX
232// + getCaptialCase(ATTRIBUTE_NAME) + OPEN_PARENTHESIS + STRING_DATA_TYPE + SPACE
233// + ATTRIBUTE_NAME)));
Bharat saraswald6f12412016-03-28 15:50:13 +0530234 }
235
236 /**
237 * Test for setter method of interface.
238 */
239 @Test
240 public void getSetterForInterfaceTest() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241 String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false,
242 GENERATE_SERVICE_AND_MANAGER);
243// assertThat(true, is(method.contains(CLASS_NAME + BUILDER + SPACE + SET_METHOD_PREFIX + "Testname")));
Bharat saraswald6f12412016-03-28 15:50:13 +0530244 }
245
246 /**
247 * Test case for of method.
248 */
249 @Test
250 public void getOfMethodest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530251 JavaAttributeInfo testAttr = getTestAttribute();
252 String method = getOfMethod(CLASS_NAME, testAttr);
253 assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
254 + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
255 }
256
257 /**
258 * Test case for setter in type def class.
259 */
260 @Test
261 public void getSetterForTypeDefClassTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530262 JavaAttributeInfo testAttr = getTestAttribute();
263 String method = getSetterForTypeDefClass(testAttr);
264 assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
265 }
266
267 /**
268 * Test case for over ride string.
269 */
270 @Test
271 public void getOverRideStringTest() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530272 String method = getOverRideString();
273 assertThat(true, is(method.contains(OVERRIDE)));
274 }
275
276 /**
277 * Returns java attribute.
278 *
279 * @return java attribute
280 */
281 private JavaAttributeInfo getTestAttribute() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530282 JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
283 testAttr.setAttributeName(ATTRIBUTE_NAME);
284 testAttr.setAttributeType(getTestYangType());
285 testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
286 return testAttr;
287 }
288
289 /**
290 * Returns java qualified info.
291 *
292 * @return java qualified info
293 */
294 private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530295 JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
296 info.setPkgInfo(JAVA_LANG);
297 info.setClassInfo(STRING_DATA_TYPE);
298 return info;
299 }
300
301 /**
302 * Returns stub YANG type.
303 *
304 * @return test YANG type
305 */
306 private YangType<?> getTestYangType() {
Bharat saraswald6f12412016-03-28 15:50:13 +0530307 YangType<?> attrType = new YangType<>();
308 attrType.setDataTypeName(STRING_DATA_TYPE);
309 attrType.setDataType(STRING);
310 return attrType;
311 }
312}