package net.northking.cctp.element.ftp;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.ssh.JschUtil;
import com.alibaba.fastjson.JSON;
import com.jcraft.jsch.Session;
import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.core.annotation.Autowired;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.element.ftp.keywords.FtpTools;
import net.northking.cctp.element.test.AbstractLibraryTest;
import net.northking.cctp.element.test.ParamsBuilder;
import net.northking.cctp.element.test.SampleExecuteContext;
import net.northking.cctp.element.test.junit.LibraryJUnit4ClassRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.testng.collections.Lists;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertTrue;

@RunWith(LibraryJUnit4ClassRunner.class)
public class FtpLibraryTest extends AbstractLibraryTest {

	//字符串小写转换
	@Test
	public void test() {

		try {
			IExecuteContext context = new SampleExecuteContext.Builder().build();
			String fileId = "6866b6d307624ad0896f28c69d8f1e69_2023_06_05_d2bbf1cbe1d14d91be29a88942d9f71e.txt";
			Map<String, String> map = new HashMap<>();
			map.put("fileId", fileId);
			map.put("fileName", "新建文本文档.txt");
			List<Map<String, String>> mapList = Arrays.asList(map);
//			FtpTools.ftpUploadFile("172.16.77.23",22,"root","gzh_0701.",fileId,"/home/test/test1/haha","test.sh",true,context);
			FtpTools.ftpUploadFile("172.16.77.24",22,"root","gzh_0701.",mapList,"/home/test/test1/haha2",true,context);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@Test
	public void test01() {
		String url = "http://192.168.77.23:8000/api/cctp-files/pub/download/6866b6d307624ad0896f28c69d8f1e69_2023_06_05_d2bbf1cbe1d14d91be29a88942d9f71e.txt";
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setConnectTimeout(15000);
		factory.setReadTimeout(15000);
		RestTemplate template = new RestTemplate(factory);
		ResponseEntity<byte[]> resultEntity = null;
		try {
			resultEntity = template.getForEntity(url, byte[].class);
		} catch (HttpClientErrorException e) {
			if (HttpStatus.NOT_FOUND == e.getStatusCode()) {
				throw new ExecuteException("下载失败", e);
			}
			throw new ExecuteException("下载失败", e);
		}
		System.out.println(resultEntity);
	}
}