Remove deprecated Yin/YangStatementSourceImpl
[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 SchemaContext context = YangParserTestUtils.parseYangResources(getClass(), "/test.yang", "/test2.yang");
138         final Set<Module> yangModules = new HashSet<>();
139         final ContextHolder cxH = new ContextHolder(context, yangModules, ImmutableSet.of());
140         assertEquals(context, cxH.getContext());
141         assertEquals(yangModules, cxH.getYangModules());
142     }
143
144     private URL makeMetaInf() throws Exception {
145         final String path = getClass().getResource("/").getPath();
146         final String metaInfPath = path + "tests/META-INF/yang";
147         final Path createDirectories = Files.createDirectories(Paths.get(metaInfPath));
148         assertNotNull(createDirectories);
149         assertEquals(metaInfPath, createDirectories.toString());
150         Runtime.getRuntime().exec("cp " + path + "/test.yang " + metaInfPath + "/");
151         Runtime.getRuntime().exec("cp " + path + "/test2.yang " + metaInfPath + "/");
152         return getClass().getResource("/tests");
153     }
154
155     private void prepareProject(final MavenProject project) throws Exception {
156         URL url = getClass().getResource("/tests");
157         if (url == null) {
158             url = makeMetaInf();
159         }
160         assertNotNull(url);
161         final File testFile = new File(getClass().getResource("/tests").toURI());
162         File testFile2 = new File(getClass().getResource("/").getPath(), "test.jar");
163         testFile2.createNewFile();
164         testFile2 = new File(getClass().getResource("/test.jar").getFile());
165
166         final Manifest manifest = new Manifest();
167         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
168         final JarOutputStream target = new JarOutputStream(new FileOutputStream(testFile2), manifest);
169         addSourceFileToTargetJar(new File(getClass().getResource("/tests/META-INF").getPath()), target);
170         target.close();
171
172         final Artifact artifact = mock(Artifact.class);
173         final Artifact artifact2 = mock(Artifact.class);
174
175         final Set<Artifact> artifacts = new HashSet<>();
176         artifacts.add(artifact);
177         artifacts.add(artifact2);
178
179         when(project.getArtifacts()).thenReturn(artifacts);
180         when(artifact.getFile()).thenReturn(testFile);
181         when(artifact2.getFile()).thenReturn(testFile2);
182     }
183
184     private void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
185         BufferedInputStream in = null;
186         try {
187             if (source.isDirectory()) {
188                 String name = source.getPath().replace("\\", "/");
189                 if (!name.isEmpty()) {
190                     if (!name.endsWith("/")) {
191                         name += "/";
192                     }
193                     final JarEntry entry = new JarEntry(name);
194                     entry.setTime(source.lastModified());
195                     target.putNextEntry(entry);
196                     target.closeEntry();
197                 }
198                 for (final File nestedFile : source.listFiles()) {
199                     addSourceFileToTargetJar(nestedFile, target);
200                 }
201                 return;
202             }
203
204             final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
205             entry.setTime(source.lastModified());
206             target.putNextEntry(entry);
207             in = new BufferedInputStream(new FileInputStream(source));
208
209             final byte[] buffer = new byte[1024];
210             while (true) {
211                 final int count = in.read(buffer);
212                 if (count == -1) {
213                     break;
214                 }
215                 target.write(buffer, 0, count);
216             }
217             target.closeEntry();
218         } finally {
219             if (in != null) {
220                 in.close();
221             }
222         }
223     }
224 }