Revert "BUG-7568: refactor yang-maven-plugin"
[yangtools.git] / yang / yang-maven-plugin / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / UtilTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import java.io.BufferedInputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.net.URL;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.jar.Attributes;
25 import java.util.jar.JarEntry;
26 import java.util.jar.JarOutputStream;
27 import java.util.jar.Manifest;
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
31 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
32 import org.apache.maven.model.Dependency;
33 import org.apache.maven.model.Plugin;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.repository.RepositorySystem;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
45 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class UtilTest {
49
50     @Test
51     public void getClassPathTest() {
52         final MavenProject project = Mockito.mock(MavenProject.class);
53         final File file = Mockito.mock(File.class);
54         final File file2 = Mockito.mock(File.class);
55         final Artifact artifact = Mockito.mock(Artifact.class);
56         final Artifact artifact2 = Mockito.mock(Artifact.class);
57
58         final Set<Artifact> artifacts = new HashSet<>();
59         artifacts.add(artifact);
60         artifacts.add(artifact2);
61
62         Mockito.when(project.getArtifacts()).thenReturn(artifacts);
63         Mockito.when(artifact.getFile()).thenReturn(file);
64         Mockito.when(file.isFile()).thenReturn(true);
65         Mockito.when(file.getName()).thenReturn("iamjar.jar");
66         Mockito.when(artifact2.getFile()).thenReturn(file2);
67         Mockito.when(file2.isDirectory()).thenReturn(true);
68
69         final List<File> files = Util.getClassPath(project);
70         Assert.assertEquals(2, files.size());
71         Assert.assertTrue(files.contains(file) && files.contains(file2));
72     }
73
74     @Test
75     public void checkClasspathTest() throws Exception {
76         final MavenProject project = Mockito.mock(MavenProject.class);
77         final Plugin plugin = Mockito.mock(Plugin.class);
78         final RepositorySystem repoSystem = Mockito.mock(RepositorySystem.class);
79         final ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class);
80         final ArtifactResolutionResult artifactResolResult = Mockito.mock(ArtifactResolutionResult.class);
81         final Artifact artifact = Mockito.mock(Artifact.class);
82         final Dependency dep = Mockito.mock(Dependency.class);
83
84         final List<ArtifactRepository> remoteRepos = new ArrayList<>();
85         remoteRepos.add(localRepo);
86
87         final Set<Artifact> artifacts = new HashSet<>();
88         artifacts.add(artifact);
89
90         final List<Dependency> listDepcy = new ArrayList<>();
91         listDepcy.add(dep);
92
93         Mockito.when(project.getPlugin(Mockito.anyString())).thenReturn(plugin);
94         Mockito.when(plugin.getDependencies()).thenReturn(listDepcy);
95         Mockito.when(artifact.getArtifactId()).thenReturn("artifactId");
96         Mockito.when(artifact.getGroupId()).thenReturn("groupId");
97         Mockito.when(artifact.getVersion()).thenReturn("SNAPSHOT");
98         Mockito.when(repoSystem.createDependencyArtifact(dep)).thenReturn(artifact);
99         Mockito.when(repoSystem.resolve(Mockito.any(ArtifactResolutionRequest.class))).thenReturn(artifactResolResult);
100         Mockito.when(artifactResolResult.getArtifacts()).thenReturn(artifacts);
101         Mockito.when(project.getDependencyArtifacts()).thenReturn(artifacts);
102
103         Util.checkClasspath(project, repoSystem, localRepo, remoteRepos);
104         Assert.assertEquals(1, artifacts.size());
105         Assert.assertEquals(1, remoteRepos.size());
106         Assert.assertEquals(1, listDepcy.size());
107     }
108
109     @Test
110     public void findYangFilesInDependenciesAsStream() throws Exception {
111         final MavenProject project = Mockito.mock(MavenProject.class);
112         prepareProject(project);
113
114         final YangsInZipsResult yangzip = Util.findYangFilesInDependenciesAsStream(project);
115         Assert.assertNotNull(yangzip);
116         Assert.assertEquals(2, yangzip.getYangStreams().size());
117         yangzip.close();
118     }
119
120     @Test
121     public void findYangFilesInDependencies() throws Exception {
122         final MavenProject project = Mockito.mock(MavenProject.class);
123         prepareProject(project);
124
125         final Collection<File> files = Util.findYangFilesInDependencies(project);
126         Assert.assertNotNull(files);
127         Assert.assertEquals(2, files.size());
128     }
129
130     @Test
131     public void contextHolderTest() throws Exception {
132         final File testYang1 = new File(getClass().getResource("/test.yang").toURI());
133         final File testYang2 = new File(getClass().getResource("/test2.yang").toURI());
134         final SchemaContext context = YangParserTestUtils.parseYangSources(testYang1, testYang2);
135         final Set<Module> yangModules = new HashSet<>();
136         final Util.ContextHolder cxH = new ContextHolder(context, yangModules, yangModules);
137         Assert.assertEquals(context, cxH.getContext());
138         Assert.assertEquals(yangModules, cxH.getYangModules());
139     }
140
141     private URL makeMetaInf() throws Exception {
142         final String path = getClass().getResource("/").getPath();
143         final String metaInfPath = path + "tests/META-INF/yang";
144         final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath));
145         Assert.assertNotNull(createDirectories);
146         Assert.assertEquals(metaInfPath, createDirectories.toString());
147         Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/");
148         Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/");
149         return getClass().getResource("/tests");
150     }
151
152     private void prepareProject(final MavenProject project) throws Exception {
153         URL url = getClass().getResource("/tests");
154         if (url == null) {
155             url = makeMetaInf();
156         }
157         Assert.assertNotNull(url);
158         final File testFile = new File(getClass().getResource("/tests").toURI());
159         File testFile2 = new File(getClass().getResource("/").getPath(), "test.jar");
160         testFile2.createNewFile();
161         testFile2 = new File(getClass().getResource("/test.jar").getFile());
162
163         final Manifest manifest = new Manifest();
164         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
165         final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
166         addSourceFileToTargetJar(new File(getClass().getResource("/tests/META-INF").getPath()), target);
167         target.close();
168
169         final Artifact artifact = Mockito.mock(Artifact.class);
170         final Artifact artifact2 = Mockito.mock(Artifact.class);
171
172         final Set<Artifact> artifacts = new HashSet<>();
173         artifacts.add(artifact);
174         artifacts.add(artifact2);
175
176         Mockito.when(project.getArtifacts()).thenReturn(artifacts);
177         Mockito.when(artifact.getFile()).thenReturn(testFile);
178         Mockito.when(artifact2.getFile()).thenReturn(testFile2);
179     }
180
181     private void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
182         BufferedInputStream in = null;
183         try {
184             if (source.isDirectory()) {
185                 String name = source.getPath().replace("\\", "/");
186                 if (!name.isEmpty()) {
187                     if (!name.endsWith("/")) {
188                         name += "/";
189                     }
190                     final JarEntry entry = new JarEntry(name);
191                     entry.setTime(source.lastModified());
192                     target.putNextEntry(entry);
193                     target.closeEntry();
194                 }
195                 for (final File nestedFile : source.listFiles()) {
196                     addSourceFileToTargetJar(nestedFile, target);
197                 }
198                 return;
199             }
200
201             final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
202             entry.setTime(source.lastModified());
203             target.putNextEntry(entry);
204             in = new BufferedInputStream(new FileInputStream(source));
205
206             final byte[] buffer = new byte[1024];
207             while (true) {
208                 final int count = in.read(buffer);
209                 if (count == -1) {
210                     break;
211                 }
212                 target.write(buffer, 0, count);
213             }
214             target.closeEntry();
215         } finally {
216             if (in != null) {
217                 in.close();
218             }
219         }
220     }
221 }