b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 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 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.InvocationTargetException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 23 | import org.apache.commons.io.FileUtils; |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 24 | import org.junit.Rule; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.rules.ExpectedException; |
| 27 | import org.onosproject.yangutils.utils.UtilConstants; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 28 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 29 | import static org.hamcrest.core.Is.is; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 30 | import static org.hamcrest.core.IsNot.not; |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 31 | import static org.junit.Assert.assertThat; |
| 32 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo; |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories; |
Gaurav Agrawal | c5f6327 | 2016-06-16 13:09:53 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 36 | |
| 37 | /** |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 38 | * Unit tests for YANG IO utils. |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 39 | */ |
| 40 | public final class YangIoUtilsTest { |
| 41 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 42 | private static final String BASE_DIR = "target/UnitTestCase"; |
| 43 | private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/"; |
| 44 | private static final String CHECK_STRING = "one, two, three, four, five, six"; |
| 45 | private static final String TRIM_STRING = "one, two, three, four, five, "; |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 46 | private static final String CHECK1 = "check1"; |
| 47 | private static final String PKG_INFO = "package-info.java"; |
| 48 | private static final String PATH = "src/main/yangmodel/"; |
| 49 | private static final String MSG = "Exception occured while creating package info file."; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 50 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 51 | /** |
| 52 | * Expected exceptions. |
| 53 | */ |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 54 | @Rule |
| 55 | public ExpectedException thrown = ExpectedException.none(); |
| 56 | |
| 57 | /** |
| 58 | * This test case checks whether the package-info file is created. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 59 | * |
| 60 | * @throws IOException when fails to do IO operations for test case |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 61 | */ |
| 62 | @Test |
| 63 | public void addPackageInfoTest() throws IOException { |
| 64 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 65 | File dirPath = new File(CREATE_PATH); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 66 | dirPath.mkdirs(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 67 | addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig()); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 68 | File filePath = new File(dirPath + File.separator + PKG_INFO); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 69 | assertThat(filePath.isFile(), is(true)); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 70 | FileUtils.deleteDirectory(new File(BASE_DIR)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 74 | * This test case checks with an additional info in the path. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 75 | * |
| 76 | * @throws IOException when fails to do IO operations for test case |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 77 | */ |
| 78 | @Test |
| 79 | public void addPackageInfoWithPathTest() throws IOException { |
| 80 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 81 | File dirPath = new File(CREATE_PATH); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 82 | dirPath.mkdirs(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 83 | addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig()); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 84 | File filePath = new File(dirPath + File.separator + PKG_INFO); |
| 85 | assertThat(filePath.isFile(), is(true)); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 86 | FileUtils.deleteDirectory(new File(BASE_DIR)); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * This test case checks with a child node. |
| 91 | * |
| 92 | * @throws IOException when fails to do IO operations for test case |
| 93 | */ |
| 94 | @Test |
| 95 | public void addPackageInfoWithChildNode() throws IOException { |
| 96 | |
| 97 | File dirPath = new File(CREATE_PATH); |
| 98 | dirPath.mkdirs(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 99 | addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig()); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 100 | File filePath = new File(dirPath + File.separator + PKG_INFO); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 101 | assertThat(filePath.isFile(), is(true)); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 102 | FileUtils.deleteDirectory(new File(BASE_DIR)); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 106 | * This test case checks whether the package-info file is created when invalid path is given. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 107 | * |
| 108 | * @throws IOException when fails to do IO operations for test case |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 109 | */ |
| 110 | @Test |
| 111 | public void addPackageInfoWithEmptyPathTest() throws IOException { |
| 112 | |
b.janani | 1fef273 | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 113 | File dirPath = new File("invalid/check"); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 114 | thrown.expect(IOException.class); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 115 | thrown.expectMessage(MSG); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 116 | addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig()); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 117 | File filePath1 = new File(dirPath + File.separator + PKG_INFO); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 118 | assertThat(filePath1.isFile(), is(false)); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 119 | FileUtils.deleteDirectory(dirPath); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 120 | FileUtils.deleteDirectory(new File(BASE_DIR)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * A private constructor is tested. |
| 125 | * |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 126 | * @throws SecurityException if any security violation is observed |
| 127 | * @throws NoSuchMethodException if when the method is not found |
| 128 | * @throws IllegalArgumentException if there is illegal argument found |
| 129 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 130 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 131 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 132 | */ |
| 133 | @Test |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 134 | public void callPrivateConstructors() |
| 135 | throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 137 | |
| 138 | Class<?>[] classesToConstruct = {YangIoUtils.class }; |
| 139 | for (Class<?> clazz : classesToConstruct) { |
| 140 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 141 | constructor.setAccessible(true); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 142 | assertThat(null, not(constructor.newInstance())); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * This test case checks if the directory is cleaned. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 148 | * |
| 149 | * @throws IOException when fails to do IO operations for test case |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 150 | */ |
| 151 | @Test |
| 152 | public void cleanGeneratedDirTest() throws IOException { |
| 153 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 154 | File baseDirPath = new File(BASE_DIR); |
| 155 | File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 156 | createNewDir.mkdirs(); |
| 157 | File createFile = new File(createNewDir + File.separator + "check1.java"); |
| 158 | createFile.createNewFile(); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 159 | deleteDirectory(baseDirPath.getAbsolutePath()); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 160 | FileUtils.deleteDirectory(createNewDir); |
| 161 | FileUtils.deleteDirectory(baseDirPath); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * This test case checks the cleaning method when an invalid path is provided. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 166 | * |
| 167 | * @throws IOException when fails to do IO operations for test case |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 168 | */ |
| 169 | @Test |
| 170 | public void cleanWithInvalidDirTest() throws IOException { |
| 171 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 172 | File baseDirPath = new File(BASE_DIR + "invalid"); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 173 | deleteDirectory(baseDirPath.getAbsolutePath()); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * This test case tests whether the directories are getting created. |
| 178 | */ |
| 179 | @Test |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 180 | public void createDirectoryTest() throws IOException { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 181 | |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 182 | File dirPath = createDirectories(CREATE_PATH); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 183 | assertThat(dirPath.isDirectory(), is(true)); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 184 | FileUtils.deleteDirectory(new File(BASE_DIR)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 188 | * Unit test case for trim at last method. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testForTrimAtLast() { |
| 192 | |
| 193 | String test = trimAtLast(CHECK_STRING, "six"); |
| 194 | assertThat(test.contains(TRIM_STRING), is(true)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 195 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 196 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 197 | /** |
| 198 | * Returns stub pluginConfig. |
| 199 | * |
| 200 | * @return stub pluginConfig |
| 201 | */ |
| 202 | private YangPluginConfig getStubPluginConfig() { |
| 203 | YangPluginConfig pluginConfig = new YangPluginConfig(); |
| 204 | pluginConfig.setConflictResolver(null); |
| 205 | return pluginConfig; |
| 206 | } |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 207 | } |