-
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
86 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
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,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; | ||
} | ||
|
||
} |