package com.njcn.gather.steady.checksquare.controller;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 数据校验接口契约测试。
 */
class SteadyChecksquareControllerTest {

    @Test
    void shouldExposeChecksquareQueryEndpointInSeparateController() throws Exception {
        RequestMapping requestMapping = SteadyChecksquareController.class.getAnnotation(RequestMapping.class);
        Assertions.assertArrayEquals(new String[]{"/steady/checksquare"}, requestMapping.value());

        Method queryMethod = SteadyChecksquareController.class.getDeclaredMethod("query", com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareHistoryQueryParam.class);
        PostMapping queryMapping = queryMethod.getAnnotation(PostMapping.class);
        Assertions.assertArrayEquals(new String[]{"/query"}, queryMapping.value());

        Method createMethod = SteadyChecksquareController.class.getDeclaredMethod("create", com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam.class);
        PostMapping createMapping = createMethod.getAnnotation(PostMapping.class);
        Assertions.assertArrayEquals(new String[]{"/create"}, createMapping.value());

        Method restartMethod = SteadyChecksquareController.class.getDeclaredMethod("restart", String.class);
        PostMapping restartMapping = restartMethod.getAnnotation(PostMapping.class);
        Assertions.assertArrayEquals(new String[]{"/restart"}, restartMapping.value());

        Method detailMethod = SteadyChecksquareController.class.getDeclaredMethod("detail", String.class);
        GetMapping detailMapping = detailMethod.getAnnotation(GetMapping.class);
        Assertions.assertArrayEquals(new String[]{"/detail"}, detailMapping.value());

        Method itemDetailMethod = SteadyChecksquareController.class.getDeclaredMethod("itemDetail",
                String.class, String.class, String.class, Integer.class, Integer.class);
        GetMapping itemDetailMapping = itemDetailMethod.getAnnotation(GetMapping.class);
        Assertions.assertArrayEquals(new String[]{"/item-detail"}, itemDetailMapping.value());

        Method deleteMethod = SteadyChecksquareController.class.getDeclaredMethod("delete", List.class);
        PostMapping deleteMapping = deleteMethod.getAnnotation(PostMapping.class);
        Assertions.assertArrayEquals(new String[]{"/delete"}, deleteMapping.value());
    }

    @Test
    void shouldKeepCreateResponseAsTaskSummaryWithoutDetailItems() throws Exception {
        Method createMethod = SteadyChecksquareController.class.getDeclaredMethod("create",
                com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam.class);
        ParameterizedType resultType = (ParameterizedType) createMethod.getGenericReturnType();

        Assertions.assertEquals(com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO.class,
                resultType.getActualTypeArguments()[0]);
        Assertions.assertThrows(NoSuchFieldException.class,
                () -> com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO.class.getDeclaredField("items"));
        Assertions.assertThrows(NoSuchFieldException.class,
                () -> com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO.class.getDeclaredField("lineId"));
    }
}
