我正在尝试传递 multiparform 数据字段
Map<String, List<FormDataBodyPart>>
,我从其余 API 获得的输入到接受多部分字段作为输入并读取它并执行所需操作的管理器类。
JsonNode data = objectMapper.readTree(requestFields.get(REQUEST).get(0).getValueAs(InputStream.class));
假设我总是只得到唯一的一个元素。当我收到邮递员请求时,该方法有效。
但是当我尝试对接受多部分表单数据字段的管理器类进行单元测试时,会抛出错误
java.lang.IllegalStateException: Entity instance does not contain the unconverted content.
at org.glassfish.jersey.media.multipart.BodyPart.getEntityAs(BodyPart.java:267)
at org.glassfish.jersey.media.multipart.FormDataBodyPart.getValueAs(FormDataBodyPart.java:291)
at com.confluxsys.iam.adc.core.impl.request.RequestManagerImpl.createRequest(RequestManagerImpl.java:178)
at com.confluxsys.iam.adc.util.request.TestRequestManagerV2.createRequestV2(TestRequestManagerV2.java:154)
测试类代码-->
String data = "{"menu":{"id":"file","value":"File","popup":{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},{"value":"Open","onclick":"OpenDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}";
InputStream jsonStream;
try (FormDataMultiPart formDataMultiPart = new FormDataMultiPart()) {
FormDataBodyPart form1 = new FormDataBodyPart();
jsonStream = new ByteArrayInputStream(config.getBytes());
form1.setMediaType(MediaType.APPLICATION_JSON_TYPE);
form1.setValue(MediaType.APPLICATION_JSON_TYPE, jsonStream);
form1.setName("part-config");
form1.getHeaders().add("Content-Length", String.valueOf(config.getBytes().length));
formDataMultiPart.bodyPart(form1);
RequestStateInfo request = requestManager.createRequest(formDataMultiPart.getFields());
}
主类代码--->
public RequestStateInfo createRequest(Map<String, List<FormDataBodyPart>> requestFields) throws SystemFailureException, com.confluxsys.iap.config.util.ObjectNotFoundException {
if (!requestFields.containsKey(REQUEST)) {
logger.error("Request json is not passed in the request payload");
throw new IllegalArgumentException("The 'request' body part is missing in the payload.");
}
try {
ObjectMapper objectMapper = new objectMapper() ;
//as data [request] will always be sent assuming it will be present at first location
JsonNode data = objectMapper.readTree(requestFields.get(REQUEST).get(0).getValueAs(InputStream.class));
/*validation after reading */
}catch (Exception e) {
throw new SystemFailureException("Error occured while creating request .Rolling back all operation .cause : ", e);
}
}
任何人都可以建议我可以对这门课进行单元测试吗?
图书馆 --> org.glassfish.jersey 2.35