<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.CategoryTypeDTO;
import com.imcode.imcms.domain.service.CategoryService;
import com.imcode.imcms.domain.service.CategoryTypeService;
import com.imcode.imcms.model.Category;
import com.imcode.imcms.model.CategoryType;
import com.imcode.imcms.persistence.entity.CategoryTypeJPA;
import com.imcode.imcms.persistence.repository.CategoryTypeRepository;
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.List;
import java.util.Optional;

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

@Transactional
public class CategoryTypeServiceTest extends WebAppSpringTestConfig {

    @Autowired
    private CategoryTypeService categoryTypeService;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private CategoryTypeRepository categoryTypeRepository;

    @Autowired
    private CategoryService categoryService;

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

    @AfterEach
    public void clearData() {
        categoryDataInitializer.cleanRepositories();
    }

    @Test
    public void get_When_Exist_Expect_Found() {
        final String testTypeName = "test_type_name" + System.currentTimeMillis();
        final CategoryTypeJPA categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
        );
        final CategoryType saved = new CategoryTypeDTO(categoryTypeRepository.save(categoryType));

        final Optional<CategoryType> oFound = categoryTypeService.get(saved.getId());

        assertTrue(oFound.isPresent());

        final CategoryType found = oFound.get();

        assertEquals(found, saved);
    }

    @Test
    public void getAllExpectedEqualsCategoryTypesAsDtoTest() {
        assertEquals(categoryDataInitializer.getCategoryTypesAsDTO(), categoryTypeService.getAll());
    }

    @Test
    public void save_When_NotExitBefore_Expect_Saved() {
        final String testTypeName = "test_type_name" + System.currentTimeMillis();
        final CategoryType categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
        );
        final CategoryType saved = categoryTypeService.save(categoryType);

        final Optional<CategoryType> oFound = categoryTypeService.get(saved.getId());

        assertTrue(oFound.isPresent());

        final CategoryType found = oFound.get();

        assertEquals(found, saved);
    }

    @Test
    public void update_When_Exist_Expect_CorrectUpdateEntity() {
        final String testTypeName = "test_type_name";
        final CategoryType categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false<commit_msg>IMCMS-335 New design to super admin page: categories tab - Add test for create<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.CategoryTypeDTO;
import com.imcode.imcms.domain.service.CategoryService;
import com.imcode.imcms.domain.service.CategoryTypeService;
import com.imcode.imcms.model.Category;
import com.imcode.imcms.model.CategoryType;
import com.imcode.imcms.persistence.entity.CategoryTypeJPA;
import com.imcode.imcms.persistence.repository.CategoryTypeRepository;
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.List;
import java.util.Optional;

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

@Transactional
public class CategoryTypeServiceTest extends WebAppSpringTestConfig {

    @Autowired
    private CategoryTypeService categoryTypeService;

    @Autowired
    private CategoryDataInitializer categoryDataInitializer;

    @Autowired
    private CategoryTypeRepository categoryTypeRepository;

    @Autowired
    private CategoryService categoryService;

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

    @AfterEach
    public void clearData() {
        categoryDataInitializer.cleanRepositories();
    }

    @Test
    public void get_When_Exist_Expect_Found() {
        final String testTypeName = "test_type_name" + System.currentTimeMillis();
        final CategoryTypeJPA categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
        );
        final CategoryType saved = new CategoryTypeDTO(categoryTypeRepository.save(categoryType));

        final Optional<CategoryType> oFound = categoryTypeService.get(saved.getId());

        assertTrue(oFound.isPresent());

        final CategoryType found = oFound.get();

        assertEquals(found, saved);
    }

    @Test
    public void getAllExpectedEqualsCategoryTypesAsDtoTest() {
        assertEquals(categoryDataInitializer.getCategoryTypesAsDTO(), categoryTypeService.getAll());
    }

    @Test
    public void save_When_NotExitBefore_Expect_Saved() {
        final String testTypeName = "test_type_name" + System.currentTimeMillis();
        final CategoryType categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
        );
        final CategoryType saved = categoryTypeService.save(categoryType);

        final Optional<CategoryType> oFound = categoryTypeService.get(saved.getId());

        assertTrue(oFound.isPresent());

        final CategoryType found = oFound.get();

        assertEquals(found, saved);
    }

    @Test
    public void create_When_CategoryNameExist_Expect_CorrectException() {
        final String testTypeName = "test_type_name";
        final CategoryType categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
        );
        final CategoryType categoryType2 = new CategoryTypeJPA(
                null, testTypeName, 0, true, false
        );
        final CategoryType firstSavedType = categoryTypeService.save(categoryType);

        assertThrows(IllegalArgumentException.class, () -> categoryTypeService.save(categoryType2)); //todo change exception
    }

    @Test
    public void update_When_Exist_Expect_CorrectUpdateEntity() {
        final String testTypeName = "test_type_name";
        final CategoryType categoryType = new CategoryTypeJPA(
                null, testTypeName, 0, false, false
{'fraction_non_alphanumeric': 0.08213876146788991, 'fraction_numerical': 0.0021502293577981653, 'mean_word_length': 3.909922589725545, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '1921741', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1877, 'n_words': 378}