如何编写用于编组和解组的Junit测试用例

时间:2018-09-17 06:58:09

标签: java soap junit jaxb

我有下面的代码片段,这些代码使用解组来读取从服务响应接收到的数据。我需要使用junit编写测试用例,以测试代码是否能够解析成功XML和错误XML。

代码段:

ExecuteLegacyAPICallResponse.ExecuteLegacyAPICallResult result = port.executeLegacyAPICall(sessionID,
                    "srtUpdateRealignment", parameters);
            List<Object> resultList = result.getContent();

Iterator<Object> itr = resultList.iterator();
            while (itr.hasNext()) {
                Object o = itr.next();
                LOGGER.info("Object---" + o + " Class----" + o.getClass());

                JAXBContext jaxBContext = JAXBContext.newInstance("webservice.client.wddx");
                Unmarshaller unmarshallerObj = jaxBContext.createUnmarshaller();
                WddxPacket wddxPacket = (WddxPacket) unmarshallerObj.unmarshal((Node) o);

                LOGGER.info("wddxPacket----" + wddxPacket);

                Struct structRealign = (Struct) wddxPacket.getData()
                        .getNullOrBooleanOrNumberOrDateTimeOrStringOrArrayOrStructOrRecordsetOrBinary().get(0);

                LOGGER.info("structRealign----" + structRealign);

                for (Var var : structRealign.getVar()) {
                    if ("return_status".equalsIgnoreCase(var.getName())) {
                        Struct dataStruct = (Struct) var
                                .getNullOrBooleanOrNumberOrDateTimeOrStringOrArrayOrStructOrRecordsetOrBinary().get(0);
                        LOGGER.info("dataStruct return_status----" + dataStruct);
                        isSuccess = isValidStatus(dataStruct);

                        if (!isSuccess) {
                            String errDescription = getStatusDescription(dataStruct);
                            throw new ServiceUnavailableException(errDescription);
                        }
                    }

                    if ("return_data".equalsIgnoreCase(var.getName())) {
                        Struct dataStruct = (Struct) var
                                .getNullOrBooleanOrNumberOrDateTimeOrStringOrArrayOrStructOrRecordsetOrBinary().get(0);
                        for (Var dataVar : dataStruct.getVar()) {
                            LOGGER.info("dataStruct return_data----" + dataStruct);
                            if ("recordset_data".equalsIgnoreCase(dataVar.getName())) {
                                Recordset recordSet = (Recordset) dataVar
                                        .getNullOrBooleanOrNumberOrDateTimeOrStringOrArrayOrStructOrRecordsetOrBinary()
                                        .get(0);
                                LOGGER.info("recordSet ----" + recordSet);
                                for (Field field : recordSet.getField()) {
                                    if ("confirmnum".equalsIgnoreCase(field.getName())) {
                                        List<Object> list = field.getNullOrBooleanOrNumberOrDateTimeOrStringOrBinary();
                                        webservice.client.wddx.String value = (webservice.client.wddx.String) list
                                                .get(0);
                                        confirmNum = value.getvalue();
                                    }
                                }
                            }
                        }
                    }
                }
            }

课程信息以org.apache.xerces.dom.ElementNSImpl的形式出现。

如何将示例XML转换为该对象,以便我的代码能够解组和解析结果?

示例XML-

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ExecuteLegacyAPICallResponse xmlns="http://sdda.schwabrt.com/">
         <ExecuteLegacyAPICallResult>
            <wddxPacket version="1.0" xmlns="">
               <header/>
               <data>
                  <struct>
                     <var name="return_status">
                        <struct>
                           <var name="source">
                              <string>SRTPAS_EXE:processRealignment</string>
                           </var>
                           <var name="severity">
                              <string>Error</string>
                           </var>
                           <var name="statuscode">
                              <number>1</number>
                           </var>
                           <var name="statusdesc">
                              <string>Could not convert variant of type (Null) into type (OleStr)</string>
                           </var>
                        </struct>
                     </var>
                     <var name="return_request">
                        <struct>
                           <var name="_tk">
                              <string>www</string>
                           </var>
                           <var name="_rpc">
                              <string>srtUpdateRealignment</string>
                           </var>
                           <var name="_sid">
                              <string>PAS-1E70CCA1CCC54479A620C1DE2D58987A-80</string>
                           </var>
                        </struct>
                     </var>
                  </struct>
               </data>
            </wddxPacket>
         </ExecuteLegacyAPICallResult>
      </ExecuteLegacyAPICallResponse>
   </soap:Body>
</soap:Envelope>

请让我知道是否需要其他输入。

0 个答案:

没有答案