package com.offcn.test;
import io.restassured.response.Response;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.sf.json.JSONObject;

import org.testng.Assert;
import org.testng.annotations.AfterClass;

import com.offcn.TestData.offcn_api_testData;
import com.offcn.TestUnti.Log;
import com.offcn.TestUnti.Mail;
import com.offcn.TestUnti.MapUtil;
import com.offcn.TestUnti.ReadProperties;
import com.offcn.TestUnti.Reflect_api;
import com.offcn.TestUnti.SheetUtils;
import com.offcn.TestUnti.StringUtils;
import com.offcn.TestUnti.XMLread;
import com.offcn.api.nwn.md5.nwngetsign;
import com.offcn.interfaces.API;
import com.offcn.listener.MyTestListener;
import com.offcn.listener.ProcessTestng;
import com.offcn.listener.ResultTestng;
import com.offcn.listener.SkipIInvokedMethodListener;
import com.offcn.process.BasicsGM;
import com.offcn.process.NWN;






import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({MyTestListener.class, SkipIInvokedMethodListener.class })
public class APITest_nwn extends NWN{

	@BeforeClass
	  public void beforeClass() {
			//测试开始删除测试所用的数据
		  if (!isClearMysql) {
			  //System.out.println("start   del **************");
			  this.cleann_n_admin_card_no_List_FromDB_beforeTest();
			  System.out.println("end     del **************");
			  
		  }
		  Log.logInfo("========测试开始========");
		  
	  }
  @Test(dataProvider = "renmai", dataProviderClass = offcn_api_testData.class)
  public void f(HashMap<String, Object> data) {
	  
	  Log.logInfo(data.get("TCNO").toString() + " Step " + data.get("Description").toString() + " is running......");
	  
	
	  Reflect_api Reflect=new Reflect_api();
	  Class<?> c=Reflect.ReflecClass(data);
	  Object object = Reflect.ReflecObj(c);
	  API obj = (API)object;
	  setDelSystemSql(c, object);//保存曾经操作过的系统

	  BasicsGM.map=new XMLread().getSystem();
	  System.out.println(BasicsGM.map);
	  
	  obj.initialize(data);
	  
	  data = obj.handleInput(data);
	  String parameter = MapUtil.getValue("parameter", data);
	  
	  
	//  System.out.println(!data.get("Request").toString().contains("get"));
	//  System.out.println(data.get("system").toString().equals("nwn_service"));
	       
	  if(data.get("system").toString().equals("nwn_service")  ){
		//静态和动态参数自动添加

		  data=setPublicParameter(c,object,data);//静态公共参数
		  data=setDynamicParameter(c,object,data);//动态公共参数
		  
		  parameter = MapUtil.getValue("parameter", data);
	     // System.out.println("parameter=====" + parameter);
		  String serviceUrl = MapUtil.getValue("serviceUrl", data);
		  String request = MapUtil.getValue("Request", data);
		  if (request.equals("get")) {
			  //url后面获取sign值  get请求
			  serviceUrl = serviceUrl + "/sign/" + nwngetsign.nwnGetSign(parameter);
			  System.out.println(nwngetsign.nwnGetSign(parameter));
			  data.put("serviceUrl", serviceUrl);
		}else {
			//url后面获取sign值  post请求
			serviceUrl = serviceUrl + "/sign/" + nwngetsign.nwnPostSign(parameter);
			System.out.println(nwngetsign.nwnPostSign(parameter));
			data.put("serviceUrl", serviceUrl);
		}
		  
	  }
	  
	  Long startTime=System.currentTimeMillis();
	  Response re = obj.SendRequest(data, data.get("serviceUrl").toString(), data.get("Request").toString());
	  Long endTime=System.currentTimeMillis();
	  
	  String time=(endTime-startTime)+"毫秒";
	  
	  String codeORerrcode="";
	  String msgORerrmsy="";
	  String result = "";
	  String body = "";
	  
	  if(re!=null){
		  
		  body=re.asString();
		  if(body.contains("<title>")){
			  int Alength="<title>".length();
			  int start=body.indexOf("<title>");
			  int end=body.indexOf("</title>")+1;
			  body="页面标题："+body.substring(start+Alength, end-1);
			  result=body;
			  if(data.get("Description").toString().contains("流程")){
				  result = obj.handleOutput(re, data);
			  }
		  }else if(body.contains("<html>")){
			  body="返回html页面，状态码："+re.getStatusCode();
			  if(data.get("Description").toString().contains("流程")){
				  result = obj.handleOutput(re, data);
			  }
		  }
		  
	  } 
	  result = obj.handleOutput(re, data);
	  
	  codeORerrcode=getCode(re);
	  msgORerrmsy=getMsg(re);
	  
	  Log.logInfo("返回结果="+StringUtils.decodeUnicode(body));
	 
	  
	  
	/*  //数据回写
	  HashMap<String, Object> ExpectResult=MapUtil.Expect(data);
	  SheetUtils sheet = new SheetUtils("DataAllWff.xls", "Output");
	  sheet.writeExcel(
			  		data.get("NO").toString(),
				  	data.get("TCNO").toString() + "_Step" + data.get("Step").toString(),
				  	data.get("Description").toString(),
				  	parameter, 
//					JSONObject.fromObject(ExpectResult).toString(),
				  	re.getStatusCode()+"",
//					StringUtils.decodeUnicode(re==null?"":re.asString()),
					StringUtils.decodeUnicode(body),
					codeORerrcode,
					msgORerrmsy,
					result,
					time
					);*/
	  
	  if(result.indexOf("Fail")!=-1){
		  String Expect1=data.get("code")==null?"":data.get("code").toString();
		  String Expect2=data.get("msg")==null?"":data.get("msg").toString();
		  String Expect3=data.get("custom")==null?"":data.get("custom").toString();
		  if(body.contains("HTML")){
			  body="异常页面信息";
		  }
		  Assert.assertEquals(StringUtils.decodeUnicode(body),Expect1+","+Expect2+","+Expect3);
	  }else{
		  Assert.assertTrue(true);
	  }
  }

  @AfterClass
  public void afterClass() {
	  Set<Map.Entry<Class<?>,Object>> set=Delsql.entrySet();
	  Iterator<Entry<Class<?>,Object>> it=set.iterator();
	  while(it.hasNext()){
		  Map.Entry<Class<?>,Object> me=it.next();
		  Class<?> c=me.getKey();
		  Object object=me.getValue();
		  this.cleanMysqlData(c,object);
	  }
	  this.CloseMysql();
	  Log.logInfo("========测试结束========");
	  
  }

}
