Enforce checkstyle in maven-plugin
[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.runners.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
24 import org.opendaylight.yangtools.yang2sources.plugin.GenerateSourcesTest.GeneratorMock;
25
26 @RunWith(MockitoJUnitRunner.class)
27 public class YangToSourcesMojoTest {
28
29     private YangToSourcesMojo mojo;
30
31     @Mock
32     private MavenProject project;
33
34     @Mock
35     private Plugin plugin;
36
37     private YangToSourcesProcessor proc;
38
39     @Test
40     public void yangToSourceMojoTest() throws Exception {
41         Mockito.when(this.project.getPlugin(YangToSourcesMojo.PLUGIN_NAME)).thenReturn(this.plugin);
42
43         this.mojo = new YangToSourcesMojo();
44         this.mojo.setProject(this.project);
45         this.mojo.execute();
46         Assert.assertNotNull(this.mojo);
47
48         final YangToSourcesProcessor processor = Mockito.mock(YangToSourcesProcessor.class);
49         this.mojo = new YangToSourcesMojo(processor);
50         this.mojo.setProject(this.project);
51         this.mojo.execute();
52         Mockito.verify(processor).conditionalExecute(false);
53     }
54
55     @Test
56     public void test() throws Exception {
57         prepareProcessor();
58         Assert.assertNotNull(this.proc);
59         this.mojo = new YangToSourcesMojo(this.proc);
60         this.mojo.setProject(this.project);
61         this.mojo.execute();
62         Assert.assertNotNull(this.mojo);
63     }
64
65     private void prepareProcessor() {
66         final File file = new File(getClass().getResource("/yang").getFile());
67         final File excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
68         final String path = file.getPath();
69         final List<CodeGeneratorArg> codeGenerators = new ArrayList<>();
70         final CodeGeneratorArg codeGeneratorArg = new CodeGeneratorArg(GeneratorMock.class.getName(),
71                 "target/YangToSourcesMojoTest-outputBaseDir");
72         codeGenerators.add(codeGeneratorArg);
73         final MavenProject mvnProject = Mockito.mock(MavenProject.class);
74         final Build build = new Build();
75         Mockito.when(mvnProject.getBuild()).thenReturn(build);
76         final boolean dependencies = true;
77         this.proc = new YangToSourcesProcessor(file, ImmutableList.of(excludedYang), codeGenerators,
78                 mvnProject, dependencies, new YangProvider());
79     }
80 }