package de.retest.recheck.review.ignore.matcher;

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

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

import de.retest.recheck.review.ignore.matcher.ElementClassMatcher.ElementClassMatcherLoader;
import de.retest.recheck.ui.descriptors.Element;
import de.retest.recheck.ui.descriptors.IdentifyingAttributes;

class ElementClassMatcherLoaderTest {

	ElementClassMatcher matcher;
	ElementClassMatcherLoader cut;

	@BeforeEach
	void setUp() {
		final IdentifyingAttributes identifyingAttributes = mock( IdentifyingAttributes.class );
		when( identifyingAttributes.get( ElementClassMatcher.CLASS_KEY ) ).thenReturn( "some-class" );

		final Element element = mock( Element.class );
		when( element.getIdentifyingAttributes() ).thenReturn( identifyingAttributes );

		matcher = new ElementClassMatcher( element );

		cut = new ElementClassMatcherLoader();
	}

	@Test
	void save_should_produce_correct_line_for_single_class_value() {
		final String line = "class=some-class";
		assertThat( cut.save( matcher ) ).isEqualTo( line );
	}

	@Test
	void load_should_produce_correct_ignore_for_single_class_value() {
		final String line = "class=some-class";
		assertThat( cut.save( cut.load( line ) ) ).isEqualTo( line );
	}

	@Test
	void save_should_produce_correct_line_for_multiple_class_values() {
		final String classValue = "one two";

		final IdentifyingAttributes identifyingAttributes = mock( IdentifyingAttributes.class );
		when( identifyingAttributes.get( ElementClassMatcher.CLASS_KEY ) ).thenReturn( classValue );

		final Element element = mock( Element.class );
		when( element.getIdentifyingAttributes() ).thenReturn( identifyingAttributes );

		final ElementClassMatcher matcher = new ElementClassMatcher( element );

		final String line = "class=" + classValue;

		assertThat( cut.save( matcher ) ).isEqualTo( line );
	}

	@Test
	void load_should_produce_correct_ignore_for_multiple_class_values() {
		final String line = "class=one two";
		assertThat( cut.save( cut.load( line ) ) ).isEqualTo( line );
	}

}

@@ -505,16 +505,36 @@
 Test %7B%0A%0A
+%09String classValue;%0A
 %09Element
@@ -623,376 +623,115 @@
 %7B%0A%09%09
-final IdentifyingAttributes identifyingAttributes = mock( IdentifyingAttributes.
+classValue = %22some-
 class
- )
+%22
 ;%0A%09%09
-when( identifyingAttributes.get( ElementClassMatcher.CLASS_KEY ) ).thenReturn( %22some-class%22 );%0A%0A%09%09final Element element =
+matcher = new ElementClassMatcher(
  mock
-( 
 Element
-.class );%0A%09%09when( element.getIdentifyingAttributes() ).thenReturn( identifyingAttributes );%0A%0A%09%09matcher = new ElementClassMatcher( element
+WithClassValue( classValue )
  );%0A
-%0A
 %09%09cu
@@ -848,50 +848,8 @@
 ) %7B%0A
-%09%09final String line = %22class=some-class%22;%0A
 %09%09as
@@ -883,36 +883,52 @@
  ) ).isEqualTo( 
-line
+toLine( classValue )
  );%0A%09%7D%0A%0A%09@Test%0A%09
@@ -1020,26 +1020,28 @@
 e = 
-%22class=some-class%22
+toLine( classValue )
 ;%0A%09%09
@@ -1225,687 +1225,900 @@
 o%22;%0A
-%0A%09%09final IdentifyingAttributes identifyingAttributes = mock( IdentifyingAttributes.class );%0A%09%09when( identifyingAttributes.get( ElementClassMatcher.CLASS_KEY ) ).thenReturn( classValue );%0A%0A%09%09final Element element = mock( Element.class );%0A%09%09when( element.getIdentifyingAttributes() ).thenReturn( identifyingAttributes );%0A%0A%09%09final ElementClassMatcher matcher = new ElementClassMatcher( element );%0A%0A%09%09final String line = %22class=%22 + classValue;%0A%0A%09%09assertThat( cut.save( matcher ) ).isEqualTo( line );%0A%09%7D%0A%0A%09@Test%0A%09void load_should_produce_correct_ignore_for_multiple_class_values() %7B%0A%09%09final String line = %22class=one two%22;%0A%09%09assertThat( cut.save( cut.load( line ) ) ).isEqualTo( line )
+%09%09final ElementClassMatcher matcher = new ElementClassMatcher( mockElementWithClassValue( classValue ) );%0A%09%09assertThat( cut.save( matcher ) ).isEqualTo( toLine( classValue ) );%0A%09%7D%0A%0A%09@Test%0A%09void load_should_produce_correct_ignore_for_multiple_class_values() %7B%0A%09%09final String line = toLine( %22one two%22 );%0A%09%09assertThat( cut.save( cut.load( line ) ) ).isEqualTo( line );%0A%09%7D%0A%0A%09private String toLine( final String classValue ) %7B%0A%09%09return ElementClassMatcher.CLASS_KEY + %22=%22 + classValue;%0A%09%7D%0A%0A%09private Element mockElementWithClassValue( final String classValue ) %7B%0A%09%09final IdentifyingAttributes identifyingAttributes = mock( IdentifyingAttributes.class );%0A%09%09when( identifyingAttributes.get( ElementClassMatcher.CLASS_KEY ) ).thenReturn( classValue );%0A%0A%09%09final Element element = mock( Element.class );%0A%09%09when( element.getIdentifyingAttributes() ).thenReturn( identifyingAttributes );%0A%0A%09%09return element
 ;%0A%09%7D

Refactor test