package api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.model.entity.KeyInfo;
import com.sunyard.chsm.param.KeyInfoQuery;
import com.sunyard.chsm.param.KeyInfoResp;
import com.sunyard.chsm.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author liulu
 * @since 2024/12/17
 */
@Slf4j
public class KeyManageTest extends BaseTest {

    private static Long keyId;

    @BeforeAll
    public static void before() throws Exception {
        KeyInfoQuery query = new KeyInfoQuery();
        byte[] res = restTemplate.postForObject("/key/pageList", JsonUtils.toJsonBytes(query), byte[].class);
        R<Page<KeyInfoResp>> r = JsonUtils.objectMapper()
                .readValue(res, new TypeReference<R<Page<KeyInfoResp>>>() {
                });
        Assertions.assertTrue(r.isSuccess());

        List<KeyInfoResp> records = r.getResult().getRecords();
        Assertions.assertFalse(CollectionUtils.isEmpty(records));
        keyId = records.iterator().next().getKeyId();
    }

    @Test
    public void keyInfoTest(){
        KeyInfo res = execute("/key/info", keyId, KeyInfo.class);
        log.info("keyInfoTest: {}", JsonUtils.toJsonString(res));
    }


}
