Skip to content

Commit

Permalink
Create migrate command to update the golden masters
Browse files Browse the repository at this point in the history
  • Loading branch information
ivonx authored and rebazer committed Aug 12, 2019
1 parent af66b0f commit 9438529
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/main/java/de/retest/recheck/cli/subcommands/Migrate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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.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 all Golden Masters." )
public class Migrate implements Runnable {

private static final String RECHECK_FOLDER = "\\src\\test\\resources\\retest\\recheck";
private static final String GOLDEN_MASTER_ABSOLUTE_PATH =
ProjectRootFinderUtil.getProjectRoot().get() + RECHECK_FOLDER;
private static final String GOLDEN_MASTER_EXTENTION = "retest.xml";
private static final Logger logger = LoggerFactory.getLogger( Migrate.class );

@Option( names = "--help", usageHelp = true, hidden = true )
private boolean displayHelp;

@Parameters( arity = "1", defaultValue = "", description = "Migrate Golden Master(s) from a specific path." )
private String goldenMasterPath;

private final GoldenMasterProviderImpl goldenMasterProvider =
new GoldenMasterProviderImpl( createSutStatePersistence() );

@Override
public void run() {
try {
migrateAllGoldenMasters( findAndConvertTheGoldenMasterPath() );
} catch ( final IOException e ) {
logger.error( "An error occurred while loading the test report.", e );
}
}

private void migrateAllGoldenMasters( final List<String> goldenMastersPaths ) {
if ( goldenMastersPaths.isEmpty() ) {
logger.warn( "There is no Golden Masters in this directory." );
}
goldenMastersPaths.stream().forEach( path -> {
File file;
try {
file = goldenMasterProvider.getGoldenMaster( path );
final SutState sutState = goldenMasterProvider.loadGoldenMaster( file );
goldenMasterProvider.saveGoldenMaster( file, sutState );
} catch ( final NoGoldenMasterFoundException e ) {
logger.error( "No Golden Master is found." );
}
} );
}

private List<String> findAndConvertTheGoldenMasterPath() throws IOException {
final Path goldenMasterRootPath = !goldenMasterPath.equals( "" ) ? Paths.get( goldenMasterPath )
: Paths.get( GOLDEN_MASTER_ABSOLUTE_PATH );
try ( Stream<Path> goldenMasterPaths = Files.walk( goldenMasterRootPath ) ) {
return goldenMasterPaths.filter( goldenMaster -> goldenMaster.endsWith( GOLDEN_MASTER_EXTENTION ) )
.map( goldenMaster -> goldenMaster.toString()
.replace( ProjectRootFinderUtil.getProjectRoot().get().toString() + "\\", "" )
.replace( "\\retest.xml", "" ) )
.collect( Collectors.toList() );
}
}

private static Persistence<SutState> createSutStatePersistence() {
return new PersistenceFactory( new HashSet<>( Arrays.asList( StdXmlClassesProvider.getXmlDataClasses() ) ) )
.getPersistence();
}

boolean isDisplayHelp() {
return displayHelp;
}

}

0 comments on commit 9438529

Please sign in to comment.