Fix for Bug 114.
[yangtools.git] / yang / yang-maven-plugin / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / GenerateSourcesTest.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.hamcrest.core.Is.is;
11 import static org.junit.Assert.*;
12 import static org.junit.matchers.JUnitMatchers.containsString;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.*;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.maven.model.Plugin;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugin.logging.Log;
26 import org.apache.maven.project.MavenProject;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
34 import org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.YangProvider;
35 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
36
37 import com.google.common.collect.Lists;
38
39 public class GenerateSourcesTest {
40
41     private String yang;
42     private YangToSourcesMojo mojo;
43     private File outDir;
44     @Mock
45     private MavenProject project;
46     @Mock
47     private Plugin plugin;
48
49     @Before
50     public void setUp() throws Exception {
51         MockitoAnnotations.initMocks(this);
52
53         yang = new File(getClass().getResource("/yang/mock.yang").toURI()).getParent();
54         outDir = new File("/outputDir");
55         YangProvider mock = mock(YangProvider.class);
56         doNothing().when(mock).addYangsToMetaInf(any(Log.class), any(MavenProject.class), any(File.class),
57                 any(File[].class));
58
59         YangToSourcesProcessor processor = new YangToSourcesProcessor(mock(Log.class), new File(yang), new File[] {},
60                 Lists.newArrayList(new CodeGeneratorArg(GeneratorMock.class.getName(), "outputDir")), project, false,
61                 mock);
62         mojo = new YangToSourcesMojo(processor);
63         doReturn(new File("")).when(project).getBasedir();
64         doReturn(Collections.emptyList()).when(plugin).getDependencies();
65         doReturn(plugin).when(project).getPlugin(YangToSourcesMojo.PLUGIN_NAME);
66         mojo.setProject(project);
67     }
68
69     @Test
70     public void test() throws Exception {
71         mojo.execute();
72         assertThat(GeneratorMock.called, is(1));
73         assertThat(GeneratorMock.outputDir, is(outDir));
74         assertThat(GeneratorMock.project, is(project));
75         assertNotNull(GeneratorMock.log);
76         assertTrue(GeneratorMock.additionalCfg.isEmpty());
77         assertThat(GeneratorMock.resourceBaseDir.toString(), containsString("target" + File.separator
78                 + "generated-resources"));
79     }
80
81     public static class GeneratorMock implements CodeGenerator {
82
83         private static int called = 0;
84         private static File outputDir;
85         private static Log log;
86         private static Map<String, String> additionalCfg;
87         private static File resourceBaseDir;
88         private static MavenProject project;
89
90         @Override
91         public Collection<File> generateSources(SchemaContext context, File outputBaseDir, Set<Module> currentModules)
92                 throws IOException {
93             called++;
94             outputDir = outputBaseDir;
95             return Lists.newArrayList();
96         }
97
98         @Override
99         public void setLog(Log log) {
100             GeneratorMock.log = log;
101         }
102
103         @Override
104         public void setAdditionalConfig(Map<String, String> additionalConfiguration) {
105             GeneratorMock.additionalCfg = additionalConfiguration;
106         }
107
108         @Override
109         public void setResourceBaseDir(File resourceBaseDir) {
110             GeneratorMock.resourceBaseDir = resourceBaseDir;
111
112         }
113
114         @Override
115         public void setMavenProject(MavenProject project) {
116             GeneratorMock.project = project;
117         }
118     }
119
120 }