Commit 944bdb50 authored by Maxime Lefrançois's avatar Maxime Lefrançois
Browse files

workflow goes through on saref-core

parent 12c004da
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
 */
package fr.emse.gitlab.saref;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@@ -46,7 +47,7 @@ import fr.emse.gitlab.saref.SAREFPipeline.Mode;
 * @author Maxime Lefrançois, Omar Qawasmeh
 *
 */
public class CLIExecution {
public class CLIExecution implements SAREFCredentialsProvider {

	private static final Logger LOG = LoggerFactory.getLogger(CLIExecution.class);

@@ -67,7 +68,9 @@ public class CLIExecution {
			.addOption(ARG_IGNORE_TERMS, ARG_IGNORE_TERMS_LONG, false, ARG_IGNORE_TERMS_MAN)
			.addOption(ARG_IGNORE_SITE, ARG_IGNORE_SITE_LONG, false, ARG_IGNORE_SITE_MAN);

	public static SAREFPipeline getSAREFPipeline(String[] args) throws RuntimeException {
	private static final Console console = System.console();
	
	public SAREFPipeline getSAREFPipeline(String[] args) throws RuntimeException {
		if (args.length == 0) {
			displayHelp(-1);
		}
@@ -119,14 +122,14 @@ public class CLIExecution {
			ignoreTerms = cl.hasOption(ARG_IGNORE_TERMS);
			ignoreSite = cl.hasOption(ARG_IGNORE_SITE);
		
			return new SAREFPipeline(directory, mode, ignoreExamples, ignoreTerms, ignoreSite);
			return new SAREFPipeline(this, directory, mode, ignoreExamples, ignoreTerms, ignoreSite);
		} catch (IOException | ParseException ex) {
			LOG.debug("Exception while parsing arguments", ex);
			throw new RuntimeException();
		}
	}

	private static void displayHelp(int returnCode) {
	private void displayHelp(int returnCode) {
		try {
			String helpHeader = IOUtils.toString(
					CLIExecution.class.getClassLoader().getResourceAsStream("cli/helpHeader.txt"), StandardCharsets.UTF_8);
@@ -144,4 +147,29 @@ public class CLIExecution {
		}
	}

	@Override
	public String getUsername(String hostName) {
		if (console != null) {
			return console.readLine(SAREF.getMessage(MESSAGE.account_name.toString(), hostName));
		} else if (System.getenv("GITLAB_CI") != null) {
			LOG.info("using username 'gitlab-ci-token'");
			return "gitlab-ci-token";
		} else {
			LOG.warn(SAREF.getMessage(MESSAGE.account_name_empty.toString(), hostName));
			return "";
		}
	}

	@Override
	public char[] getPassword(String hostName) {
		if (console != null) {
			return console.readPassword(SAREF.getMessage(MESSAGE.account_password.toString(), hostName));
		} else if (System.getenv("GITLAB_CI") != null) {
			return System.getenv("CI_JOB_TOKEN").toCharArray();
		} else {
			LOG.warn(SAREF.getMessage(MESSAGE.account_password_empty.toString(), hostName));
			return "".toCharArray();
		}
	}

}
+57 −18
Original line number Diff line number Diff line
@@ -26,11 +26,19 @@
package fr.emse.gitlab.saref;

import java.awt.Desktop;
import java.io.Console;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import javax.swing.SwingUtilities;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.emse.gitlab.saref.gui.MainFrame;

/**
 *
 * @author Maxime Lefrançois
@@ -41,23 +49,54 @@ public class Main {
	private static final Logger LOG = LoggerFactory.getLogger(Main.class);

	public static void main(String[] args) {
		final Console console = System.console();
		boolean hasConsole = console != null;
		boolean supported = Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
		if (args.length == 0 && !GraphicsEnvironment.isHeadless()) {
			mainGUI();
		} else {
			mainCLI(args);
		} 
	}

		if (args != null) {
			final SAREFPipeline pipeline = CLIExecution.getSAREFPipeline(args);
	private static void mainCLI(String[] args) {
		CLIExecution cliExecution = new CLIExecution();
		final SAREFPipeline pipeline = cliExecution.getSAREFPipeline(args);
		try {
			boolean reportGenerated = pipeline.run();
			if (reportGenerated) {
				try {	
				pipeline.run();
					final File reportHTML = new File(pipeline.targetDir, "site/report.html");
					if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
						Desktop.getDesktop().browse(reportHTML.toURI());
					} else {
						System.out.println("\n\n\nURL to the SAREF pipeline report:\n" + reportHTML.getCanonicalPath());
					}
				} catch (IOException ex) {
					throw new SAREFPipelineException(ex);
				}
			}
		} catch (SAREFPipelineException ex) {
			LOG.error("Exception:", ex);
			System.exit(-1);
		}
		} else {
			while(true) {
				final SAREFPipeline pipeline = GUIExecution.getSAREFPipeline();
				pipeline.run();
	}

	private static void mainGUI() {
		try {
			String info = IOUtils.toString(Main.class.getClassLoader().getResourceAsStream("cli/info.txt"),
					StandardCharsets.UTF_8);
			System.out.println(info);
			MainFrame application = new MainFrame();
			SwingUtilities.updateComponentTreeUI(application);
			java.awt.EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					application.toFront();
					application.repaint();
				}
			});
		} catch (Exception ex) {
			LOG.warn("Exception ", ex);
			System.exit(-1);
		}
	}

}
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ public class SAREF {

	public static final String SAREF_PORTAL_STATIC_GIT = "https://gitlab.emse.fr/saref/saref-portal-static.git";
//	public static final String SAREF_PORTAL_STATIC_GIT = "https://forge.etsi.org/rep/SAREF/saref-portal-static.git"; // uncomment when the project is public
	public static final String LOGGER_BASE = "fr.emse.gitlab.saref.entities.tests";
	public static final String LOGGER_BASE = "saref.pipeline";
	
	public static <T> String getMessage(String key, Object... args) {
		String msg = BUNDLE.getString(key);
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 ETSI
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package fr.emse.gitlab.saref;

public interface SAREFCredentialsProvider {
	
	public static enum MESSAGE {
		account_name, account_name_empty,
		account_password, account_password_empty;
	}

	public String getUsername(String host);

	public char[] getPassword(String host);
}
+27 −27
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ import fr.emse.gitlab.saref.tests.TestSuitesAppender;
public class SAREFPipeline {

	private static final Logger LOG = LoggerFactory.getLogger(SAREFPipeline.class);
	private static final String START = SAREF.getMessage("start");

	private static final String NAME_TARGET = "target";
	private static final String NAME_LOG_FILE = "output.log";
@@ -62,6 +61,8 @@ public class SAREFPipeline {

	private final TestSuites testSuites = new TestSuites("SAREF pipeline");
	private final Map<String, Logger> LOGGER_MAP = new HashMap<>();

	public final SAREFCredentialsProvider credentialsProvider;
	public final File directory;
	public final File targetDir;
	public final Mode mode;
@@ -91,7 +92,8 @@ public class SAREFPipeline {
		return ontologyManager;
	}

	public SAREFPipeline(File directory, Mode mode, boolean ignoreExamples, boolean ignoreTerms, boolean ignoreSite) {
	public SAREFPipeline(SAREFCredentialsProvider credentialsProvider, File directory, Mode mode, boolean ignoreExamples, boolean ignoreTerms, boolean ignoreSite) {
		this.credentialsProvider = credentialsProvider;
		this.directory = directory;
		targetDir = new File(directory, NAME_TARGET);
		this.mode = mode;
@@ -100,12 +102,20 @@ public class SAREFPipeline {
		this.ignoreSite = ignoreSite;
	}

	void run() throws SAREFPipelineException {
		LOG.info(START);
		initialize();
	public boolean run() throws SAREFPipelineException {
		LOG.info(SAREF.getMessage("start", directory));
		try {
			logger = getLogger(START);

			if (mode == Mode.CLEAN) {
				FileUtils.forceDelete(targetDir);
				return false;
			}
			FileUtils.forceMkdir(targetDir);
			prepareLogFile();
		} catch (IOException ex) {
			throw new SAREFPipelineException(ex);
		}
		try {
			logger = getLogger(SAREF.getMessage("start", directory));
			datasetManager = new DatasetManager(this, logger);
			datasetManager.emptyDataset();

@@ -126,6 +136,8 @@ public class SAREFPipeline {
			if (testSuites.getErrors() > 0) {
				throw new SAREFPipelineException();
			}

			return writeReport();
		} catch (IOException ex) {
			writeReport();
			throw new SAREFPipelineException(ex);
@@ -135,19 +147,6 @@ public class SAREFPipeline {
		}
	}

	private void initialize() throws SAREFPipelineException {
		try {
			if (mode == Mode.CLEAN) {
				FileUtils.forceDelete(targetDir);
				System.exit(0);
			}
			FileUtils.forceMkdir(targetDir);
			prepareLogFile();
		} catch (IOException ex) {
			throw new SAREFPipelineException(ex);
		}
	}

	/**
	 * Prepare the log file in the target directory repository.
	 */
@@ -174,7 +173,7 @@ public class SAREFPipeline {
		return logger;
	}

	private void writeReport() {
	private boolean writeReport() {
		try {
			File report = new File(targetDir, "report_output.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(TestSuites.class);
@@ -185,12 +184,13 @@ public class SAREFPipeline {
			final StringWriter sw = new StringWriter();
			jaxbMarshaller.marshal(testSuites, sw);
			if (siteManager != null) {
				siteManager.writeReport(sw);
				return siteManager.writeReport(sw);
			}
		} catch (JAXBException | IOException ex) {
			LOG.error("Exception:", ex);
			ex.printStackTrace();
		}
		return false;
	}

}
Loading