<commit_before>package com.worth.ifs.competition.security;

import com.worth.ifs.BaseServiceSecurityTest;
import com.worth.ifs.commons.service.ServiceResult;
import com.worth.ifs.competition.resource.CompetitionResource;
import com.worth.ifs.competition.resource.CompetitionSetupCompletedSectionResource;
import com.worth.ifs.competition.resource.CompetitionSetupSectionResource;
import com.worth.ifs.competition.resource.CompetitionTypeResource;
import com.worth.ifs.competition.transactional.CompetitionSetupService;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static com.worth.ifs.user.builder.RoleResourceBuilder.newRoleResource;
import static com.worth.ifs.user.builder.UserResourceBuilder.newUserResource;
import static com.worth.ifs.user.resource.UserRoleType.COMP_ADMIN;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.MockitoAnnotations.initMocks;

/**
 * Testing the permission rules applied to the secured methods in OrganisationService.  This set of tests tests for the
 * individual rules that are called whenever an OrganisationService method is called.  They do not however test the logic
 * within those rules
 */
public class CompetitionSetupServiceSecurityTest extends BaseServiceSecurityTest<CompetitionSetupService> {

    private CompetitionPermissionRules rules;

    @Before
    public void lookupPermissionRules() {

        rules = getMockPermissionRulesBean(CompetitionPermissionRules.class);

        initMocks(this);
    }

    @Override
    protected Class<? extends CompetitionSetupService> getServiceClass() {
        return TestCompetitionSetupService.class;
    }

    @Test
    public void testAllAccessDenied() {
        setLoggedInUser(null);
        assertAccessDenied(() -> service.findAllCompetitionSections(), () -> {
            verifyNoMoreInteractions(rules);
        });
        Long competitionId = 2L;
        assertAccessDenied(() -> service.findAllCompetitionSectionsStatuses(competitionId), () -> {
            verifyNoMoreInteractions(rules);
        });

        assertAccessDenied(() -> service.create(), () -> {
            verifyNoMoreInteractions(rules);
        });
        Long sectionId = 3L;
        assertAccessDenied(() -> service.markSectionComplete(competitionId, sectionId), () -> {
            verifyNoMoreInteractions(rules);
        });
        assertAccessDenied(() -> service.markSectionInComplete(competitionId, sectionId), () -> {
            verifyNoMoreInteractions(rules);
        });
        assertAccessDenied(() -> service.findAllTypes(), () -> {
            verifyNoMoreInteractions(rules);
        });
    }

    @Test
    public void testAllAccessAllowed() {
        setLoggedInUser(newUserResource().withRolesGlobal(singletonList(newRoleResource().withType(COMP_ADMIN).build())).build());

        service.findAllCompetitionSections();
        service.findAllTypes();
        Long competitionId = 2L;
        service.findAllCompetitionSectionsStatuses(competitionId);
        service.create();
        Long sectionId = 3L;
        service.markSectionComplete(competitionId, sectionId);
        service.markSectionInComplete(competitionId, sectionId);
    }

    @Test
    public void findAllCompetitionSectionsStatuses() {
        setLoggedInUser(null);

    }

    /**
     * Dummy implementation (for satisfying Spring Security's need to read parameter information from
     * methods, which is lost when using mocks)
     */
    public static class TestCompetitionSetupService implements CompetitionSetupService {

        @Override
        public ServiceResult<List<CompetitionSetupCompletedSectionResource>> findAllCompetitionSectionsStatuses(Long competitionId) {
            return null;
        }

        @Override
        public ServiceResult<List<CompetitionSetupSectionResource>> findAllCompetitionSections() {
            return null;
        }

        @Override
        public ServiceResult<CompetitionResource> update(Long id, CompetitionResource competitionResource) {
            return null;
        }

        @Override
        public ServiceResult<CompetitionResource> create() {
            return null;
        }

        @Override
        public ServiceResult<Void> markSectionComplete(Long competitionId, Long sectionId) {
            return null;
        }

        @Override
        public ServiceResult<Void> markSectionInComplete(Long competitionId, Long sectionId) {
            return null;
        }

        @Override
        public ServiceResult<List<CompetitionTypeResource>> findAllTypes() {
            return null;
        }
    }<commit_msg>INFUND-2984 unit test<commit_after>package com.worth.ifs.competition.security;

import com.worth.ifs.BaseServiceSecurityTest;
import com.worth.ifs.commons.service.ServiceResult;
import com.worth.ifs.competition.resource.CompetitionResource;
import com.worth.ifs.competition.resource.CompetitionSetupCompletedSectionResource;
import com.worth.ifs.competition.resource.CompetitionSetupSectionResource;
import com.worth.ifs.competition.resource.CompetitionTypeResource;
import com.worth.ifs.competition.transactional.CompetitionSetupService;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.List;

import static com.worth.ifs.user.builder.RoleResourceBuilder.newRoleResource;
import static com.worth.ifs.user.builder.UserResourceBuilder.newUserResource;
import static com.worth.ifs.user.resource.UserRoleType.COMP_ADMIN;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.MockitoAnnotations.initMocks;

/**
 * Testing the permission rules applied to the secured methods in OrganisationService.  This set of tests tests for the
 * individual rules that are called whenever an OrganisationService method is called.  They do not however test the logic
 * within those rules
 */
public class CompetitionSetupServiceSecurityTest extends BaseServiceSecurityTest<CompetitionSetupService> {

    private CompetitionPermissionRules rules;

    @Before
    public void lookupPermissionRules() {

        rules = getMockPermissionRulesBean(CompetitionPermissionRules.class);

        initMocks(this);
    }

    @Override
    protected Class<? extends CompetitionSetupService> getServiceClass() {
        return TestCompetitionSetupService.class;
    }

    @Test
    public void testAllAccessDenied() {
        setLoggedInUser(null);
        assertAccessDenied(() -> service.findAllCompetitionSections(), () -> {
            verifyNoMoreInteractions(rules);
        });
        Long competitionId = 2L;
        assertAccessDenied(() -> service.findAllCompetitionSectionsStatuses(competitionId), () -> {
            verifyNoMoreInteractions(rules);
        });

        assertAccessDenied(() -> service.create(), () -> {
            verifyNoMoreInteractions(rules);
        });
        Long sectionId = 3L;
        assertAccessDenied(() -> service.markSectionComplete(competitionId, sectionId), () -> {
            verifyNoMoreInteractions(rules);
        });
        assertAccessDenied(() -> service.markSectionInComplete(competitionId, sectionId), () -> {
            verifyNoMoreInteractions(rules);
        });
        assertAccessDenied(() -> service.findAllTypes(), () -> {
            verifyNoMoreInteractions(rules);
        });
    }

    @Test
    public void testAllAccessAllowed() {
        setLoggedInUser(newUserResource().withRolesGlobal(singletonList(newRoleResource().withType(COMP_ADMIN).build())).build());

        service.findAllCompetitionSections();
        service.findAllTypes();
        Long competitionId = 2L;
        service.findAllCompetitionSectionsStatuses(competitionId);
        service.create();
        Long sectionId = 3L;
        service.markSectionComplete(competitionId, sectionId);
        service.markSectionInComplete(competitionId, sectionId);
    }

    @Test
    public void findAllCompetitionSectionsStatuses() {
        setLoggedInUser(null);

    }

    /**
     * Dummy implementation (for satisfying Spring Security's need to read parameter information from
     * methods, which is lost when using mocks)
     */
    public static class TestCompetitionSetupService implements CompetitionSetupService {

        @Override
        public ServiceResult<List<CompetitionSetupCompletedSectionResource>> findAllCompetitionSectionsStatuses(Long competitionId) {
            return null;
        }

        @Override
        public ServiceResult<List<CompetitionSetupSectionResource>> findAllCompetitionSections() {
            return null;
        }

        @Override
        public ServiceResult<String> generateCompetitionCode(Long id, LocalDateTime dateTime) {
            return null;
        }

        @Override
        public ServiceResult<CompetitionResource> update(Long id, CompetitionResource competitionResource) {
            return null;
        }

        @Override
        public ServiceResult<CompetitionResource> create() {
            return null;
        }

        @Override
        public ServiceResult<Void> markSectionComplete(Long competitionId, Long sectionId) {
            return null;
        }

        @Override
        public ServiceResult<Void> markSectionInComplete(Long competitionId, Long sectionId) {
            return null;
        }

        @Override
        public ServiceResult<List<CompetitionTypeResource>> findAllTypes() {
            return null;
        }
    }
{'fraction_non_alphanumeric': 0.08565153733528551, 'fraction_numerical': 0.0012549675800041832, 'mean_word_length': 3.323236889692586, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 52, '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': '3322191', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2355, 'n_words': 481}