mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 21:26:01 +08:00
修复toMap方法的bug,并加入单元测试代码
This commit is contained in:
@ -4,9 +4,10 @@ import com.google.common.collect.Maps;
|
|||||||
import com.thoughtworks.xstream.XStream;
|
import com.thoughtworks.xstream.XStream;
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
import io.restassured.internal.path.xml.NodeChildrenImpl;
|
import io.restassured.internal.path.xml.NodeChildrenImpl;
|
||||||
|
import io.restassured.internal.path.xml.NodeImpl;
|
||||||
import io.restassured.path.xml.XmlPath;
|
import io.restassured.path.xml.XmlPath;
|
||||||
import io.restassured.path.xml.element.Node;
|
import io.restassured.path.xml.element.Node;
|
||||||
import io.restassured.path.xml.element.NodeChildren;
|
import io.restassured.path.xml.exception.XmlPathException;
|
||||||
import me.chanjar.weixin.common.util.ToStringUtils;
|
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -186,12 +187,23 @@ public abstract class WxPayBaseResult {
|
|||||||
public Map<String, String> toMap() {
|
public Map<String, String> toMap() {
|
||||||
Map<String, String> result = Maps.newHashMap();
|
Map<String, String> result = Maps.newHashMap();
|
||||||
XmlPath xmlPath = new XmlPath(this.xmlString);
|
XmlPath xmlPath = new XmlPath(this.xmlString);
|
||||||
NodeChildren nodeChildren = xmlPath.getNodeChildren("xml");
|
NodeImpl rootNode = null;
|
||||||
Iterator<Node> iterator = nodeChildren.nodeIterator();
|
try {
|
||||||
|
rootNode = xmlPath.get("xml");
|
||||||
|
} catch (XmlPathException e) {
|
||||||
|
throw new RuntimeException("xml数据有问题,请核实!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rootNode == null) {
|
||||||
|
throw new RuntimeException("xml数据有问题,请核实!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<Node> iterator = rootNode.children().nodeIterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Node node = iterator.next();
|
Node node = iterator.next();
|
||||||
result.put(node.name(), node.value());
|
result.put(node.name(), node.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
package me.chanjar.weixin.mp.bean.pay.result;
|
||||||
|
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* Created by Binary Wang on 2017-01-04.
|
||||||
|
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class WxPayBaseResultTest {
|
||||||
|
@Test
|
||||||
|
public void testToMap() throws Exception {
|
||||||
|
WxPayOrderQueryResult result = new WxPayOrderQueryResult();
|
||||||
|
result.setXmlString("<xml>\n" +
|
||||||
|
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
|
||||||
|
" <return_msg><![CDATA[OK]]></return_msg>\n" +
|
||||||
|
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
|
||||||
|
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
|
||||||
|
" <device_info><![CDATA[1000]]></device_info>\n" +
|
||||||
|
" <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
|
||||||
|
" <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>\n" +
|
||||||
|
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
|
||||||
|
" <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>\n" +
|
||||||
|
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
|
||||||
|
" <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
|
||||||
|
" <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
|
||||||
|
" <total_fee>1</total_fee>\n" +
|
||||||
|
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
|
||||||
|
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
|
||||||
|
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
|
||||||
|
" <attach><![CDATA[订单额外描述]]></attach>\n" +
|
||||||
|
" <time_end><![CDATA[20141111170043]]></time_end>\n" +
|
||||||
|
" <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
|
||||||
|
"</xml>");
|
||||||
|
Map<String, String> map = result.toMap();
|
||||||
|
System.out.println(map);
|
||||||
|
|
||||||
|
Assert.assertEquals("SUCCESS", map.get("return_code"));
|
||||||
|
Assert.assertEquals("订单额外描述", map.get("attach"));
|
||||||
|
|
||||||
|
result.setXmlString("");
|
||||||
|
System.out.println(result.toMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user