package com.ruoyi.ccdi.project.controller;

import com.ruoyi.ccdi.project.service.ICcdiProjectService;
import io.swagger.v3.oas.annotations.Operation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith(MockitoExtension.class)
class CcdiProjectControllerTest {

    @InjectMocks
    private CcdiProjectController controller;

    @Mock
    private ICcdiProjectService projectService;

    @Test
    void shouldArchiveProject() throws Exception {
        Method method = CcdiProjectController.class.getMethod("archiveProject", Long.class);
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class);
        Operation operation = method.getAnnotation(Operation.class);

        assertNotNull(postMapping);
        assertEquals("/{projectId}/archive", postMapping.value()[0]);
        assertNotNull(preAuthorize);
        assertEquals("@ss.hasPermi('ccdi:project:edit')", preAuthorize.value());
        assertNotNull(operation);
        assertEquals("归档项目", operation.summary());
    }

    @Test
    void shouldRestoreProject() throws Exception {
        Method method = CcdiProjectController.class.getMethod("restoreProject", Long.class);
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class);
        Operation operation = method.getAnnotation(Operation.class);

        assertNotNull(postMapping);
        assertEquals("/{projectId}/restore", postMapping.value()[0]);
        assertNotNull(preAuthorize);
        assertEquals("@ss.hasPermi('ccdi:project:edit')", preAuthorize.value());
        assertNotNull(operation);
        assertEquals("恢复项目", operation.summary());
    }

    @Test
    void shouldAllowProjectListUsersToReachBusinessDeleteCheck() throws Exception {
        Method method = CcdiProjectController.class.getMethod("deleteProject", Long.class);
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class);
        Operation operation = method.getAnnotation(Operation.class);

        assertNotNull(deleteMapping);
        assertEquals("/{projectId}", deleteMapping.value()[0]);
        assertNotNull(preAuthorize);
        assertEquals("@ss.hasPermi('ccdi:project:list')", preAuthorize.value());
        assertNotNull(operation);
        assertEquals("删除项目", operation.summary());
    }
}
