
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();
        query.attributes().color(new Color(Color.Value.RED));

        Utils.sortBySimilarityToQuery(query, unsortedCases);

        // top two items are red?
        Utils.dumpToConsole(unsortedCases, query);
        assertThat(
                unsortedCases.get(0).attributes().color().getValueWeights()[Color.Value.RED
                        .ordinal()]).isEqualTo(1.0);
        assertThat(
                unsortedCases.get(1).attributes().color().getValueWeights()[Color.Value.RED
                        .ordinal()]).isEqualTo(1.0);

        // equal weight on all colors
        query.attributes().color(new Color());

        Utils.sortBySimilarityToQuery(query, unsortedCases);

        // nothing should have changed
        Utils.dumpToConsole(unsortedCases, query);
        assertThat(
                unsortedCases.get(0).attributes().color().getValueWeights()[Color.Value.RED
                        .ordinal()]).isEqualTo(1.0);
        assertThat(
                unsortedCases.get(1).attributes().color().getValueWeights()[Color.Value.RED
                        .ordinal()]).isEqualTo(1.0);
    }

}

@@ -609,32 +609,49 @@
    unsortedCases
+%0A                
 .%0A              
@@ -687,37 +687,44 @@
 ew Attributes().
-color
+putAttribute
 (new Color(Color
@@ -812,38 +812,70 @@
 new Attributes()
-.color
+%0A                        .putAttribute
 (new Color(Color
@@ -965,38 +965,70 @@
 new Attributes()
-.color
+%0A                        .putAttribute
 (new Color(Color
@@ -1125,32 +1125,64 @@
 butes().
-color(new Color(
+putAttribute(new Color(%0A                        
 Color.Va
@@ -1269,38 +1269,70 @@
 new Attributes()
-.color
+%0A                        .putAttribute
 (new Color(Color
@@ -1427,37 +1427,44 @@
 ry.attributes().
-color
+putAttribute
 (new Color(Color
@@ -1687,38 +1687,57 @@
 0).attributes().
-c
+getAttributeById(C
 olor
-(
+.ID
 ).getValueWeight
@@ -1871,38 +1871,57 @@
 1).attributes().
-c
+getAttributeById(C
 olor
-(
+.ID
 ).getValueWeight
@@ -2059,21 +2059,28 @@
 butes().
-color
+putAttribute
 (new Col
@@ -2301,38 +2301,57 @@
 0).attributes().
-c
+getAttributeById(C
 olor
-(
+.ID
 ).getValueWeight
@@ -2497,14 +2497,33 @@
 s().
-c
+getAttributeById(C
 olor
-(
+.ID
 ).ge

Fix UtilsTest: use new put and get methods.