blob: 6766aeccd8a5bd0652b4461d899f0f109b41d6dd [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
b.janani66b749c2016-02-24 12:23:03 +053019import java.io.File;
20import java.io.IOException;
Bharat saraswalc34b9442016-03-28 15:50:13 +053021import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
Bharat saraswale304c252016-08-16 20:56:20 +053023
Bharat saraswalc2d3be12016-06-16 00:29:12 +053024import org.apache.commons.io.FileUtils;
Bharat saraswalc34b9442016-03-28 15:50:13 +053025import org.junit.Rule;
26import org.junit.Test;
27import org.junit.rules.ExpectedException;
28import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswale304c252016-08-16 20:56:20 +053029import org.onosproject.yangutils.utils.io.YangPluginConfig;
b.janani66b749c2016-02-24 12:23:03 +053030
b.janani66b749c2016-02-24 12:23:03 +053031import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053032import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053033import static org.junit.Assert.assertThat;
34import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswalc34b9442016-03-28 15:50:13 +053035import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
Gaurav Agrawalde75bea2016-06-16 13:09:53 +053036import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053037import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053038
39/**
Bharat saraswal780eca32016-04-05 12:45:45 +053040 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053041 */
42public final class YangIoUtilsTest {
43
Bharat saraswalc34b9442016-03-28 15:50:13 +053044 private static final String BASE_DIR = "target/UnitTestCase";
45 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
46 private static final String CHECK_STRING = "one, two, three, four, five, six";
47 private static final String TRIM_STRING = "one, two, three, four, five, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053048 private static final String CHECK1 = "check1";
49 private static final String PKG_INFO = "package-info.java";
50 private static final String PATH = "src/main/yangmodel/";
Bharat saraswal8beac342016-08-04 02:00:03 +053051 private static final String MSG = "Exception occurred while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053052
Bharat saraswalc34b9442016-03-28 15:50:13 +053053 /**
54 * Expected exceptions.
55 */
b.janani66b749c2016-02-24 12:23:03 +053056 @Rule
57 public ExpectedException thrown = ExpectedException.none();
58
59 /**
60 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053061 *
62 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053063 */
64 @Test
65 public void addPackageInfoTest() throws IOException {
66
Bharat saraswalc34b9442016-03-28 15:50:13 +053067 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053068 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053069 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053070 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053071 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +053072 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +053073 }
74
75 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053076 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053077 *
78 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053079 */
80 @Test
81 public void addPackageInfoWithPathTest() throws IOException {
82
Bharat saraswalc34b9442016-03-28 15:50:13 +053083 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053084 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053085 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053086 File filePath = new File(dirPath + File.separator + PKG_INFO);
87 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +053088 FileUtils.deleteDirectory(new File(BASE_DIR));
Bharat saraswal250a7472016-05-12 13:16:57 +053089 }
90
91 /**
92 * This test case checks with a child node.
93 *
94 * @throws IOException when fails to do IO operations for test case
95 */
96 @Test
97 public void addPackageInfoWithChildNode() throws IOException {
98
99 File dirPath = new File(CREATE_PATH);
100 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530101 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530102 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530103 assertThat(filePath.isFile(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530104 FileUtils.deleteDirectory(new File(BASE_DIR));
Bharat saraswal022dae92016-03-04 20:08:09 +0530105 }
106
107 /**
b.janani66b749c2016-02-24 12:23:03 +0530108 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530109 *
110 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530111 */
112 @Test
113 public void addPackageInfoWithEmptyPathTest() throws IOException {
114
b.jananie6d43af2016-03-04 12:29:05 +0530115 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530116 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530117 thrown.expectMessage(MSG);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530118 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530119 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530120 assertThat(filePath1.isFile(), is(false));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530121 FileUtils.deleteDirectory(dirPath);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530122 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +0530123 }
124
125 /**
126 * A private constructor is tested.
127 *
Bharat saraswale304c252016-08-16 20:56:20 +0530128 * @throws SecurityException if any security violation is observed
129 * @throws NoSuchMethodException if when the method is not found
130 * @throws IllegalArgumentException if there is illegal argument found
131 * @throws InstantiationException if instantiation is provoked for the private constructor
132 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswal022dae92016-03-04 20:08:09 +0530133 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530134 */
135 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530136 public void callPrivateConstructors()
137 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530138 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530139
Bharat saraswale304c252016-08-16 20:56:20 +0530140 Class<?>[] classesToConstruct = {YangIoUtils.class};
b.janani66b749c2016-02-24 12:23:03 +0530141 for (Class<?> clazz : classesToConstruct) {
142 Constructor<?> constructor = clazz.getDeclaredConstructor();
143 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530144 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530145 }
146 }
147
148 /**
149 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530150 *
151 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530152 */
153 @Test
154 public void cleanGeneratedDirTest() throws IOException {
155
Bharat saraswalc34b9442016-03-28 15:50:13 +0530156 File baseDirPath = new File(BASE_DIR);
157 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530158 createNewDir.mkdirs();
159 File createFile = new File(createNewDir + File.separator + "check1.java");
160 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530161 deleteDirectory(baseDirPath.getAbsolutePath());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530162 FileUtils.deleteDirectory(createNewDir);
163 FileUtils.deleteDirectory(baseDirPath);
b.janani66b749c2016-02-24 12:23:03 +0530164 }
165
166 /**
167 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530168 *
169 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530170 */
171 @Test
172 public void cleanWithInvalidDirTest() throws IOException {
173
Bharat saraswalc34b9442016-03-28 15:50:13 +0530174 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530175 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530176 }
177
178 /**
179 * This test case tests whether the directories are getting created.
180 */
181 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530182 public void createDirectoryTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530183
Bharat saraswalc34b9442016-03-28 15:50:13 +0530184 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530185 assertThat(dirPath.isDirectory(), is(true));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530186 FileUtils.deleteDirectory(new File(BASE_DIR));
b.janani66b749c2016-02-24 12:23:03 +0530187 }
188
189 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530190 * Unit test case for trim at last method.
191 */
192 @Test
193 public void testForTrimAtLast() {
194
195 String test = trimAtLast(CHECK_STRING, "six");
196 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530197 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530198
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530199 /**
200 * Returns stub pluginConfig.
201 *
202 * @return stub pluginConfig
203 */
204 private YangPluginConfig getStubPluginConfig() {
205 YangPluginConfig pluginConfig = new YangPluginConfig();
206 pluginConfig.setConflictResolver(null);
207 return pluginConfig;
208 }
Bharat saraswale304c252016-08-16 20:56:20 +0530209
Bharat saraswal022dae92016-03-04 20:08:09 +0530210}