blob: 39b03fe6bbdb3c417cb266fb899b589023884683 [file] [log] [blame]
Bharat saraswalc2d3be12016-06-16 00:29:12 +05301/*
2 * Copyright 2016-present 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
17package org.onosproject.yangutils.linker;
18
19import java.io.File;
20import java.io.IOException;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24import java.util.ListIterator;
25
26import org.apache.maven.plugin.MojoExecutionException;
27import org.junit.Test;
28import org.onosproject.yangutils.datamodel.YangContainer;
29import org.onosproject.yangutils.datamodel.YangDerivedInfo;
30import org.onosproject.yangutils.datamodel.YangGrouping;
31import org.onosproject.yangutils.datamodel.YangLeaf;
32import org.onosproject.yangutils.datamodel.YangNode;
33import org.onosproject.yangutils.plugin.manager.YangFileInfo;
34import org.onosproject.yangutils.plugin.manager.YangUtilManager;
35import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
40import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
41import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
42import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deSerializeDataModel;
43import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
44import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.parseJarFile;
45
46/**
47 * Unit test case for inter-jar linker.
48 */
49public class InterJarLinkingTest {
50
51 private final YangUtilManager utilManager = new YangUtilManager();
52
53 private static final String TARGET = "target/interJarFileLinking";
54 private static final String SEARCH_DIR_FOR_YANG_FILES = "src/test/resources/interJarFileLinking/yangFiles";
55 private static final String SEARCH_DIR_FOR_SINGLE_JAR_FILES = "src/test/resources/interJarFileLinking/"
56 + "jarFiles/single";
57 private static final String SEARCH_DIR_FOR_MULTI_JAR_FILES = "src/test/resources/interJarFileLinking/"
58 + "jarFiles/multi";
59
60 /**
61 * Unit test case for a single jar dependency.
62 *
63 * @throws IOException when fails to do IO operations
64 * @throws MojoExecutionException when fails to do mojo operations
65 */
66 @Test
67 public void processSingleJarLinking()
68 throws IOException, MojoExecutionException {
69 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(SEARCH_DIR_FOR_YANG_FILES));
70
71 int size1 = utilManager.getYangFileInfoSet().size();
72
73 for (String file : getListOfTestJar(SEARCH_DIR_FOR_SINGLE_JAR_FILES)) {
74 addInterJarRootNodes(file);
75 }
76
77 utilManager.parseYangFileInfoSet();
78
79 utilManager.resolveDependenciesUsingLinker();
80
81 Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
82
83 YangFileInfo yangFileInfo = yangFileInfoIterator.next();
84
85 int size2 = utilManager.getYangFileInfoSet().size();
86 assertThat(true, is(size1 != size2));
87 assertThat(true, is(yangFileInfo.getRootNode().getName().equals("port-pair")));
88
89 deleteDirectory(TARGET);
90
91 }
92
93 /**
94 * Unit test case for a multiple jar dependency.
95 *
96 * @throws IOException when fails to do IO operations
97 * @throws MojoExecutionException when fails to do mojo operations
98 */
99 @Test
100 public void processMultipleJarLinking()
101 throws IOException, MojoExecutionException {
102 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(SEARCH_DIR_FOR_YANG_FILES));
103
104 int size1 = utilManager.getYangFileInfoSet().size();
105
106 for (String file : getListOfTestJar(SEARCH_DIR_FOR_MULTI_JAR_FILES)) {
107 addInterJarRootNodes(file);
108 }
109
110 utilManager.parseYangFileInfoSet();
111
112 utilManager.resolveDependenciesUsingLinker();
113
114 Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
115
116 YangFileInfo yangFileInfo = yangFileInfoIterator.next();
117
118 int size2 = utilManager.getYangFileInfoSet().size();
119 assertThat(true, is(size1 != size2));
120 assertThat(true, is(yangFileInfo.getRootNode().getName().equals("port-pair")));
121
122 yangFileInfo = yangFileInfoIterator.next();
123 assertThat(true, is(yangFileInfo.getRootNode().getName().equals("flow-classifier")));
124
125 /*
126 * grouping flow-classifier {
127 * container flow-classifier {
128 * leaf id {
129 * type flow-classifier-id;
130 * }
131 *
132 * leaf tenant-id {
133 * type port-pair:tenant-id;
134 * }
135 * .
136 * .
137 * .
138 *
139 */
140
141 YangNode node = yangFileInfo.getRootNode();
142 node = node.getChild();
143 while (node != null) {
144 if (node instanceof YangGrouping) {
145 break;
146 }
147 node = node.getNextSibling();
148 }
149
150 node = node.getChild();
151 ListIterator<YangLeaf> leafIterator = ((YangContainer) node).getListOfLeaf().listIterator();
152 YangLeaf leafInfo = leafIterator.next();
153
154 assertThat(leafInfo.getName(), is("id"));
155 assertThat(leafInfo.getDataType().getDataTypeName(), is("flow-classifier-id"));
156 assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
157
158 leafInfo = leafIterator.next();
159
160 assertThat(leafInfo.getName(), is("tenant-id"));
161 assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
162
163 assertThat(true, is(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef()
164 .getName().equals("tenant-id")));
165
166 assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
167
168 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
169
170 // Check for the effective built-in type.
171 assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
172
173 deleteDirectory(TARGET);
174 }
175
176 /**
177 * Returns list of test jar files.
178 *
179 * @param searchdir search directory
180 * @return list of test jar files
181 */
182 private List<String> getListOfTestJar(String searchdir) {
183 List<String> jarFiles = new ArrayList<>();
184
185 File directory = new File(searchdir + "/");
186 File[] files = directory.listFiles();
187
188 for (File file : files) {
189 jarFiles.add(file.toString());
190 }
191
192 return jarFiles;
193 }
194
195 /**
196 * Adds data model nodes of jar to file info set.
197 *
198 * @param jarFile jar file name
199 * @throws IOException when fails to do IO operations
200 */
201 private void addInterJarRootNodes(String jarFile) throws IOException {
202 try {
203 List<YangNode> interJarResolvedNodes = deSerializeDataModel(parseJarFile(jarFile, TARGET));
204
205 for (YangNode node : interJarResolvedNodes) {
206 YangFileInfo dependentFileInfo = new YangFileInfo();
207 dependentFileInfo.setRootNode(node);
208 dependentFileInfo.setForTranslator(false);
209 dependentFileInfo.setYangFileName(node.getName());
210 utilManager.getYangFileInfoSet().add(dependentFileInfo);
211 }
212 } catch (IOException e) {
213 throw new IOException("failed to resolve in interjar scenario.");
214 }
215 }
216
217}