blob: c10ed039b2bbca25a267757b72d024d11bb3eb60 [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;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053029import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Bharat saraswalc34b9442016-03-28 15:50:13 +053030import org.onosproject.yangutils.utils.UtilConstants;
b.janani66b749c2016-02-24 12:23:03 +053031import org.sonatype.plexus.build.incremental.BuildContext;
32import org.sonatype.plexus.build.incremental.DefaultBuildContext;
33
Bharat saraswalc2d3be12016-06-16 00:29:12 +053034import static org.apache.commons.io.FileUtils.deleteDirectory;
b.janani66b749c2016-02-24 12:23:03 +053035import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053036import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053037import static org.junit.Assert.assertThat;
38import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToCompilationRoot;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053040import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
42import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053043
44/**
Bharat saraswal780eca32016-04-05 12:45:45 +053045 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053046 */
47public final class YangIoUtilsTest {
48
Bharat saraswalc34b9442016-03-28 15:50:13 +053049 private static final String BASE_DIR = "target/UnitTestCase";
50 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
51 private static final String CHECK_STRING = "one, two, three, four, five, six";
52 private static final String TRIM_STRING = "one, two, three, four, five, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053053 private static final String CHECK1 = "check1";
54 private static final String PKG_INFO = "package-info.java";
55 private static final String PATH = "src/main/yangmodel/";
56 private static final String MSG = "Exception occured while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053057
Bharat saraswalc34b9442016-03-28 15:50:13 +053058 /**
59 * Expected exceptions.
60 */
b.janani66b749c2016-02-24 12:23:03 +053061 @Rule
62 public ExpectedException thrown = ExpectedException.none();
63
64 /**
65 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053066 *
67 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053068 */
69 @Test
70 public void addPackageInfoTest() throws IOException {
71
Bharat saraswalc34b9442016-03-28 15:50:13 +053072 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053073 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053074 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053075 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053076 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053077 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +053078 }
79
80 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053081 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053082 *
83 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053084 */
85 @Test
86 public void addPackageInfoWithPathTest() throws IOException {
87
Bharat saraswalc34b9442016-03-28 15:50:13 +053088 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053089 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053090 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053091 File filePath = new File(dirPath + File.separator + PKG_INFO);
92 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053093 FileUtils.deleteDirectory(dirPath);
Bharat saraswal250a7472016-05-12 13:16:57 +053094 }
95
96 /**
97 * This test case checks with a child node.
98 *
99 * @throws IOException when fails to do IO operations for test case
100 */
101 @Test
102 public void addPackageInfoWithChildNode() throws IOException {
103
104 File dirPath = new File(CREATE_PATH);
105 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530106 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530107 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530108 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530109 FileUtils.deleteDirectory(dirPath);
Bharat saraswal022dae92016-03-04 20:08:09 +0530110 }
111
112 /**
b.janani66b749c2016-02-24 12:23:03 +0530113 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530114 *
115 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530116 */
117 @Test
118 public void addPackageInfoWithEmptyPathTest() throws IOException {
119
b.jananie6d43af2016-03-04 12:29:05 +0530120 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530121 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530122 thrown.expectMessage(MSG);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530123 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530124 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530125 assertThat(filePath1.isFile(), is(false));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530126 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530127 }
128
129 /**
130 * A private constructor is tested.
131 *
Bharat saraswal022dae92016-03-04 20:08:09 +0530132 * @throws SecurityException if any security violation is observed
133 * @throws NoSuchMethodException if when the method is not found
134 * @throws IllegalArgumentException if there is illegal argument found
135 * @throws InstantiationException if instantiation is provoked for the private constructor
136 * @throws IllegalAccessException if instance is provoked or a method is provoked
137 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530138 */
139 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530140 public void callPrivateConstructors()
141 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530142 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530143
144 Class<?>[] classesToConstruct = {YangIoUtils.class };
145 for (Class<?> clazz : classesToConstruct) {
146 Constructor<?> constructor = clazz.getDeclaredConstructor();
147 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530148 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530149 }
150 }
151
152 /**
153 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530154 *
155 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530156 */
157 @Test
158 public void cleanGeneratedDirTest() throws IOException {
159
Bharat saraswalc34b9442016-03-28 15:50:13 +0530160 File baseDirPath = new File(BASE_DIR);
161 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530162 createNewDir.mkdirs();
163 File createFile = new File(createNewDir + File.separator + "check1.java");
164 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530165 deleteDirectory(baseDirPath.getAbsolutePath());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530166 FileUtils.deleteDirectory(createNewDir);
167 FileUtils.deleteDirectory(baseDirPath);
b.janani66b749c2016-02-24 12:23:03 +0530168 }
169
170 /**
171 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530172 *
173 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530174 */
175 @Test
176 public void cleanWithInvalidDirTest() throws IOException {
177
Bharat saraswalc34b9442016-03-28 15:50:13 +0530178 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530180 }
181
182 /**
183 * This test case tests whether the directories are getting created.
184 */
185 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530186 public void createDirectoryTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530187
Bharat saraswalc34b9442016-03-28 15:50:13 +0530188 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530189 assertThat(dirPath.isDirectory(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530190 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530191 }
192
193 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530194 * This test case checks whether the source is getting added.
b.janani66b749c2016-02-24 12:23:03 +0530195 */
196 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530197 public void testForAddSource() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530198
199 MavenProject project = new MavenProject();
200 BuildContext context = new DefaultBuildContext();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530201 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani66b749c2016-02-24 12:23:03 +0530202 sourceDir.mkdirs();
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530203 addToCompilationRoot(sourceDir.toString(), project, context);
204 FileUtils.deleteDirectory(sourceDir);
Bharat saraswalc34b9442016-03-28 15:50:13 +0530205 }
206
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530207 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530208 * Unit test case for trim at last method.
209 */
210 @Test
211 public void testForTrimAtLast() {
212
213 String test = trimAtLast(CHECK_STRING, "six");
214 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530215 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530216
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530217 /**
218 * Returns stub pluginConfig.
219 *
220 * @return stub pluginConfig
221 */
222 private YangPluginConfig getStubPluginConfig() {
223 YangPluginConfig pluginConfig = new YangPluginConfig();
224 pluginConfig.setConflictResolver(null);
225 return pluginConfig;
226 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530227}