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 committed Aug 14, 2019
1 parent 3cd65d9 commit 88298d5
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 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,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<String> 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<String> 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<Path> 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<SutState> createSutStatePersistence() {
return new PersistenceFactory( new HashSet<>( Arrays.asList( StdXmlClassesProvider.getXmlDataClasses() ) ) )
.getPersistence();
}

boolean isDisplayHelp() {
return displayHelp;
}

}

0 comments on commit 88298d5

Please sign in to comment.