blob: 38d18c2077b4aa06b062cbd761588eb16e1ebf2e [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;
b.janani66b749c2016-02-24 12:23:03 +053023
Bharat saraswalc2d3be12016-06-16 00:29:12 +053024import org.apache.commons.io.FileUtils;
b.janani66b749c2016-02-24 12:23:03 +053025import org.apache.maven.project.MavenProject;
Bharat saraswalc34b9442016-03-28 15:50:13 +053026import org.junit.Rule;
27import org.junit.Test;
28import org.junit.rules.ExpectedException;
29import org.onosproject.yangutils.utils.UtilConstants;
b.janani66b749c2016-02-24 12:23:03 +053030import org.sonatype.plexus.build.incremental.BuildContext;
31import org.sonatype.plexus.build.incremental.DefaultBuildContext;
32
b.janani66b749c2016-02-24 12:23:03 +053033import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053034import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053035import static org.junit.Assert.assertThat;
36import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053037import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.addToCompilationRoot;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
40import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053041
42/**
Bharat saraswal780eca32016-04-05 12:45:45 +053043 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053044 */
45public final class YangIoUtilsTest {
46
Bharat saraswalc34b9442016-03-28 15:50:13 +053047 private static final String BASE_DIR = "target/UnitTestCase";
48 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
49 private static final String CHECK_STRING = "one, two, three, four, five, six";
50 private static final String TRIM_STRING = "one, two, three, four, five, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053051 private static final String CHECK1 = "check1";
52 private static final String PKG_INFO = "package-info.java";
53 private static final String PATH = "src/main/yangmodel/";
54 private static final String MSG = "Exception occured while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053055
Bharat saraswalc34b9442016-03-28 15:50:13 +053056 /**
57 * Expected exceptions.
58 */
b.janani66b749c2016-02-24 12:23:03 +053059 @Rule
60 public ExpectedException thrown = ExpectedException.none();
61
62 /**
63 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053064 *
65 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053066 */
67 @Test
68 public void addPackageInfoTest() throws IOException {
69
Bharat saraswalc34b9442016-03-28 15:50:13 +053070 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053071 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053072 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053073 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053074 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053075 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +053076 }
77
78 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053079 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053080 *
81 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053082 */
83 @Test
84 public void addPackageInfoWithPathTest() throws IOException {
85
Bharat saraswalc34b9442016-03-28 15:50:13 +053086 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053087 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053088 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053089 File filePath = new File(dirPath + File.separator + PKG_INFO);
90 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053091 FileUtils.deleteDirectory(dirPath);
Bharat saraswal250a7472016-05-12 13:16:57 +053092 }
93
94 /**
95 * This test case checks with a child node.
96 *
97 * @throws IOException when fails to do IO operations for test case
98 */
99 @Test
100 public void addPackageInfoWithChildNode() throws IOException {
101
102 File dirPath = new File(CREATE_PATH);
103 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530104 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530105 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530106 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530107 FileUtils.deleteDirectory(dirPath);
Bharat saraswal022dae92016-03-04 20:08:09 +0530108 }
109
110 /**
b.janani66b749c2016-02-24 12:23:03 +0530111 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530112 *
113 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530114 */
115 @Test
116 public void addPackageInfoWithEmptyPathTest() throws IOException {
117
b.jananie6d43af2016-03-04 12:29:05 +0530118 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530119 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530120 thrown.expectMessage(MSG);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530121 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530122 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530123 assertThat(filePath1.isFile(), is(false));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530124 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530125 }
126
127 /**
128 * A private constructor is tested.
129 *
Bharat saraswal022dae92016-03-04 20:08:09 +0530130 * @throws SecurityException if any security violation is observed
131 * @throws NoSuchMethodException if when the method is not found
132 * @throws IllegalArgumentException if there is illegal argument found
133 * @throws InstantiationException if instantiation is provoked for the private constructor
134 * @throws IllegalAccessException if instance is provoked or a method is provoked
135 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530136 */
137 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530138 public void callPrivateConstructors()
139 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530140 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530141
142 Class<?>[] classesToConstruct = {YangIoUtils.class };
143 for (Class<?> clazz : classesToConstruct) {
144 Constructor<?> constructor = clazz.getDeclaredConstructor();
145 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530146 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530147 }
148 }
149
150 /**
151 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530152 *
153 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530154 */
155 @Test
156 public void cleanGeneratedDirTest() throws IOException {
157
Bharat saraswalc34b9442016-03-28 15:50:13 +0530158 File baseDirPath = new File(BASE_DIR);
159 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530160 createNewDir.mkdirs();
161 File createFile = new File(createNewDir + File.separator + "check1.java");
162 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530163 deleteDirectory(baseDirPath.getAbsolutePath());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530164 FileUtils.deleteDirectory(createNewDir);
165 FileUtils.deleteDirectory(baseDirPath);
b.janani66b749c2016-02-24 12:23:03 +0530166 }
167
168 /**
169 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530170 *
171 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530172 */
173 @Test
174 public void cleanWithInvalidDirTest() throws IOException {
175
Bharat saraswalc34b9442016-03-28 15:50:13 +0530176 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530177 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530178 }
179
180 /**
181 * This test case tests whether the directories are getting created.
182 */
183 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530184 public void createDirectoryTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530185
Bharat saraswalc34b9442016-03-28 15:50:13 +0530186 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530187 assertThat(dirPath.isDirectory(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530188 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530189 }
190
191 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530192 * This test case checks whether the source is getting added.
b.janani66b749c2016-02-24 12:23:03 +0530193 */
194 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530195 public void testForAddSource() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530196
197 MavenProject project = new MavenProject();
198 BuildContext context = new DefaultBuildContext();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530199 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani66b749c2016-02-24 12:23:03 +0530200 sourceDir.mkdirs();
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530201 addToCompilationRoot(sourceDir.toString(), project, context);
202 FileUtils.deleteDirectory(sourceDir);
Bharat saraswalc34b9442016-03-28 15:50:13 +0530203 }
204
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530205 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530206 * Unit test case for trim at last method.
207 */
208 @Test
209 public void testForTrimAtLast() {
210
211 String test = trimAtLast(CHECK_STRING, "six");
212 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530213 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530214
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530215 /**
216 * Returns stub pluginConfig.
217 *
218 * @return stub pluginConfig
219 */
220 private YangPluginConfig getStubPluginConfig() {
221 YangPluginConfig pluginConfig = new YangPluginConfig();
222 pluginConfig.setConflictResolver(null);
223 return pluginConfig;
224 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530225}