Merge "Package names for enclosed Types of TOs were changed to fully qualified. Fully...
[controller.git] / opendaylight / sal / yang-prototype / yang / maven-yang-plugin / src / test / java / org / opendaylight / controller / 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.controller.yang2sources.plugin;
9
10 import static org.hamcrest.core.Is.*;
11 import static org.junit.Assert.*;
12 import static org.junit.matchers.JUnitMatchers.*;
13 import static org.mockito.Matchers.*;
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.Map;
20 import java.util.Set;
21
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.project.MavenProject;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.yang.model.api.Module;
30 import org.opendaylight.controller.yang.model.api.SchemaContext;
31 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
32 import org.opendaylight.controller.yang2sources.plugin.YangToSourcesProcessor.YangProvider;
33 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
34
35 import com.google.common.collect.Lists;
36
37 public class GenerateSourcesTest {
38
39     private String yang;
40     private YangToSourcesMojo mojo;
41     private File outDir;
42     @Mock
43     private MavenProject project;
44
45     @Before
46     public void setUp() throws MojoFailureException {
47         MockitoAnnotations.initMocks(this);
48
49         yang = new File(getClass().getResource("/yang/mock.yang").getFile())
50                 .getParent();
51         outDir = new File("/outputDir");
52         YangProvider mock = mock(YangProvider.class);
53         doNothing().when(mock).addYangsToMETA_INF(any(Log.class),
54                 any(MavenProject.class), any(File.class));
55
56         YangToSourcesProcessor processor = new YangToSourcesProcessor(
57                 mock(Log.class), new File(yang),
58                 Lists.newArrayList(new CodeGeneratorArg(GeneratorMock.class
59                         .getName(), "outputDir")), project, false,
60                 mock);
61         mojo = new YangToSourcesMojo(processor);
62         doReturn(new File("")).when(project).getBasedir();
63         mojo.project = project;
64     }
65
66     @Test
67     public void test() throws Exception {
68         mojo.execute();
69         assertThat(GeneratorMock.called, is(1));
70         assertThat(GeneratorMock.outputDir, is(outDir));
71         assertThat(GeneratorMock.project, is(project));
72         assertNotNull(GeneratorMock.log);
73         assertTrue(GeneratorMock.additionalCfg.isEmpty());
74         assertThat(GeneratorMock.resourceBaseDir.toString(),
75                 containsString("target" + File.separator
76                         + "generated-resources"));
77     }
78
79     public static class GeneratorMock implements CodeGenerator {
80
81         private static int called = 0;
82         private static File outputDir;
83         private static Log log;
84         private static Map<String, String> additionalCfg;
85         private static File resourceBaseDir;
86         private static MavenProject project;
87
88         @Override
89         public Collection<File> generateSources(SchemaContext context,
90                 File outputBaseDir, Set<Module> currentModules)
91                 throws IOException {
92             called++;
93             outputDir = outputBaseDir;
94             return Lists.newArrayList();
95         }
96
97         @Override
98         public void setLog(Log log) {
99             GeneratorMock.log = log;
100         }
101
102         @Override
103         public void setAdditionalConfig(
104                 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 }