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