
package com.uwetrottmann.shopr.algorithm.test;

import static org.fest.assertions.api.Assertions.assertThat;

import com.uwetrottmann.shopr.algorithm.Query;
import com.uwetrottmann.shopr.algorithm.Utils;
import com.uwetrottmann.shopr.algorithm.model.Attributes;
import com.uwetrottmann.shopr.algorithm.model.Color;
import com.uwetrottmann.shopr.algorithm.model.Item;

import org.junit.Test;

import java.util.ArrayList;

public class UtilsTest {

    @Test
    public void testSortBySimilarityToQuery() {
        // unsorted list of items
        ArrayList<Item> unsortedCases = new ArrayList<Item>();
        unsortedCases.
                add(new Item().attributes(new Attributes().color(new Color(Color.Value.RED))));
        unsortedCases.
                add(new Item().attributes(new Attributes().color(new Color(Color.Value.BLACK))));
        unsortedCases
                .add(new Item().attributes(new Attributes().color(new Color(Color.Value.RED))));
        unsortedCases
                .add(new Item().attributes(new Attributes().color(new Color(Color.Value.GREEN))));
        unsortedCases
                .add(new Item().attributes(new Attributes().color(new Color(Color.Value.BLUE))));

        // sample query
        Query query = new Query().attributes(new Attributes().color(new Color(Color.Value.RED)));

        Utils.sortBySimilarityToQuery(query, unsortedCases);

        for (Item item : unsortedCases) {
            System.out.println("Item " + item.attributes().color().toString()
                    + " querySimilarity: " + item.querySimilarity());
        }

        // top item is red?
        assertThat(
                unsortedCases.get(0).attributes().color().getValueWeights()[Color.Value.RED
                        .ordinal()]).isEqualTo(1.0);
    }

}

@@ -1394,198 +1394,566 @@
     
-for (Item item : unsortedCases) %7B%0A            System.out.println(%22Item %22 + item.attributes().color().toString()%0A              
+// top two items are red?%0A        dumpToConsole(unsortedCases, query);%0A        assertThat(%0A                unsortedCases.get(0).attributes().color().getValueWeights()%5BColor.Value.RED%0A                        .ordinal()%5D).isEqualTo(1.0);%0A        assertThat(%0A                unsortedCases.get(1).attributes().color().getValueWeights()%5BColor.Value.RED%0A                        .ordinal()%5D).isEqualTo(1.0);%0A%0A        // equal weight on all colors%0A
       
-+ %22
+ 
  query
-Similarity: %22 + item.querySimilarity()
+.attributes().color(new Color());%0A%0A        Utils.sortBySimilarityToQuery(query, unsortedCases
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -1952,38 +1952,248 @@
     
-%7D%0A%0A        // top item is red?
+// nothing should have changed%0A        dumpToConsole(unsortedCases, query);%0A        assertThat(%0A                unsortedCases.get(0).attributes().color().getValueWeights()%5BColor.Value.RED%0A                        .ordinal()%5D).isEqualTo(1.0);
 %0A   
@@ -2235,33 +2235,33 @@
 sortedCases.get(
-0
+1
 ).attributes().c
@@ -2361,10 +2361,350 @@
 %0A    %7D%0A%0A
+    private void dumpToConsole(ArrayList%3CItem%3E unsortedCases, Query query) %7B%0A        System.out.println(%22Query %22 + query.attributes().color());%0A        for (Item item : unsortedCases) %7B%0A            System.out.println(%22Item %22 + item.attributes().color()%0A                    + %22 querySimilarity: %22 + item.querySimilarity());%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Test sorting with equal weights in query.