Browse Source

test: 短信用户投诉分析查询接口测试

weijianghai 2 năm trước cách đây
mục cha
commit
fe80f6f9c4

+ 16 - 25
src/main/java/com/nokia/domainb/capability/UsercomplaintQuerys.java

@@ -1,43 +1,34 @@
 package com.nokia.domainb.capability;
 
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
 import java.util.HashMap;
 import java.util.Map;
 
-import cn.chinaunicom.open.common.Const;
-import cn.chinaunicom.open.nlgxptconnection.COMPConnection;
-import cn.chinaunicom.open.nlgxptconnection.COMPConnectionContext;
-import cn.chinaunicom.open.nlgxptconnection.impl.COMPJsonConnection;
-
 /*
  * 短信用户投诉分析查询接口
  * json_chinaUnicom_hb18_complaintManagement_usercomplaintQuerys
  */
 public class UsercomplaintQuerys extends AbstractQuery {
-    // TODO 正式上线后才能获取到正式环境url
-    private static final String url = "";
+    private static final String url = "http://133.96.94.184:47996/ete/v1/rest/openApi/usercomplaintQuerys";
     private static final String urlTest = "http://10.124.150.230:8000/api/chinaUnicom/hb18/complaintManagement/usercomplaintQuerys/v1";
 
-    public COMPConnectionContext query(String mobile, String destAddr, String complaintType, String isMobile,
+    public String query(String mobile, String destAddr, String complaintType, String isMobile,
             String flowNo, String beginTime, String endTime, String token, boolean test) {
-        String realAppSecret = test ? appSecretTest : appSecret;
         String realUrl = test ? urlTest : url;
-        COMPConnection compConnection = new COMPJsonConnection(appId, realAppSecret);
-
         // REQ
         Map<String, Object> req = new HashMap<>();
-
-        Map<String, Object> userComplaintQuerysReq = new HashMap<>();
-        req.put("USERCOMPLAINT_QUERYS_REQ", userComplaintQuerysReq);
-
-        userComplaintQuerysReq.put("mobile", mobile);
-        userComplaintQuerysReq.put("destAddr", destAddr);
-        userComplaintQuerysReq.put("complaintType", complaintType);
-        userComplaintQuerysReq.put("isMobile", isMobile);
-        userComplaintQuerysReq.put("flowNo", flowNo);
-        userComplaintQuerysReq.put("beginTime", beginTime);
-        userComplaintQuerysReq.put("endTime", endTime);
-        userComplaintQuerysReq.put("token", token);
-
-        return compConnection.excute(Const.HttpMethodType.POST, realUrl, req, false);
+        req.put("mobile", mobile);
+        req.put("destAddr", destAddr);
+        req.put("complaintType", complaintType);
+        req.put("isMobile", isMobile);
+        req.put("flowNo", flowNo);
+        req.put("beginTime", beginTime);
+        req.put("endTime", endTime);
+        req.put("token", token);
+        RestTemplate restTemplate = new RestTemplate();
+        ResponseEntity<String> response = restTemplate.postForEntity(realUrl, req, String.class);
+        return response.getBody();
     }
 }

+ 68 - 54
src/main/java/com/nokia/domainb/service/DomainService.java

@@ -160,47 +160,38 @@ public class DomainService {
 
     public R qryowefee(QryowefeeDTO dto) {
         // 用户信用度查询
-        CompletableFuture<R> t1 = CompletableFuture.supplyAsync(() -> {
-            GetEcredit getEcredit = new GetEcredit();
-            String result = getEcredit.query(dto.getPhoneNumber());
-            JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
-            JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
-                    .getAsJsonObject("GET_ECREDIT_RSP")
-                    .getAsJsonObject("RSP");
-            String rspCode = rsp.get("RSP_CODE").getAsString();
-            if (!"0000".equals(rspCode)) {
-                String rspDesc = rsp.get("RSP_DESC").getAsString();
-                log.warn("getEcredit: {} -> {}", dto.getPhoneNumber(), rspDesc);
-                return R.error().message(rspDesc);
-            }
-
-            JsonObject data = rsp.getAsJsonArray("DATA").get(0).getAsJsonObject();
-            JsonObject creditInfo = data.getAsJsonObject("CREDIT_INFO");
-            if (creditInfo == null) {
-                String detail = data.get("detail").getAsString();
-                return R.error().message(detail);
-            }
-
-            QryowefeeVO vo = new QryowefeeVO();
-            JsonElement creditClassElement = creditInfo.get("CREDIT_CLASS");
-            if (creditClassElement != null) {
-                vo.setCreditClass(creditClassElement.getAsString());
+        CompletableFuture<R> t1 = getEcreditCF(dto);
+        // 欠费查询
+        CompletableFuture<R> t2 = qryOweFeeCF(dto);
+        // 整合结果
+        return CompletableFuture.allOf(t1, t2).thenApply(r -> {
+            R r1 = t1.join();
+            if (r1.getSuccess().equals(false)) {
+                return r1;
             }
 
-            JsonElement totalCreditValueElement = creditInfo.get("TOTAL_CREDIT_VALUE");
-            if (totalCreditValueElement != null) {
-                vo.setTotalCreditValue(totalCreditValueElement.getAsString());
+            R r2 = t2.join();
+            if (r2.getSuccess().equals(false)) {
+                return r2;
             }
 
-            JsonElement availableCreditValueElement = creditInfo.get("AVAILABLE_CREDIT_VALUE");
-            if (availableCreditValueElement != null) {
-                vo.setAvailableCreditValue(availableCreditValueElement.getAsString());
-            }
+            QryowefeeVO v1 = (QryowefeeVO) r1.getData();
+            QryowefeeVO v2 = (QryowefeeVO) r2.getData();
+            v1.setPayFee(v2.getPayFee());
+            v1.setBalanceFee(v2.getBalanceFee());
+            v1.setRealFee(v2.getRealFee());
+            return R.ok().data(v1);
+        }).exceptionally(e -> {
+            log.error("欠费接口整合异常: {}", e.getMessage(), e);
+            return R.error().message(e.getMessage());
+        }).join();
+    }
 
-            return R.ok().data(vo);
-        }, qryowefeeExecutor);
-        // 欠费查询
-        CompletableFuture<R> t2 = CompletableFuture.supplyAsync(() -> {
+    /**
+     * 欠费查询
+     */
+    private CompletableFuture<R> qryOweFeeCF(QryowefeeDTO dto) {
+        return CompletableFuture.supplyAsync(() -> {
             // 路由查询
             R route = route(new RouteDTO(dto.getFromSystem(), dto.getPhoneNumber()));
             if (route.getSuccess().equals(false)) {
@@ -251,27 +242,50 @@ public class DomainService {
 
             return R.ok().data(vo);
         }, qryowefeeExecutor);
-        // 整合结果
-        return CompletableFuture.allOf(t1, t2).thenApply(r -> {
-            R r1 = t1.join();
-            if (r1.getSuccess().equals(false)) {
-                return r1;
+    }
+
+    /**
+     * 用户信用度查询
+     */
+    private CompletableFuture<R> getEcreditCF(QryowefeeDTO dto) {
+        return CompletableFuture.supplyAsync(() -> {
+            GetEcredit getEcredit = new GetEcredit();
+            String result = getEcredit.query(dto.getPhoneNumber());
+            JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
+            JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
+                    .getAsJsonObject("GET_ECREDIT_RSP")
+                    .getAsJsonObject("RSP");
+            String rspCode = rsp.get("RSP_CODE").getAsString();
+            if (!"0000".equals(rspCode)) {
+                String rspDesc = rsp.get("RSP_DESC").getAsString();
+                log.warn("getEcredit: {} -> {}", dto.getPhoneNumber(), rspDesc);
+                return R.error().message(rspDesc);
             }
 
-            R r2 = t2.join();
-            if (r2.getSuccess().equals(false)) {
-                return r2;
+            JsonObject data = rsp.getAsJsonArray("DATA").get(0).getAsJsonObject();
+            JsonObject creditInfo = data.getAsJsonObject("CREDIT_INFO");
+            if (creditInfo == null) {
+                String detail = data.get("detail").getAsString();
+                return R.error().message(detail);
             }
 
-            QryowefeeVO v1 = (QryowefeeVO) r1.getData();
-            QryowefeeVO v2 = (QryowefeeVO) r2.getData();
-            v1.setPayFee(v2.getPayFee());
-            v1.setBalanceFee(v2.getBalanceFee());
-            v1.setRealFee(v2.getRealFee());
-            return R.ok().data(v1);
-        }).exceptionally(e -> {
-            log.error("欠费接口整合异常: {}", e.getMessage(), e);
-            return R.error().message(e.getMessage());
-        }).join();
+            QryowefeeVO vo = new QryowefeeVO();
+            JsonElement creditClassElement = creditInfo.get("CREDIT_CLASS");
+            if (creditClassElement != null) {
+                vo.setCreditClass(creditClassElement.getAsString());
+            }
+
+            JsonElement totalCreditValueElement = creditInfo.get("TOTAL_CREDIT_VALUE");
+            if (totalCreditValueElement != null) {
+                vo.setTotalCreditValue(totalCreditValueElement.getAsString());
+            }
+
+            JsonElement availableCreditValueElement = creditInfo.get("AVAILABLE_CREDIT_VALUE");
+            if (availableCreditValueElement != null) {
+                vo.setAvailableCreditValue(availableCreditValueElement.getAsString());
+            }
+
+            return R.ok().data(vo);
+        }, qryowefeeExecutor);
     }
 }

+ 84 - 0
src/main/java/com/nokia/domainb/util/AESUtil.java

@@ -0,0 +1,84 @@
+package com.nokia.domainb.util;
+
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.SecretKeySpec;
+/**
+ *
+ * @author Administrator
+ *
+ */
+public class AESUtil {
+
+    // 加密
+    public static String Encrypt(String sSrc, String sKey) throws Exception {
+        if (sKey == null) {
+            System.out.print("Key为空null");
+            return null;
+        }
+        // 判断Key是否为16位
+        if (sKey.length() != 16) {
+            System.out.print("Key长度不是16位");
+            return null;
+        }
+        byte[] raw = sKey.getBytes("utf-8");
+        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
+        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
+        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
+        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
+
+        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
+    }
+
+    // 解密
+    public static String Decrypt(String sSrc, String sKey) throws Exception {
+        try {
+            // 判断Key是否正确
+            if (sKey == null) {
+                System.out.print("Key为空null");
+                return null;
+            }
+            // 判断Key是否为16位
+            if (sKey.length() != 16) {
+                System.out.print("Key长度不是16位");
+                return null;
+            }
+            byte[] raw = sKey.getBytes("utf-8");
+            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
+            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
+            try {
+                byte[] original = cipher.doFinal(encrypted1);
+                String originalString = new String(original,"utf-8");
+                return originalString;
+            } catch (Exception e) {
+                System.out.println(e.toString());
+                return null;
+            }
+        } catch (Exception ex) {
+            System.out.println(ex.toString());
+            return null;
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        /*
+         * 此处使用AES-128-ECB加密模式,key需要为16位。
+         */
+        String cKey = "qg4uY8nV7fPhvUXt";
+        // 需要加密的字串
+        String cSrc = "20220719101400_test";
+        System.out.println(cSrc);
+        // 加密
+        String enString = AESUtil.Encrypt(cSrc, cKey);
+        System.out.println("加密后的字串是:" + enString);
+
+        // 解密
+        String DeString = AESUtil.Decrypt(enString, cKey);
+        System.out.println("解密后的字串是:" + DeString);
+
+        System.out.println(cSrc.split("_")[0]);
+    }
+}

+ 158 - 11
src/test/java/com/nokia/domainb/capability/UsercomplaintQuerysTest.java

@@ -1,20 +1,167 @@
 package com.nokia.domainb.capability;
 
+import com.alibaba.fastjson2.JSON;
+import com.nokia.domainb.util.AESUtil;
+import de.siegmar.fastcsv.reader.CsvReader;
+import de.siegmar.fastcsv.reader.CsvRow;
+import de.siegmar.fastcsv.writer.CsvWriter;
+import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
+import org.springframework.util.StopWatch;
 
-import cn.chinaunicom.open.nlgxptconnection.COMPConnectionContext;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.Map;
+
+@Slf4j
+class UsercomplaintQuerysTest {
+    public static final String TEST_DIR = "z:/";
+
+    @Test
+    void test() {
+        String d1 = TEST_DIR + "103.csv";
+        String d2 = TEST_DIR + "101.csv";
+        String result1 = TEST_DIR + "无法发送测试结果.csv";
+        String result2 = TEST_DIR + "无法接收测试结果.csv";
+        try (CsvReader r1 = CsvReader.builder().build(Paths.get(d1), StandardCharsets.UTF_8);
+             CsvReader r2 = CsvReader.builder().build(Paths.get(d2), StandardCharsets.UTF_8);
+             OutputStreamWriter osw1 = new OutputStreamWriter(Files.newOutputStream(Paths.get(result1)),
+                     StandardCharsets.UTF_8);
+             CsvWriter w1 = CsvWriter.builder().build(osw1);
+             OutputStreamWriter osw2 = new OutputStreamWriter(Files.newOutputStream(Paths.get(result2)),
+                     StandardCharsets.UTF_8);
+             CsvWriter w2 = CsvWriter.builder().build(osw2);) {
+            w1.writeRow("入参", "时间", "调用时长", "结果");
+            for (CsvRow row : r1) {
+                if (row.getOriginalLineNumber() == 1) {
+                    continue;
+                }
+
+                String mobile = row.getField(1);
+                LocalDateTime beginTimeLocalDateTime = getLocalDateTime(row.getField(2));
+                String beginTime = getTimeString(beginTimeLocalDateTime);
+//                String endTime = "";
+                String endTime = getTimeString(beginTimeLocalDateTime.plusDays(3));
+                String cKey = "qg4uY8nV7fPhvUXt";
+                String cSrc = getTimeString() +  "_" + "test";
+                String token = AESUtil.Encrypt(cSrc, cKey);
+                String complaintType = "1";
+                String isMobile = "";
+                String flowNo = "103";
+                String destAddr = "";
+                Map<String, Object> map = new HashMap<>();
+                map.put("mobile", mobile);
+                map.put("destAddr", destAddr);
+                map.put("complaintType", complaintType);
+                map.put("isMobile", isMobile);
+                map.put("flowNo", flowNo);
+                map.put("beginTime", beginTime);
+                map.put("endTime", endTime);
+                map.put("token", token);
+                UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
+                String result;
+                String now = LocalDateTime.now().toString();
+                StopWatch stopWatch = new StopWatch();
+                stopWatch.start();
+                try {
+                    result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
+                            endTime, token, false);
+                } catch (Exception e) {
+                    result = e.getMessage();
+                }
+                stopWatch.stop();
+                log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
+                w1.writeRow(JSON.toJSONString(map), now, String.valueOf(stopWatch.getTotalTimeMillis()),
+                        JSON.toJSONString(result));
+            }
+
+            w2.writeRow("入参", "时间", "调用时长", "结果");
+            for (CsvRow row : r2) {
+                if (row.getOriginalLineNumber() == 1) {
+                    continue;
+                }
+
+                String mobile = row.getField(1);
+                LocalDateTime beginTimeLocalDateTime = getLocalDateTime(row.getField(2));
+                String beginTime = getTimeString(beginTimeLocalDateTime);
+//                String endTime = "";
+                String endTime = getTimeString(beginTimeLocalDateTime.plusDays(3));
+                String cKey = "qg4uY8nV7fPhvUXt";
+                String cSrc = getTimeString() +  "_" + "test";
+                String token = AESUtil.Encrypt(cSrc, cKey);
+                String complaintType = "2";
+                String isMobile = "";
+                String flowNo = "101";
+                String destAddr = "";
+                Map<String, Object> map = new HashMap<>();
+                map.put("mobile", mobile);
+                map.put("destAddr", destAddr);
+                map.put("complaintType", complaintType);
+                map.put("isMobile", isMobile);
+                map.put("flowNo", flowNo);
+                map.put("beginTime", beginTime);
+                map.put("endTime", endTime);
+                map.put("token", token);
+                UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
+                String result;
+                String now = LocalDateTime.now().toString();
+                StopWatch stopWatch = new StopWatch();
+                stopWatch.start();
+                try {
+                    result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
+                            endTime, token, false);
+                } catch (Exception e) {
+                    result = e.getMessage();
+                }
+                stopWatch.stop();
+                log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
+                w2.writeRow(JSON.toJSONString(map), now, String.valueOf(stopWatch.getTotalTimeMillis()),
+                        JSON.toJSONString(result));
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-public class UsercomplaintQuerysTest {
-    /*
-     * 测试环境
-     */
     @Test
-    void testQuery() {
+    void testQuery() throws Exception {
+        String cKey = "qg4uY8nV7fPhvUXt";
+        String cSrc = getTimeString() +  "_" + "test";
+        String token = AESUtil.Encrypt(cSrc, cKey);
+        String mobile = "15633026835";
+        String complaintType = "1";
+        String isMobile = "";
+//        String isMobile = "1";
+        String flowNo = "103";
+        String beginTime = "20220824203620";
+//        String beginTime = getTimeString("2022-08-23 18:43:56");
+//        String endTime = getTimeString(LocalDateTime.parse(beginTime, DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).plusDays(1));
+//        String endTime = "";
+        String endTime = "20220825203620";
+        String destAddr = "";
         UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
-        COMPConnectionContext result = usercomplaintQuerys.query("13231899751", "", "1", "2", "101", "20220709000000",
-                "20220710235959",
-                "A4ZWiyWpfJmeqFIRUTz6whuCJWcgRNuAiTI0SSIwehc=", true);
-        System.out.println(result.getRequestContext());
-        System.out.println(result.getResponseContext());
+        StopWatch stopWatch = new StopWatch();
+        stopWatch.start();
+        String result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
+                endTime, token, false);
+        stopWatch.stop();
+        log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
+    }
+
+    private String getTimeString(LocalDateTime time) {
+        return time.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
+    }
+
+    private String getTimeString() {
+        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
+    }
+
+    private LocalDateTime getLocalDateTime(String time) {
+        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
     }
 }