package de.retest.recheck.persistence.bin;

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.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" );
		kryoPersistence.save( identifier, new de.retest.recheck.test.Test( tests ) );
		kryoPersistence.load( identifier );
	}

	@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." );
	}

}

@@ -33,24 +33,82 @@
 tence.bin;%0A%0A
+import static org.assertj.core.api.Assertions.assertThat;%0A
 import stati
@@ -1091,32 +1091,122 @@
 ./test.test%22 );%0A
+%09%09final de.retest.recheck.test.Test persisted = new de.retest.recheck.test.Test( tests );%0A
 %09%09kryoPersistenc
@@ -1217,35 +1217,52 @@
 ve( identifier, 
-new
+persisted );%0A%09%09final
  de.retest.reche
@@ -1277,23 +1277,18 @@
 Test
-( tests ) );%0A%09%09
+ loaded = 
 kryo
@@ -1318,16 +1318,130 @@
 ifier );
+%0A%0A%09%09assertThat( persisted.getRelativeActionSequencePaths() ).isEqualTo( loaded.getRelativeActionSequencePaths() );
 %0A%09%7D%0A%0A%09@T

Add assertion to satisfy sonarcloud