/* * Copyright (c) 2013 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.it; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import com.google.common.base.Joiner; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.Arrays; import java.util.Optional; import java.util.Properties; import org.apache.maven.it.VerificationException; import org.apache.maven.it.Verifier; import org.junit.BeforeClass; import org.junit.Test; public class YangToSourcesPluginTestIT { // TODO Test yang files in transitive dependencies @Test public void testYangRootNotExist() throws Exception { Verifier v = setUp("test-parent/YangRootNotExist/", false); v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory"); } @Test public void testCorrect() throws Exception { Verifier v = setUp("test-parent/Correct/", false); verifyCorrectLog(v); } @Test public void testAdditionalConfiguration() throws Exception { Verifier v = setUp("test-parent/AdditionalConfig/", false); v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}"); v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}"); v.verifyTextInLog(File.separator + "files marked as resources: META-INF/yang"); v.verifyTextInLog( Joiner.on(File.separator).join( Arrays.asList("target", "generated-sources", "spi")) + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl"); } @Test public void testMissingYangInDep() throws Exception { try { setUp("test-parent/MissingYangInDep/", false); } catch (VerificationException e) { assertVerificationException( e, "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " + "[unknownDep] was not found."); return; } fail("Verification exception should have been thrown"); } static void verifyCorrectLog(final Verifier v) throws VerificationException { v.verifyErrorFreeLog(); v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from"); v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl"); v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null"); } @Test public void testNoGenerators() throws Exception { Verifier v = setUp("test-parent/NoGenerators/", false); v.verifyErrorFreeLog(); v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided"); } @Test public void testInvalidVersion() throws Exception { Verifier v = setUp("test-parent/InvalidVersion/", false); v.verifyErrorFreeLog(); v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:"); } @Test public void testUnknownGenerator() throws Exception { Verifier v = setUp("test-parent/UnknownGenerator/", true); v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator"); v.verifyTextInLog("java.lang.ClassNotFoundException: unknown"); v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl"); v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null"); v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}"); } @Test public void testNoYangFiles() throws Exception { Verifier v = setUp("test-parent/NoYangFiles/", false); v.verifyTextInLog("[INFO] yang-to-sources: No input files found"); } static void assertVerificationException(final VerificationException e, final String string) { assertThat(e.getMessage(), containsString(string)); } static Verifier setUp(final String project, final boolean ignoreF) throws VerificationException, URISyntaxException, IOException { final URL path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml"); File parent = new File(path.toURI()); Verifier verifier = new Verifier(parent.getParent()); if (ignoreF) { verifier.addCliOption("-fn"); } final Optional<String> maybeSettings = getEffectiveSettingsXML(); if (maybeSettings.isPresent()) { verifier.addCliOption("-gs"); verifier.addCliOption(maybeSettings.get()); } verifier.setMavenDebug(true); verifier.executeGoal("generate-sources"); return verifier; } @Test public void testNoOutputDir() throws Exception { Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false); verifyCorrectLog(v); } @Test public void testFindResourceOnCp() throws Exception { Verifier v1 = setUp("test-parent/GenerateTest1/", false); v1.executeGoal("clean"); v1.executeGoal("package"); String buildDir = getMavenBuildDirectory(v1); v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang"); v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang"); v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang"); Verifier v2 = setUp("test-parent/GenerateTest2/", false); v2.executeGoal("clean"); v2.executeGoal("package"); buildDir = getMavenBuildDirectory(v2); v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang"); v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang"); v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang"); v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang"); } private static String getMavenBuildDirectory(Verifier verifier) throws IOException { Properties sp = new Properties(); try (InputStream is = new FileInputStream(verifier.getBasedir() + "/it-project.properties")) { sp.load(is); } return sp.getProperty("target.dir"); } private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException, IOException { final URL path = YangToSourcesPluginTestIT.class.getResource("/test-parent/pom.xml"); File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile(); File effectiveSettingsXML = new File(buildDir, "effective-settings.xml"); if (effectiveSettingsXML.exists()) { return Optional.of(effectiveSettingsXML.getAbsolutePath()); } else { fail(effectiveSettingsXML.getAbsolutePath()); return Optional.empty(); } } }