package com.dsk.test;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.web.client.RestTemplate;

/**
 * @author tanyang
 * @create 2023-08-07 13:30
 **/
@Slf4j
@SpringJUnitConfig
@SpringBootTest
public class JunitTest {


    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void  testApi(){

        String requestBody="[\n" +
                "\t{\n" +
                "\t\t\"distribution\": 1,\n" +
                "\t\t\"params\": {\n" +
                "\t\t\t\"current\": 1,\n" +
                "\t\t\t\"fields\": \"\",\n" +
                "\t\t\t\"orderBy\": \"astId desc\",\n" +
                "\t\t\t\"page\": 9,\n" +
                "\t\t\t\"perpage\": 200\n" +
                "\t\t},\n" +
                "\t\t\"psrType\": \"0301\"\n" +
                "\t}\n" +
                "]";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);


        ResponseEntity<String> responseEntity = restTemplate.exchange("http://25.84.40.150:32100/PSROenter/queryServices/commonQuery", HttpMethod.POST, requestEntity, String.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String responseBody = responseEntity.getBody();
            log.info("获取结果：{}",responseBody);
            // 处理响应结果
//            return responseBody;
        } else {
            // 处理错误情况
            int statusCodeValue = responseEntity.getStatusCodeValue();
            String responseBody = responseEntity.getBody();
            log.info("错误结果{}",responseBody);
//            return "状态吗错误："+ statusCodeValue;
        }

    }

}
