b.janani | 66b749c | 2016-02-24 12:23:03 +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.utils.io.impl; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.junit.Rule; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | import org.onosproject.yangutils.utils.UtilConstants; |
| 23 | |
| 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | import org.apache.maven.project.MavenProject; |
| 28 | import org.sonatype.plexus.build.incremental.BuildContext; |
| 29 | import org.sonatype.plexus.build.incremental.DefaultBuildContext; |
| 30 | |
| 31 | import org.slf4j.Logger; |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | import static org.junit.Assert.assertNotNull; |
| 35 | import static org.hamcrest.core.Is.is; |
| 36 | import java.lang.reflect.Constructor; |
| 37 | import java.lang.reflect.InvocationTargetException; |
| 38 | |
| 39 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 40 | * Unit tests for YANG io utils. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 41 | */ |
| 42 | public final class YangIoUtilsTest { |
| 43 | |
| 44 | public static String baseDir = "target/UnitTestCase"; |
| 45 | |
| 46 | public static String createPath = baseDir + File.separator + "dir1/dir2/dir3/dir4/"; |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | @Rule |
| 51 | public ExpectedException thrown = ExpectedException.none(); |
| 52 | |
| 53 | /** |
| 54 | * This test case checks whether the package-info file is created. |
| 55 | */ |
| 56 | @Test |
| 57 | public void addPackageInfoTest() throws IOException { |
| 58 | |
| 59 | File dirPath = new File(createPath); |
| 60 | dirPath.mkdirs(); |
| 61 | CopyrightHeader.parseCopyrightHeader(); |
| 62 | YangIoUtils.addPackageInfo(dirPath, "check1", createPath); |
| 63 | File filePath = new File(dirPath + File.separator + "package-info.java"); |
| 64 | assertThat(filePath.isFile(), is(true)); |
| 65 | } |
| 66 | |
| 67 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 68 | * This test case checks with an additional info in the path. |
| 69 | */ |
| 70 | @Test |
| 71 | public void addPackageInfoWithPathTest() throws IOException { |
| 72 | |
| 73 | File dirPath = new File(createPath); |
| 74 | dirPath.mkdirs(); |
| 75 | CopyrightHeader.parseCopyrightHeader(); |
| 76 | YangIoUtils.addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + createPath); |
| 77 | File filePath = new File(dirPath + File.separator + "package-info.java"); |
| 78 | assertThat(filePath.isFile(), is(true)); |
| 79 | } |
| 80 | |
| 81 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 82 | * This test case checks whether the package-info file is created when invalid path is given. |
| 83 | */ |
| 84 | @Test |
| 85 | public void addPackageInfoWithEmptyPathTest() throws IOException { |
| 86 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 87 | File dirPath = new File("invalid/check"); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 88 | thrown.expect(IOException.class); |
| 89 | thrown.expectMessage("Exception occured while creating package info file."); |
| 90 | YangIoUtils.addPackageInfo(dirPath, "check1", createPath); |
| 91 | File filePath1 = new File(dirPath + File.separator + "package-info.java"); |
| 92 | assertThat(filePath1.isFile(), is(false)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * A private constructor is tested. |
| 97 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 98 | * @throws SecurityException if any security violation is observed |
| 99 | * @throws NoSuchMethodException if when the method is not found |
| 100 | * @throws IllegalArgumentException if there is illegal argument found |
| 101 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 102 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 103 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 104 | */ |
| 105 | @Test |
| 106 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 108 | |
| 109 | Class<?>[] classesToConstruct = {YangIoUtils.class }; |
| 110 | for (Class<?> clazz : classesToConstruct) { |
| 111 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 112 | constructor.setAccessible(true); |
| 113 | assertNotNull(constructor.newInstance()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * This test case checks if the directory is cleaned. |
| 119 | */ |
| 120 | @Test |
| 121 | public void cleanGeneratedDirTest() throws IOException { |
| 122 | |
| 123 | File baseDirPath = new File(baseDir); |
| 124 | File createNewDir = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR); |
| 125 | createNewDir.mkdirs(); |
| 126 | File createFile = new File(createNewDir + File.separator + "check1.java"); |
| 127 | createFile.createNewFile(); |
| 128 | YangIoUtils.clean(baseDirPath.getAbsolutePath()); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * This test case checks the cleaning method when an invalid path is provided. |
| 133 | */ |
| 134 | @Test |
| 135 | public void cleanWithInvalidDirTest() throws IOException { |
| 136 | |
| 137 | File baseDirPath = new File(baseDir + "invalid"); |
| 138 | YangIoUtils.clean(baseDirPath.getAbsolutePath()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * This test case tests whether the directories are getting created. |
| 143 | */ |
| 144 | @Test |
| 145 | public void createDirectoryTest() { |
| 146 | |
| 147 | File dirPath = YangIoUtils.createDirectories(createPath); |
| 148 | assertThat(dirPath.isDirectory(), is(true)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * This testcase checks whether the source is getting added. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testForAddSource() { |
| 156 | |
| 157 | MavenProject project = new MavenProject(); |
| 158 | BuildContext context = new DefaultBuildContext(); |
| 159 | File sourceDir = new File(baseDir + File.separator + "yang"); |
| 160 | sourceDir.mkdirs(); |
| 161 | YangIoUtils.addToSource(sourceDir.toString(), project, context); |
| 162 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 163 | } |