-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create migrate command to update the golden masters
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/main/java/de/retest/recheck/cli/subcommands/Migrate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |