blob: 204bd7b100dc45d294b0969faf7cc722033159f9 [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
24import org.apache.maven.project.MavenProject;
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;
b.janani66b749c2016-02-24 12:23:03 +053029import org.sonatype.plexus.build.incremental.BuildContext;
30import org.sonatype.plexus.build.incremental.DefaultBuildContext;
31
b.janani66b749c2016-02-24 12:23:03 +053032import static org.hamcrest.core.Is.is;
Bharat saraswal780eca32016-04-05 12:45:45 +053033import static org.hamcrest.core.IsNot.not;
Bharat saraswalc34b9442016-03-28 15:50:13 +053034import static org.junit.Assert.assertThat;
35import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
36import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053037import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Bharat saraswalc34b9442016-03-28 15:50:13 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
39import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
b.janani66b749c2016-02-24 12:23:03 +053040
41/**
Bharat saraswal780eca32016-04-05 12:45:45 +053042 * Unit tests for YANG IO utils.
b.janani66b749c2016-02-24 12:23:03 +053043 */
44public final class YangIoUtilsTest {
45
Bharat saraswalc34b9442016-03-28 15:50:13 +053046 private static final String BASE_DIR = "target/UnitTestCase";
47 private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
48 private static final String CHECK_STRING = "one, two, three, four, five, six";
49 private static final String TRIM_STRING = "one, two, three, four, five, ";
Bharat saraswal250a7472016-05-12 13:16:57 +053050 private static final String CHECK1 = "check1";
51 private static final String PKG_INFO = "package-info.java";
52 private static final String PATH = "src/main/yangmodel/";
53 private static final String MSG = "Exception occured while creating package info file.";
b.janani66b749c2016-02-24 12:23:03 +053054
Bharat saraswalc34b9442016-03-28 15:50:13 +053055 /**
56 * Expected exceptions.
57 */
b.janani66b749c2016-02-24 12:23:03 +053058 @Rule
59 public ExpectedException thrown = ExpectedException.none();
60
61 /**
62 * This test case checks whether the package-info file is created.
Bharat saraswalc34b9442016-03-28 15:50:13 +053063 *
64 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +053065 */
66 @Test
67 public void addPackageInfoTest() throws IOException {
68
Bharat saraswalc34b9442016-03-28 15:50:13 +053069 File dirPath = new File(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +053070 dirPath.mkdirs();
Bharat saraswal250a7472016-05-12 13:16:57 +053071 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
72 File filePath = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +053073 assertThat(filePath.isFile(), is(true));
74 }
75
76 /**
Bharat saraswal022dae92016-03-04 20:08:09 +053077 * This test case checks with an additional info in the path.
Bharat saraswalc34b9442016-03-28 15:50:13 +053078 *
79 * @throws IOException when fails to do IO operations for test case
Bharat saraswal022dae92016-03-04 20:08:09 +053080 */
81 @Test
82 public void addPackageInfoWithPathTest() throws IOException {
83
Bharat saraswalc34b9442016-03-28 15:50:13 +053084 File dirPath = new File(CREATE_PATH);
Bharat saraswal022dae92016-03-04 20:08:09 +053085 dirPath.mkdirs();
Bharat saraswal250a7472016-05-12 13:16:57 +053086 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false);
87 File filePath = new File(dirPath + File.separator + PKG_INFO);
88 assertThat(filePath.isFile(), is(true));
89 }
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();
101 addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true);
102 File filePath = new File(dirPath + File.separator + PKG_INFO);
Bharat saraswal022dae92016-03-04 20:08:09 +0530103 assertThat(filePath.isFile(), is(true));
104 }
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);
117 addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
118 File filePath1 = new File(dirPath + File.separator + PKG_INFO);
b.janani66b749c2016-02-24 12:23:03 +0530119 assertThat(filePath1.isFile(), is(false));
120 }
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
133 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530134 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +0530135
136 Class<?>[] classesToConstruct = {YangIoUtils.class };
137 for (Class<?> clazz : classesToConstruct) {
138 Constructor<?> constructor = clazz.getDeclaredConstructor();
139 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +0530140 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +0530141 }
142 }
143
144 /**
145 * This test case checks if the directory is cleaned.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530146 *
147 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530148 */
149 @Test
150 public void cleanGeneratedDirTest() throws IOException {
151
Bharat saraswalc34b9442016-03-28 15:50:13 +0530152 File baseDirPath = new File(BASE_DIR);
153 File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
b.janani66b749c2016-02-24 12:23:03 +0530154 createNewDir.mkdirs();
155 File createFile = new File(createNewDir + File.separator + "check1.java");
156 createFile.createNewFile();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530157 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530158 }
159
160 /**
161 * This test case checks the cleaning method when an invalid path is provided.
Bharat saraswalc34b9442016-03-28 15:50:13 +0530162 *
163 * @throws IOException when fails to do IO operations for test case
b.janani66b749c2016-02-24 12:23:03 +0530164 */
165 @Test
166 public void cleanWithInvalidDirTest() throws IOException {
167
Bharat saraswalc34b9442016-03-28 15:50:13 +0530168 File baseDirPath = new File(BASE_DIR + "invalid");
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530169 deleteDirectory(baseDirPath.getAbsolutePath());
b.janani66b749c2016-02-24 12:23:03 +0530170 }
171
172 /**
173 * This test case tests whether the directories are getting created.
174 */
175 @Test
176 public void createDirectoryTest() {
177
Bharat saraswalc34b9442016-03-28 15:50:13 +0530178 File dirPath = createDirectories(CREATE_PATH);
b.janani66b749c2016-02-24 12:23:03 +0530179 assertThat(dirPath.isDirectory(), is(true));
180 }
181
182 /**
Bharat saraswalc34b9442016-03-28 15:50:13 +0530183 * This test case checks whether the source is getting added.
b.janani66b749c2016-02-24 12:23:03 +0530184 */
185 @Test
186 public void testForAddSource() {
187
188 MavenProject project = new MavenProject();
189 BuildContext context = new DefaultBuildContext();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530190 File sourceDir = new File(BASE_DIR + File.separator + "yang");
b.janani66b749c2016-02-24 12:23:03 +0530191 sourceDir.mkdirs();
Bharat saraswalc34b9442016-03-28 15:50:13 +0530192 addToSource(sourceDir.toString(), project, context);
193 }
194
195 /*
196 * Unit test case for trim at last method.
197 */
198 @Test
199 public void testForTrimAtLast() {
200
201 String test = trimAtLast(CHECK_STRING, "six");
202 assertThat(test.contains(TRIM_STRING), is(true));
b.janani66b749c2016-02-24 12:23:03 +0530203 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530204
Bharat saraswal022dae92016-03-04 20:08:09 +0530205}