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

import com.imcode.imcms.components.datainitializer.CategoryDataInitializer;
import com.imcode.imcms.components.datainitializer.DocumentDataInitializer;
import com.imcode.imcms.components.datainitializer.UserDataInitializer;
import com.imcode.imcms.components.datainitializer.VersionDataInitializer;
import com.imcode.imcms.config.TestConfig;
import com.imcode.imcms.controller.AbstractControllerTest;
import com.imcode.imcms.domain.dto.*;
import com.imcode.imcms.domain.exception.DocumentNotExistException;
import com.imcode.imcms.domain.service.*;
import com.imcode.imcms.mapping.jpa.User;
import com.imcode.imcms.model.Role;
import com.imcode.imcms.model.TextDocumentTemplate;
import com.imcode.imcms.persistence.entity.DocumentUrlJPA;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.entity.Meta.Permission;
import com.imcode.imcms.persistence.entity.Version;
import com.imcode.imcms.persistence.repository.DocumentUrlRepository;
import imcode.server.Imcms;
import imcode.server.document.NoPermissionToEditDocumentException;
import imcode.server.user.RoleId;
import imcode.server.user.UserDomainObject;
import org.apache.commons.lang3.NotImplementedException;
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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.imcode.imcms.persistence.entity.Meta.DisabledLanguageShowMode.DO_NOT_SHOW;
import static com.imcode.imcms.persistence.entity.Meta.DisabledLanguageShowMode.SHOW_IN_DEFAULT_LANGUAGE;
import static org.junit.Assert.*;

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

    private TextDocumentDTO createdTextDoc;
    private FileDocumentDTO createdFileDoc;

    @Autowired
    private DocumentDataInitializer documentDataInitializer;

    @Autowired
    private UserDataInitializer userDataInitializer;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TextDocumentTemplateService textDocumentTemplateService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private CommonContentService commonContentService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DocumentService<DocumentDTO> documentService;

    @Autowired
    private DocumentService<UrlDocumentDTO> urlDocumentService;

    @Autowired
    private DocumentUrlRepository documentUrlRepository;

    @Autowired
    private VersionDataInitializer versionDataInitializer;

    @Override
    protected String controllerPath() {
        return "/documents";
    }

    @Before
    public void setUp() throws Exception {
        createdTextDoc = documentDataInitializer.createTextDocument();
        createdFileDoc = documentDataInitializer.createFileDocument();

        final UserDomainObject user = new UserDomainObject(1);
        user.addRoleId(RoleId.SUPERADMIN);
        Imcms.setUser(user); // means current user is admin now
    }

    @Test
    public void getUrlDocument_When_DocIdIsNull_Expect_DefaultUrlDocumentIsReturned() throws Exception {
        final Meta.DocumentType documentType = Meta.DocumentType.URL;

        final MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(controllerPath())
                .param("type", documentType.toString());

        final String response = getJsonResponse(requestBuilder);

        final UrlDocumentDTO urlDocumentDTO = fromJson(response, UrlDocumentDTO.class);

        final DocumentUrlDTO actualDocumentUrlDTO = urlDocumentDTO.getDocumentUrlDTO();
        final DocumentUrlDTO expectedDocumentUrlDTO = UrlDocumentDTO.createEmpty(documentService.createEmpty()).getDocumentUrlDTO();

        assertNull(urlDocumentDTO.getId());
        assertEquals(urlDocumentDTO.getType(), documentType);
        assertFalse(urlDocumentDTO.getCommonContents().isEmpty());
        assertEquals(urlDocumentDTO.getCommonContents(), commonContentService.createCommonContents());
        assertEquals(urlDocumentDTO.getPublicationStatus(), Meta.PublicationStatus.NEW);
        assertEquals(expectedDocumentUrlDTO, actualDocumentUrlDTO);
    }

    @Test
    public void getUrlDocument_When_DocumentExists_Expect_Returned() throws Exception {
        final UrlDocumentDTO empty = urlDocumentService.createEmpty();
        empty.setDocumentUrlDTO(null);

        final int savedId = urlDocumentService.save(empty);

        final DocumentUrlJPA documentUrlJPA = new DocumentUrlJPA();
        documentUrlJPA.setUrlFrameName("test");
        documentUrlJPA.setUrl("test");
        documentUrlJPA.setUrlLanguagePrefix("t");
        documentUrlJPA.setUrlTarget("test");
        documentUrlJPA.setUrlText("test");

        final Version version = versionDataInitializer.createData(0, savedId);
        documentUrlJPA.setVersion(version);
<commit_msg>IMCMS-233 - Apply new UI to the admin panel and editors: - Fixed naming.<commit_after>package com.imcode.imcms.controller.api;

import com.imcode.imcms.components.datainitializer.CategoryDataInitializer;
import com.imcode.imcms.components.datainitializer.DocumentDataInitializer;
import com.imcode.imcms.components.datainitializer.UserDataInitializer;
import com.imcode.imcms.components.datainitializer.VersionDataInitializer;
import com.imcode.imcms.config.TestConfig;
import com.imcode.imcms.controller.AbstractControllerTest;
import com.imcode.imcms.domain.dto.*;
import com.imcode.imcms.domain.exception.DocumentNotExistException;
import com.imcode.imcms.domain.service.*;
import com.imcode.imcms.mapping.jpa.User;
import com.imcode.imcms.model.Role;
import com.imcode.imcms.model.TextDocumentTemplate;
import com.imcode.imcms.persistence.entity.DocumentUrlJPA;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.entity.Meta.Permission;
import com.imcode.imcms.persistence.entity.Version;
import com.imcode.imcms.persistence.repository.DocumentUrlRepository;
import imcode.server.Imcms;
import imcode.server.document.NoPermissionToEditDocumentException;
import imcode.server.user.RoleId;
import imcode.server.user.UserDomainObject;
import org.apache.commons.lang3.NotImplementedException;
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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.imcode.imcms.persistence.entity.Meta.DisabledLanguageShowMode.DO_NOT_SHOW;
import static com.imcode.imcms.persistence.entity.Meta.DisabledLanguageShowMode.SHOW_IN_DEFAULT_LANGUAGE;
import static org.junit.Assert.*;

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

    private TextDocumentDTO createdTextDoc;
    private FileDocumentDTO createdFileDoc;

    @Autowired
    private DocumentDataInitializer documentDataInitializer;

    @Autowired
    private UserDataInitializer userDataInitializer;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TextDocumentTemplateService textDocumentTemplateService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private CommonContentService commonContentService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DocumentService<DocumentDTO> documentService;

    @Autowired
    private DocumentService<UrlDocumentDTO> urlDocumentService;

    @Autowired
    private DocumentUrlRepository documentUrlRepository;

    @Autowired
    private VersionDataInitializer versionDataInitializer;

    @Override
    protected String controllerPath() {
        return "/documents";
    }

    @Before
    public void setUp() throws Exception {
        createdTextDoc = documentDataInitializer.createTextDocument();
        createdFileDoc = documentDataInitializer.createFileDocument();

        final UserDomainObject user = new UserDomainObject(1);
        user.addRoleId(RoleId.SUPERADMIN);
        Imcms.setUser(user); // means current user is admin now
    }

    @Test
    public void getUrlDocument_When_DocIdIsNull_Expect_DefaultUrlDocumentIsReturned() throws Exception {
        final Meta.DocumentType documentType = Meta.DocumentType.URL;

        final MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(controllerPath())
                .param("type", documentType.toString());

        final String response = getJsonResponse(requestBuilder);

        final UrlDocumentDTO urlDocumentDTO = fromJson(response, UrlDocumentDTO.class);

        final DocumentUrlDTO actualDocumentUrlDTO = urlDocumentDTO.getDocumentURL();
        final DocumentUrlDTO expectedDocumentUrlDTO = UrlDocumentDTO.createEmpty(documentService.createEmpty()).getDocumentURL();

        assertNull(urlDocumentDTO.getId());
        assertEquals(urlDocumentDTO.getType(), documentType);
        assertFalse(urlDocumentDTO.getCommonContents().isEmpty());
        assertEquals(urlDocumentDTO.getCommonContents(), commonContentService.createCommonContents());
        assertEquals(urlDocumentDTO.getPublicationStatus(), Meta.PublicationStatus.NEW);
        assertEquals(expectedDocumentUrlDTO, actualDocumentUrlDTO);
    }

    @Test
    public void getUrlDocument_When_DocumentExists_Expect_Returned() throws Exception {
        final UrlDocumentDTO empty = urlDocumentService.createEmpty();
        empty.setDocumentURL(null);

        final int savedId = urlDocumentService.save(empty);

        final DocumentUrlJPA documentUrlJPA = new DocumentUrlJPA();
        documentUrlJPA.setUrlFrameName("test");
        documentUrlJPA.setUrl("test");
        documentUrlJPA.setUrlLanguagePrefix("t");
        documentUrlJPA.setUrlTarget("test");
        documentUrlJPA.setUrlText("test");

        final Version version = versionDataInitializer.createData(0, savedId);
        documentUrlJPA.setVersion(version);

{'fraction_non_alphanumeric': 0.0815807921486155, 'fraction_numerical': 0.0013144058885383807, 'mean_word_length': 5.182556879739979, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '141242', 'n_tokens_mistral': 3051, 'n_tokens_neox': 3069, 'n_words': 515}