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