package io.wdd.server.coreService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.service.ProjectInfoService;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@SpringBootTest
public class TestCoreProjectService {

    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private CoreProjectService projectService;

    @Test
    public void testProjectQueryByEntityWithNullEntity() {
        Page<ProjectInfoPO> result = projectService.projectQueryByEntity(null);
        assertNull(result);
    }

    @Test
    public void testProjectQueryByEntityWithNullProjectNameAndProjectNamespace() {
        ProjectQueryEntity entity = new ProjectQueryEntity();
        Page<ProjectInfoPO> result = projectService.projectQueryByEntity(entity);
        assertNull(result);
    }

    @SneakyThrows
    @Test
    public void testProjectQueryByEntityWithNullPageNumberAndPageSize() {
        ProjectQueryEntity entity = new ProjectQueryEntity();
//        entity.setProjectName("");
        entity.setProjectNamespace("wdd");
        Page<ProjectInfoPO> result = projectService.projectQueryByEntity(entity);

        String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
        System.out.println(s);
        assertNotNull(result);
        Assertions.assertEquals(1, result.getCurrent());
        Assertions.assertEquals(10, result.getSize());
    }

    @Test
    public void testProjectQueryByEntityWithValidEntity() {
        ProjectQueryEntity entity = new ProjectQueryEntity();
//        entity.setProjectName("Test Project");
        entity.setProjectNamespace("wdd");
        entity.setPageNumber(2);
        entity.setPageSize(20);

        Page<ProjectInfoPO> expectedPage = new Page<>(2, 20);
        when(projectInfoService.getBaseMapper().selectList(any())).thenReturn(new ArrayList<>());

        Page<ProjectInfoPO> result = projectService.projectQueryByEntity(entity);
        assertNotNull(result);
        Assertions.assertEquals(expectedPage.getCurrent(), result.getCurrent());
        Assertions.assertEquals(expectedPage.getSize(), result.getSize());
    }
}
