首页 > 代码库 > JSON,字符串,MAP转换

JSON,字符串,MAP转换

package com.tree.autotest.testcase.IAuditBillDetailService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.datatrees.basisdata.bankbill.service.IAuditBillDetailService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.tree.autotest.BaseJunit4Test;
import com.tree.autotest.commons.CommonUtil_2;
import com.tree.autotest.commons.TestCase;
import com.tree.autotest.jdbc.DBIdConstant;
import com.tree.autotest.jdbc.ManagedSqlUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* Created by lyh on 17/2/23.
*/
public class Test06 extends BaseJunit4Test{
@Autowired
private IAuditBillDetailService iAuditBillDetailService;

private boolean handleOk;
private String insertSql1,insertSql2,insertSql3,insertSql4;
private static String excel;
private static Map allCases;
private Integer testuser;
private Integer billid;
private String spare;

private Map<Short, String> bankMap = Maps.newHashMap();

@PostConstruct
public void init() {
bankMap.put((short)1, "招商银行");
bankMap.put((short)2, "广发银行");
bankMap.put((short)3, "光大银行");
bankMap.put((short)4, "华夏银行");
bankMap.put((short)5, "建设银行");
bankMap.put((short)6, "民生银行");
bankMap.put((short)7, "农业银行");
bankMap.put((short)8, "浦发银行");
bankMap.put((short)9, "兴业银行");
bankMap.put((short)10, "中国银行");
bankMap.put((short)11, "中信银行");
bankMap.put((short)12, "工商银行");
bankMap.put((short)13, "交通银行");
bankMap.put((short)14, "平安银行");
bankMap.put((short)15, "邮政储蓄银行");
bankMap.put((short)16, "广州农商银行");
bankMap.put((short)17, "浙江农信");
bankMap.put((short)18, "上海银行");
bankMap.put((short)19, "广州银行");
bankMap.put((short)20, "北京银行");
bankMap.put((short)21, "南京银行");
bankMap.put((short)22, "徽商银行");
bankMap.put((short)23, "天津银行");
bankMap.put((short)24, "苏州银行");
bankMap.put((short)25, "江苏银行");
bankMap.put((short)26, "宁波银行");
bankMap.put((short)27, "杭州银行");
bankMap.put((short)28, "重庆银行");
bankMap.put((short)29, "长沙银行");
bankMap.put((short)30, "民泰银行");
bankMap.put((short)31, "台州银行");
bankMap.put((short)32, "浙商银行");
}


@Before
public void setUp() throws SQLException {
excel = "src/main/resources/case/IAuditBillDetailService/TestT06.xlsx";
allCases = CommonUtil_2.getAllCases(excel);
}

@Test
public void test_case_1() throws Exception {

//获取用户ID
TestCase testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");
testuser = Integer.valueOf(testcase.param1);
billid = Integer.valueOf(testcase.param2);
spare = testcase.param3;


//获取SQL
insertSql1 = testcase.stepSql;
insertSql2 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-2").stepSql;
insertSql3 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-3").stepSql;
insertSql4 = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-4").stepSql;

//删除,添加数据
handleOk &= deleteData(testuser,spare);
handleOk &= ManagedSqlUtil.insertBySql(insertSql1, DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.insertBySql(insertSql2,DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.insertBySql(insertSql3,DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.insertBySql(insertSql4,DBIdConstant.MySql_Basisdata_ID);
//更新数据
handleOk &=updateBillId(testuser,spare);
// Date date =DateTime.now().toDate();
// DateTime deadline = new DateTime(date).plusMonths(-18);
// DateTime specialDeadline = deadline.dayOfMonth().withMinimumValue();

// System.out.println("DateUtils.getDateTimeNow().toDate()" + specialDeadline);

//预期值列表
List<BankBillDetail_test> excepted_detaillist = get_excepted_details(testuser);

//++++++++++++++实际值+++++++++++++
String actual_value =http://www.mamicode.com/
iAuditBillDetailService.T06(testuser);
System.out.println("actual_value值:" + actual_value);
//字符串解析
String summary = JSON.parseObject(actual_value).getString("Summary");
String Detail = JSON.parseObject(actual_value).getString("Detail");
JSONArray list =JSON.parseArray(Detail);
//断言
Assert.assertEquals("List", summary);
Assert.assertNotNull(list);
for(int i =0;i<list.size();i++) {

String st = list.get(i).toString();//list单个元素转换为字符串
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(st, Map.class);//将字符串转换为Map.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=sdf.parse(map.get("PostDate").toString());//将字符串转换为Date类型
Assert.assertEquals(bankMap.get(excepted_detaillist.get(i).getBankId()), map.get("BankName"));
Assert.assertEquals(excepted_detaillist.get(i).getCardNo(), map.get("CardNo"));
Assert.assertEquals(excepted_detaillist.get(i).getTransTypeLevel1(), map.get("Type"));
Assert.assertEquals(excepted_detaillist.get(i).getDescription(), map.get("Description"));
Assert.assertEquals(excepted_detaillist.get(i).getPostDate().getTime(),date.getTime());
}

}


@After
public void tearDown() throws SQLException {
handleOk &= deleteData(testuser,spare);
}

//定义数据清理
public boolean deleteData(Integer userid,String spare) {
String deleteSql1 = "delete from t_bill where userid="+userid+" ;";
String deleteSql2 = "delete from t_bill_detail where spare1=‘"+spare+"‘;";
String deleteSql3 = "delete from t_bill_extrainfo where userid = "+userid+";";
String deleteSql4 = "delete from t_bill_installment_summary where spare1=‘"+spare+"‘ ;";
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql1, DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql2, DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql3, DBIdConstant.MySql_Basisdata_ID);
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql4, DBIdConstant.MySql_Basisdata_ID);
return handleOk;
}

public Boolean updateBillId(Integer userId,String Spare) throws SQLException{
String searchSql="select Id from t_bill where userid="+userId+";";
ResultSet rs =ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);
List<Integer> list = new ArrayList<Integer>();
while(rs.next()){
list.add(rs.getInt(1));
}
//更新t_bill_detail
String updateSql1="update t_bill_detail set billid="+list.get(0)+" where spare1=‘"+Spare+"‘;";
handleOk &= ManagedSqlUtil.updateBySql(updateSql1,DBIdConstant.MySql_Basisdata_ID);
//更新t_bill_extrainfo
String updateSql2="update t_bill_extrainfo set billid="+list.get(0)+" where userid="+userId+";";
handleOk &= ManagedSqlUtil.updateBySql(updateSql2, DBIdConstant.MySql_Basisdata_ID);
//更新t_bill_installment_summary
String updateSql3="update t_bill_installment_summary set billid="+list.get(0)+" where spare1=‘"+Spare+"‘;";
handleOk &= ManagedSqlUtil.updateBySql(updateSql3, DBIdConstant.MySql_Basisdata_ID);
return handleOk;
}
public List<BankBillDetail_test> get_excepted_details(Integer userid) throws SQLException {
String searchSql = "select BankId,CardNo,Level1TransType,Description,AmountMoney,PostDate from t_bill_detail where level1TransType=‘Level1_Comsumption‘ and TransactionTypeId =8 and billid in (select id from t_bill where userid="+userid+");";
ResultSet rs =ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);
List<BankBillDetail_test> list = new ArrayList<BankBillDetail_test>();
while(rs.next()){
BankBillDetail_test bankBillDetail = new BankBillDetail_test();
bankBillDetail.setBankId(rs.getShort("BankId"));
bankBillDetail.setCardNo(rs.getString("CardNo"));
bankBillDetail.setTransTypeLevel1(rs.getString("Level1TransType"));
bankBillDetail.setDescription(rs.getString("Description"));
bankBillDetail.setAmountManey(rs.getDouble("AmountMoney"));
bankBillDetail.setPostDate(rs.getDate("PostDate"));
list.add(bankBillDetail);
}
return list;
}
}

JSON,字符串,MAP转换