package uk.ac.ebi.atlas.model;

import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;
import java.util.NoSuchElementException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class GeneProfileTest {

    private GeneProfile subject;

    private Expression expression1 = new Expression(new ExperimentRun("RUN_ACCESSION_1"), 2.2D);
    private Expression expression2 = new Expression(new ExperimentRun("RUN_ACCESSION_2"), 3D);

    @Before
    public void setUp() throws Exception {
        subject = GeneProfile.forGeneId("EMBL-1")
                    .addExpression(expression1)
                    .addExpression(expression2)
                    .create();
    }

    @Test
    public void testGetGeneSpecificity() throws Exception {
        assertThat(subject.getGeneSpecificity(), is(2));
    }

    @Test
    public void iteratorReturnsExpressionsAndThenNull() throws Exception {
        //given
        Iterator<GeneExpression> profileIterator = subject.iterator();

        //then
        assertThat(profileIterator.next().getLevel(), isOneOf(2.2D, 3D));
        assertThat(profileIterator.next().getLevel(), isOneOf(2.2D, 3D));
        //and
        assertThat(profileIterator.hasNext(), is(false));
    }

    @Test
    public void builderAddExpressionTest() {
        //given
        GeneProfile.Builder builder = GeneProfile.forGeneId("ENS1");
        builder.withCutoff(3D);

        Expression expressionSmaller = new Expression(new ExperimentRun("RUN_ACCESSION_1"), 2.2D);
        builder.addExpression(expressionSmaller);

        Expression expressionEquals = new Expression(new ExperimentRun("RUN_ACCESSION_2"), 3D);
        builder.addExpression(expressionEquals);

        Expression expressionBigger = new Expression(new ExperimentRun("RUN_ACCESSION_3"), 4D);
        builder.addExpression(expressionBigger);

        //when
        Iterator<GeneExpression> profileIterator = builder.create().iterator();

        //then
        assertThat(profileIterator.next().getLevel(), is(4D));
        //and
        assertThat(profileIterator.hasNext(), is(false));

    }
}


@@ -25,16 +25,55 @@
 model;%0A%0A
+import com.google.common.collect.Sets;%0A
 import o
@@ -134,57 +134,9 @@
 til.
-Iterator;%0Aimport java.util.NoSuchElementException
+*
 ;%0A%0Ai
@@ -469,32 +469,91 @@
 RUN_ACCESSION_2%22
+, Sets.newHashSet(new FactorValue(%22organism part%22,%22trunk%22))
 ), 3D);%0A%0A    @Be
@@ -2201,11 +2201,1037 @@
 %0A%0A    %7D%0A
+%0A    @Test%0A    public void filterByOrganismPartsShouldRemoveExpressionsWithUnwontedFactorValues()%7B%0A        Set%3CString%3E organismParts = Sets.newHashSet(%22beack%22, %22trunk%22);%0A%0A        Iterable%3CGeneExpression%3E geneExpressions = subject.filterByOrganismParts(organismParts);%0A%0A        for(GeneExpression expression: geneExpressions)%7B%0A            assertThat(expression.getOrganismPart(), is(not(%22beack%22)));%0A            assertThat(expression.getOrganismPart(), is(%22trunk%22));%0A        %7D%0A    %7D%0A%0A%0A    @Test%0A    public void filterByOrganismPartsShouldKeepAllExpressionsWhenNoOrganismPartIsSpecified()%7B%0A        Set%3CString%3E organismParts = Sets.newHashSet();%0A%0A        Iterable%3CGeneExpression%3E geneExpressions = subject.filterByOrganismParts(organismParts);%0A%0A        List%3CGeneExpression%3E filteredGeneExpressions = new ArrayList%3CGeneExpression%3E();%0A%0A        for(GeneExpression geneExpression: geneExpressions)%7B%0A            filteredGeneExpressions.add(geneExpression);%0A        %7D%0A%0A        assertThat(filteredGeneExpressions.size(), is(2));%0A%0A    %7D%0A%0A
 %7D%0A%0A

test driven refactoring