package com.agileboot.integrationTest.db;

import com.agileboot.integrationTest.IntegrationTestApplication;
import com.agileboot.domain.system.menu.db.SysMenuEntity;
import com.agileboot.domain.system.menu.db.SysMenuService;
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = IntegrationTestApplication.class)
@RunWith(SpringRunner.class)
class SysMenuServiceImplTest {

    private static final int COMMON_ROLE_COLLABORATION_MENU_COUNT = 7;

    @Resource
    SysMenuService menuService;

    @Test
    @Rollback
    void testGetMenuListByUserId() {
        List<SysMenuEntity> registerUserMenus = menuService.getMenuListByUserId(2L);

        Assertions.assertEquals(COMMON_ROLE_COLLABORATION_MENU_COUNT, registerUserMenus.size());
        Assertions.assertTrue(registerUserMenus.stream().allMatch(menu -> menu.getMenuId() >= 3000L));
    }

    @Test
    @Rollback
    void testGetMenuIdsByRoleId() {
        List<Long> registerUserMenuIds = menuService.getMenuIdsByRoleId(2L);

        Assertions.assertEquals(COMMON_ROLE_COLLABORATION_MENU_COUNT, registerUserMenuIds.size());
        Assertions.assertTrue(registerUserMenuIds.contains(3000L));
        Assertions.assertTrue(registerUserMenuIds.contains(3006L));
    }

    @Test
    @Rollback
    void testIsMenuNameDuplicated() {
        boolean addWithSame = menuService.isMenuNameDuplicated("用户管理", null, 1L);
        boolean updateWithSame = menuService.isMenuNameDuplicated("用户管理", 5L, 1L);
        boolean addWithoutSame = menuService.isMenuNameDuplicated("用户管理", null, 2L);

        Assertions.assertTrue(addWithSame);
        Assertions.assertFalse(updateWithSame);
        Assertions.assertFalse(addWithoutSame);
    }

    @Test
    @Rollback
    void testHasChildrenMenus() {
        boolean hasChildrenMenu = menuService.hasChildrenMenu(5L);
        boolean hasNotChildrenMenu = menuService.hasChildrenMenu(20L);

        Assertions.assertTrue(hasChildrenMenu);
        Assertions.assertFalse(hasNotChildrenMenu);
    }

    @Test
    @Rollback
    void testIsMenuAssignToRole() {
        boolean isAssignToRole = menuService.isMenuAssignToRoles(3000L);
        boolean isNotAssignToRole = menuService.isMenuAssignToRoles(1L);

        Assertions.assertFalse(isNotAssignToRole);
        Assertions.assertTrue(isAssignToRole);
    }



}
