| | |
| | | package com.gs.xiaomi; |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.crypto.SmUtil; |
| | | import cn.hutool.crypto.digest.DigestUtil; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.gs.xiaomi.config.DataAcquisitionConfiguration; |
| | | import com.gs.xiaomi.dto.BCS101Request; |
| | | import com.gs.xiaomi.service.BCS101ApiService; |
| | | import com.gs.xiaomi.service.XM104Service; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.test.context.SpringBootTest; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | |
| | | @SpringBootTest |
| | | class XiaomiApplicationTests { |
| | |
| | | void testBCS101ApiService() throws Exception { |
| | | // 创建测试请求参数 |
| | | BCS101Request request = new BCS101Request(); |
| | | request.setSupplierId("100071"); |
| | | request.setDocNo("9311991542"); |
| | | request.setSupplierId("177301"); |
| | | request.setDocNo("9311036039"); |
| | | request.setDocType("ASNGR"); |
| | | request.setPageNo(1); |
| | | request.setPageSize(1000); |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | void test1() throws UnsupportedEncodingException, JsonProcessingException { |
| | | // 创建测试请求对象 |
| | | BCS101Request request = new BCS101Request(); |
| | | request.setSupplierId("177301"); |
| | | request.setDocNo("9311036039"); |
| | | request.setDocType("ASNGR"); |
| | | request.setPageNo(1); |
| | | request.setPageSize(1000); |
| | | |
| | | |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | System.out.println("=== BCS101 API 实际请求参数详情 ===\n"); |
| | | |
| | | //时间 |
| | | String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss"); |
| | | System.out.println(yyyyMMddHHmmss); |
| | | |
| | | // 1. 原始JSON数据 |
| | | String jsonData = objectMapper.writeValueAsString(request); |
| | | System.out.println("1. 原始JSON数据:"); |
| | | System.out.println(jsonData); |
| | | System.out.println(); |
| | | |
| | | // 2. Base64编码 |
| | | String base64Data = Base64.getEncoder().encodeToString(jsonData.getBytes("UTF-8")); |
| | | System.out.println("2. Base64编码后:"); |
| | | System.out.println(base64Data); |
| | | System.out.println(); |
| | | |
| | | // 3. URL编码 |
| | | String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8"); |
| | | System.out.println("3. URL编码后:"); |
| | | System.out.println(urlEncodedData); |
| | | System.out.println(); |
| | | |
| | | // 4. Form数据 |
| | | String formData = "data=" + urlEncodedData; |
| | | System.out.println("4. Form数据 (请求Body):"); |
| | | System.out.println(formData); |
| | | System.out.println(); |
| | | |
| | | // 5. 请求URL |
| | | System.out.println("5. 请求URL:"); |
| | | System.out.println(DataAcquisitionConfiguration.BCS_101_URL); |
| | | System.out.println(); |
| | | |
| | | // 6. Basic Auth |
| | | String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; |
| | | String auth = Base64.getEncoder().encodeToString(credentials.getBytes("UTF-8")); |
| | | System.out.println("6. Basic Auth信息:"); |
| | | System.out.println("用户名: " + DataAcquisitionConfiguration.BCS_USER_NAME); |
| | | System.out.println("密码: " + DataAcquisitionConfiguration.BCS_PWD); |
| | | System.out.println("Base64编码: " + auth); |
| | | System.out.println(); |
| | | |
| | | // 7. X5协议参数 |
| | | String appId = DataAcquisitionConfiguration.BCS_APP_ID; |
| | | String appKey = DataAcquisitionConfiguration.BCS_APP_Key; |
| | | long timestamp = System.currentTimeMillis() / 1000; |
| | | |
| | | System.out.println("7. X5协议参数:"); |
| | | System.out.println("App ID: " + appId); |
| | | System.out.println("App Key: " + appKey); |
| | | System.out.println("Timestamp: " + timestamp); |
| | | System.out.println(); |
| | | |
| | | // 8. X5签名计算 |
| | | String signString = appId + appKey + timestamp + formData; |
| | | String signature; |
| | | try { |
| | | signature = SmUtil.sm3(signString).toUpperCase(); |
| | | System.out.println("8. X5签名 (使用SM3):"); |
| | | } catch (Exception e) { |
| | | signature = DigestUtil.md5Hex(signString).toUpperCase(); |
| | | System.out.println("8. X5签名 (使用MD5):"); |
| | | } |
| | | System.out.println("签名字符串: " + signString); |
| | | System.out.println("签名值: " + signature); |
| | | System.out.println(); |
| | | |
| | | // 9. 完整的HTTP请求头 |
| | | System.out.println("9. 完整的HTTP请求头:"); |
| | | System.out.println("Content-Type: application/x-www-form-urlencoded"); |
| | | System.out.println("Authorization: Basic " + auth); |
| | | System.out.println("X-App-Id: " + appId); |
| | | System.out.println("X-Timestamp: " + timestamp); |
| | | System.out.println("X-Signature: " + signature); |
| | | System.out.println(); |
| | | |
| | | // 10. 完整的cURL命令示例 |
| | | System.out.println("10. 完整的cURL命令示例:"); |
| | | System.out.println("curl -X POST \\"); |
| | | System.out.println(" '" + DataAcquisitionConfiguration.BCS_101_URL + "' \\"); |
| | | System.out.println(" -H 'Content-Type: application/x-www-form-urlencoded' \\"); |
| | | System.out.println(" -H 'Authorization: Basic " + auth + "' \\"); |
| | | System.out.println(" -H 'X-App-Id: " + appId + "' \\"); |
| | | System.out.println(" -H 'X-Timestamp: " + timestamp + "' \\"); |
| | | System.out.println(" -H 'X-Signature: " + signature + "' \\"); |
| | | System.out.println(" -d '" + formData + "'"); |
| | | System.out.println(); |
| | | |
| | | // 11. Postman格式 |
| | | System.out.println("11. Postman测试格式:"); |
| | | System.out.println("Method: POST"); |
| | | System.out.println("URL: " + DataAcquisitionConfiguration.BCS_101_URL); |
| | | System.out.println("Headers:"); |
| | | System.out.println(" Content-Type: application/x-www-form-urlencoded"); |
| | | System.out.println(" Authorization: Basic " + auth); |
| | | System.out.println(" X-App-Id: " + appId); |
| | | System.out.println(" X-Timestamp: " + timestamp); |
| | | System.out.println(" X-Signature: " + signature); |
| | | System.out.println("Body (x-www-form-urlencoded):"); |
| | | System.out.println(" data: " + urlEncodedData); |
| | | System.out.println(); |
| | | |
| | | // 12. 实际调用API并获取响应 |
| | | System.out.println("12. === 实际API调用和响应 ==="); |
| | | StringBuilder responseResult = new StringBuilder(); |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(Basic Auth版本) ==="); |
| | | try { |
| | | String response = bcs101ApiService.getBCS101Data(request); |
| | | System.out.println("BCS101 API Response: " + response); |
| | | responseResult.append("Basic Auth响应: ").append(response.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API调用异常: " + e.getMessage()); |
| | | responseResult.append("Basic Auth异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(带X5协议版本) ==="); |
| | | try { |
| | | String responseWithX5 = bcs101ApiService.getBCS101DataWithX5(request, true); |
| | | System.out.println("BCS101 API Response (X5): " + responseWithX5); |
| | | responseResult.append("X5协议响应: ").append(responseWithX5.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API X5调用异常: " + e.getMessage()); |
| | | responseResult.append("X5协议异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(调试版本) ==="); |
| | | try { |
| | | String debugResponse = bcs101ApiService.getBCS101DataDebug(request); |
| | | System.out.println("BCS101 API Debug Response: " + debugResponse); |
| | | responseResult.append("调试版本响应: ").append(debugResponse.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API 调试版本调用异常: " + e.getMessage()); |
| | | responseResult.append("调试版本异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(简化版本) ==="); |
| | | try { |
| | | String simpleResponse = bcs101ApiService.getBCS101DataSimple(request); |
| | | System.out.println("BCS101 API Simple Response: " + simpleResponse); |
| | | responseResult.append("简化版本响应: ").append(simpleResponse.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API 简化版本调用异常: " + e.getMessage()); |
| | | responseResult.append("简化版本异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(X5协议标准版本) ==="); |
| | | try { |
| | | String x5StandardResponse = bcs101ApiService.getBCS101DataX5Standard(request); |
| | | System.out.println("BCS101 API X5Standard Response: " + x5StandardResponse); |
| | | responseResult.append("X5标准版本响应: ").append(x5StandardResponse.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API X5标准版本调用异常: " + e.getMessage()); |
| | | responseResult.append("X5标准版本异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | System.out.println("\n=== 测试BCS101 API服务(X5协议变体版本) ==="); |
| | | try { |
| | | String x5VariantResponse = bcs101ApiService.getBCS101DataX5Variant(request); |
| | | System.out.println("BCS101 API X5Variant Response: " + x5VariantResponse); |
| | | responseResult.append("X5变体版本响应: ").append(x5VariantResponse.length()).append("字符; "); |
| | | } catch (Exception e) { |
| | | System.out.println("BCS101 API X5变体版本调用异常: " + e.getMessage()); |
| | | responseResult.append("X5变体版本异常: ").append(e.getMessage()).append("; "); |
| | | } |
| | | |
| | | // 返回汇总信息 |
| | | String summary = String.format("请求参数[供应商ID: %s, 单据号: %s, 签名: %s, 时间戳: %d] | 响应汇总[%s]", |
| | | request.getSupplierId(), request.getDocNo(), signature, timestamp, responseResult.toString()); |
| | | System.out.println("\n=== 测试汇总 ==="); |
| | | System.out.println(summary); |
| | | } |
| | | } |