Bug 3899: Milestone: Increase test coverage for Yangtools 57/34657/4
authorJakub Toth <jatoth@cisco.com>
Mon, 15 Feb 2016 07:49:17 +0000 (08:49 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 17 Feb 2016 14:31:41 +0000 (14:31 +0000)
YangToSourcesMojoTest

Change-Id: I3e22b99eec2e8ec974241ea9bcc0a800045529a2
Signed-off-by: Jakub Toth <jatoth@cisco.com>
yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java [new file with mode: 0644]

diff --git a/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java b/yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java
new file mode 100644 (file)
index 0000000..9e31f25
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang2sources.plugin;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.maven.model.Build;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.project.MavenProject;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
+import org.opendaylight.yangtools.yang2sources.plugin.GenerateSourcesTest.GeneratorMock;
+import org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.YangProvider;
+
+@RunWith(MockitoJUnitRunner.class)
+public class YangToSourcesMojoTest {
+
+    private YangToSourcesMojo mojo;
+
+    @Mock
+    private MavenProject project;
+
+    @Mock
+    private Plugin plugin;
+
+    private YangToSourcesProcessor proc;
+
+    @Test
+    public void yangToSourceMojoTest() throws Exception{
+        Mockito.when(this.project.getPlugin(YangToSourcesMojo.PLUGIN_NAME)).thenReturn(this.plugin);
+
+        this.mojo = new YangToSourcesMojo();
+        this.mojo.setProject(this.project);
+        this.mojo.execute();
+        Assert.assertNotNull(this.mojo);
+
+        final YangToSourcesProcessor processor = Mockito.mock(YangToSourcesProcessor.class);
+        this.mojo = new YangToSourcesMojo(processor);
+        this.mojo.setProject(this.project);
+        this.mojo.execute();
+        Mockito.verify(processor).execute();
+    }
+
+    @Test
+    public void test() throws Exception {
+        prepareProcessor();
+        Assert.assertNotNull(this.proc);
+        this.mojo = new YangToSourcesMojo(this.proc);
+        this.mojo.setProject(this.project);
+        this.mojo.execute();
+        Assert.assertNotNull(this.mojo);
+    }
+
+    private void prepareProcessor() {
+        final File file = new File(getClass().getResource("/yang").getFile());
+        final File excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
+        final String path = file.getPath();
+        final File[] yangFilesRootDir = { excludedYang };
+        final List<CodeGeneratorArg> codeGenerators = new ArrayList<>();
+        final CodeGeneratorArg codeGeneratorArg = new CodeGeneratorArg(GeneratorMock.class.getName(), path);
+        codeGenerators.add(codeGeneratorArg);
+        final MavenProject mvnProject = Mockito.mock(MavenProject.class);
+        final Build build = new Build();
+        Mockito.when(mvnProject.getBuild()).thenReturn(build);
+        final boolean dependencies = true;
+        this.proc = new YangToSourcesProcessor(file, yangFilesRootDir, codeGenerators,
+                mvnProject, dependencies, new YangProvider());
+    }
+}