package com.nokia.sms;

import com.nokia.sms.vo.MultiNumberSms;
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.context.annotation.Import;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Stream;

@Slf4j
@SpringBootTest
@Import(TestConfig.class)
class ApiTests {
    @Autowired
    private ThreadPoolExecutor requestExecutor;

    @Test
    void test() {
        String url = "http://192.168.70.125:12081/api/sms/sendByPhoneNumbers";
        MultiNumberSms dto = new MultiNumberSms();
        dto.setFromSystem("test");
        dto.setPhoneNumbers(Stream.of("13231899751", "13020841628").toArray(String[]::new));
        dto.setContent("这是一条比较长的测试短信，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十，一二三四五六七八九十。");
        dto.setSmsType("test");
        dto.setInternalId("000001");
        CompletableFuture.allOf(Stream.iterate(1, n -> n + 1).limit(100).map(t ->
                CompletableFuture.runAsync(() -> {
                    try {
                        log.info(">>>>>>>>>start {}", t);
                        RestTemplate template = new RestTemplate();
                        ResponseEntity<Object> r = template.postForEntity(url, dto, Object.class);
                        log.info(">>>>>>>>>end {} -> {}", t, r.getStatusCode());
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }, requestExecutor)).toArray(CompletableFuture[]::new)).join();
    }
}
