b.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 19 | import org.junit.Test; |
| 20 | import static org.hamcrest.core.Is.is; |
| 21 | import static org.junit.Assert.assertThat; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 24 | import org.onosproject.yangutils.datamodel.YangType; |
| 25 | import org.onosproject.yangutils.translator.tojava.AttributeInfo; |
| 26 | import org.onosproject.yangutils.translator.tojava.ImportInfo; |
| 27 | import org.onosproject.yangutils.utils.UtilConstants; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 28 | import java.lang.reflect.Constructor; |
| 29 | import java.lang.reflect.InvocationTargetException; |
| 30 | |
b.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 31 | /** |
| 32 | * Unit tests for generated methods from the file type. |
| 33 | */ |
| 34 | public 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 S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 42 | * @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 saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 45 | * @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.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 48 | */ |
| 49 | @Test |
| 50 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 51 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 52 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 53 | Class<?>[] classesToConstruct = {MethodsGenerator.class }; |
b.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 54 | for (Class<?> clazz : classesToConstruct) { |
| 55 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 56 | constructor.setAccessible(true); |
| 57 | assertNotNull(constructor.newInstance()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 62 | * 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.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 87 | */ |
| 88 | @Test |
| 89 | public void getValuesTest() { |
| 90 | String stringConstructor = MethodsGenerator.getConstructorString("testname"); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 91 | 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.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 98 | String stringBuild = MethodsGenerator.getBuildString("testname"); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 99 | assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD) |
| 100 | && stringBuild.contains(UtilConstants.RETURN), is(true)); |
| 101 | |
b.janani | 8b8ebdc | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 102 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 103 | |
| 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 | } |