blob: 52c69d3620c04a30cff6bd2d05a5eddc2d365ab9 [file] [log] [blame]
b.janani66b749c2016-02-24 12:23:03 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani66b749c2016-02-24 12:23:03 +05303 *
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.utils.io.impl;
18
Bharat saraswalc2d3be12016-06-16 00:29:12 +053019import org.apache.commons.io.FileUtils;
Bharat saraswalc34b9442016-03-28 15:50:13 +053020import org.junit.Rule;
21import org.junit.Test;
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053024
25import java.io.File;
26import java.io.IOException;
27import java.lang.reflect.Constructor;
28import java.lang.reflect.InvocationTargetException;
b.janani66b749c2016-02-24 12:23:03 +053029
b.janani66b749c2016-02-24 12:23:03 +053030import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053031import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053032import static org.junit.Assert.assertThat;
33import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswalc34b9442016-03-28 15:50:13 +053034import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
Gaurav Agrawalde75bea2016-06-16 13:09:53 +053035import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053036import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053037
38/**
Bharat saraswal780eca32016-04-05 12:45:45 +053039 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053040 */
41public final class YangIoUtilsTest {
42
Bharat saraswalc34b9442016-03-28 15:50:13 +053043 private static final String BASE_DIR = "target/UnitTestCase";
44 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
45 private static final String CHECK_STRING = "one, two, three, four, five, six";
46 private static final String TRIM_STRING = "one, two, three, four, five, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053047 private static final String CHECK1 = "check1";
48 private static final String PKG_INFO = "package-info.java";
49 private static final String PATH = "src/main/yangmodel/";
Bharat saraswal8beac342016-08-04 02:00:03 +053050 private static final String MSG = "Exception occurred while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053051
Bharat saraswalc34b9442016-03-28 15:50:13 +053052 /**
53 * Expected exceptions.
54 */
b.janani66b749c2016-02-24 12:23:03 +053055 @Rule
56 public ExpectedException thrown = ExpectedException.none();
57
58 /**
59 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053060 *
61 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053062 */
63 @Test
64 public void addPackageInfoTest() throws IOException {
65
Bharat saraswalc34b9442016-03-28 15:50:13 +053066 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053067 dirPath.mkdirs();
Bharat saraswal9fab16b2016-09-23 23:27:24 +053068 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
Bharat saraswal250a7472016-05-12 13:16:57 +053069 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053070 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +053071 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +053072 }
73
74 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053075 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053076 *
77 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053078 */
79 @Test
80 public void addPackageInfoWithPathTest() throws IOException {
81
Bharat saraswalc34b9442016-03-28 15:50:13 +053082 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053083 dirPath.mkdirs();
Bharat saraswal9fab16b2016-09-23 23:27:24 +053084 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false);
Bharat saraswal250a7472016-05-12 13:16:57 +053085 File filePath = new File(dirPath + File.separator + PKG_INFO);
86 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +053087 FileUtils.deleteDirectory(new File(BASE_DIR));
Bharat saraswal250a7472016-05-12 13:16:57 +053088 }
89
90 /**
91 * This test case checks with a child node.
92 *
93 * @throws IOException when fails to do IO operations for test case
94 */
95 @Test
96 public void addPackageInfoWithChildNode() throws IOException {
97
98 File dirPath = new File(CREATE_PATH);
99 dirPath.mkdirs();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530100 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true);
Bharat saraswal250a7472016-05-12 13:16:57 +0530101 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530102 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530103 FileUtils.deleteDirectory(new File(BASE_DIR));
Bharat saraswal022dae92016-03-04 20:08:09 +0530104 }
105
106 /**
b.janani66b749c2016-02-24 12:23:03 +0530107 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530108 *
109 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530110 */
111 @Test
112 public void addPackageInfoWithEmptyPathTest() throws IOException {
113
b.jananie6d43af2016-03-04 12:29:05 +0530114 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530115 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530116 thrown.expectMessage(MSG);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530117 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
Bharat saraswal250a7472016-05-12 13:16:57 +0530118 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530119 assertThat(filePath1.isFile(), is(false));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530120 FileUtils.deleteDirectory(dirPath);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530121 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +0530122 }
123
124 /**
125 * A private constructor is tested.
126 *
Bharat saraswale304c252016-08-16 20:56:20 +0530127 * @throws SecurityException if any security violation is observed
128 * @throws NoSuchMethodException if when the method is not found
129 * @throws IllegalArgumentException if there is illegal argument found
130 * @throws InstantiationException if instantiation is provoked for the private constructor
131 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswal022dae92016-03-04 20:08:09 +0530132 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530133 */
134 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530135 public void callPrivateConstructors()
136 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530137 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530138
Bharat saraswale304c252016-08-16 20:56:20 +0530139 Class<?>[] classesToConstruct = {YangIoUtils.class};
b.janani66b749c2016-02-24 12:23:03 +0530140 for (Class<?> clazz : classesToConstruct) {
141 Constructor<?> constructor = clazz.getDeclaredConstructor();
142 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530143 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530144 }
145 }
146
147 /**
148 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530149 *
150 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530151 */
152 @Test
153 public void cleanGeneratedDirTest() throws IOException {
154
Bharat saraswalc34b9442016-03-28 15:50:13 +0530155 File baseDirPath = new File(BASE_DIR);
156 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530157 createNewDir.mkdirs();
158 File createFile = new File(createNewDir + File.separator + "check1.java");
159 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530160 deleteDirectory(baseDirPath.getAbsolutePath());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530161 FileUtils.deleteDirectory(createNewDir);
162 FileUtils.deleteDirectory(baseDirPath);
b.janani66b749c2016-02-24 12:23:03 +0530163 }
164
165 /**
166 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530167 *
168 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530169 */
170 @Test
171 public void cleanWithInvalidDirTest() throws IOException {
172
Bharat saraswalc34b9442016-03-28 15:50:13 +0530173 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530174 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530175 }
176
177 /**
178 * This test case tests whether the directories are getting created.
179 */
180 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530181 public void createDirectoryTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530182
Bharat saraswalc34b9442016-03-28 15:50:13 +0530183 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530184 assertThat(dirPath.isDirectory(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530185 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +0530186 }
187
188 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530189 * Unit test case for trim at last method.
190 */
191 @Test
192 public void testForTrimAtLast() {
193
194 String test = trimAtLast(CHECK_STRING, "six");
195 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530196 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530197
Bharat saraswal022dae92016-03-04 20:08:09 +0530198}