package de.retest.recheck.ui.review;

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

import java.util.Arrays;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.retest.recheck.report.ActionReplayResult;
import de.retest.recheck.ui.descriptors.IdentifyingAttributes;
import de.retest.recheck.ui.diff.ElementDifference;
import de.retest.recheck.ui.diff.RootElementDifference;
import de.retest.recheck.ui.diff.StateDifference;
import de.retest.recheck.ui.image.Screenshot;

class ScreenshotChangesTest {

	ActionReplayResult actionWithDifferences;

	Screenshot screenshot;

	IdentifyingAttributes identifyingForPresentScreenshot;
	IdentifyingAttributes identifyingForMissingScreenshot;

	@BeforeEach
	void setUp() {
		screenshot = mock( Screenshot.class );
		identifyingForPresentScreenshot = mock( IdentifyingAttributes.class );
		identifyingForMissingScreenshot = mock( IdentifyingAttributes.class );

		final ElementDifference elementWithScreenshot = mock( ElementDifference.class );
		when( elementWithScreenshot.getIdentifyingAttributes() ).thenReturn( identifyingForPresentScreenshot );
		when( elementWithScreenshot.getActualScreenshot() ).thenReturn( screenshot );

		final RootElementDifference rootElementWithScreenshot = mock( RootElementDifference.class );
		when( rootElementWithScreenshot.getElementDifference() ).thenReturn( elementWithScreenshot );
		when( rootElementWithScreenshot.getActualScreenshot() ).thenReturn( screenshot );

		final ElementDifference elementWithoutScreenshot = mock( ElementDifference.class );
		when( elementWithoutScreenshot.getIdentifyingAttributes() ).thenReturn( identifyingForMissingScreenshot );

		final RootElementDifference rootElementWithoutScreenshot = mock( RootElementDifference.class );
		when( rootElementWithoutScreenshot.getElementDifference() ).thenReturn( elementWithoutScreenshot );

		final StateDifference stateDifference = mock( StateDifference.class );
		when( stateDifference.getRootElementDifferences() )
				.thenReturn( Arrays.asList( rootElementWithScreenshot, rootElementWithoutScreenshot ) );

		actionWithDifferences = mock( ActionReplayResult.class );
		when( actionWithDifferences.getStateDifference() ).thenReturn( stateDifference );
	}

	@Test
	void actual_should_properly_lookup_element_screenshots() throws Exception {
		final ScreenshotChanges cut = ScreenshotChanges.actual( actionWithDifferences );

		assertThat( cut.getScreenshot( identifyingForPresentScreenshot ) ).isEqualTo( screenshot );
		assertThat( cut.getScreenshot( identifyingForMissingScreenshot ) ).isNull();
	}

	@Test
	void actual_should_not_produce_null_pointer_with_null_screenshots() throws Exception {
		assertThatCode( () -> ScreenshotChanges.actual( actionWithDifferences ) ).doesNotThrowAnyException();
	}
}

@@ -2943,11 +2943,291 @@
 on();%0A%09%7D
+%0A%0A%09@Test%0A%09void actual_should_not_throw_if_difference_is_null() throws Exception %7B%0A%09%09final ActionReplayResult actionWithoutDifferences = mock( ActionReplayResult.class );%0A%0A%09%09assertThatCode( () -%3E ScreenshotChanges.actual( actionWithoutDifferences ) ).doesNotThrowAnyException();%0A%09%7D
 %0A%7D%0A

Write test that recreates failing behavior