<commit_before>package com.imcode.imcms.controller.core;

import com.imcode.imcms.api.DocumentLanguageDisabledException;
import com.imcode.imcms.components.datainitializer.LanguageDataInitializer;
import com.imcode.imcms.components.datainitializer.TextDocumentDataInitializer;
import com.imcode.imcms.config.TestConfig;
import com.imcode.imcms.domain.dto.LanguageDTO;
import com.imcode.imcms.domain.dto.TextDocumentDTO;
import com.imcode.imcms.domain.service.CommonContentService;
import com.imcode.imcms.model.CommonContent;
import com.imcode.imcms.model.Roles;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.repository.MetaRepository;
import imcode.server.Imcms;
import imcode.server.LanguageMapper;
import imcode.server.user.UserDomainObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static imcode.server.ImcmsConstants.VIEW_DOC_PATH;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
public class ViewDocumentControllerTest {

    private static final String VIEW_DOC = VIEW_DOC_PATH + "/";

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private LanguageDataInitializer languageDataInitializer;

    @Autowired
    private TextDocumentDataInitializer documentDataInitializer;

    @Autowired
    private CommonContentService commonContentService;

    @Autowired
    private MetaRepository metaRepository;

    private List<LanguageDTO> languages;
    private TextDocumentDTO textDocument;

    @Before
    public void setUp() {
        final UserDomainObject user = new UserDomainObject(1);
        user.setLanguageIso639_2("eng");
        user.addRoleId(Roles.SUPER_ADMIN.getId());
        Imcms.setUser(user);

        this.languages = languageDataInitializer.createData();
        this.textDocument = documentDataInitializer.createTextDocument();
    }

    // 0 -> en
    // 1 -> sv

    @Test
    public void getDocument_WhenEnLanguageSetAndUserHasAllEnabledLanguages_Expect_ReturnedEnDocument()
            throws Exception {

        testWhenAllUsersLanguagesIsEnabled(0);
    }

    @Test
    public void getDocument_WhenSvLanguageSetAndUserHasAllEnabledLanguages_Expect_ReturnedSvDocument()
            throws Exception {

        testWhenAllUsersLanguagesIsEnabled(1);
    }

    @Test(expected = DocumentLanguageDisabledException.class)
    public void getDocument_WhenEnLanguageSetAndUserHasAllDisabledLanguages_Expect_DisabledException()
            throws Throwable {

        testWhenAllUsersLanguagesIsDisabled(0);
    }

    @Test(expected = DocumentLanguageDisabledException.class)
    public void getDocument_WhenSvLanguageSetAndUserHasAllDisabledLanguages_Expect_DisabledException()
            throws Throwable {

        testWhenAllUsersLanguagesIsDisabled(1);
    }

    @Test
    public void getDocument_WhenEnLanguageSetAndUserHasEnabledOnlyEnLanguage_Expect_ReturnedEnDocument()
            throws Exception {

        testWhenAllUsersHaveSpecifiedLanguageCorrespondingToRequested(0);
    }

    @Test
    public void getDocument_WhenSvLanguageSetAndUserHasEnabledOnlySvLanguage_Expect_ReturnedSvDocument()
            throws Exception {

        testWhenAllUsersHaveSpecifiedLanguageCorrespondingToRequested(1);
    }

    @Test
    public void getDocument_whenEnLanguageSetAndUserDoesNotHaveThisLanguage_Expect_ReturnedDocumentInDefaultLanguage()
            throws Exception {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionSHOW_IN_DEFAULT_LANGUAGESet(0);

    }

    @Test
    public void getDocument_whenSvLanguageSetAndUserDoesNotHaveThisLanguage_Expect_ReturnedDocumentInDefaultLanguage()
            throws Exception {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionSHOW_IN_DEFAULT_LANGUAGESet(1);
    }

    @Test(expected = DocumentLanguageDisabledException.class)
    public void getDocument_whenEnLanguageSetAndUserDoesNotHaveThisLanguage_Expect_DisabledException()
            throws Throwable {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(0);

    }

    @Test(expected = DocumentLanguageDisabledException.class)
    public void getDocument_whenSvLanguageSetAndUserDoesNotHaveThisLanguage_Expect_DisabledException()
            throws Throwable {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(1);
    }

    private void testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(int languageIndex)
            throws Throwable {

        final LanguageDTO language = languages.get(languageIndex);
        Imcms.setLanguage(language);

        final Integer docId = textDocument.getId();

        final Meta metaDoc = metaRepository.getOne(docId);
        metaDoc.setDisabledLanguageShowMode(Meta.DisabledLanguageShowMode.DO_NOT_SHOW);
        metaRepository.save(metaDoc);

        final List<CommonContent> commonContents = textDocument.getCommonContents();
<commit_msg>IMCMS-336 New design to super admin page: profiles tab - Update test from junit4 to junit5<commit_after>package com.imcode.imcms.controller.core;

import com.imcode.imcms.WebAppSpringTestConfig;
import com.imcode.imcms.api.DocumentLanguageDisabledException;
import com.imcode.imcms.components.datainitializer.LanguageDataInitializer;
import com.imcode.imcms.components.datainitializer.TextDocumentDataInitializer;
import com.imcode.imcms.domain.dto.LanguageDTO;
import com.imcode.imcms.domain.dto.TextDocumentDTO;
import com.imcode.imcms.domain.service.CommonContentService;
import com.imcode.imcms.model.CommonContent;
import com.imcode.imcms.model.Roles;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.repository.MetaRepository;
import imcode.server.Imcms;
import imcode.server.LanguageMapper;
import imcode.server.user.UserDomainObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static imcode.server.ImcmsConstants.VIEW_DOC_PATH;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@Transactional
public class ViewDocumentControllerTest extends WebAppSpringTestConfig {

    private static final String VIEW_DOC = VIEW_DOC_PATH + "/";

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private LanguageDataInitializer languageDataInitializer;

    @Autowired
    private TextDocumentDataInitializer documentDataInitializer;

    @Autowired
    private CommonContentService commonContentService;

    @Autowired
    private MetaRepository metaRepository;

    private List<LanguageDTO> languages;
    private TextDocumentDTO textDocument;

    @BeforeEach
    public void setUp() {
        final UserDomainObject user = new UserDomainObject(1);
        user.setLanguageIso639_2("eng");
        user.addRoleId(Roles.SUPER_ADMIN.getId());
        Imcms.setUser(user);

        this.languages = languageDataInitializer.createData();
        this.textDocument = documentDataInitializer.createTextDocument();
    }

    // 0 -> en
    // 1 -> sv

    @Test
    public void getDocument_WhenEnLanguageSetAndUserHasAllEnabledLanguages_Expect_ReturnedEnDocument()
            throws Exception {

        testWhenAllUsersLanguagesIsEnabled(0);
    }

    @Test
    public void getDocument_WhenSvLanguageSetAndUserHasAllEnabledLanguages_Expect_ReturnedSvDocument()
            throws Exception {

        testWhenAllUsersLanguagesIsEnabled(1);
    }

    @Test
    public void getDocument_WhenEnLanguageSetAndUserHasAllDisabledLanguages_Expect_DisabledException() {

        assertThrows(DocumentLanguageDisabledException.class,
                () -> testWhenAllUsersLanguagesIsDisabled(0));
    }

    @Test
    public void getDocument_WhenSvLanguageSetAndUserHasAllDisabledLanguages_Expect_DisabledException() {

        assertThrows(DocumentLanguageDisabledException.class,
                () -> testWhenAllUsersLanguagesIsDisabled(1));
    }

    @Test
    public void getDocument_WhenEnLanguageSetAndUserHasEnabledOnlyEnLanguage_Expect_ReturnedEnDocument()
            throws Exception {

        testWhenAllUsersHaveSpecifiedLanguageCorrespondingToRequested(0);
    }

    @Test
    public void getDocument_WhenSvLanguageSetAndUserHasEnabledOnlySvLanguage_Expect_ReturnedSvDocument()
            throws Exception {

        testWhenAllUsersHaveSpecifiedLanguageCorrespondingToRequested(1);
    }

    @Test
    public void getDocument_whenEnLanguageSetAndUserDoesNotHaveThisLanguage_Expect_ReturnedDocumentInDefaultLanguage()
            throws Exception {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionSHOW_IN_DEFAULT_LANGUAGESet(0);

    }

    @Test
    public void getDocument_whenSvLanguageSetAndUserDoesNotHaveThisLanguage_Expect_ReturnedDocumentInDefaultLanguage()
            throws Exception {

        testWhenUserDoesNotHaveSpecificLanguageAndOptionSHOW_IN_DEFAULT_LANGUAGESet(1);
    }

    @Test
    public void getDocument_whenEnLanguageSetAndUserDoesNotHaveThisLanguage_Expect_DisabledException() {

        assertThrows(DocumentLanguageDisabledException.class,
                () -> testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(0));
    }

    @Test
    public void getDocument_whenSvLanguageSetAndUserDoesNotHaveThisLanguage_Expect_DisabledException() {

        assertThrows(DocumentLanguageDisabledException.class,
                () -> testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(1));
    }

    private void testWhenUserDoesNotHaveSpecificLanguageAndOptionDO_NOT_SHOWSet(int languageIndex)
            throws Throwable {

        final LanguageDTO language = languages.get(languageIndex);
        Imcms.setLanguage(language);

        final Integer docId = textDocument.getId();

        final Meta metaDoc = metaRepository.getOne(docId);
        metaDoc.setDisabledLanguageShowMode(Meta.DisabledLanguageShowMode.DO_NOT_SHOW);
        metaRepository.save(metaDoc);

        final List<CommonContent> commonContents = textDocument.getCommonContents();

{'fraction_non_alphanumeric': 0.06603945926196565, 'fraction_numerical': 0.0038363171355498722, 'mean_word_length': 4.6554752066115705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '7153639', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2947, 'n_words': 435}