package io.wdd.server.controller;

import io.wdd.common.response.R;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.server.beans.vo.ProjectServerVO;
import io.wdd.server.coreService.CoreProjectService;
import io.wdd.server.coreService.CoreServerService;
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 java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@SpringBootTest
public class ProjectServerTest {

    @Resource
    ProjectServerController projectServerController;

    @Resource
    CoreProjectService coreProjectService;

    @Resource
    CoreServerService coreServerService;


    @Test
    public void test_create_valid_input_success() {

        List<ProjectInfoPO> projectInfoPOS = coreProjectService.projectGetAll();
        ProjectInfoPO projectInfoPO = projectInfoPOS
                .stream()
                .skip(new Random().nextInt(projectInfoPOS.size()))
                .findFirst()
                .get();

        List<ServerInfoPO> serverInfoVOList = coreServerService.serverGetAll();
        List<Long> serverIdList = serverInfoVOList
                .stream()
                .skip(new Random().nextInt(serverInfoVOList.size()))
                .map(
                        serverInfoPO -> serverInfoPO.getServerId()
                )
                .collect(Collectors.toList());

        serverIdList
                .stream()
                .forEach(
                        serverId -> {
                            R<String> result = projectServerController.projectServerCreate(
                                    projectInfoPO.getProjectId(),
                                    serverId
                            );
                            assertEquals(
                                    "创建Project-Server成功！",
                                    result.getData()
                            );
                        }
                );


    }

    @Test
    public void test_query_all_project_servers() {
        R<List<ProjectServerVO>> result = projectServerController.projectServerAll();
        assertNotNull(result.getData());
    }


    @Test
    public void test_query_nonexistent_project_server_error() {
        Long nonexistentProjectId = -1L;
        R<ProjectServerVO> result = projectServerController.projectServerOne(nonexistentProjectId);
        assertEquals(
                null,
                result.getData()
        );
    }

    @Test
    public void test_create_invalid_project_id_error() {
        Long invalidProjectId = -1L;
        List<ServerInfoPO> serverInfoVOList = coreServerService.serverGetAll();
        ServerInfoPO serverInfoPO = serverInfoVOList
                .stream()
                .skip(new Random().nextInt(serverInfoVOList.size()))
                .findFirst()
                .get();

        R<String> result = projectServerController.projectServerCreate(
                invalidProjectId,
                serverInfoPO.getServerId()
        );
        assertEquals(
                "创建Project-Server失败！",
                result.getData()
        );
    }

    @Test
    public void test_create_invalid_server_id_error() {
        List<ProjectInfoPO> projectInfoPOS = coreProjectService.projectGetAll();
        ProjectInfoPO projectInfoPO = projectInfoPOS
                .stream()
                .skip(new Random().nextInt(projectInfoPOS.size()))
                .findFirst()
                .get();

        Long invalidServerId = -1L;
        R<String> result = projectServerController.projectServerCreate(
                projectInfoPO.getProjectId(),
                invalidServerId
        );
        assertEquals(
                "创建Project-Server失败！",
                result.getData()
        );
    }

}
