构建mq

1.First 写四个类

 2.构建push body

public static String generateMqJsonMessage(String filePath, String keyName, String eventType) throws ParseException {
String result = null;
File f = new File(filePath);
String json = null;
try {
json = FileUtil.readFileToString(f, Constant.FILE_ENCODING);

// Generate Random Test Data
CommonDataBean dataBean = new MqMessageDataBean();
dataBean = dataBean.getTestData(JAVA_FAKER_LANG);

String applicationId=getBundle().getProperty("applicationId").toString();
logger.info(" ### applicationId -> "+applicationId);
Object calendarEventName=getBundle().getProperty("calendarEventName");
logger.info(" ### calendarEventName -> "+calendarEventName);
List calendarEventNames=new ArrayList<>();
if(calendarEventName!=null)
calendarEventNames.add(calendarEventName.toString());
JSONObject jsonObject = new JSONObject(json);
String mqMsgStructureStr = jsonObject.getJSONObject(keyName).toString();
DocumentContext mqMsgStructureObj = JsonPath.using(Configuration.defaultConfiguration())
.parse(mqMsgStructureStr);

Object actionDate = getBundle().getProperty("actionDate");
logger.info(" ### actionDate -> "+actionDate.toString());

mqMsgStructureObj.set("$..correlateId", ((MqMessageDataBean) dataBean).getCorrelateId());
mqMsgStructureObj.set("$..payload.actionDate", actionDate.toString());
mqMsgStructureObj.set("$..timestamp", actionDate.toString());
mqMsgStructureObj.set("$..payload.correlateId", ((MqMessageDataBean) dataBean).getCorrelateId());
//mqMsgStructureObj.set("$..payload.id", ((MqMessageDataBean) dataBean).getEventId()); // random
// generate
mqMsgStructureObj.set("$..businessKey", ((MqMessageDataBean) dataBean).getCorrelateId());
// mqMsgStructureObj.set("$..eventType", eventType);
mqMsgStructureObj.set("$..eventId", ((MqMessageDataBean) dataBean).getEventId());// need random generate

List applications=new ArrayList<>();
applications.add(applicationId);
mqMsgStructureObj.set("$..policyNumbers", applications);
if(calendarEventNames.size()>0){
mqMsgStructureObj.set("$..events", calendarEventNames);
}else{
mqMsgStructureObj.delete("$..events");
}

String filledJsonStr = mqMsgStructureObj.jsonString();
logger.info(String.format(" ### generateMqJsonMessage -> filledJsonStr -> %s", filledJsonStr));

result = filledJsonStr;

} catch (IOException e) {
logger.log(Level.INFO, "generateMqJsonMessage() fail", e);
}

return result;
}

 3.建立连接,填写exchange,queue,routekey,payload

    @QAFTestStep(description = "start connect Cloud RabbitMq Exchange {exchange} and send json messsage {variableName} with RounteKey {routeKey}")
public static void sendMqmessageWithJson(String exchange, String variableName, String routeKey) {
String mqJsonMessage = (String) getBundle().getProperty(variableName);
logger.info("mqJsonMessage-->"+mqJsonMessage);
Sender.publishWithPropJson(exchange, Constant.RABBITMQ_QUEUE_QAQUEUE, routeKey, mqJsonMessage);
}


原文地址:https://www.cnblogs.com/sunfeiyang/p/13157934.html