<commit_before>package com.imcode.imcms.domain.service.api;

import com.imcode.imcms.WebAppSpringTestConfig;
import com.imcode.imcms.components.datainitializer.CategoryDataInitializer;
import com.imcode.imcms.domain.dto.CategoryDTO;
import com.imcode.imcms.domain.dto.CategoryTypeDTO;
import com.imcode.imcms.domain.dto.FileDocumentDTO;
import com.imcode.imcms.domain.dto.TextDocumentDTO;
import com.imcode.imcms.domain.dto.UrlDocumentDTO;
import com.imcode.imcms.domain.exception.DirectoryNotEmptyException;
import com.imcode.imcms.domain.factory.DocumentDtoFactory;
import com.imcode.imcms.domain.service.CategoryService;
import com.imcode.imcms.domain.service.DocumentService;
import com.imcode.imcms.model.Category;
import com.imcode.imcms.model.Roles;
import com.imcode.imcms.persistence.entity.CategoryJPA;
import com.imcode.imcms.persistence.entity.CategoryTypeJPA;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.repository.CategoryRepository;
import com.imcode.imcms.persistence.repository.MetaRepository;
import imcode.server.Imcms;
import imcode.server.user.UserDomainObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @see CategoryService
 */
@Transactional
public class CategoryServiceTest extends WebAppSpringTestConfig {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MetaRepository metaRepository;

    @Autowired
    private DocumentService<TextDocumentDTO> textDocumentService;

    @Autowired
    private DocumentService<UrlDocumentDTO> urlDocumentService;

    @Autowired
    private DocumentService<FileDocumentDTO> fileDocumentService;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private DocumentDtoFactory documentDtoFactory;

    @BeforeEach
    public void setUp() {
        categoryDataInitializer.createData(4);
    }

    @AfterEach
    public void cleanUp() {
        //categoryDataInitializer.cleanRepositories(); todo: really need this ?
    }

    @Test
    public void getAll_Expected_CorrectEntities() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        assertEquals(categoriesDTO.size(), categoryService.getAll().size());
    }

    @Test
    public void getById_When_CategoryExist_Expect_CorrectEntity() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        assertNotNull(firstCategoryDTO);

        final Integer id = firstCategoryDTO.getId();
        assertTrue(categoryService.getById(id).isPresent());
    }

    @Test
    public void save_When_CategoryNotExistBefore_Expect_Saved() {
        final List<CategoryTypeDTO> categoryTypesDTO = categoryDataInitializer.getCategoryTypesAsDTO();
        final CategoryTypeJPA firstCategoryType = new CategoryTypeJPA(categoryTypesDTO.get(0));
        final Category category = new CategoryJPA("name", "dummy", "", firstCategoryType);
        final Category savedCategory = categoryService.save(category);

        assertTrue(categoryService.getAll().contains(savedCategory));
    }

    @Test
    public void save_When_CategoryNameExist_Expect_CorrectException() {
        final List<CategoryTypeDTO> categoryTypesDTO = categoryDataInitializer.getCategoryTypesAsDTO();
        final List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        final CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final CategoryTypeJPA firstCategoryType = new CategoryTypeJPA(categoryTypesDTO.get(0));
        final Category category = new CategoryJPA(firstCategoryDTO.getName(), "dummy", "", firstCategoryType);

        assertThrows(IllegalArgumentException.class, () -> categoryService.save(category));
    }

    @Test
    public void update_When_GetExistCategory_Expected_UpdatedCorrectEntity() {
        final List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        final CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final String otherName = "Other_category_name";
        firstCategoryDTO.setName(otherName);

        Category savedUpdateCategory = categoryService.update(firstCategoryDTO);

        assertTrue(categoryService.getAll().contains(savedUpdateCategory));

        assertNotNull(savedUpdateCategory);
        assertEquals(firstCategoryDTO.getId(), savedUpdateCategory.getId());
    }

    @Test
    public void update_When_CategoryNameExist_Expected_CorrectException() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        final String existName = categoriesDTO.get(1).getName();
        firstCategoryDTO.setName(existName);

        assertThrows(IllegalArgumentException.class, () -> categoryService.update(firstCategoryDTO));
    }

    @Test
    public void update_When_CategoryNameIsEmpty_Expected_CorrectException() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        firstCategoryDTO.setName("");

        assertThrows(RuntimeException.class, () -> categoryService.update(firstCategoryDTO));
    }

    @Test
    public void delete_When_DocumentHasNotCategory_Expect_Deleted() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final Integer firstCategoryId = firstCategoryDTO.getId();

        assertTrue(categoryService.getById(firstCategoryId).isPresent());<commit_msg>IMCMS-335 New design to super admin page: categories tab - Remove clean up and change expected exception<commit_after>package com.imcode.imcms.domain.service.api;

import com.imcode.imcms.WebAppSpringTestConfig;
import com.imcode.imcms.components.datainitializer.CategoryDataInitializer;
import com.imcode.imcms.domain.dto.CategoryDTO;
import com.imcode.imcms.domain.dto.CategoryTypeDTO;
import com.imcode.imcms.domain.dto.FileDocumentDTO;
import com.imcode.imcms.domain.dto.TextDocumentDTO;
import com.imcode.imcms.domain.dto.UrlDocumentDTO;
import com.imcode.imcms.domain.exception.DirectoryNotEmptyException;
import com.imcode.imcms.domain.factory.DocumentDtoFactory;
import com.imcode.imcms.domain.service.CategoryService;
import com.imcode.imcms.domain.service.DocumentService;
import com.imcode.imcms.model.Category;
import com.imcode.imcms.model.Roles;
import com.imcode.imcms.persistence.entity.CategoryJPA;
import com.imcode.imcms.persistence.entity.CategoryTypeJPA;
import com.imcode.imcms.persistence.entity.Meta;
import com.imcode.imcms.persistence.repository.CategoryRepository;
import com.imcode.imcms.persistence.repository.MetaRepository;
import imcode.server.Imcms;
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.dao.DataIntegrityViolationException;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @see CategoryService
 */
@Transactional
public class CategoryServiceTest extends WebAppSpringTestConfig {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MetaRepository metaRepository;

    @Autowired
    private DocumentService<TextDocumentDTO> textDocumentService;

    @Autowired
    private DocumentService<UrlDocumentDTO> urlDocumentService;

    @Autowired
    private DocumentService<FileDocumentDTO> fileDocumentService;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private DocumentDtoFactory documentDtoFactory;

    @BeforeEach
    public void setUp() {
        categoryDataInitializer.createData(4);
    }

    @Test
    public void getAll_Expected_CorrectEntities() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        assertEquals(categoriesDTO.size(), categoryService.getAll().size());
    }

    @Test
    public void getById_When_CategoryExist_Expect_CorrectEntity() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        assertNotNull(firstCategoryDTO);

        final Integer id = firstCategoryDTO.getId();
        assertTrue(categoryService.getById(id).isPresent());
    }

    @Test
    public void save_When_CategoryNotExistBefore_Expect_Saved() {
        final List<CategoryTypeDTO> categoryTypesDTO = categoryDataInitializer.getCategoryTypesAsDTO();
        final CategoryTypeJPA firstCategoryType = new CategoryTypeJPA(categoryTypesDTO.get(0));
        final Category category = new CategoryJPA("name", "dummy", "", firstCategoryType);
        final Category savedCategory = categoryService.save(category);

        assertTrue(categoryService.getAll().contains(savedCategory));
    }

    @Test
    public void save_When_CategoryNameExist_Expect_CorrectException() {
        final List<CategoryTypeDTO> categoryTypesDTO = categoryDataInitializer.getCategoryTypesAsDTO();
        final List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        final CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final CategoryTypeJPA firstCategoryType = new CategoryTypeJPA(categoryTypesDTO.get(0));
        final Category category = new CategoryJPA(firstCategoryDTO.getName(), "dummy", "", firstCategoryType);

        assertThrows(DataIntegrityViolationException.class, () -> categoryService.save(category));
    }

    @Test
    public void update_When_GetExistCategory_Expected_UpdatedCorrectEntity() {
        final List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        final CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final String otherName = "Other_category_name";
        firstCategoryDTO.setName(otherName);

        Category savedUpdateCategory = categoryService.update(firstCategoryDTO);

        assertTrue(categoryService.getAll().contains(savedUpdateCategory));

        assertNotNull(savedUpdateCategory);
        assertEquals(firstCategoryDTO.getId(), savedUpdateCategory.getId());
    }

    @Test
    public void update_When_CategoryNameExist_Expected_CorrectException() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        final String existName = categoriesDTO.get(1).getName();
        firstCategoryDTO.setName(existName);

        assertEquals(existName, firstCategoryDTO.getName());

        assertThrows(DataIntegrityViolationException.class, () -> categoryService.update(firstCategoryDTO));
    }

    @Test
    public void update_When_CategoryNameIsEmpty_Expected_CorrectEntity() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);

        firstCategoryDTO.setName("");

        assertNotNull(categoryService.update(firstCategoryDTO));
    }

    @Test
    public void delete_When_DocumentHasNotCategory_Expect_Deleted() {
        List<CategoryDTO> categoriesDTO = categoryDataInitializer.getCategoriesAsDTO();
        CategoryDTO firstCategoryDTO = categoriesDTO.get(0);
        final Integer firstCategoryId = firstCategoryDTO.getId();

        assertTrue(categoryService.getById(firstCategoryId).isPresent());
{'fraction_non_alphanumeric': 0.08733338784855671, 'fraction_numerical': 0.0018807752064764086, 'mean_word_length': 4.843287147634974, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 32, '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': '420887', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3364, 'n_words': 538}