diff --git a/src/main/java/de/retest/recheck/cli/subcommands/Migrate.java b/src/main/java/de/retest/recheck/cli/subcommands/Migrate.java new file mode 100644 index 00000000..3717610d --- /dev/null +++ b/src/main/java/de/retest/recheck/cli/subcommands/Migrate.java @@ -0,0 +1,86 @@ +package de.retest.recheck.cli.subcommands; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.retest.recheck.Properties; +import de.retest.recheck.configuration.ProjectRootFinderUtil; +import de.retest.recheck.persistence.GoldenMasterProviderImpl; +import de.retest.recheck.persistence.NoGoldenMasterFoundException; +import de.retest.recheck.persistence.Persistence; +import de.retest.recheck.persistence.PersistenceFactory; +import de.retest.recheck.persistence.xml.util.StdXmlClassesProvider; +import de.retest.recheck.ui.descriptors.SutState; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; + +@Command( name = "migrate", description = "Migrate Golden Master(s)." ) +public class Migrate implements Runnable { + + private static final String RECHECK_FOLDER = "/src/test/resources/retest/recheck"; + private static final Logger logger = LoggerFactory.getLogger( Migrate.class ); + + @Option( names = "--help", usageHelp = true, hidden = true ) + private boolean displayHelp; + + @Parameters( arity = "1", defaultValue = "", description = "The path of the Golden Master(s)." ) + private String goldenMasterPath; + + private final GoldenMasterProviderImpl goldenMasterProvider = + new GoldenMasterProviderImpl( createSutStatePersistence() ); + + @Override + public void run() { + try { + migrateAllGoldenMasters( getAllGoldenMasters() ); + } catch ( final IOException e ) { + logger.error( "The directory '{}' does not exist.", e.getMessage() ); + } catch ( final NoGoldenMasterFoundException e ) { + logger.error( "There is no Golden Master(s) in the directory '{}'.", e.getFilename() ); + } + } + + private void migrateAllGoldenMasters( final List goldenMastersPaths ) throws NoGoldenMasterFoundException { + if ( goldenMastersPaths.isEmpty() ) { + throw new NoGoldenMasterFoundException( goldenMasterPath ); + } + goldenMastersPaths.stream().forEach( path -> { + final File file = new File( path ); + final SutState sutState = goldenMasterProvider.loadGoldenMaster( file ); + goldenMasterProvider.saveGoldenMaster( file, sutState ); + } ); + } + + private List getAllGoldenMasters() throws IOException { + final String path = !goldenMasterPath.equals( "" ) ? goldenMasterPath : RECHECK_FOLDER; + final File gmPath = + ProjectRootFinderUtil.getProjectRoot().map( gm -> Paths.get( gm.toAbsolutePath().toString(), path ) ) + .map( Path::toFile ).orElseThrow( () -> new IOException() ); + try ( final Stream goldenMasterPaths = Files.walk( gmPath.toPath() ) ) { + return goldenMasterPaths.filter( goldenMaster -> goldenMaster.endsWith( Properties.DEFAULT_XML_FILE_NAME ) ) + .map( goldenMaster -> goldenMaster.getParent().toString() ).collect( Collectors.toList() ); + } + } + + private static Persistence createSutStatePersistence() { + return new PersistenceFactory( new HashSet<>( Arrays.asList( StdXmlClassesProvider.getXmlDataClasses() ) ) ) + .getPersistence(); + } + + boolean isDisplayHelp() { + return displayHelp; + } + +}