package org.pdxfinder.commands.dataloaders;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.neo4j.ogm.json.JSONArray;
import org.neo4j.ogm.json.JSONObject;
import org.pdxfinder.commands.BaseTest;
import org.pdxfinder.graph.dao.*;
import org.pdxfinder.graph.repositories.SpecimenRepository;
import org.pdxfinder.services.DataImportService;
import org.pdxfinder.services.UtilityService;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

public class LoadIRCCTest extends BaseTest {

    private Group transnationalAccess;
    private Group badAccess;

    private Group provider;

    @Before public void init() {
        MockitoAnnotations.initMocks(this);
        transnationalAccess = new Group();
        transnationalAccess.setName("transnational access");
        badAccess = new Group();
        badAccess.setName("another group");

        provider = new Group(
                "Test IRCC Provider",
                "IRCC-CRC",
                "Test provider description",
                "Academia",
                "Joe Bloggs",
                "ircc.example.com");
    }

    @MockBean UtilityService utilityService;
    @MockBean DataImportService dataImportService;
    @MockBean SpecimenRepository specimenRepository;
    @InjectMocks private LoadIRCC loader;

    @Test
    public void Given_ValidSpecimen_WhenLoadSpecimen_Then_SpecimenAddedToModel() throws Exception {
        ModelCreation modelCreation = new ModelCreation("1");
        JSONArray specimens = new JSONArray(
    "[{" +
                "\"Specimen ID\":\"CRCTEST\"," +
                "\"Engraftment Type\":\"tissue fragment\"," +
                "\"Engraftment Site\":\"subcutis right\"," +
                "\"Strain\":\"NOD SCID\"," +
                "\"Passage\":\"1\"," +
                "\"Platforms\":[ " +
                    "{\"Platform\":\"RealTimePCR_GCN\"}" +
                "]" +
            "}]"
        );
        JSONObject json = specimens.getJSONObject(0);
        Specimen specimen = new Specimen();
        specimen.setExternalId(json.getString("Specimen ID"));
        specimen.setPassage(json.getString("Passage"));

        loader.dto.setModelCreation(modelCreation);
        loader.dto.setSpecimens(specimens);
        loader.dto.setProviderGroup(provider);

        when(dataImportService.getSpecimen(
            modelCreation,
            json.getString("Specimen ID"),
            loader.dto.getProviderGroup().getAbbreviation(),
            json.getString("Passage"))
        ).thenReturn(specimen);

        loader.step13LoadSpecimens();

        assertEquals(loader.dto.getSpecimens().getJSONObject(0).getString("Specimen ID"), "CRCTEST");
        assertThat(loader.dto.getModelCreation().getSpecimens().contains(specimen), is(true));
    }

    @Test
    public void Given_IRCCCRCModel_When_AddAccessModality_Then_AddToTransnationalAccessGroup() {
        when(dataImportService.getAccessibilityGroup("", "transnational access"))
                .thenReturn(transnationalAccess);

        ModelCreation mc = new ModelCreation();
        loader.dto.setModelCreation(mc);
        loader.addAccessModality();

        assertThat(loader.dto.getModelCreation().getGroups().contains(transnationalAccess), is(true));
        assertThat(loader.dto.getModelCreation().getGroups().contains(badAccess), is(false));
    }

}
@@ -1136,28 +1136,24 @@
             
-    
 %22Test IRCC P
@@ -1174,20 +1174,16 @@
         
-    
 %22IRCC-CR
@@ -1186,20 +1186,16 @@
 C-CRC%22,%0A
-    
         
@@ -1239,20 +1239,16 @@
         
-    
 %22Academi
@@ -1251,20 +1251,16 @@
 demia%22,%0A
-    
         
@@ -1277,20 +1277,16 @@
 loggs%22,%0A
-    
         
@@ -1726,16 +1726,24 @@
 NArray(%0A
+        
     %22%5B%7B%22
@@ -1741,24 +1741,24 @@
      %22%5B%7B%22 +%0A
-
             
@@ -2816,16 +2816,28 @@
 tEquals(
+ %22CRCTEST%22, 
 loader.d
@@ -2899,19 +2899,8 @@
 ID%22)
-, %22CRCTEST%22
 );%0A 
@@ -3570,9 +3570,10 @@
 %0A    %7D%0A%0A
-
 %7D
+%0A

Fix code smell (test arguments wrong way round)