Merge branch 'master' of ../controller
[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.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.collect.ImmutableSet;
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
26 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
27 import org.apache.maven.model.Dependency;
28 import org.apache.maven.model.Plugin;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.repository.RepositorySystem;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class UtilTest {
40
41     @Test
42     public void getClassPathTest() {
43         final MavenProject project = mock(MavenProject.class);
44         final File file = mock(File.class);
45         final File file2 = mock(File.class);
46         final Artifact artifact = mock(Artifact.class);
47         final Artifact artifact2 = mock(Artifact.class);
48
49         final Set<Artifact> artifacts = new HashSet<>();
50         artifacts.add(artifact);
51         artifacts.add(artifact2);
52
53         when(project.getArtifacts()).thenReturn(artifacts);
54         when(artifact.getFile()).thenReturn(file);
55         when(file.isFile()).thenReturn(true);
56         when(file.getName()).thenReturn("iamjar.jar");
57         when(artifact2.getFile()).thenReturn(file2);
58         when(file2.isDirectory()).thenReturn(true);
59
60         final List<File> files = Util.getClassPath(project);
61         assertEquals(2, files.size());
62         assertTrue(files.contains(file) && files.contains(file2));
63     }
64
65     @Test
66     public void checkClasspathTest() throws Exception {
67         final MavenProject project = mock(MavenProject.class);
68         final Plugin plugin = mock(Plugin.class);
69         final RepositorySystem repoSystem = mock(RepositorySystem.class);
70         final ArtifactRepository localRepo = mock(ArtifactRepository.class);
71         final ArtifactResolutionResult artifactResolResult = mock(ArtifactResolutionResult.class);
72         final Artifact artifact = mock(Artifact.class);
73         final Dependency dep = mock(Dependency.class);
74
75         final List<ArtifactRepository> remoteRepos = new ArrayList<>();
76         remoteRepos.add(localRepo);
77
78         final Set<Artifact> artifacts = new HashSet<>();
79         artifacts.add(artifact);
80
81         final List<Dependency> listDepcy = new ArrayList<>();
82         listDepcy.add(dep);
83
84         when(project.getPlugin(anyString())).thenReturn(plugin);
85         when(plugin.getDependencies()).thenReturn(listDepcy);
86         when(artifact.getArtifactId()).thenReturn("artifactId");
87         when(artifact.getGroupId()).thenReturn("groupId");
88         when(artifact.getVersion()).thenReturn("SNAPSHOT");
89         when(repoSystem.createDependencyArtifact(dep)).thenReturn(artifact);
90         when(repoSystem.resolve(any(ArtifactResolutionRequest.class))).thenReturn(artifactResolResult);
91         when(artifactResolResult.getArtifacts()).thenReturn(artifacts);
92         when(project.getDependencyArtifacts()).thenReturn(artifacts);
93
94         Util.checkClasspath(project, repoSystem, localRepo, remoteRepos);
95         assertEquals(1, artifacts.size());
96         assertEquals(1, remoteRepos.size());
97         assertEquals(1, listDepcy.size());
98     }
99
100     @Test
101     public void contextHolderTest() throws Exception {
102         final EffectiveModelContext context = YangParserTestUtils.parseYangResources(getClass(), "/test.yang",
103             "/test2.yang");
104         final Set<Module> yangModules = new HashSet<>();
105         final ContextHolder cxH = new ContextHolder(context, yangModules, ImmutableSet.of());
106         assertEquals(context, cxH.getContext());
107         assertEquals(yangModules, cxH.getYangModules());
108     }
109 }