janani b
Committed by Thomas Vachuska

[ONOS-3871]Yang file-scanner implementation and UT.

Change-Id: I6eb5fd72b8a919ce7776443c59694c5e5411400d
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import java.io.File;
20 +import java.io.IOException;
21 +import java.util.LinkedList;
22 +import java.util.List;
23 +import java.util.Stack;
24 +
25 +/**
26 + * Provides the IO services for Yangutils-maven-Plugin.
27 + */
28 +public final class YangFileScanner {
29 +
30 + /**
31 + * Default constructor.
32 + */
33 + private YangFileScanner() {
34 + }
35 +
36 + /**
37 + * Returns the list of yang files.
38 + *
39 + * @param root specified directory
40 + * @return list of yang files.
41 + * @throws IOException when files get deleted while performing the operations.
42 + */
43 + public static List<String> getYangFiles(String root) throws IOException {
44 +
45 + List<String> store = new LinkedList<>();
46 + Stack<String> stack = new Stack<>();
47 + stack.push(root);
48 + File file;
49 + File[] filelist;
50 + try {
51 + while (!stack.empty()) {
52 + root = stack.pop();
53 + file = new File(root);
54 + filelist = file.listFiles();
55 + if (filelist == null) {
56 + continue;
57 + }
58 + for (File current : filelist) {
59 + if (current.isDirectory()) {
60 + stack.push(current.toString());
61 + } else {
62 + String yangFile = current.getCanonicalPath();
63 + if (yangFile.endsWith(".yang")) {
64 + store.add(yangFile);
65 + }
66 + }
67 + }
68 + }
69 + return store;
70 + } catch (IOException e) {
71 + throw new IOException("IOException occured");
72 + }
73 + }
74 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import java.io.IOException;
20 +
21 +import org.junit.Test;
22 +import java.io.File;
23 +import java.util.List;
24 +import org.slf4j.Logger;
25 +import static org.slf4j.LoggerFactory.getLogger;
26 +
27 +/**
28 + * Unit tests for searching yang files.
29 + */
30 +public class YangFileScannerTest {
31 +
32 + private final Logger log = getLogger(getClass());
33 +
34 + String baseDir = "target/UnitTestCase";
35 +
36 + /**
37 + * Checks an empty directory.
38 + */
39 + @Test
40 + public void testWithSingleEmptyDirectoryInRoot() {
41 + try {
42 + File dir = new File(baseDir);
43 + dir.mkdirs();
44 + List<String> list = YangFileScanner.getYangFiles(baseDir.toString());
45 + } catch (IOException e) {
46 + log.info("IO Exception throwed");
47 + }
48 + }
49 +
50 + /**
51 + * Checks multiple empty directories in root directory.
52 + */
53 + @Test
54 + public void testWithMultiEmptyDirectoriesInRoot() {
55 + try {
56 + String dir = "emptyDir";
57 + String dir1 = "emptyDir1";
58 + String dir2 = "emptyDir2";
59 + String dir3 = "emptyDir3";
60 + String dir4 = "emptyDir4";
61 + File firstpath = createDirectory(dir);
62 + File firstpath1 = createDirectory(dir1);
63 + File firstpath2 = createDirectory(dir2);
64 + File firstpath3 = createDirectory(dir3);
65 + File firstpath4 = createDirectory(dir4);
66 + List<String> list = YangFileScanner.getYangFiles(baseDir.toString());
67 + } catch (IOException e) {
68 + log.info("IO Exception throwed");
69 + }
70 + }
71 +
72 + /**
73 + * Checks one directory with one .yang file.
74 + */
75 + @Test
76 + public void testWithSingleDirectorySingleFileInRoot() {
77 + try {
78 + String dir1 = "level1";
79 + String firstFileName1 = "secondFile.yang";
80 + File firstpath1 = createDirectory(dir1);
81 + createFile(firstpath1, firstFileName1);
82 + List<String> list = YangFileScanner.getYangFiles(baseDir.toString());
83 + } catch (IOException e) {
84 + log.info("IO Exception throwed");
85 + }
86 + }
87 +
88 + /**
89 + * Checks one directory with many .yang file.
90 + */
91 + @Test
92 + public void testWithSingleDirectoryMultiFilesInRoot() {
93 + try {
94 + String dir2 = "level2";
95 + String firstFileName2 = "thirdFile.yang";
96 + String firstFileName3 = "fourthFile.yang";
97 + String firstFileName4 = "fifthFile.yang";
98 + String firstFileName5 = "sixthFile.yang";
99 + File firstpath2 = createDirectory(dir2);
100 + createFile(firstpath2, firstFileName2);
101 + createFile(firstpath2, firstFileName3);
102 + createFile(firstpath2, firstFileName4);
103 + createFile(firstpath2, firstFileName5);
104 + List<String> list = YangFileScanner.getYangFiles(baseDir.toString());
105 + } catch (IOException e) {
106 + log.info("IO Exception throwed");
107 + }
108 + }
109 +
110 + /**
111 + * Checks multi directories with many .yang file.
112 + */
113 + @Test
114 + public void testWithMultiDirectoriesMultiFiles() {
115 + try {
116 + String dir2 = "newDir1/newDir2/newDir3/newDir4";
117 + File dir3 = new File("target/UnitTestCase/newDir1");
118 + File dir4 = new File("target/UnitTestCase/newDir1/newDir2");
119 + File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3");
120 + File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4");
121 + String firstFileName2 = "thirdFile.yang";
122 + String firstFileName3 = "fourthFile.yang";
123 + String firstFileName4 = "fifthFile.yang";
124 + String firstFileName5 = "sixthFile.yang";
125 + File firstpath2 = createDirectory(dir2);
126 + createFile(firstpath2, firstFileName2);
127 + createFile(firstpath2, firstFileName3);
128 + createFile(firstpath2, firstFileName4);
129 + createFile(dir3, firstFileName5);
130 + createFile(dir3, firstFileName2);
131 + createFile(dir3, firstFileName3);
132 + createFile(dir3, firstFileName4);
133 + createFile(dir3, firstFileName5);
134 + createFile(dir4, firstFileName2);
135 + createFile(dir4, firstFileName3);
136 + createFile(dir4, firstFileName4);
137 + createFile(dir4, firstFileName5);
138 + createFile(dir5, firstFileName2);
139 + createFile(dir5, firstFileName3);
140 + createFile(dir5, firstFileName4);
141 + createFile(dir5, firstFileName5);
142 + createFile(dir6, firstFileName2);
143 + createFile(dir6, firstFileName3);
144 + createFile(dir6, firstFileName4);
145 + createFile(dir6, firstFileName5);
146 + List<String> list = YangFileScanner.getYangFiles(baseDir.toString());
147 + } catch (IOException e) {
148 + log.info("IO Exception throwed");
149 + }
150 + }
151 +
152 + /**
153 + * Method used for creating multiple directories inside the target file.
154 + *
155 + * @param path directory path
156 + * @return directory path
157 + */
158 + public File createDirectory(String path) {
159 + File myDir = new File(baseDir + File.separator + path);
160 + myDir.mkdirs();
161 + return myDir;
162 + }
163 +
164 + /**
165 + * Method used for creating file inside the specified directory.
166 + *
167 + * @param myDir my current dirctory
168 + * @param fileName file name
169 + * @throws IOException io exception when fails to create a file.
170 + */
171 + public void createFile(File myDir, String fileName) throws IOException {
172 + File file = null;
173 + try {
174 + file = new File(myDir + File.separator + fileName);
175 + file.createNewFile();
176 + } catch (final IOException e) {
177 + throw new IOException("IOException occured");
178 + }
179 + }
180 +}