package uk.ac.ebi.atlas.experiments;

import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.ac.ebi.atlas.model.experiment.Experiment;
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperimentTest;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ExperimentsInfoListServiceTest {

    private static final String EXPECTED_JSON =
          "{\n" +
                  "  \"aaData\": [\n" +
                  "    {\n" +
                  "      \"experimentType\": \"RNASEQ_MRNA_BASELINE\",\n" +
                  "      \"experimentAccession\": \"accession\",\n" +
                  "      \"experimentDescription\": \"description\",\n" +
                  "      \"lastUpdate\": \"22-12-2016\",\n" +
                  "      \"numberOfAssays\": 0,\n" +
                  "      \"numberOfContrasts\": 0,\n" +
                  "      \"species\": \"species\",\n" +
                  "      \"kingdom\": \"kingdom\",\n" +
                  "      \"experimentalFactors\": [],\n" +
                  "      \"arrayDesigns\": [],\n" +
                  "      \"arrayDesignNames\": []\n" +
                  "    }\n" +
                  "  ]\n" +
                  "}";


    @Mock
    private ExperimentTrader experimentTrader;

    private ExperimentInfoListService subject;

    @Before
    public void setUp() throws Exception {

        when(experimentTrader.getPublicExperiments(ExperimentType.RNASEQ_MRNA_BASELINE))
                .thenReturn(ImmutableSet.<Experiment>of(BaselineExperimentTest.mockExperiment()));

        subject =
                new ExperimentInfoListService(experimentTrader, ImmutableSet.of(ExperimentType.RNASEQ_MRNA_BASELINE));

    }



    @Test
    public void sizeIsRight() throws Exception {
        JsonArray result = subject.getExperimentsJson().get("aaData").getAsJsonArray();
       assertThat(result.size(), is(1));
    }

    @Test
    public void formatIsInSyncWithWhatWeExpectAndTheDataOfMockBaselineExperiment() throws Exception {
        JsonElement result = subject.getExperimentsJson();
        assertThat(result, is(new Gson().fromJson(EXPECTED_JSON, JsonElement.class)));
    }
}
@@ -852,24 +852,16 @@
         
-        
 %22  %5C%22aaD
@@ -880,32 +880,24 @@
 +%0A          
-        
 %22    %7B%5Cn%22 +%0A
@@ -902,32 +902,24 @@
 +%0A          
-   
-     
 %22      %5C%22exp
@@ -970,32 +970,24 @@
 +%0A          
-    
-    
 %22      %5C%22exp
@@ -1032,32 +1032,24 @@
 +%0A          
-     
-   
 %22      %5C%22exp
@@ -1098,32 +1098,24 @@
 +%0A          
-        
 %22      %5C%22las
@@ -1131,18 +1131,22 @@
 : %5C%22
-22-12-2016
+%5C%5Cd+-%5C%5Cd+-%5C%5Cd+
 %5C%22,%5C
@@ -1156,32 +1156,24 @@
 +%0A          
-      
-  
 %22      %5C%22num
@@ -1191,32 +1191,24 @@
 %5C%22: 0,%5Cn%22 +%0A
-        
           %22 
@@ -1249,32 +1249,24 @@
 +%0A          
-        
 %22      %5C%22spe
@@ -1297,32 +1297,24 @@
 +%0A          
- 
-       
 %22      %5C%22kin
@@ -1345,32 +1345,24 @@
 +%0A          
-  
-      
 %22      %5C%22exp
@@ -1396,32 +1396,24 @@
 +%0A          
-   
-     
 %22      %5C%22arr
@@ -1440,32 +1440,24 @@
 +%0A          
-     
-   
 %22      %5C%22arr
@@ -1491,24 +1491,16 @@
         
-      
-  
 %22    %7D%5Cn
@@ -1513,24 +1513,16 @@
         
-       
- 
 %22  %5D%5Cn%22 
@@ -1523,24 +1523,16 @@
  %5D%5Cn%22 +%0A
-        
         
@@ -2433,66 +2433,345 @@
 sult
-, is(new Gson().fromJson(EXPECTED_JSON, JsonElement.class)
+.toString().matches(%0A                EXPECTED_JSON.replace(%22 %22, %22%22)%0A                             .replace(%22%5Cn%22, %22%22)%0A                             .replace(%22%5B%22,%22%5C%5C%5B%22)%0A                             .replace(%22%5D%22,%22%5C%5C%5D%22)%0A                             .replace(%22%7B%22,%22%5C%5C%7B%22)%0A                             .replace(%22%7D%22,%22%5C%5C%7D%22)),%0A                is(true
 ));%0A

Make test more robust, fixes http://bar:8085/browse/ATLAS-ATLASBASE-73