package de.retest.recheck.ui.diff;

import static de.retest.recheck.ui.diff.ElementBuilder.toAttributes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import java.util.TreeSet;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import de.retest.recheck.ui.Path;
import de.retest.recheck.ui.PathElement;
import de.retest.recheck.ui.descriptors.Element;
import de.retest.recheck.ui.descriptors.IdentifyingAttributes;

class MatchTest {

	public static final double ONE_SIMILARITY = 1.0d / IdentifyingAttributes.PERFECT_SIMILARITY;

	@Test
	void test_better_comp_always_comes_out_first() {
		final Element comp1 = getElement( comp1.class, 1 );
		final Match match1 = Match.of( 0.9d + ONE_SIMILARITY, comp1 );

		final Element comp2 = getElement( comp2.class, 2 );
		final Match match2 = Match.of( 0.9d, comp2 );

		final TreeSet<Match> bestMatches = new TreeSet<>();
		bestMatches.add( match1 );
		bestMatches.add( match2 );

		Assertions.assertThat( bestMatches.pollFirst().element ).isSameAs( comp1 );
		Assertions.assertThat( bestMatches.pollFirst().element ).isSameAs( comp2 );
	}

	@Test
	void same_score_doesnt_overwrite() {
		final Element comp1 = getElement( comp2.class, 2 );
		final Match match1 = Match.of( 0.9d, comp1 );

		final Element comp2 = getElement( comp2.class, 2 );
		final Match match2 = Match.of( 0.9d, comp2 );

		final TreeSet<Match> bestMatches = new TreeSet<>();
		bestMatches.add( match1 );
		bestMatches.add( match2 );

		Assertions.assertThat( bestMatches.size() ).isEqualTo( 2 );
	}

	@Test
	void equal_match_should_yield_similarity_of_1() throws Exception {
		assertThat( Match.ofEqual( mock( Element.class ) ).similarity ).isOne();
	}

	private Element getElement( final Class<?> clazz, final int cnt ) {
		return Element.create( "id", mock( Element.class ),
				IdentifyingAttributes.create( Path.path( new PathElement( clazz.getSimpleName(), 1 ) ), clazz ),
				toAttributes( "color", "green" ) );
	}

	private class comp1 extends Element {
		private static final long serialVersionUID = 1L;
	};

	private class comp2 extends Element {
		private static final long serialVersionUID = 1L;
	};
}

@@ -228,48 +228,8 @@
 t;%0A%0A
-import org.assertj.core.api.Assertions;%0A
 impo
@@ -259,16 +259,16 @@
 i.Test;%0A
+
 %0Aimport 
@@ -515,17 +515,16 @@
 TY = 1.0
-d
  / Ident
@@ -708,17 +708,16 @@
 .of( 0.9
-d
  + ONE_S
@@ -819,33 +819,32 @@
  = Match.of( 0.9
-d
 , comp2 );%0A%0A%09%09fi
@@ -950,35 +950,24 @@
 atch2 );%0A%0A%09%09
-Assertions.
 assertThat( 
@@ -1021,27 +1021,16 @@
 p1 );%0A%09%09
-Assertions.
 assertTh
@@ -1221,25 +1221,24 @@
 atch.of( 0.9
-d
 , comp1 );%0A%0A
@@ -1327,17 +1327,16 @@
 .of( 0.9
-d
 , comp2 
@@ -1456,21 +1456,10 @@
 );%0A%0A
+
 %09%09
-Assertions.
 asse

Refactor test