Remove yang-maven-plugin interfaces
[yangtools.git] / plugin / yang-maven-plugin / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / AbstractCodeGeneratorTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyCollection;
12 import static org.mockito.ArgumentMatchers.anyMap;
13 import static org.mockito.Mockito.doCallRealMethod;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.lenient;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.mockStatic;
18
19 import com.google.common.collect.Iterators;
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.ServiceLoader;
23 import org.apache.maven.model.Build;
24 import org.apache.maven.model.Plugin;
25 import org.apache.maven.project.MavenProject;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.MockedStatic;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
31 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
32 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
33
34 @RunWith(MockitoJUnitRunner.Silent.class)
35 public abstract class AbstractCodeGeneratorTest {
36     @Mock
37     MavenProject project;
38     @Mock
39     YangProvider yangProvider;
40     @Mock
41     private Build build;
42     @Mock
43     private Plugin plugin;
44
45     final YangToSourcesMojo setupMojo(final YangToSourcesProcessor processor) {
46         doReturn("target/").when(build).getDirectory();
47 //        doReturn(new File("")).when(project).getBasedir();
48         doReturn(build).when(project).getBuild();
49
50         doReturn(List.of()).when(plugin).getDependencies();
51         doReturn(plugin).when(project).getPlugin(YangToSourcesMojo.PLUGIN_NAME);
52
53         try {
54             lenient().doNothing().when(yangProvider).addYangsToMetaInf(any(MavenProject.class), anyCollection());
55         } catch (IOException e) {
56             throw new AssertionError(e);
57         }
58
59         final YangToSourcesMojo mojo = new YangToSourcesMojo(processor);
60         mojo.setProject(project);
61         return mojo;
62     }
63
64     @SuppressWarnings({ "rawtypes", "checkstyle:illegalCatch" })
65     final void assertMojoExecution(final YangToSourcesProcessor processor, final Prepare prepare, final Verify verify) {
66         try (MockedStatic<ServiceLoader> staticLoader = mockStatic(ServiceLoader.class)) {
67             final FileGenerator generator = mock(FileGenerator.class);
68             doCallRealMethod().when(generator).importResolutionMode();
69
70             final FileGeneratorFactory factory = mock(FileGeneratorFactory.class);
71             doReturn("mockGenerator").when(factory).getIdentifier();
72
73             try {
74                 doReturn(generator).when(factory).newFileGenerator(anyMap());
75             } catch (FileGeneratorException e) {
76                 throw new AssertionError(e);
77             }
78
79             final ServiceLoader<?> loader = mock(ServiceLoader.class);
80             doReturn(Iterators.singletonIterator(factory)).when(loader).iterator();
81             staticLoader.when(() -> ServiceLoader.load(FileGeneratorFactory.class)).thenReturn(loader);
82
83             final YangToSourcesMojo mojo = setupMojo(processor);
84             try {
85                 prepare.prepare(generator);
86                 mojo.execute();
87                 verify.verify(generator);
88             } catch (Exception e) {
89                 throw new AssertionError(e);
90             }
91         }
92     }
93
94     @FunctionalInterface
95     interface Prepare {
96         void prepare(FileGenerator mock) throws Exception;
97     }
98
99     @FunctionalInterface
100     interface Verify {
101         void verify(FileGenerator mock) throws Exception;
102     }
103 }