package uk.ac.ox.zoo.seeg.abraid.mp.modeloutputhandler.web;

import org.joda.time.DateTime;
import org.junit.Test;
import uk.ac.ox.zoo.seeg.abraid.mp.common.domain.ModelRun;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.workflow.ModelRunWorkflowService;

import static org.mockito.Mockito.*;


/**
 * Tests the DiseaseExtentGenerationHandler class.
 * Copyright (c) 2014 University of Oxford
 */
public class DiseaseExtentGenerationHandlerTest {

    private static final String TEST_MODEL_RUN_NAME = "deng_2014-05-16-13-28-57_482ae3ca-ab30-414d-acce-388baae7d83c";
    private static final int TEST_MODEL_RUN_DISEASE_GROUP_ID = 87;

    @Test
    public void handleGeneratesDiseaseExtent() throws Exception {
        // Arrange
        ModelRunWorkflowService modelRunWorkflowService = mock(ModelRunWorkflowService.class);
        DiseaseExtentGenerationHandler handler = new DiseaseExtentGenerationHandler(modelRunWorkflowService);
        ModelRun modelRun = new ModelRun(TEST_MODEL_RUN_NAME, TEST_MODEL_RUN_DISEASE_GROUP_ID, DateTime.now());

        // Act
        handler.handle(modelRun);

        // Assert
        verify(modelRunWorkflowService, times(1)).generateDiseaseExtent(TEST_MODEL_RUN_DISEASE_GROUP_ID);
    }
}

@@ -157,24 +157,158 @@
 .domain.
-ModelRun
+DiseaseGroup;%0Aimport uk.ac.ox.zoo.seeg.abraid.mp.common.domain.ModelRun;%0Aimport uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.DiseaseService
 ;%0Aimport
@@ -726,31 +726,16 @@
 nal int 
-TEST_MODEL_RUN_
 DISEASE_
@@ -804,16 +804,43 @@
 seExtent
+IfAutomaticModelRunsEnabled
 () throw
@@ -872,16 +872,139 @@
 Arrange%0A
+        DiseaseGroup diseaseGroup = new DiseaseGroup(DISEASE_GROUP_ID);%0A        diseaseGroup.setAutomaticModelRuns(true);%0A%0A
         
@@ -1090,16 +1090,109 @@
 class);%0A
+        DiseaseService diseaseService = mockDiseaseService(DISEASE_GROUP_ID, diseaseGroup);%0A%0A
         
@@ -1231,16 +1231,32 @@
 andler =
+%0A               
  new Dis
@@ -1306,16 +1306,32 @@
 wService
+, diseaseService
 );%0A     
@@ -1387,31 +1387,16 @@
 N_NAME, 
-TEST_MODEL_RUN_
 DISEASE_
@@ -1563,16 +1563,932 @@
 eExtent(
+diseaseGroup);%0A    %7D%0A%0A    private DiseaseService mockDiseaseService(int id, DiseaseGroup diseaseGroup) %7B%0A        DiseaseService diseaseService = mock(DiseaseService.class);%0A        when(diseaseService.getDiseaseGroupById(id)).thenReturn(diseaseGroup);%0A        return diseaseService;%0A    %7D%0A%0A    @Test%0A    public void handleDoesNotGenerateDiseaseExtentIfAutomaticModelRunsNotEnabled() throws Exception %7B%0A        // Arrange%0A        DiseaseGroup diseaseGroup = new DiseaseGroup(DISEASE_GROUP_ID);%0A        diseaseGroup.setAutomaticModelRuns(false);%0A%0A        ModelRunWorkflowService modelRunWorkflowService = mock(ModelRunWorkflowService.class);%0A        DiseaseService diseaseService = mockDiseaseService(DISEASE_GROUP_ID, diseaseGroup);%0A%0A        DiseaseExtentGenerationHandler handler =%0A                new DiseaseExtentGenerationHandler(modelRunWorkflowService, diseaseService);%0A        ModelRun modelRun = new ModelRun(
 TEST_MOD
@@ -2494,16 +2494,22 @@
 DEL_RUN_
+NAME, 
 DISEASE_
@@ -2512,23 +2512,195 @@
 ASE_GROUP_ID
+, DateTime.now());%0A%0A        // Act%0A        handler.handle(modelRun);%0A%0A        // Assert%0A        verify(modelRunWorkflowService, times(0)).generateDiseaseExtent(diseaseGroup
 );%0A    %7D%0A%7D%0A

Test handler only generates disease extent if automatic model runs enabled