Mx2MtTest.java 8.85 KB
Newer Older
1 2 3 4
package com.brilliance;

import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.constants.Mx2MtConstants;
5
import com.brilliance.swift.mx2mt.AbstractMx2MtCreator;
6 7 8
import com.brilliance.swift.vo.SwiftTranslationErrorInfo;
import com.brilliance.swift.vo.SwiftTranslationReport;
import org.apache.commons.io.FileUtils;
9 10 11 12 13
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
14 15
import org.junit.Test;

16
import java.io.BufferedWriter;
17
import java.io.File;
18
import java.io.FileWriter;
19
import java.io.IOException;
20 21 22 23
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
24 25
import java.util.regex.Matcher;
import java.util.regex.Pattern;
26 27 28 29 30

public class Mx2MtTest {

    public void printSwiftTranslationReport(SwiftTranslationReport str) {
        if (str != null) {
31 32 33
            System.out.println("MX Type = " + str.getMxType());
            System.out.println("MT Type = " + str.getMtType());
            System.out.println("Translation Result = " + str.getTranslationResult());
34 35 36 37
            List<SwiftTranslationErrorInfo> errorInfos = str.getErrorInfos();
            if (errorInfos != null && errorInfos.size() > 0) {
                for (int i=0; i<errorInfos.size(); i++) {
                    SwiftTranslationErrorInfo errorInfo = errorInfos.get(i);
38
                    System.out.println(errorInfo.toString());
39 40 41 42 43 44 45 46 47 48 49 50 51 52
                }
            }
            System.out.println(str.getMessage());
        }
    }

    public void test(String source, Map<String, Object> extraMap) {
        File file = FileUtils.toFile(Mx2MtTest.class.getResource(source));
        SwiftTranslationReport str = SwiftTransfer.mx2MtPlus(file, null, extraMap);
        printSwiftTranslationReport(str);
    }

    @Test
    public void test103() {
zhanghou committed
53
        test("/swiftXml/Pacs00800108.xml", null);
54 55 56 57 58 59 60 61 62
    }

    @Test
    public void test103Retn() {
        test("/swiftXml/MxPacs004001_Pacs008.xml", null);
    }

    @Test
    public void test202() {
zhanghou committed
63
        test("/swiftXml/Pacs00900108.xml", null);
64 65 66 67 68 69 70
    }

    @Test
    public void test202Cov() {
        test("/swiftXml/MxPacs00900108_COV.xml", null);
    }

71 72 73 74 75
    @Test
    public void test202Retn() {
        test("/swiftXml/MxPacs004001_Pacs009.xml", null);
    }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    @Test
    public void test900() {
        test("/swiftXml/MxCamt05400108_DEBIT.xml", null);
    }

    @Test
    public void test910() {
        test("/swiftXml/MxCamt05400108_CREDIT.xml", null);
    }

    @Test
    public void test940() {
        test("/swiftXml/MxCamt05300108_940.xml", null);
    }

    @Test
    public void test950() {
        test("/swiftXml/MxCamt05300108_950.xml", null);
    }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    @Test
    public void test942() {
        test("/swiftXml/MXcamt05200108.xml", null);
    }

    @Test
    public void test210() {
        test("/swiftXml/MxCamt05700106.xml", null);
    }

    @Test
    public void test199() {
        test("/swiftXml/MxPacs00200110_199.xml", null);
    }

    @Test
    public void test299() {
        test("/swiftXml/MxPacs00200110_299.xml", null);
    }

    @Test
    public void test192() {
        test("/swiftXml/MxCamt05600108_192.xml", null);
    }

    @Test
    public void test292() {
        test("/swiftXml/MxCamt05600108_292.xml", null);
    }

    @Test
    public void test196() {
        test("/swiftXml/MxCamt02900109_196.xml", null);
    }

    @Test
    public void test296() {
        test("/swiftXml/MxCamt02900109_296.xml", null);
    }

zhanghou committed
136 137 138 139 140
    @Test
    public void test920() {
        test("/swiftXml/MxCamt06000105.xml", null);
    }

141
    @Test
142 143 144
    public void testXmlFilePath() {
        SwiftTranslationReport str = SwiftTransfer.mx2MtPlus("d:/test/MxPacs00800108.xml", true, null, null);
        System.out.println(str.getMessage());
145 146
    }

147 148

    @Test
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    public void testSwiftFile() {
        try {
            File file = FileUtils.toFile(Mx2MtTest.class.getResource("/swiftXml/test.xml"));
            String xml = FileUtils.readFileToString(file);
            String regex = "<!--(.*?)-->";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
            Matcher m = p.matcher(xml);
            String mt = "";
            while (m.find()) {
                String message = m.group(1);
                if (message.indexOf("{4:") > -1) {
                    mt = message;
                }
            }
            mt = mt.trim().replace("^~", Mx2MtConstants.NEW_LINE);
            //去掉首行空格
            String[] mts = mt.split(Mx2MtConstants.NEW_LINE);
            mt = "";
            for (int i=0; i<mts.length; i++) {
                mt += mts[i].trim();
                if (i < (mts.length-1)) {
                    mt += Mx2MtConstants.NEW_LINE;
                }
            }
            System.out.println(mt);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

    @Test
    public void test() {
        String tagName = "445566/TELB/123/HOLD/456456/PHOB/1123/HOLD/789/UDLC/yyy/HOLD/xxx/TELB/qqq";
        String regex = "([/(UDLC|PHOB|TELB)/]{1}[0-9a-zA-Z/\\-\\?:\\(\\)\\.,'\\+]*)";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(tagName);
        while (m.find()){
            String str = m.group(1);
            //System.out.println(str);
        }
        tagName = "123/UDLC/123/PHOB/456";
        String[] strs = tagName.split("(?=/UDLC/|/TELB/|/PHOB/)");
        System.out.println(strs[0]);

        String tmpPath = "Document/FIToFICstmrCdtTrf/CdtTrfTxInf/ChrgsInf(1)/Amt".replaceAll("\\([0-9]+\\)", "");
        System.out.println(tmpPath);

        System.out.println("60".matches("50[CK]?"));
    }

    /**
     * 找到xsd中所有节点都是必填的PATH放到map中
     * @param prefixPath
     * @param nodeList
     * @param list
     */
    private void buildPath(String prefixPath, List nodeList, List<String> list) {
        for (int i=0; i<nodeList.size(); i++) {
            Element e = (Element)nodeList.get(i);
            String type = e.attributeValue("type");
            List nodeList1 = e.selectNodes("//xs:simpleType[@name='"+type+"']");
            if ("0".equals(e.attributeValue("minOccurs"))) {
                continue;
            }
            if (nodeList1 != null && nodeList1.size() > 0) {
                list.add(prefixPath + "." + e.attributeValue("name"));
            } else {
                nodeList1 = e.selectNodes("//xs:complexType[@name='"+type+"']");
                Element element = (Element)nodeList1.get(0);
                nodeList1 = element.selectNodes(".//xs:element");
                buildPath(prefixPath + "." + e.attributeValue("name"), nodeList1, list);
            }
        }
    }

    public void readXsd2Txt(String xsdName) throws DocumentException, IOException {
        URL resource = AbstractMx2MtCreator.class.getResource("/xsd/"+xsdName+".xsd");
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(resource);
        Element element = document.getRootElement();
        List<Node> nodeList = element.selectNodes(".//xs:element[@name='Document']");
        element = (Element)nodeList.get(0);
        String type = element.attributeValue("type");
        nodeList = element.selectNodes("//xs:complexType[@name='"+type+"']");
        element = (Element)nodeList.get(0);
        nodeList = element.selectNodes(".//xs:element");
        List<String> list = new ArrayList<>();
        buildPath("Document", nodeList, list);
        File file = new File(System.getProperty("user.dir")+"\\src\\main\\resources\\xmlPathIgnore\\"+xsdName+".txt");
        List<String> orgList = FileUtils.readLines(file);
        orgList.addAll(list);
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        for (String str : orgList) {
            bw.write(str);
            bw.newLine();
        }
        bw.close();
        fw.close();
    }

    @Test
    public void testXsd2Txt() throws DocumentException, IOException {
        List<String> xsdList = new ArrayList<>();
        xsdList.add("camt02900109");
        xsdList.add("camt05200108");
        xsdList.add("camt05300108");
        xsdList.add("camt05400108");
        xsdList.add("camt05600108");
        xsdList.add("camt05700106");
        xsdList.add("camt06000105");
        xsdList.add("pacs00200110");
        xsdList.add("pacs00400109");
        xsdList.add("pacs00800108");
        xsdList.add("pacs00900108");
        for (String xsdName : xsdList) {
            readXsd2Txt(xsdName);
        }
    }
chengzhuoshen committed
268 269 270

    @Test
    public void testMx2MtMap() throws IOException {
271
        File file = FileUtils.toFile(Mx2MtTest.class.getResource("/swiftXml/MxPacs004001_Pacs008.xml"));
chengzhuoshen committed
272 273 274 275 276 277
        String xmlStr = FileUtils.readFileToString(file);
        Map<String, String> maps = SwiftTransfer.mx2MtMap(xmlStr, null);
        for (String key : maps.keySet()) {
            System.out.println(key + "=" + maps.get(key));
        }
    }
278
}