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