blob: 91a723fd1d11be6eb7bd8d1ab33105f19897d1de [file] [log] [blame]
b.janani8b8ebdc2016-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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053019import org.junit.Test;
20import static org.hamcrest.core.Is.is;
21import static org.junit.Assert.assertThat;
22import static org.junit.Assert.assertNotNull;
23import org.onosproject.yangutils.datamodel.YangDataTypes;
24import org.onosproject.yangutils.datamodel.YangType;
25import org.onosproject.yangutils.translator.tojava.AttributeInfo;
26import org.onosproject.yangutils.translator.tojava.ImportInfo;
27import org.onosproject.yangutils.utils.UtilConstants;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053028import java.lang.reflect.Constructor;
29import java.lang.reflect.InvocationTargetException;
30
b.janani8b8ebdc2016-02-25 12:25:55 +053031/**
32 * Unit tests for generated methods from the file type.
33 */
34public final class MethodsGeneratorTest {
35
36 public static AttributeInfo testAttr = new AttributeInfo();
37 public static YangType<?> attrType = new YangType<>();
38
39 /**
40 * Unit test for private constructor.
41 *
Vinod Kumar Sc4216002016-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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053045 * @throws InstantiationException if instantiation is provoked for the private constructor
46 * @throws IllegalAccessException if instance is provoked or a method is provoked
47 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani8b8ebdc2016-02-25 12:25:55 +053048 */
49 @Test
50 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053051 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani8b8ebdc2016-02-25 12:25:55 +053052
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053053 Class<?>[] classesToConstruct = {MethodsGenerator.class };
b.janani8b8ebdc2016-02-25 12:25:55 +053054 for (Class<?> clazz : classesToConstruct) {
55 Constructor<?> constructor = clazz.getDeclaredConstructor();
56 constructor.setAccessible(true);
57 assertNotNull(constructor.newInstance());
58 }
59 }
60
61 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053062 * Unit test case for checking the parse builder and typedef constructor.
63 */
64 @Test
65 public void getParseBuilderInterfaceMethodConstructorTest() {
66 ImportInfo forSetter = new ImportInfo();
67 attrType.setDataTypeName("binary");
68 attrType.getDataTypeName();
69 attrType.setDataType(YangDataTypes.BINARY);
70 attrType.getDataType();
71 testAttr.setAttributeName("attributeTest");
72 testAttr.setAttributeType(attrType);
73 forSetter.setPkgInfo("test1/test3");
74 forSetter.setClassInfo("This class contains");
75 testAttr.setImportInfo(forSetter);
76 String parseBuilderInterface = MethodsGenerator.parseBuilderInterfaceMethodString(testAttr, "newTestName");
77 assertThat(parseBuilderInterface.contains("attributeTest") && parseBuilderInterface.contains("newTestName"),
78 is(true));
79 String parseBuilderInterfaceBuild = MethodsGenerator.parseBuilderInterfaceBuildMethodString("testname7");
80 assertThat(parseBuilderInterfaceBuild.contains("Builds object of")
81 && parseBuilderInterfaceBuild.contains("testname7"), is(true));
82 String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
83 }
84
85 /**
86 * Unit test case for checking the values received from constructor, default constructor and build string formation.
b.janani8b8ebdc2016-02-25 12:25:55 +053087 */
88 @Test
89 public void getValuesTest() {
90 String stringConstructor = MethodsGenerator.getConstructorString("testname");
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053091 assertThat(stringConstructor.contains(UtilConstants.JAVA_DOC_CONSTRUCTOR)
92 && stringConstructor.contains(UtilConstants.JAVA_DOC_PARAM)
93 && stringConstructor.contains(UtilConstants.BUILDER_OBJECT), is(true));
94 String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString("testnameBuilder", "public");
95 assertThat(stringDefaultConstructor.contains(UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR)
96 && stringDefaultConstructor.contains(UtilConstants.BUILDER)
97 && stringDefaultConstructor.contains("testname"), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053098 String stringBuild = MethodsGenerator.getBuildString("testname");
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053099 assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD)
100 && stringBuild.contains(UtilConstants.RETURN), is(true));
101
b.janani8b8ebdc2016-02-25 12:25:55 +0530102 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530103
104 /**
105 * Unit test for checking the values received for class getter, class and typedef setters with list data type.
106 */
107 @Test
108 public void getGetterSetterTest() {
109
110 ImportInfo forGetterSetter = new ImportInfo();
111 attrType.setDataTypeName("int");
112 attrType.getDataTypeName();
113 attrType.setDataType(YangDataTypes.UINT8);
114 attrType.getDataType();
115 testAttr.setAttributeName("AttributeTest1");
116 testAttr.setAttributeType(attrType);
117 forGetterSetter.setPkgInfo(null);
118 forGetterSetter.setClassInfo("This class contains");
119 testAttr.setImportInfo(forGetterSetter);
120 testAttr.setListAttr(true);
121 String getterForClass = MethodsGenerator.getGetterForClass(testAttr);
122 assertThat(getterForClass.contains(UtilConstants.GET_METHOD_PREFIX) && getterForClass.contains("List<")
123 && getterForClass.contains("attributeTest1"), is(true));
124 String setterForClass = MethodsGenerator.getSetterForClass(testAttr, "TestThis");
125 assertThat(setterForClass.contains(UtilConstants.SET_METHOD_PREFIX) && setterForClass.contains("List<")
126 && setterForClass.contains("attributeTest1"), is(true));
127 String typeDefSetter = MethodsGenerator.getSetterForTypeDefClass(testAttr);
128 assertThat(typeDefSetter.contains(UtilConstants.SET_METHOD_PREFIX) && typeDefSetter.contains("List<")
129 && typeDefSetter.contains("attributeTest1") && typeDefSetter.contains("this."), is(true));
130 }
131
132 /**
133 * Unit test case for checking the parse builder and typedef constructor with list data type.
134 */
135 @Test
136 public void getConstructorWithListTypeTest() {
137 ImportInfo forSetter = new ImportInfo();
138 attrType.setDataTypeName("binary");
139 attrType.getDataTypeName();
140 attrType.setDataType(YangDataTypes.BINARY);
141 attrType.getDataType();
142 testAttr.setAttributeName("attributeTest");
143 testAttr.setAttributeType(attrType);
144 forSetter.setPkgInfo(null);
145 forSetter.setClassInfo("This class contains");
146 testAttr.setImportInfo(forSetter);
147 testAttr.setListAttr(true);
148 String parseBuilderInterface = MethodsGenerator.parseBuilderInterfaceMethodString(testAttr, "newTestName");
149 assertThat(parseBuilderInterface.contains("attributeTest") && parseBuilderInterface.contains("List<"),
150 is(true));
151 String parseBuilderInterfaceBuild = MethodsGenerator.parseBuilderInterfaceBuildMethodString("testname7");
152 assertThat(parseBuilderInterfaceBuild.contains("Builds object of")
153 && parseBuilderInterfaceBuild.contains("testname7"), is(true));
154 String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
155 assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname")
156 && stringTypeDef.contains(UtilConstants.THIS), is(true));
157 }
158}