package com.zsc.bill.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.bill.domain.AuthorityBuilder;
import com.zsc.bill.exception.ConstraintException;
import com.zsc.bill.modules.system.dto.AuthorityDto;
import com.zsc.bill.modules.system.entity.Authority;
import com.zsc.bill.modules.system.repo.AuthorityRepository;
import com.zsc.bill.modules.system.service.AuthorityService;
import jakarta.annotation.Resource;
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.boot.test.context.SpringBootTest;

import java.util.List;

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

@SpringBootTest
public class AuthorityServiceTest {
    @Resource
    private AuthorityRepository repo;
    @Autowired
    private AuthorityService service;



    private Authority aut1;
    private Authority aut2;

    @BeforeEach
    void setUp() {
        aut1 = AuthorityBuilder.aAuthority().name("TEST_AUTHORITY_ONE").build();
        repo.insert(aut1);
        aut2 = AuthorityBuilder.aAuthority().name("TEST_AUTHORITY_TWO").build();
        repo.insert(aut2);
    }

    @Test
    void list() {
        LambdaQueryWrapper<Authority> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(2, service.list(queryWrapper.like(Authority::getName, "TEST_AUTHORITY")).size());
        assertEquals(1, service.list(queryWrapper.eq(Authority::getName, aut1.getName())).size());
        assertEquals(2, service.list().size());
    }

    @Test
    void createAuthority() {
        AuthorityDto dto = new AuthorityDto();
        dto.setName("TEST_AUTHORITY");
        dto.setEnabled(true);
        dto.setRemark("测试权限增加");
        AuthorityDto dto2 = new AuthorityDto();
        dto2.setName(aut2.getName());
        dto2.setEnabled(aut2.getEnabled());
        dto2.setRemark(aut2.getRemark());
        Authority authority=service.create(dto);
        assertNotNull(authority.getId());
        List<Authority> list = service.list();
        assertEquals(3, list.size());
        // 不能创建其他已存在的同名同代码部门
        assertThrows(ConstraintException.class, () -> service.create(dto2));
    }

    @Test
    void updateAuthority() {
        AuthorityDto dto = new AuthorityDto();
        dto.setName("TEST_AUTHORITY_BBS");
        dto.setRemark("测试权限增加...");
        assertTrue(service.update(dto, aut2.id));
        Authority authority = service.getOne(new LambdaQueryWrapper<Authority>().eq(Authority::getName, dto.getName()));
        assertEquals(authority.getName(), dto.getName());
        assertEquals(authority.getId(), aut2.id);
        // 不能改为其他已存在的同名同代码部门
        assertThrows(ConstraintException.class,
                () -> service.update(new AuthorityDto(aut1.getName(), true,null), aut2.id));
    }

    @AfterEach
    void tearDown() {
        repo.delete(new QueryWrapper<>());
    }
}
