<commit_before>package org.innovateuk.ifs.competition.controller;

import com.google.common.collect.Sets;
import org.innovateuk.ifs.BaseControllerIntegrationTest;
import org.innovateuk.ifs.application.domain.Application;
import org.innovateuk.ifs.application.repository.ApplicationRepository;
import org.innovateuk.ifs.assessment.domain.Assessment;
import org.innovateuk.ifs.assessment.repository.AssessmentRepository;
import org.innovateuk.ifs.category.repository.CompetitionCategoryLinkRepository;
import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.competition.domain.Competition;
import org.innovateuk.ifs.competition.domain.Milestone;
import org.innovateuk.ifs.competition.repository.CompetitionRepository;
import org.innovateuk.ifs.competition.repository.MilestoneRepository;
import org.innovateuk.ifs.competition.resource.*;
import org.innovateuk.ifs.competition.resource.fixtures.CompetitionCoFundersResourceFixture;
import org.innovateuk.ifs.workflow.domain.ActivityType;
import org.innovateuk.ifs.workflow.repository.ActivityStateRepository;
import org.innovateuk.ifs.workflow.resource.State;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.innovateuk.ifs.application.builder.ApplicationBuilder.newApplication;
import static org.innovateuk.ifs.assessment.builder.AssessmentBuilder.newAssessment;
import static org.junit.Assert.*;

/**
 * Integration test for testing the rest servcies of the competition controller
 */
public class CompetitionControllerIntegrationTest extends BaseControllerIntegrationTest<CompetitionController> {

//    @Autowired
//    private CategoryLinkRepository categoryLinkRepository;

    @Autowired
    private CompetitionCategoryLinkRepository competitionCategoryLinkRepository;

    @Autowired
    private CompetitionRepository competitionRepository;

    @Autowired
    private MilestoneRepository milestoneRepository;

    @Autowired
    private AssessmentRepository assessmentRepository;

    @Autowired
    private ApplicationRepository applicationRepository;

    @Autowired
    private ActivityStateRepository activityStateRepository;

    private static final int EXISTING_CATEGORY_LINK_BEFORE_TEST = 2;
    private static final Long COMPETITION_ID = 1L;

    private static final String COMPETITION_NAME_UPDATED = "Competition name updated";
    private static final int INNOVATION_SECTOR_ID = 1;
    private static final String INNOVATION_SECTOR_NAME = "Health and life sciences";
    private static final int INNOVATION_AREA_ID = 9;
    private static final int INNOVATION_AREA_ID_TWO = 10;
    private static final int INNOVATION_AREA_ID_THREE = 11;
    private static final String INNOVATION_AREA_NAME = "User Experience";
    private static final String INNOVATION_AREA_NAME_TWO = "Emerging Tech and Industries";
    private static final String INNOVATION_AREA_NAME_THREE = "Robotics and AS";
    private static final String EXISTING_COMPETITION_NAME = "Connected digital additive manufacturing";

    private final LocalDateTime now = LocalDateTime.now();
    private final LocalDateTime eightDaysAgo = now.minusDays(8);
    private final LocalDateTime sevenDaysAgo = now.minusDays(7);
    private final LocalDateTime sixDaysAgo = now.minusDays(6);
    private final LocalDateTime fiveDaysAgo = now.minusDays(5);
    private final LocalDateTime fourDaysAgo = now.minusDays(4);
    private final LocalDateTime threeDaysAgo = now.minusDays(3);
    private final LocalDateTime twoDaysAgo = now.minusDays(2);
    private final LocalDateTime oneDayAgo = now.minusDays(1);
    private final LocalDateTime oneDayAhead = now.plusDays(1);
    private final LocalDateTime twoDaysAhead = now.plusDays(2);
    private final LocalDateTime threeDaysAhead = now.plusDays(3);
    private final LocalDateTime fourDaysAhead = now.plusDays(4);
    private final LocalDateTime fiveDaysAhead = now.plusDays(5);
    private final LocalDateTime sixDaysAhead = now.plusDays(6);
    private final LocalDateTime sevenDaysAhead = now.plusDays(7);
    private final LocalDateTime eightDaysAhead = now.plusDays(8);

    @Override
    @Autowired
    protected void setControllerUnderTest(CompetitionController controller) {
        this.controller = controller;
    }

    @Before
    public void setLoggedInUserOnThread() {
        loginCompAdmin();
    }

    @Test
    public void getAllCompetitions() throws Exception {
        List<CompetitionResource> competitions = checkCompetitionCount(2);
        checkExistingCompetition(competitions.get(0));
    }

    @Test
    public void getOneCompetitions() throws Exception {
        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(COMPETITION_ID);
        assertTrue(competitionsResult.isSuccess());
        CompetitionResource competition = competitionsResult.getSuccessObject();

        checkExistingCompetition(competition);
    }


    @Test
    public void createCompetition() throws Exception {
        checkCompetitionCount(2);
        createNewCompetition();

        int expectedCompetitionCount = 3;
        List<CompetitionResource> competitions = checkCompetitionCount(expectedCompetitionCount);

        checkExistingCompetition(competitions.get(0));
        checkNewCompetition(competitions.get(1));
    }

    @Test
    public void competitionCodeGeneration() throws Exception {
        // Setup test data
        checkCompetitionCount(2);
        createNewCompetition();
        createNewCompetition();
        createNewCompetition();
        List<CompetitionResource> competitions = checkCompetitionCount(5);

        // Generate number 1 in this month year combination
        RestResult<String> generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(0).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-1", generatedCode.getSuccessObject());
        flushAndClearSession();


        // Generate number 2 in this month year combination
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(1).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-2", generatedCode.getSuccessObject());
        flushAndClearSession();

        // Generate number 3 in this month year combination
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(2).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-3", generatedCode.getSuccessObject());

        // if generated twice the first code should not be updated.
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2020, 11, 11, 12, 00), competitions.get(2).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-3", generatedCode.getSuccessObject());

    }

    private List<CompetitionResource> checkCompetitionCount(int expectedCompetitionCount) {
        RestResult<List<CompetitionResource>> allCompetitionsResult = controller.findAll();
        assertTrue(allCompetitionsResult.isSuccess());
        List<CompetitionResource> competitions = allCompetitionsResult.getSuccessObject();
        assertThat("Checking if the amount of competitions is what we expect.", competitions, hasSize(expectedCompetitionCount));
        return competitions;
    }

    @Test
    public void updateCompetition() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        assertEquals(COMPETITION_NAME_UPDATED, savedCompetition.getName());
    }

    @Test
    public void updateCompetitionCategories() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = Collections.singleton(Long.valueOf(INNOVATION_AREA_ID));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        checkUpdatedCompetitionCategories(savedCompetition);
    }

    @Test
    public void updateCompetitionCategories_multipleInnovationAreas() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(
                Long.valueOf(INNOVATION_AREA_ID),
                Long.valueOf(INNOVATION_AREA_ID_TWO),
                Long.valueOf(INNOVATION_AREA_ID_THREE)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();

        assertThat(savedCompetition.getInnovationAreas(), hasItems(Long.valueOf(INNOVATION_AREA_ID), Long.valueOf(INNOVATION_AREA_ID_TWO), Long.valueOf(INNOVATION_AREA_ID_THREE)));
        assertEquals(3, savedCompetition.getInnovationAreas().size());
        assertThat(savedCompetition.getInnovationAreaNames(), hasItems(INNOVATION_AREA_NAME, INNOVATION_AREA_NAME_TWO, INNOVATION_AREA_NAME_THREE));
        assertEquals(3, savedCompetition.getInnovationAreaNames().size());
    }

    @Test
    public void closeAssessment() throws Exception {
        RestResult<Void> closeResult = controller.closeAssessment(COMPETITION_ID);
        assertTrue("Assert close assessment is success", closeResult.isSuccess());
        RestResult<CompetitionResource> getResult = controller.getCompetitionById(COMPETITION_ID);
        assertTrue("Assert get is success", getResult.isSuccess());
        CompetitionResource retrievedCompetition = getResult.getSuccessObject();
        retrievedCompetition.getCompetitionStatus();
    }


    @Test
    public void updateCompetitionCoFunders() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);

        //With one co-funder
        competition.setFunders(CompetitionCoFundersResourceFixture.getNewTestCoFundersResouces(1, competition.getId()));
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        checkCompetitionCount(3);
        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        assertEquals(1, savedCompetition.getFunders().size());

        // Now re-insert with 2 co-funders
        competition.setFunders(CompetitionCoFundersResourceFixture.getNewTestCoFundersResouces(2, competition.getId()));
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        savedCompetition = saveResult.getSuccessObject();
        // we should expect in total two co-funders.
        assertEquals(2, savedCompetition.getFunders().size());
    }

    @Test
    public void competitionCategorySaving() throws Exception {
        checkCompetitionCount(2);
        // Create new competition
        CompetitionResource competition = createNewCompetition();
        checkCompetitionCount(3);
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST, competitionCategoryLinkRepository.count());


        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        // Check if the categorylink is only stored once.
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 2, competitionCategoryLinkRepository.count());

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        checkUpdatedCompetitionCategories(savedCompetition);

        // check that the link is not duplicated
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 2, competitionCategoryLinkRepository.count());

        // check that the link is removed
        competition.setInnovationSector(null);
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 1, competitionCategoryLinkRepository.count());

        // check that the link is updated (or removed and added)
        Set<Long> areaIds2 = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID_TWO)));
        competition.setInnovationAreas(areaIds2);
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 1, competitionCategoryLinkRepository.count());

        checkCompetitionCount(3);
    }

    @Test
    public void competitionCompletedSections() throws Exception {
        Long competitionId = 7L;
        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);

        assertEquals(0, competitionsResult.getSuccessObject().getSectionSetupStatus().size());
    }

    @Test
    public void competitionCompleteSection() throws Exception {
        Long competitionId = 7L;

        controller.markSectionComplete(competitionId, CompetitionSetupSection.INITIAL_DETAILS);

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);
        assertEquals(Boolean.TRUE, competitionsResult.getSuccessObject().getSectionSetupStatus().get(CompetitionSetupSection.INITIAL_DETAILS));
    }

    @Test
    public void competitionInCompleteSection() throws Exception {
        Long competitionId = 7L;

        controller.markSectionInComplete(competitionId, CompetitionSetupSection.INITIAL_DETAILS);

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);
        assertEquals(Boolean.FALSE, competitionsResult.getSuccessObject().getSectionSetupStatus().get(CompetitionSetupSection.INITIAL_DETAILS));
    }

    @Test
    public void markAsSetup() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        controller.markAsSetup(competition.getId()).getSuccessObject();

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competition.getId());
        competitionsResult.getSuccessObject().getCompetitionStatus().equals(CompetitionStatus.READY_TO_OPEN);
    }

    @Test
    @Ignore("Ignored to investigate failures on Bamboo")
    public void notifyAssessors() throws Exception {
        CompetitionResource closedCompetition = createWithDates(twoDaysAgo, oneDayAgo, twoDaysAhead, threeDaysAhead, fourDaysAhead, fiveDaysAhead, sixDaysAhead, sevenDaysAhead);
        List<Long> assessmentIds = createCreatedAssessmentsWithCompetition(closedCompetition.getId(), 2);
        RestResult<Void> notifyResult = controller.notifyAssessors(closedCompetition.getId());
        assertTrue("Notify assessors is a success", notifyResult.isSuccess());
        RestResult<CompetitionResource> getResult = controller.getCompetitionById(closedCompetition.getId());
        assertTrue("Assert get is success", getResult.isSuccess());
        CompetitionResource retrievedCompetition = getResult.getSuccessObject();<commit_msg>Adding ignored test back in to test that rollback of different test fixes erroneous test ordering issues<commit_after>package org.innovateuk.ifs.competition.controller;

import com.google.common.collect.Sets;
import org.innovateuk.ifs.BaseControllerIntegrationTest;
import org.innovateuk.ifs.application.domain.Application;
import org.innovateuk.ifs.application.repository.ApplicationRepository;
import org.innovateuk.ifs.assessment.domain.Assessment;
import org.innovateuk.ifs.assessment.repository.AssessmentRepository;
import org.innovateuk.ifs.category.repository.CompetitionCategoryLinkRepository;
import org.innovateuk.ifs.commons.rest.RestResult;
import org.innovateuk.ifs.competition.domain.Competition;
import org.innovateuk.ifs.competition.domain.Milestone;
import org.innovateuk.ifs.competition.repository.CompetitionRepository;
import org.innovateuk.ifs.competition.repository.MilestoneRepository;
import org.innovateuk.ifs.competition.resource.*;
import org.innovateuk.ifs.competition.resource.fixtures.CompetitionCoFundersResourceFixture;
import org.innovateuk.ifs.workflow.domain.ActivityType;
import org.innovateuk.ifs.workflow.repository.ActivityStateRepository;
import org.innovateuk.ifs.workflow.resource.State;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.innovateuk.ifs.application.builder.ApplicationBuilder.newApplication;
import static org.innovateuk.ifs.assessment.builder.AssessmentBuilder.newAssessment;
import static org.junit.Assert.*;

/**
 * Integration test for testing the rest servcies of the competition controller
 */
public class CompetitionControllerIntegrationTest extends BaseControllerIntegrationTest<CompetitionController> {

//    @Autowired
//    private CategoryLinkRepository categoryLinkRepository;

    @Autowired
    private CompetitionCategoryLinkRepository competitionCategoryLinkRepository;

    @Autowired
    private CompetitionRepository competitionRepository;

    @Autowired
    private MilestoneRepository milestoneRepository;

    @Autowired
    private AssessmentRepository assessmentRepository;

    @Autowired
    private ApplicationRepository applicationRepository;

    @Autowired
    private ActivityStateRepository activityStateRepository;

    private static final int EXISTING_CATEGORY_LINK_BEFORE_TEST = 2;
    private static final Long COMPETITION_ID = 1L;

    private static final String COMPETITION_NAME_UPDATED = "Competition name updated";
    private static final int INNOVATION_SECTOR_ID = 1;
    private static final String INNOVATION_SECTOR_NAME = "Health and life sciences";
    private static final int INNOVATION_AREA_ID = 9;
    private static final int INNOVATION_AREA_ID_TWO = 10;
    private static final int INNOVATION_AREA_ID_THREE = 11;
    private static final String INNOVATION_AREA_NAME = "User Experience";
    private static final String INNOVATION_AREA_NAME_TWO = "Emerging Tech and Industries";
    private static final String INNOVATION_AREA_NAME_THREE = "Robotics and AS";
    private static final String EXISTING_COMPETITION_NAME = "Connected digital additive manufacturing";

    private final LocalDateTime now = LocalDateTime.now();
    private final LocalDateTime eightDaysAgo = now.minusDays(8);
    private final LocalDateTime sevenDaysAgo = now.minusDays(7);
    private final LocalDateTime sixDaysAgo = now.minusDays(6);
    private final LocalDateTime fiveDaysAgo = now.minusDays(5);
    private final LocalDateTime fourDaysAgo = now.minusDays(4);
    private final LocalDateTime threeDaysAgo = now.minusDays(3);
    private final LocalDateTime twoDaysAgo = now.minusDays(2);
    private final LocalDateTime oneDayAgo = now.minusDays(1);
    private final LocalDateTime oneDayAhead = now.plusDays(1);
    private final LocalDateTime twoDaysAhead = now.plusDays(2);
    private final LocalDateTime threeDaysAhead = now.plusDays(3);
    private final LocalDateTime fourDaysAhead = now.plusDays(4);
    private final LocalDateTime fiveDaysAhead = now.plusDays(5);
    private final LocalDateTime sixDaysAhead = now.plusDays(6);
    private final LocalDateTime sevenDaysAhead = now.plusDays(7);
    private final LocalDateTime eightDaysAhead = now.plusDays(8);

    @Override
    @Autowired
    protected void setControllerUnderTest(CompetitionController controller) {
        this.controller = controller;
    }

    @Before
    public void setLoggedInUserOnThread() {
        loginCompAdmin();
    }

    @Test
    public void getAllCompetitions() throws Exception {
        List<CompetitionResource> competitions = checkCompetitionCount(2);
        checkExistingCompetition(competitions.get(0));
    }

    @Test
    public void getOneCompetitions() throws Exception {
        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(COMPETITION_ID);
        assertTrue(competitionsResult.isSuccess());
        CompetitionResource competition = competitionsResult.getSuccessObject();

        checkExistingCompetition(competition);
    }


    @Test
    public void createCompetition() throws Exception {
        checkCompetitionCount(2);
        createNewCompetition();

        int expectedCompetitionCount = 3;
        List<CompetitionResource> competitions = checkCompetitionCount(expectedCompetitionCount);

        checkExistingCompetition(competitions.get(0));
        checkNewCompetition(competitions.get(1));
    }

    @Test
    public void competitionCodeGeneration() throws Exception {
        // Setup test data
        checkCompetitionCount(2);
        createNewCompetition();
        createNewCompetition();
        createNewCompetition();
        List<CompetitionResource> competitions = checkCompetitionCount(5);

        // Generate number 1 in this month year combination
        RestResult<String> generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(0).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-1", generatedCode.getSuccessObject());
        flushAndClearSession();


        // Generate number 2 in this month year combination
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(1).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-2", generatedCode.getSuccessObject());
        flushAndClearSession();

        // Generate number 3 in this month year combination
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2016, 6, 5, 12, 00), competitions.get(2).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-3", generatedCode.getSuccessObject());

        // if generated twice the first code should not be updated.
        generatedCode = controller.generateCompetitionCode(LocalDateTime.of(2020, 11, 11, 12, 00), competitions.get(2).getId());
        assertTrue(generatedCode.isSuccess());
        assertEquals("1606-3", generatedCode.getSuccessObject());

    }

    private List<CompetitionResource> checkCompetitionCount(int expectedCompetitionCount) {
        RestResult<List<CompetitionResource>> allCompetitionsResult = controller.findAll();
        assertTrue(allCompetitionsResult.isSuccess());
        List<CompetitionResource> competitions = allCompetitionsResult.getSuccessObject();
        assertThat("Checking if the amount of competitions is what we expect.", competitions, hasSize(expectedCompetitionCount));
        return competitions;
    }

    @Test
    public void updateCompetition() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        assertEquals(COMPETITION_NAME_UPDATED, savedCompetition.getName());
    }

    @Test
    public void updateCompetitionCategories() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = Collections.singleton(Long.valueOf(INNOVATION_AREA_ID));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        checkUpdatedCompetitionCategories(savedCompetition);
    }

    @Test
    public void updateCompetitionCategories_multipleInnovationAreas() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(
                Long.valueOf(INNOVATION_AREA_ID),
                Long.valueOf(INNOVATION_AREA_ID_TWO),
                Long.valueOf(INNOVATION_AREA_ID_THREE)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());

        checkCompetitionCount(3);

        CompetitionResource savedCompetition = saveResult.getSuccessObject();

        assertThat(savedCompetition.getInnovationAreas(), hasItems(Long.valueOf(INNOVATION_AREA_ID), Long.valueOf(INNOVATION_AREA_ID_TWO), Long.valueOf(INNOVATION_AREA_ID_THREE)));
        assertEquals(3, savedCompetition.getInnovationAreas().size());
        assertThat(savedCompetition.getInnovationAreaNames(), hasItems(INNOVATION_AREA_NAME, INNOVATION_AREA_NAME_TWO, INNOVATION_AREA_NAME_THREE));
        assertEquals(3, savedCompetition.getInnovationAreaNames().size());
    }

    @Test
    public void closeAssessment() throws Exception {
        RestResult<Void> closeResult = controller.closeAssessment(COMPETITION_ID);
        assertTrue("Assert close assessment is success", closeResult.isSuccess());
        RestResult<CompetitionResource> getResult = controller.getCompetitionById(COMPETITION_ID);
        assertTrue("Assert get is success", getResult.isSuccess());
        CompetitionResource retrievedCompetition = getResult.getSuccessObject();
        retrievedCompetition.getCompetitionStatus();
    }


    @Test
    public void updateCompetitionCoFunders() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);

        //With one co-funder
        competition.setFunders(CompetitionCoFundersResourceFixture.getNewTestCoFundersResouces(1, competition.getId()));
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        checkCompetitionCount(3);
        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        assertEquals(1, savedCompetition.getFunders().size());

        // Now re-insert with 2 co-funders
        competition.setFunders(CompetitionCoFundersResourceFixture.getNewTestCoFundersResouces(2, competition.getId()));
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        savedCompetition = saveResult.getSuccessObject();
        // we should expect in total two co-funders.
        assertEquals(2, savedCompetition.getFunders().size());
    }

    @Test
    public void competitionCategorySaving() throws Exception {
        checkCompetitionCount(2);
        // Create new competition
        CompetitionResource competition = createNewCompetition();
        checkCompetitionCount(3);
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST, competitionCategoryLinkRepository.count());


        // Update competition
        competition.setName(COMPETITION_NAME_UPDATED);
        Long sectorId = Long.valueOf(INNOVATION_SECTOR_ID);
        Set<Long> areaIds = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID)));
        competition.setInnovationSector(sectorId);
        competition.setInnovationAreas(areaIds);
        // Check if the categorylink is only stored once.
        RestResult<CompetitionResource> saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 2, competitionCategoryLinkRepository.count());

        CompetitionResource savedCompetition = saveResult.getSuccessObject();
        checkUpdatedCompetitionCategories(savedCompetition);

        // check that the link is not duplicated
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 2, competitionCategoryLinkRepository.count());

        // check that the link is removed
        competition.setInnovationSector(null);
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 1, competitionCategoryLinkRepository.count());

        // check that the link is updated (or removed and added)
        Set<Long> areaIds2 = new HashSet<>(Arrays.asList(Long.valueOf(INNOVATION_AREA_ID_TWO)));
        competition.setInnovationAreas(areaIds2);
        saveResult = controller.saveCompetition(competition, competition.getId());
        assertTrue("Assert save is success", saveResult.isSuccess());
        assertEquals(EXISTING_CATEGORY_LINK_BEFORE_TEST + 1, competitionCategoryLinkRepository.count());

        checkCompetitionCount(3);
    }

    @Test
    public void competitionCompletedSections() throws Exception {
        Long competitionId = 7L;
        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);

        assertEquals(0, competitionsResult.getSuccessObject().getSectionSetupStatus().size());
    }

    @Test
    public void competitionCompleteSection() throws Exception {
        Long competitionId = 7L;

        controller.markSectionComplete(competitionId, CompetitionSetupSection.INITIAL_DETAILS);

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);
        assertEquals(Boolean.TRUE, competitionsResult.getSuccessObject().getSectionSetupStatus().get(CompetitionSetupSection.INITIAL_DETAILS));
    }

    @Test
    public void competitionInCompleteSection() throws Exception {
        Long competitionId = 7L;

        controller.markSectionInComplete(competitionId, CompetitionSetupSection.INITIAL_DETAILS);

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competitionId);
        assertEquals(Boolean.FALSE, competitionsResult.getSuccessObject().getSectionSetupStatus().get(CompetitionSetupSection.INITIAL_DETAILS));
    }

    @Test
    public void markAsSetup() throws Exception {
        checkCompetitionCount(2);

        // Create new competition
        CompetitionResource competition = createNewCompetition();

        checkCompetitionCount(3);

        controller.markAsSetup(competition.getId()).getSuccessObject();

        RestResult<CompetitionResource> competitionsResult = controller.getCompetitionById(competition.getId());
        competitionsResult.getSuccessObject().getCompetitionStatus().equals(CompetitionStatus.READY_TO_OPEN);
    }

    @Test
    public void notifyAssessors() throws Exception {
        CompetitionResource closedCompetition = createWithDates(twoDaysAgo, oneDayAgo, twoDaysAhead, threeDaysAhead, fourDaysAhead, fiveDaysAhead, sixDaysAhead, sevenDaysAhead);
        List<Long> assessmentIds = createCreatedAssessmentsWithCompetition(closedCompetition.getId(), 2);
        RestResult<Void> notifyResult = controller.notifyAssessors(closedCompetition.getId());
        assertTrue("Notify assessors is a success", notifyResult.isSuccess());
        RestResult<CompetitionResource> getResult = controller.getCompetitionById(closedCompetition.getId());
        assertTrue("Assert get is success", getResult.isSuccess());
        CompetitionResource retrievedCompetition = getResult.getSuccessObject();
{'fraction_non_alphanumeric': 0.08460057479282346, 'fraction_numerical': 0.007589497474817936, 'mean_word_length': 4.358050530722081, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, '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': '4236331', 'n_tokens_mistral': 9771, 'n_tokens_neox': 9201, 'n_words': 1809}