blob: e1b167d37b3adbc5bb49bd1e5aa11cc3a25108e2 [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 saraswalc2d3be12016-06-16 00:29:12 +053023import org.apache.commons.io.FileUtils;
Bharat saraswalc34b9442016-03-28 15:50:13 +053024import org.junit.Rule;
25import org.junit.Test;
26import org.junit.rules.ExpectedException;
27import org.onosproject.yangutils.utils.UtilConstants;
b.janani66b749c2016-02-24 12:23:03 +053028
b.janani66b749c2016-02-24 12:23:03 +053029import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053030import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053031import static org.junit.Assert.assertThat;
32import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswalc34b9442016-03-28 15:50:13 +053033import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
Gaurav Agrawalde75bea2016-06-16 13:09:53 +053034import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053035import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053036
37/**
Bharat saraswal780eca32016-04-05 12:45:45 +053038 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053039 */
40public final class YangIoUtilsTest {
41
Bharat saraswalc34b9442016-03-28 15:50:13 +053042 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 saraswal250a7472016-05-12 13:16:57 +053046 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.janani66b749c2016-02-24 12:23:03 +053050
Bharat saraswalc34b9442016-03-28 15:50:13 +053051 /**
52 * Expected exceptions.
53 */
b.janani66b749c2016-02-24 12:23:03 +053054 @Rule
55 public ExpectedException thrown = ExpectedException.none();
56
57 /**
58 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053059 *
60 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053061 */
62 @Test
63 public void addPackageInfoTest() throws IOException {
64
Bharat saraswalc34b9442016-03-28 15:50:13 +053065 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053066 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053067 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053068 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053069 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053070 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +053071 }
72
73 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053074 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053075 *
76 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053077 */
78 @Test
79 public void addPackageInfoWithPathTest() throws IOException {
80
Bharat saraswalc34b9442016-03-28 15:50:13 +053081 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053082 dirPath.mkdirs();
Bharat saraswal715d3fc2016-05-17 19:59:16 +053083 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +053084 File filePath = new File(dirPath + File.separator + PKG_INFO);
85 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +053086 FileUtils.deleteDirectory(dirPath);
Bharat saraswal250a7472016-05-12 13:16:57 +053087 }
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 saraswal715d3fc2016-05-17 19:59:16 +053099 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530100 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530101 assertThat(filePath.isFile(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530102 FileUtils.deleteDirectory(dirPath);
Bharat saraswal022dae92016-03-04 20:08:09 +0530103 }
104
105 /**
b.janani66b749c2016-02-24 12:23:03 +0530106 * This test case checks whether the package-info file is created when invalid path is given.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530107 *
108 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530109 */
110 @Test
111 public void addPackageInfoWithEmptyPathTest() throws IOException {
112
b.jananie6d43af2016-03-04 12:29:05 +0530113 File dirPath = new File("invalid/check");
b.janani66b749c2016-02-24 12:23:03 +0530114 thrown.expect(IOException.class);
Bharat saraswal250a7472016-05-12 13:16:57 +0530115 thrown.expectMessage(MSG);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530116 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
Bharat saraswal250a7472016-05-12 13:16:57 +0530117 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530118 assertThat(filePath1.isFile(), is(false));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530119 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530120 }
121
122 /**
123 * A private constructor is tested.
124 *
Bharat saraswal022dae92016-03-04 20:08:09 +0530125 * @throws SecurityException if any security violation is observed
126 * @throws NoSuchMethodException if when the method is not found
127 * @throws IllegalArgumentException if there is illegal argument found
128 * @throws InstantiationException if instantiation is provoked for the private constructor
129 * @throws IllegalAccessException if instance is provoked or a method is provoked
130 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +0530131 */
132 @Test
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530133 public void callPrivateConstructors()
134 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530135 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530136
137 Class<?>[] classesToConstruct = {YangIoUtils.class };
138 for (Class<?> clazz : classesToConstruct) {
139 Constructor<?> constructor = clazz.getDeclaredConstructor();
140 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530141 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530142 }
143 }
144
145 /**
146 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530147 *
148 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530149 */
150 @Test
151 public void cleanGeneratedDirTest() throws IOException {
152
Bharat saraswalc34b9442016-03-28 15:50:13 +0530153 File baseDirPath = new File(BASE_DIR);
154 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530155 createNewDir.mkdirs();
156 File createFile = new File(createNewDir + File.separator + "check1.java");
157 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530158 deleteDirectory(baseDirPath.getAbsolutePath());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530159 FileUtils.deleteDirectory(createNewDir);
160 FileUtils.deleteDirectory(baseDirPath);
b.janani66b749c2016-02-24 12:23:03 +0530161 }
162
163 /**
164 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530165 *
166 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530167 */
168 @Test
169 public void cleanWithInvalidDirTest() throws IOException {
170
Bharat saraswalc34b9442016-03-28 15:50:13 +0530171 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530172 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530173 }
174
175 /**
176 * This test case tests whether the directories are getting created.
177 */
178 @Test
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530179 public void createDirectoryTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530180
Bharat saraswalc34b9442016-03-28 15:50:13 +0530181 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530182 assertThat(dirPath.isDirectory(), is(true));
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530183 FileUtils.deleteDirectory(dirPath);
b.janani66b749c2016-02-24 12:23:03 +0530184 }
185
186 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530187 * Unit test case for trim at last method.
188 */
189 @Test
190 public void testForTrimAtLast() {
191
192 String test = trimAtLast(CHECK_STRING, "six");
193 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530194 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530195
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530196 /**
197 * Returns stub pluginConfig.
198 *
199 * @return stub pluginConfig
200 */
201 private YangPluginConfig getStubPluginConfig() {
202 YangPluginConfig pluginConfig = new YangPluginConfig();
203 pluginConfig.setConflictResolver(null);
204 return pluginConfig;
205 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530206}