package de.retest.recheck.persistence.bin;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;

import de.retest.recheck.persistence.IncompatibleReportVersionException;
import de.retest.recheck.util.VersionProvider;

class KryoPersistenceTest {

	@Test
	void roundtrip_should_work( @TempDir final Path temp ) throws IOException {
		final Path file = temp.resolve( "test.test" );
		Files.createFile( file );
		final URI identifier = file.toUri();

		final KryoPersistence<de.retest.recheck.test.Test> kryoPersistence = new KryoPersistence<>();
		final ArrayList<String> tests = new ArrayList<>();
		tests.add( "../test.test" );
		final de.retest.recheck.test.Test persisted = new de.retest.recheck.test.Test( tests );
		kryoPersistence.save( identifier, persisted );
		final de.retest.recheck.test.Test loaded = kryoPersistence.load( identifier );

		assertThat( persisted.getRelativeActionSequencePaths() ).isEqualTo( loaded.getRelativeActionSequencePaths() );
	}

	@Test
	void incompatible_version_should_give_persisting_version( @TempDir final Path temp ) throws IOException {
		final Path file = temp.resolve( "incompatible-test.test" );
		Files.createFile( file );
		final URI identifier = file.toUri();

		final KryoPersistence<de.retest.recheck.test.Test> kryoPersistence = new KryoPersistence<>();
		final ArrayList<String> tests = new ArrayList<>();
		tests.add( "../test.test" );
		kryoPersistence.save( identifier, new de.retest.recheck.test.Test( tests ) );

		final Kryo kryoMock = mock( Kryo.class );
		when( kryoMock.readClassAndObject( Mockito.any() ) ).thenThrow( KryoException.class );
		final KryoPersistence<de.retest.recheck.test.Test> differentKryoPersistence =
				new KryoPersistence<>( kryoMock, "old Version" );

		assertThatThrownBy( () -> differentKryoPersistence.load( identifier ) )
				.isInstanceOf( IncompatibleReportVersionException.class )
				.hasMessageContaining( "Incompatible recheck versions: report was written with "
						+ VersionProvider.RETEST_VERSION + ", but read with old Version." );
	}

	@Test
	void unknown_version_should_give_correct_error() throws IOException {
		final Path file = Paths.get( "src/test/resources/de/retest/recheck/persistence/bin/old.report" );
		final URI identifier = file.toUri();

		final KryoPersistence<de.retest.recheck.test.Test> differentKryoPersistence = new KryoPersistence<>();

		assertThatThrownBy( () -> differentKryoPersistence.load( identifier ) )
				.isInstanceOf( IncompatibleReportVersionException.class ).hasMessageContaining(
						"Incompatible recheck versions: report was written with an old recheck version (pre 1.5.0), but read with "
								+ VersionProvider.RETEST_VERSION + "." );
	}
}

@@ -161,16 +161,64 @@
 rownBy;%0A
+import static org.mockito.ArgumentMatchers.any;%0A
 import s
@@ -530,36 +530,8 @@
 Dir;
-%0Aimport org.mockito.Mockito;
 %0A%0Aim
@@ -1067,92 +1067,8 @@
 ();%0A
-%09%09final ArrayList%3CString%3E tests = new ArrayList%3C%3E();%0A%09%09tests.add( %22../test.test%22 );%0A
 %09%09fi
@@ -1115,47 +1115,24 @@
 d = 
-new de.retest.recheck.test.Test( tests 
+createDummyTest(
 );%0A%09
@@ -1370,32 +1370,227 @@
 ePaths() );%0A%09%7D%0A%0A
+%09public de.retest.recheck.test.Test createDummyTest() %7B%0A%09%09final ArrayList%3CString%3E tests = new ArrayList%3C%3E();%0A%09%09tests.add( %22../test.test%22 );%0A%09%09return new de.retest.recheck.test.Test( tests );%0A%09%7D%0A%0A
 %09@Test%0A%09void inc
@@ -1919,165 +1919,58 @@
 ;%0A%09%09
-final ArrayList%3CString%3E tests = new ArrayList%3C%3E();%0A%09%09tests.add( %22../test.test%22 );%0A%09%09kryoPersistence.save( identifier, new de.retest.recheck.test.Test( tests 
+kryoPersistence.save( identifier, createDummyTest(
 ) );
@@ -2052,24 +2052,16 @@
 Object( 
-Mockito.
 any() ) 

Refactor to improve test