<commit_before>package uk.ac.ox.zoo.seeg.abraid.mp.common.service.workflow.support;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import uk.ac.ox.zoo.seeg.abraid.mp.common.domain.*;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.DiseaseService;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.ExpertService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import static org.mockito.Matchers.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

/**
 * Tests the DiseaseExtentGenerator class.
 *
 * Copyright (c) 2014 University of Oxford
 */
public class DiseaseExtentGeneratorTest {
    private DiseaseExtentGenerator diseaseExtentGenerator;
    private DiseaseService diseaseService;
    private ExpertService expertService;

    private DiseaseExtentClass presenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.PRESENCE, 100);
    private DiseaseExtentClass possiblePresenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.POSSIBLE_PRESENCE, 50);
    private DiseaseExtentClass uncertainDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.UNCERTAIN, 0);
    private DiseaseExtentClass possibleAbsenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.POSSIBLE_ABSENCE, -50);
    private DiseaseExtentClass absenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.ABSENCE, -100);

    private int diseaseGroupId = 87;
    private DiseaseGroup diseaseGroup;
    private List<? extends AdminUnitGlobalOrTropical> adminUnits;

    @Before
    public void setUp() {
        diseaseService = mock(DiseaseService.class);
        expertService = mock(ExpertService.class);
        adminUnits = getAdminUnits();

        diseaseExtentGenerator = new DiseaseExtentGenerator(diseaseService, expertService);
        mockGetDiseaseExtentClass(presenceDiseaseExtentClass);
        mockGetDiseaseExtentClass(possiblePresenceDiseaseExtentClass);
        mockGetDiseaseExtentClass(uncertainDiseaseExtentClass);
        DateTimeUtils.setCurrentMillisFixed(DateTime.now().getMillis());

        diseaseGroup = new DiseaseGroup(diseaseGroupId, null, "Dengue", DiseaseGroupType.SINGLE);
        diseaseGroup.setGlobal(false);
        diseaseGroup.setAutomaticModelRuns(true);
    }

    @Test
    public void generateDiseaseExtentSetsAllToUncertainIfNoRelevantOccurrencesExist() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtentAllUncertain(DateTime.now());
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, new ArrayList<DiseaseOccurrenceForDiseaseExtent>());
        mockGetExistingDiseaseExtent(new ArrayList<AdminUnitDiseaseExtentClass>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(0);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateInitialDiseaseExtentForTypicalCase() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtent(DateTime.now());
        standardMocks();
        mockGetDiseaseOccurrencesForInitialDiseaseExtent(getOccurrences());
        mockGetExistingDiseaseExtent(new ArrayList<AdminUnitDiseaseExtentClass>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(0);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentSetsAllToUncertainIfNoRelevantOccurrencesOrReviewsExist() {
        // Arrange - variables
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);

        // Arrange - set the expected disease extent to be the initial disease extent, with all changed to uncertain
        // and classChangedDate set appropriately
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtent(createdDate);
        for (AdminUnitDiseaseExtentClass extentClass : expectedDiseaseExtent) {
            extentClass.setOccurrenceCount(0);
            if (!extentClass.getDiseaseExtentClass().equals(uncertainDiseaseExtentClass)) {
                extentClass.setDiseaseExtentClass(uncertainDiseaseExtentClass);
                extentClass.setClassChangedDate(DateTime.now());
            }
        }

        // Arrange - mocks
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, new ArrayList<DiseaseOccurrenceForDiseaseExtent>());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesOnly() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesOnly(createdDate, updatedDate);
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesAndReviews() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesAndReviews(createdDate, updatedDate);
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(getReviews());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesOnlyWithAutomaticModelRunsDisabled() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesOnly(createdDate, updatedDate);
        standardMocks();
        when(diseaseService.getDiseaseOccurrencesForDiseaseExtent(eq(diseaseGroupId),
                eq(parameters.getMinimumValidationWeighting()), eq((DateTime)null),
                same(parameters.getFeedIds()))).thenReturn(getOccurrences());

        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    private DiseaseExtentParameters createParameters() {
        int maximumYearsAgo = 1;<commit_msg>Fix checkstyle violation<commit_after>package uk.ac.ox.zoo.seeg.abraid.mp.common.service.workflow.support;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import uk.ac.ox.zoo.seeg.abraid.mp.common.domain.*;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.DiseaseService;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.ExpertService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import static org.mockito.Matchers.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

/**
 * Tests the DiseaseExtentGenerator class.
 *
 * Copyright (c) 2014 University of Oxford
 */
public class DiseaseExtentGeneratorTest {
    private DiseaseExtentGenerator diseaseExtentGenerator;
    private DiseaseService diseaseService;
    private ExpertService expertService;

    private DiseaseExtentClass presenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.PRESENCE, 100);
    private DiseaseExtentClass possiblePresenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.POSSIBLE_PRESENCE, 50);
    private DiseaseExtentClass uncertainDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.UNCERTAIN, 0);
    private DiseaseExtentClass possibleAbsenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.POSSIBLE_ABSENCE, -50);
    private DiseaseExtentClass absenceDiseaseExtentClass = new DiseaseExtentClass(DiseaseExtentClass.ABSENCE, -100);

    private int diseaseGroupId = 87;
    private DiseaseGroup diseaseGroup;
    private List<? extends AdminUnitGlobalOrTropical> adminUnits;

    @Before
    public void setUp() {
        diseaseService = mock(DiseaseService.class);
        expertService = mock(ExpertService.class);
        adminUnits = getAdminUnits();

        diseaseExtentGenerator = new DiseaseExtentGenerator(diseaseService, expertService);
        mockGetDiseaseExtentClass(presenceDiseaseExtentClass);
        mockGetDiseaseExtentClass(possiblePresenceDiseaseExtentClass);
        mockGetDiseaseExtentClass(uncertainDiseaseExtentClass);
        DateTimeUtils.setCurrentMillisFixed(DateTime.now().getMillis());

        diseaseGroup = new DiseaseGroup(diseaseGroupId, null, "Dengue", DiseaseGroupType.SINGLE);
        diseaseGroup.setGlobal(false);
        diseaseGroup.setAutomaticModelRuns(true);
    }

    @Test
    public void generateDiseaseExtentSetsAllToUncertainIfNoRelevantOccurrencesExist() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtentAllUncertain(DateTime.now());
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, new ArrayList<DiseaseOccurrenceForDiseaseExtent>());
        mockGetExistingDiseaseExtent(new ArrayList<AdminUnitDiseaseExtentClass>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(0);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateInitialDiseaseExtentForTypicalCase() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtent(DateTime.now());
        standardMocks();
        mockGetDiseaseOccurrencesForInitialDiseaseExtent(getOccurrences());
        mockGetExistingDiseaseExtent(new ArrayList<AdminUnitDiseaseExtentClass>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(0);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentSetsAllToUncertainIfNoRelevantOccurrencesOrReviewsExist() {
        // Arrange - variables
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);

        // Arrange - set the expected disease extent to be the initial disease extent, with all changed to uncertain
        // and classChangedDate set appropriately
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getInitialDiseaseExtent(createdDate);
        for (AdminUnitDiseaseExtentClass extentClass : expectedDiseaseExtent) {
            extentClass.setOccurrenceCount(0);
            if (!extentClass.getDiseaseExtentClass().equals(uncertainDiseaseExtentClass)) {
                extentClass.setDiseaseExtentClass(uncertainDiseaseExtentClass);
                extentClass.setClassChangedDate(DateTime.now());
            }
        }

        // Arrange - mocks
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, new ArrayList<DiseaseOccurrenceForDiseaseExtent>());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesOnly() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesOnly(createdDate, updatedDate);
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesAndReviews() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesAndReviews(createdDate, updatedDate);
        standardMocks();
        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(getReviews());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    @Test
    public void generateUpdatedDiseaseExtentOccurrencesOnlyWithAutomaticModelRunsDisabled() {
        // Arrange
        DiseaseExtentParameters parameters = createParameters();
        DateTime createdDate = DateTime.now().minusDays(1);
        DateTime updatedDate = DateTime.now();
        List<AdminUnitDiseaseExtentClass> existingDiseaseExtent = getInitialDiseaseExtent(createdDate);
        List<AdminUnitDiseaseExtentClass> expectedDiseaseExtent = getUpdatedDiseaseExtentOccurrencesOnly(createdDate, updatedDate);
        standardMocks();
        when(diseaseService.getDiseaseOccurrencesForDiseaseExtent(eq(diseaseGroupId),
                eq(parameters.getMinimumValidationWeighting()), eq((DateTime) null),
                same(parameters.getFeedIds()))).thenReturn(getOccurrences());

        mockGetDiseaseOccurrencesForUpdatedDiseaseExtent(parameters, getOccurrences());
        mockGetExistingDiseaseExtent(existingDiseaseExtent);
        mockGetRelevantReviews(new ArrayList<AdminUnitReview>());

        // Act
        diseaseExtentGenerator.generateDiseaseExtent(diseaseGroupId, parameters);

        // Assert
        expectGetDiseaseOccurrencesForDiseaseExtent(1);
        expectGetRelevantReviews(1);
        expectSaveAdminUnitDiseaseExtentClass(expectedDiseaseExtent);
        expectUpdateAggregatedDiseaseExtent(1);
    }

    private DiseaseExtentParameters createParameters() {
        int maximumYearsAgo = 1;
{'fraction_non_alphanumeric': 0.07467601444656893, 'fraction_numerical': 0.004355215636286382, 'mean_word_length': 4.35218874360432, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6445863', 'n_tokens_mistral': 5526, 'n_tokens_neox': 5066, 'n_words': 710}