Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-maven-plugin / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesMojoTest.java
1 /*
2  * Copyright (c) 2016 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 com.google.common.collect.ImmutableList;
11 import java.io.File;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.apache.maven.model.Build;
15 import org.apache.maven.model.Plugin;
16 import org.apache.maven.project.MavenProject;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
24 import org.opendaylight.yangtools.yang2sources.plugin.GenerateSourcesTest.GeneratorMock;
25 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
26
27 @RunWith(MockitoJUnitRunner.class)
28 public class YangToSourcesMojoTest {
29
30     private YangToSourcesMojo mojo;
31
32     @Mock
33     private MavenProject project;
34
35     @Mock
36     private Plugin plugin;
37
38     private YangToSourcesProcessor proc;
39
40     @Test
41     public void yangToSourceMojoTest() throws Exception {
42         Mockito.when(this.project.getPlugin(YangToSourcesMojo.PLUGIN_NAME)).thenReturn(this.plugin);
43
44         this.mojo = new YangToSourcesMojo();
45         this.mojo.setProject(this.project);
46         this.mojo.buildContext = new DefaultBuildContext();
47         this.mojo.execute();
48         Assert.assertNotNull(this.mojo);
49
50         final YangToSourcesProcessor processor = Mockito.mock(YangToSourcesProcessor.class);
51         this.mojo = new YangToSourcesMojo(processor);
52         this.mojo.setProject(this.project);
53         this.mojo.execute();
54         Mockito.verify(processor).conditionalExecute(false);
55     }
56
57     @Test
58     public void test() throws Exception {
59         prepareProcessor();
60         Assert.assertNotNull(this.proc);
61         this.mojo = new YangToSourcesMojo(this.proc);
62         this.mojo.setProject(this.project);
63         this.mojo.execute();
64         Assert.assertNotNull(this.mojo);
65     }
66
67     private void prepareProcessor() {
68         final File file = new File(getClass().getResource("/yang").getFile());
69         final File excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
70         final String path = file.getPath();
71         final List<CodeGeneratorArg> codeGenerators = new ArrayList<>();
72         final CodeGeneratorArg codeGeneratorArg = new CodeGeneratorArg(GeneratorMock.class.getName(),
73                 "target/YangToSourcesMojoTest-outputBaseDir");
74         codeGenerators.add(codeGeneratorArg);
75         final MavenProject mvnProject = Mockito.mock(MavenProject.class);
76         final Build build = new Build();
77         Mockito.when(mvnProject.getBuild()).thenReturn(build);
78         final boolean dependencies = true;
79         this.proc = new YangToSourcesProcessor(file, ImmutableList.of(excludedYang), codeGenerators,
80                 mvnProject, dependencies, YangProvider.getInstance());
81     }
82 }