package uk.ac.ebi.atlas.model.experiment;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import uk.ac.ebi.atlas.model.DescribesDataColumns;
import uk.ac.ebi.atlas.model.experiment.Experiment;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import javax.annotation.Nullable;
import javax.inject.Inject;

import java.util.List;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "/dispatcher-servlet.xml"})
public class ExperimentGroupingsForHeatmapIT {

    @Inject
    ExperimentTrader experimentTrader;

    @Test
    public void testSomeGoodExperiments() throws Exception {

        outputInFineFormatForExperiment("E-MTAB-513");
        outputInFineFormatForExperiment("E-MTAB-2706");
        outputInFineFormatForExperiment("E-GEOD-54705");
        outputInFineFormatForExperiment("E-MTAB-4260");

    }

    /*
    For differential experiments, a few groupings repeat and we might need something else for them.

     */

    public void outputInFineFormatForExperiment(String accession){
        Experiment<DescribesDataColumns> experiment = experimentTrader.getPublicExperiment(accession);

        List<String> allDescriptorIds = FluentIterable.from(experiment.getDataColumnDescriptors()).transform(
                new Function<DescribesDataColumns, String>() {
                    @Nullable
                    @Override
                    public String apply(@Nullable DescribesDataColumns describesDataColumns) {
                        return describesDataColumns.getId();
                    }
                }).toList();

        JsonArray result = experiment.groupingsForHeatmap();

        assertThat(result.size(), greaterThan(0));

        for(JsonElement element: result){
            assertTrue(element.getAsJsonObject().has("name"));
            assertTrue(element.getAsJsonObject().has("selected"));
            assertThat(element.getAsJsonObject().get("groupings").getAsJsonArray().size(), greaterThan(0));
            for(JsonElement grouping: element.getAsJsonObject().get("groupings").getAsJsonArray()){
                assertThat(grouping.getAsJsonArray().size(), is(2));
                assertThat(grouping.getAsJsonArray().get(1).getAsJsonArray().size(), greaterThan(0));
                for(JsonElement groupingValue : grouping.getAsJsonArray().get(1).getAsJsonArray()){
                    assertTrue(allDescriptorIds.contains(groupingValue.getAsString()));
                }
            }
        }
    }
}
@@ -40,97 +40,8 @@
 t;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.collect.FluentIterable;%0A
 impo
@@ -416,60 +416,8 @@
 ns;%0A
-import uk.ac.ebi.atlas.model.experiment.Experiment;%0A
 impo
@@ -465,42 +465,8 @@
 r;%0A%0A
-import javax.annotation.Nullable;%0A
 impo
@@ -512,16 +512,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -590,43 +590,145 @@
 ers.
-*;%0Aimport static org.junit.Assert.*
+greaterThan;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0A@
@@ -1397,17 +1397,16 @@
 r them.%0A
-%0A
      */%0A
@@ -1621,28 +1621,8 @@
 s = 
-FluentIterable.from(
 expe
@@ -1658,20 +1658,17 @@
 rs()
-).transfor
+.strea
 m(
+)
 %0A   
@@ -1684,21 +1684,13 @@
     
-new Function%3C
+.map(
 Desc
@@ -1709,278 +1709,52 @@
 umns
-, String%3E() %7B%0A                    @Nullable%0A                    @Override%0A                    public String apply(@Nullable DescribesDataColumns describesDataColumns) %7B%0A                        return describesDataColumns.getId();%0A                    %7D%0A                %7D)
+::getId)%0A                .collect(Collectors
 .toL
@@ -1758,16 +1758,17 @@
 toList()
+)
 ;%0A%0A     

Migrate Guava functional thing to Java