blob: fddd7a98d4fa1c0eb8e4cdc20e8effcc668e432e [file] [log] [blame]
b.janani0d4517a2016-02-25 12:25:55 +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
Vinod Kumar S08710982016-03-03 19:55:30 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022import org.junit.Test;
23import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
25import org.onosproject.yangutils.utils.UtilConstants;
26
27import static org.hamcrest.core.Is.is;
28import static org.junit.Assert.assertNotNull;
29import static org.junit.Assert.assertThat;
30
b.janani0d4517a2016-02-25 12:25:55 +053031/**
32 * Unit tests for generated methods from the file type.
33 */
34public final class MethodsGeneratorTest {
35
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053036 public static JavaAttributeInfo testAttr;
b.janani0d4517a2016-02-25 12:25:55 +053037 public static YangType<?> attrType = new YangType<>();
38
39 /**
40 * Unit test for private constructor.
41 *
Vinod Kumar S08710982016-03-03 19:55:30 +053042 * @throws SecurityException if any security violation is observed
43 * @throws NoSuchMethodException if when the method is not found
44 * @throws IllegalArgumentException if there is illegal argument found
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045 * @throws InstantiationException if instantiation is provoked for the
46 * private constructor
47 * @throws IllegalAccessException if instance is provoked or a method is
48 * provoked
49 * @throws InvocationTargetException when an exception occurs by the method
50 * or constructor
b.janani0d4517a2016-02-25 12:25:55 +053051 */
52 @Test
53 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053054 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani0d4517a2016-02-25 12:25:55 +053055
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053056 Class<?>[] classesToConstruct = {
57 MethodsGenerator.class
58 };
b.janani0d4517a2016-02-25 12:25:55 +053059 for (Class<?> clazz : classesToConstruct) {
60 Constructor<?> constructor = clazz.getDeclaredConstructor();
61 constructor.setAccessible(true);
62 assertNotNull(constructor.newInstance());
63 }
64 }
65
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053066 // /**
67 // * Unit test case for checking the parse builder and typedef constructor.
68 // */
69 // @Test
70 // public void getParseBuilderInterfaceMethodConstructorTest() {
71 //
72 // JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
73 // attrType.setDataTypeName("binary");
74 // attrType.getDataTypeName();
75 // attrType.setDataType(YangDataTypes.BINARY);
76 // attrType.getDataType();
77 // testAttr.setAttributeName("attributeTest");
78 // testAttr.setAttributeType(attrType);
79 // forSetter.setPkgInfo("test1/test3");
80 // forSetter.setClassInfo("This class contains");
81 // testAttr.setImportInfo(forSetter);
82 // String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
83 // }
Bharat saraswal022dae92016-03-04 20:08:09 +053084
85 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 * Unit test case for checking the values received from constructor, default
87 * constructor and build string formation.
b.janani0d4517a2016-02-25 12:25:55 +053088 */
89 @Test
90 public void getValuesTest() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091
b.janani0d4517a2016-02-25 12:25:55 +053092 String stringConstructor = MethodsGenerator.getConstructorString("testname");
Bharat saraswal022dae92016-03-04 20:08:09 +053093 assertThat(stringConstructor.contains(UtilConstants.JAVA_DOC_CONSTRUCTOR)
94 && stringConstructor.contains(UtilConstants.JAVA_DOC_PARAM)
95 && stringConstructor.contains(UtilConstants.BUILDER_OBJECT), is(true));
96 String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString("testnameBuilder", "public");
97 assertThat(stringDefaultConstructor.contains(UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR)
98 && stringDefaultConstructor.contains(UtilConstants.BUILDER)
99 && stringDefaultConstructor.contains("testname"), is(true));
b.janani0d4517a2016-02-25 12:25:55 +0530100 String stringBuild = MethodsGenerator.getBuildString("testname");
Bharat saraswal022dae92016-03-04 20:08:09 +0530101 assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD)
102 && stringBuild.contains(UtilConstants.RETURN), is(true));
103
b.janani0d4517a2016-02-25 12:25:55 +0530104 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530105
106 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530107 * Unit test for checking the values received for class getter, class and
108 * typedef setters with list data type.
Bharat saraswal022dae92016-03-04 20:08:09 +0530109 */
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 // @Test
111 // public void getGetterSetterTest() {
112 //
113 // JavaQualifiedTypeInfo forGetterSetter = new JavaQualifiedTypeInfo();
114 // attrType.setDataTypeName("int");
115 // attrType.getDataTypeName();
116 // attrType.setDataType(YangDataTypes.UINT8);
117 // attrType.getDataType();
118 // testAttr.setAttributeName("AttributeTest1");
119 // testAttr.setAttributeType(attrType);
120 // forGetterSetter.setPkgInfo("null");
121 // forGetterSetter.setClassInfo("This class contains");
122 // testAttr.setImportInfo(forGetterSetter);
123 // testAttr.setListAttr(true);
124 // String getterForClass = MethodsGenerator.getGetterForClass(testAttr);
125 // assertThat(getterForClass.contains(UtilConstants.GET_METHOD_PREFIX) && getterForClass.contains("List<")
126 // && getterForClass.contains("attributeTest1"), is(true));
127 // String setterForClass = MethodsGenerator.getSetterForClass(testAttr, "TestThis");
128 // assertThat(setterForClass.contains(UtilConstants.SET_METHOD_PREFIX) && setterForClass.contains("List<")
129 // && setterForClass.contains("attributeTest1"), is(true));
130 // String typeDefSetter = MethodsGenerator.getSetterForTypeDefClass(testAttr);
131 // assertThat(typeDefSetter.contains(UtilConstants.SET_METHOD_PREFIX) && typeDefSetter.contains("List<")
132 // && typeDefSetter.contains("attributeTest1") && typeDefSetter.contains("this."), is(true));
133 // }
Bharat saraswal022dae92016-03-04 20:08:09 +0530134
135 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 * Unit test case for checking the parse builder and typedef constructor
137 * with list data type.
Bharat saraswal022dae92016-03-04 20:08:09 +0530138 */
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530139 // @Test
140 // public void getConstructorWithListTypeTest() {
141 //
142 // JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
143 // attrType.setDataTypeName("binary");
144 // attrType.getDataTypeName();
145 // attrType.setDataType(YangDataTypes.BINARY);
146 // attrType.getDataType();
147 // testAttr.setAttributeName("attributeTest");
148 // testAttr.setAttributeType(attrType);
149 // forSetter.setPkgInfo("null");
150 // forSetter.setClassInfo("This class contains");
151 // testAttr.setImportInfo(forSetter);
152 // testAttr.setListAttr(true);
153 // String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
154 // assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname")
155 // && stringTypeDef.contains(UtilConstants.THIS), is(true));
156 // }
Bharat saraswal022dae92016-03-04 20:08:09 +0530157}