package com.jiayue.ipfcst.client.test;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jiayue.ipfcst.client.ClientApplication;
import com.jiayue.ipfcst.client.domain.entity.FileCreateLog;
import com.jiayue.ipfcst.client.utils.Constant;
import com.jiayue.ipfcst.client.utils.UtilTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author bizy
 * @version 3.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ClientApplication.class})// 指定启动类
@Slf4j
public class BaseTest {

	@Test
	public void test() {
		String body = HttpRequest.get("http://localhost:9010/client/getDownLoadFile/" + Constant.proBean.getStationCode()).execute().body();
		JSONObject json = JSONUtil.parseObj(body);
		String code = json.get("code").toString();
		String data = json.get("data").toString();
		if (code.equals("0") && data.length() > 0) {
			JSONArray array = JSONUtil.parseArray(data);
			List<FileCreateLog> list = array.toList(FileCreateLog.class);
			String path = UtilTools.judeDirExists(Constant.proBean.getFtpPath());
			for (FileCreateLog fileCreateLog : list) {
				if (fileCreateLog.getFileDownLoadStatus().equals("否")) {
					String url = "http://localhost:9010/client/getFileById?id=" + fileCreateLog.getId();
					HttpUtil.downloadFile(url, path + fileCreateLog.getFileName());
				}
			}
		}
	}

	@Test
	public void test1() {
		String body = HttpRequest.get("http://192.168.9.100:8082/api/forcast").execute().body();
		JSONObject json = JSONUtil.parseObj(body);
		String forcastUrl = json.get("forcastUrl").toString();
		String stationCode = json.get("stationCode").toString();
		if (forcastUrl.length() > 0 && stationCode.length() > 0) {
			Constant.baseBean.setCloudAddr(forcastUrl);
			Constant.baseBean.setStationCode(stationCode);
		}
	}

	@Test
	public void test2() {
		try {
			String rootPath = System.getProperty("user.dir");
			MavenXpp3Reader reader = new MavenXpp3Reader();
			String myPom = rootPath + File.separator + "pom.xml";
			Model model = reader.read(new FileReader(myPom));
			String version = model.getVersion();//获取版本号
			System.out.println(version);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
	}
}
