Clean up YangToSourcesMojo 20/104620/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 13:05:29 +0000 (14:05 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 13:05:29 +0000 (14:05 +0100)
We have a weird combination of injected and instantiated processors
here, which really serves only for a very bad UT. Since we are testing
YangToSourcesMojo through integration tests, we can forgo all of this.

This leads to a more straightforward execution, as now the Mojo acts
only as a thin interface towards maven. All the logic is contained in
Processor.

Change-Id: Ia64e69ed6a44e3052f43dcc978e066b080928e73
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojo.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/AbstractCodeGeneratorTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java [deleted file]

index 3a6c88e07ca2a3137b9f1164fa6f96b480432dcb..4700e942cf3e97cda12ba942b4bcf8b9d72fd7da 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -81,10 +80,7 @@ public final class YangToSourcesMojo extends AbstractMojo {
     private boolean inspectDependencies;
 
     @Component
-    @VisibleForTesting
-    BuildContext buildContext;
-
-    private YangToSourcesProcessor yangToSourcesProcessor;
+    private BuildContext buildContext;
 
     @Component
     private RepositorySystem repoSystem;
@@ -107,15 +103,6 @@ public final class YangToSourcesMojo extends AbstractMojo {
 
     }
 
-    @VisibleForTesting
-    YangToSourcesMojo(final YangToSourcesProcessor processor) {
-        yangToSourcesProcessor = processor;
-    }
-
-    public void setProject(final MavenProject project) {
-        this.project = project;
-    }
-
     @Override
     @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
     public void execute() throws MojoExecutionException, MojoFailureException {
@@ -126,15 +113,12 @@ public final class YangToSourcesMojo extends AbstractMojo {
         }
 
         Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
-        if (yangToSourcesProcessor == null) {
-            // defaults to ${basedir}/src/main/yang
-            File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
-            Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
+        // defaults to ${basedir}/src/main/yang
+        File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
+        Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
 
-            yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
-                excludedFiles, arrayToList(fileGenerators), project, inspectDependencies);
-        }
-        yangToSourcesProcessor.execute();
+        new YangToSourcesProcessor(buildContext, yangFilesRootFile, excludedFiles, arrayToList(fileGenerators), project,
+            inspectDependencies).execute();
     }
 
     private static <T> List<T> arrayToList(final T[] array) {
index b93eee5ab24281adda8e008c0a8ac7c0cd0007cb..0ae253f0b6d30837c46d430417a06745ddfe2404 100644 (file)
@@ -23,6 +23,7 @@ import java.util.ServiceLoader;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
+import org.junit.Before;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockedStatic;
@@ -42,28 +43,22 @@ public abstract class AbstractCodeGeneratorTest {
     @Mock
     private Plugin plugin;
 
-    final YangToSourcesMojo setupMojo(final YangToSourcesProcessor processor) {
+    @Before
+    public void before() {
         doReturn("target/").when(build).getDirectory();
-//        doReturn(new File("")).when(project).getBasedir();
         doReturn(build).when(project).getBuild();
 
-        doReturn(List.of()).when(plugin).getDependencies();
-        doReturn(plugin).when(project).getPlugin(YangToSourcesMojo.PLUGIN_NAME);
-
         try {
             lenient().when(yangProvider.addYangsToMetaInf(any(MavenProject.class), anyCollection()))
                     .thenReturn(List.of());
         } catch (IOException e) {
             throw new AssertionError(e);
         }
-
-        final YangToSourcesMojo mojo = new YangToSourcesMojo(processor);
-        mojo.setProject(project);
-        return mojo;
     }
 
     @SuppressWarnings({ "rawtypes", "checkstyle:illegalCatch" })
-    final void assertMojoExecution(final YangToSourcesProcessor processor, final Prepare prepare, final Verify verify) {
+    static final void assertMojoExecution(final YangToSourcesProcessor processor, final Prepare prepare,
+            final Verify verify) {
         try (MockedStatic<ServiceLoader> staticLoader = mockStatic(ServiceLoader.class)) {
             final FileGenerator generator = mock(FileGenerator.class);
             doCallRealMethod().when(generator).importResolutionMode();
@@ -81,10 +76,9 @@ public abstract class AbstractCodeGeneratorTest {
             doReturn(Iterators.singletonIterator(factory)).when(loader).iterator();
             staticLoader.when(() -> ServiceLoader.load(FileGeneratorFactory.class)).thenReturn(loader);
 
-            final YangToSourcesMojo mojo = setupMojo(processor);
             try {
                 prepare.prepare(generator);
-                mojo.execute();
+                processor.execute();
                 verify.verify(generator);
             } catch (Exception e) {
                 throw new AssertionError(e);
diff --git a/plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java b/plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java
deleted file mode 100644 (file)
index b15224c..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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 static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-
-import org.apache.maven.model.Plugin;
-import org.apache.maven.project.MavenProject;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.sonatype.plexus.build.incremental.DefaultBuildContext;
-
-@RunWith(MockitoJUnitRunner.class)
-public class YangToSourcesMojoTest {
-    @Mock
-    private MavenProject project;
-    @Mock
-    private Plugin plugin;
-
-    @Test
-    public void yangToSourceMojoTest() throws Exception {
-        doReturn(plugin).when(project).getPlugin(YangToSourcesMojo.PLUGIN_NAME);
-
-        YangToSourcesMojo mojo = new YangToSourcesMojo();
-        mojo.setProject(project);
-        mojo.buildContext = new DefaultBuildContext();
-        mojo.execute();
-        assertNotNull(mojo);
-
-        final YangToSourcesProcessor processor = Mockito.mock(YangToSourcesProcessor.class);
-        mojo = new YangToSourcesMojo(processor);
-        mojo.setProject(project);
-        mojo.execute();
-        verify(processor).execute();
-    }
-}