b.janani | 0d4517a | 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 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 22 | import org.junit.Test; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangType; |
| 24 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 25 | import org.onosproject.yangutils.translator.tojava.AttributeInfo; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 26 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 27 | import static org.hamcrest.core.Is.is; |
| 28 | import static org.junit.Assert.assertNotNull; |
| 29 | import static org.junit.Assert.assertThat; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 30 | |
| 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 | 0871098 | 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 |
| 45 | * @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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 51 | */ |
| 52 | @Test |
| 53 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 54 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 55 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 56 | Class<?>[] classesToConstruct = { |
| 57 | MethodsGenerator.class }; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 58 | for (Class<?> clazz : classesToConstruct) { |
| 59 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 60 | constructor.setAccessible(true); |
| 61 | assertNotNull(constructor.newInstance()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 66 | * Unit test for checking the values received from constructor, default |
| 67 | * constructor and build string formation. |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 68 | */ |
| 69 | @Test |
| 70 | public void getValuesTest() { |
| 71 | String stringConstructor = MethodsGenerator.getConstructorString("testname"); |
| 72 | assertThat( |
| 73 | stringConstructor.contains("Construct the object of testnameImpl.") |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 74 | && stringConstructor.contains("@param testnameObject builder object of testname") |
| 75 | && stringConstructor.contains("public testnameImpl(testnameBuilder testnameObject) {"), |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 76 | is(true)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 77 | String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString( |
| 78 | GeneratedFileType.BUILDER_CLASS_MASK, |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 79 | "testname"); |
| 80 | assertThat(stringDefaultConstructor.contains("Default Constructor.") |
| 81 | && stringDefaultConstructor.contains("public testnameBuilder() {") |
| 82 | && stringDefaultConstructor.contains("}"), is(true)); |
| 83 | String stringBuild = MethodsGenerator.getBuildString("testname"); |
| 84 | assertThat( |
| 85 | stringBuild.contains("public testname build() {") |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame^] | 86 | && stringBuild.contains("return new testnameImpl(this);") && stringBuild.contains("}"), |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 87 | is(true)); |
| 88 | } |
| 89 | } |