package de.retest.suite.flow;


import static de.retest.Properties.WORK_DIRECTORY;
import static de.retest.persistence.RecheckStateFileProviderImpl.RECHECK_PROJECT_ROOT;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.retest.Properties;
import de.retest.configuration.Configuration;
import de.retest.persistence.Persistence;
import de.retest.persistence.PersistenceFactory;
import de.retest.persistence.xml.util.StdXmlClassesProvider;
import de.retest.ui.descriptors.Element;
import de.retest.ui.descriptors.SutState;
import de.retest.ui.diff.AttributeDifference;
import de.retest.ui.review.ReviewResult;
import de.retest.ui.review.SuiteChangeSet;
import de.retest.util.junit.vintage.SystemProperty;

public class ApplyChangesToStatesFlowTest {

	private static final Logger logger = LoggerFactory.getLogger( ApplyChangesToStatesFlowTest.class );

	private static final String SUITE = "ApplyChangesToStatesFlowTest";
	private static final String TEST = "check_GUI_with_review_license";
	private static final String ACTION = "launch";
	private static final String FILE = "target/test-classes/recheck/" + SUITE + "/" + TEST + "." + ACTION + ".recheck";

	@Rule
	public SystemProperty workDirProp = new SystemProperty( WORK_DIRECTORY );

	@Rule
	public SystemProperty recheckDirProp = new SystemProperty( RECHECK_PROJECT_ROOT );

	@Before
	public void setUp() {
		Configuration.resetRetest();
		workDirProp.setValue( "target/test-classes" );
		logger.info( "Workspace now pointing to {}.", Configuration.getRetestWorkspace() );
		recheckDirProp.setValue( Properties.getReTestInstallDir().toString() );
	}

	@Test
	public void apply_should_apply_changes_to_states() throws Exception {
		final PersistenceFactory persistenceFactory =
				new PersistenceFactory( new HashSet<>( Arrays.asList( StdXmlClassesProvider.getXmlDataClasses() ) ) );
		final Persistence<SutState> persistence = persistenceFactory.getPersistence();
		final File file = new File( FILE );
		final SutState before = persistence.load( file.toURI() );
		logger.debug( "Before loaded" );

		final ReviewResult reviewResult = new ReviewResult();
		final SuiteChangeSet acceptedChanges = reviewResult.createSuiteChangeSet( SUITE, "" );
		final Element toChange = before.getRootElements().get( 0 ).getContainedElements().get( 0 )
				.getContainedElements().get( 0 ).getContainedElements().get( 0 ).getContainedElements().get( 0 )
				.getContainedElements().get( 0 );
		acceptedChanges.createTestChangeSet().createActionChangeSet( ACTION, file.getPath() ).getAttributesChanges()
				.add( toChange.getIdentifyingAttributes(), new AttributeDifference( "enabled", "false", "true" ) );
		logger.debug( "AcceptedChanges created." );

		ApplyChangesToStatesFlow.apply( persistence, acceptedChanges );
		final SutState after = persistence.load( file.toURI() );
		final Element changed = after.getRootElements().get( 0 ).getContainedElements().get( 0 ).getContainedElements()
				.get( 0 ).getContainedElements().get( 0 ).getContainedElements().get( 0 ).getContainedElements()
				.get( 0 );

		assertThat( changed.getAttributes().get( "enabled" ) ).isEqualTo( true );

		// even more advanced: check if difference between before and after is acceptedChanges!
	}
}

@@ -28,60 +28,8 @@
 w;%0A%0A
-%0Aimport static de.retest.Properties.WORK_DIRECTORY;%0A
 impo
@@ -239,24 +239,64 @@
 l.HashSet;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import org.j
@@ -369,83 +369,37 @@
 org.
-slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport de.retest.Properties;
+junit.rules.TemporaryFolder;%0A
 %0Aimp
@@ -908,110 +908,8 @@
  %7B%0A%0A
-%09private static final Logger logger = LoggerFactory.getLogger( ApplyChangesToStatesFlowTest.class );%0A%0A
 %09pri
@@ -1127,33 +1127,32 @@
 FILE = %22
-target/test-class
+src/test/resourc
 es/reche
@@ -1226,72 +1226,62 @@
 lic 
-SystemProperty workDirProp = new SystemProperty( WORK_DIRECTORY 
+TemporaryFolder temporaryFolder = new TemporaryFolder(
 );%0A%0A
@@ -1406,138 +1406,29 @@
 p() 
-%7B%0A%09%09Configuration.resetRetest();%0A%09%09workDirProp.setValue( %22target/test-classes%22 );%0A%09%09logger.info( %22Workspace now pointing to %7B%7D.%22, 
+throws Exception %7B%0A%09%09
 Conf
@@ -1441,17 +1441,19 @@
 ion.
-g
+res
 etRetest
 Work
@@ -1452,20 +1452,9 @@
 test
-Workspace() 
+(
 );%0A%09
@@ -1483,51 +1483,11 @@
 ue( 
-Properties.getReTestInstallDir().toString()
+%22/%22
  );%0A
@@ -1719,24 +1719,25 @@
 es() ) ) );%0A
+%0A
 %09%09final Pers
@@ -1818,17 +1818,25 @@
 al File 
-f
+originalF
 ile = ne
@@ -1863,91 +1863,221 @@
 nal 
-SutState before = persistence.load( file.toURI() );%0A%09%09logger.debug( %22Before loaded%22
+File temporaryFile = temporaryFolder.newFile(%22check_GUI_with_review_license.launch.recheck%22);%0A%0A%09%09FileUtils.copyFile( originalFile, temporaryFile );%0A%09%09final SutState before = persistence.load( temporaryFile.toURI()
  );%0A
@@ -2525,17 +2525,26 @@
 ACTION, 
-f
+temporaryF
 ile.getP
@@ -2681,54 +2681,8 @@
 ) );
-%0A%09%09logger.debug( %22AcceptedChanges created.%22 );
 %0A%0A%09%09
@@ -2788,17 +2788,26 @@
 e.load( 
-f
+temporaryF
 ile.toUR

Improve test