package io.wdd.server.controller;

import io.wdd.common.response.R;
import io.wdd.server.beans.po.ServerRolePO;
import io.wdd.server.beans.vo.ServerRoleVO;
import net.datafaker.Faker;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

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

@SpringBootTest
public class ServerRoleTest {

    @Resource
    RoleController roleController;

    @Resource
    Faker fakerInstance;


    // Test that roleGetAll() returns a non-empty list of ServerRolePO objects
    @Test
    public void test_roleGetAll_returnsNonEmptyList() {
        List<ServerRolePO> result = roleController
                .roleGetAll()
                .getData();
        assertNotNull(result);
        assertFalse(result.isEmpty());
    }

    // Test that roleCreate() creates a new ServerRolePO object successfully
    @Test
    public void test_roleCreate_createsNewServerRolePO() {
        ServerRoleVO serverRoleVO = new ServerRoleVO();
        // Set properties of serverRoleVO
        serverRoleVO.setRoleName(fakerInstance
                                         .dungeonsAndDragons()
                                         .cities());
        serverRoleVO.setRoleNum(fakerInstance
                                        .number()
                                        .numberBetween(
                                                10,
                                                99
                                        ));

        R<String> result = roleController.roleCreate(serverRoleVO);
        assertEquals(
                "创建角色成功！",
                result.getData()
        );
    }

    // Test that roleUpdate() updates an existing ServerRolePO object successfully
    @Test
    public void test_roleUpdate_updatesExistingServerRolePO() {
        List<ServerRolePO> serverRolePOList = roleController
                .roleGetAll()
                .getData();


        int size = serverRolePOList.size();
        if (!(size > 0)) {
            System.out.println("没有查询到内容！ 跳过");
        }

        // Set properties of serverRolePO
        ServerRolePO serverRolePO = serverRolePOList
                .stream()
                .skip(new Random().nextInt(size))
                .findFirst()
                .get();

        System.out.println("serverRolePO old = " + serverRolePO);
        serverRolePO.setRoleName(fakerInstance
                                         .dungeonsAndDragons()
                                         .cities());

        R<String> result = roleController.roleUpdate(serverRolePO);

        assertEquals(
                "更新角色成功！",
                result.getData()
        );
    }

    // Test that roleCreate() fails when given invalid input
    @Test
    public void test_roleCreate_failsWithInvalidInput() {
        ServerRoleVO serverRoleVO = new ServerRoleVO();
        // Set invalid properties of serverRoleVO

        R<String> result = roleController.roleCreate(serverRoleVO);
        assertEquals(
                "创建角色失败！",
                result.getData()
        );
    }

    // Test that roleUpdate() fails when given invalid input
    @Test
    public void test_roleUpdate_failsWithInvalidInput() {
        ServerRolePO serverRolePO = new ServerRolePO();
        // Set invalid properties of serverRolePO

        R<String> result = roleController.roleUpdate(serverRolePO);
        assertEquals(
                "更新角色失败！",
                result.getData()
        );
    }

    // Test that roleDelete() fails when given invalid input
    @Test
    public void test_roleDelete_failsWithInvalidInput() {
        Long roleId = null; // Set invalid roleId


        R<String> result = roleController.roleDelete(roleId);
        assertEquals(
                "删除角色失败！",
                result.getData()
        );
    }


}
