Best Citrus code snippet using com.consol.citrus.ws.message.SoapFault.from
Source:WebServiceEndpoint.java
...106 addSoapHeaders(response, replyMessage);107 addMimeHeaders(response, replyMessage);108 } else {109 if (log.isDebugEnabled()) {110 log.debug("No reply message from endpoint adapter '" + endpointAdapter + "'");111 }112 log.warn("No SOAP response for calling client");113 }114 }115 private void addSoapAttachments(MimeMessage response, Message replyMessage) {116 if (replyMessage instanceof com.consol.citrus.ws.message.SoapMessage) {117 List<SoapAttachment> soapAttachments = ((com.consol.citrus.ws.message.SoapMessage) replyMessage).getAttachments();118 soapAttachments.stream()119 .filter(soapAttachment -> !soapAttachment.isMtomInline())120 .forEach(soapAttachment -> {121 String contentId = soapAttachment.getContentId();122 if (!contentId.startsWith("<")) {123 contentId = "<" + contentId + ">";124 }125 response.addAttachment(contentId, soapAttachment.getDataHandler());126 });127 }128 }129 /**130 * If Http status code is set on reply message headers simulate Http error with status code.131 * No SOAP response is sent back in this case.132 * @param replyMessage133 * @return134 * @throws IOException 135 */136 private boolean simulateHttpStatusCode(Message replyMessage) throws IOException {137 if (replyMessage == null || CollectionUtils.isEmpty(replyMessage.getHeaders())) {138 return false;139 }140 141 for (Entry<String, Object> headerEntry : replyMessage.getHeaders().entrySet()) {142 if (headerEntry.getKey().equalsIgnoreCase(SoapMessageHeaders.HTTP_STATUS_CODE)) {143 WebServiceConnection connection = TransportContextHolder.getTransportContext().getConnection();144 145 int statusCode = Integer.valueOf(headerEntry.getValue().toString());146 if (connection instanceof HttpServletConnection) {147 ((HttpServletConnection)connection).setFault(false);148 ((HttpServletConnection)connection).getHttpServletResponse().setStatus(statusCode);149 return true;150 } else {151 log.warn("Unable to set custom Http status code on connection other than HttpServletConnection (" + connection.getClass().getName() + ")");152 }153 }154 }155 156 return false;157 }158 /**159 * Adds mime headers outside of SOAP envelope. Header entries that go to this header section 160 * must have internal http header prefix defined in {@link com.consol.citrus.ws.message.SoapMessageHeaders}.161 * @param response the soap response message.162 * @param replyMessage the internal reply message.163 */164 private void addMimeHeaders(SoapMessage response, Message replyMessage) {165 for (Entry<String, Object> headerEntry : replyMessage.getHeaders().entrySet()) {166 if (headerEntry.getKey().toLowerCase().startsWith(SoapMessageHeaders.HTTP_PREFIX)) {167 String headerName = headerEntry.getKey().substring(SoapMessageHeaders.HTTP_PREFIX.length());168 169 if (response instanceof SaajSoapMessage) {170 SaajSoapMessage saajSoapMessage = (SaajSoapMessage) response;171 MimeHeaders headers = saajSoapMessage.getSaajMessage().getMimeHeaders();172 headers.setHeader(headerName, headerEntry.getValue().toString());173 } else if (response instanceof AxiomSoapMessage) {174 log.warn("Unable to set mime message header '" + headerName + "' on AxiomSoapMessage - unsupported");175 } else {176 log.warn("Unsupported SOAP message implementation - unable to set mime message header '" + headerName + "'");177 }178 }179 }180 }181 /**182 * Add message payload as SOAP body element to the SOAP response.183 * @param response184 * @param replyMessage185 */186 private void addSoapBody(SoapMessage response, Message replyMessage) throws TransformerException {187 if (!(replyMessage.getPayload() instanceof String) || 188 StringUtils.hasText(replyMessage.getPayload(String.class))) {189 Source responseSource = getPayloadAsSource(replyMessage.getPayload());190 191 TransformerFactory transformerFactory = TransformerFactory.newInstance();192 Transformer transformer = transformerFactory.newTransformer();193 194 transformer.transform(responseSource, response.getPayloadResult());195 }196 }197 198 /**199 * Translates message headers to SOAP headers in response.200 * @param response201 * @param replyMessage202 */203 private void addSoapHeaders(SoapMessage response, Message replyMessage) throws TransformerException {204 for (Entry<String, Object> headerEntry : replyMessage.getHeaders().entrySet()) {205 if (MessageHeaderUtils.isSpringInternalHeader(headerEntry.getKey()) ||206 headerEntry.getKey().startsWith(DEFAULT_JMS_HEADER_PREFIX)) {207 continue;208 }209 if (headerEntry.getKey().equalsIgnoreCase(SoapMessageHeaders.SOAP_ACTION)) {210 response.setSoapAction(headerEntry.getValue().toString());211 } else if (!headerEntry.getKey().startsWith(MessageHeaders.PREFIX)) {212 SoapHeaderElement headerElement;213 if (QNameUtils.validateQName(headerEntry.getKey())) {214 QName qname = QNameUtils.parseQNameString(headerEntry.getKey());215 if (StringUtils.hasText(qname.getNamespaceURI())) {216 headerElement = response.getSoapHeader().addHeaderElement(qname);217 } else {218 headerElement = response.getSoapHeader().addHeaderElement(getDefaultQName(headerEntry.getKey()));219 }220 } else {221 throw new SoapHeaderException("Failed to add SOAP header '" + headerEntry.getKey() + "', " +222 "because of invalid QName");223 }224 headerElement.setText(headerEntry.getValue().toString());225 }226 }227 for (String headerData : replyMessage.getHeaderData()) {228 TransformerFactory transformerFactory = TransformerFactory.newInstance();229 Transformer transformer = transformerFactory.newTransformer();230 transformer.transform(new StringSource(headerData),231 response.getSoapHeader().getResult());232 }233 }234 /**235 * Adds a SOAP fault to the SOAP response body. The SOAP fault is declared236 * as QName string in the response message's header (see {@link com.consol.citrus.ws.message.SoapMessageHeaders})237 * 238 * @param response239 * @param replyMessage240 */241 private void addSoapFault(SoapMessage response, SoapFault replyMessage) throws TransformerException {242 SoapBody soapBody = response.getSoapBody();243 org.springframework.ws.soap.SoapFault soapFault;244 245 if (SoapFaultDefinition.SERVER.equals(replyMessage.getFaultCodeQName()) ||246 SoapFaultDefinition.RECEIVER.equals(replyMessage.getFaultCodeQName())) {247 soapFault = soapBody.addServerOrReceiverFault(replyMessage.getFaultString(),248 replyMessage.getLocale());249 } else if (SoapFaultDefinition.CLIENT.equals(replyMessage.getFaultCodeQName()) ||250 SoapFaultDefinition.SENDER.equals(replyMessage.getFaultCodeQName())) {251 soapFault = soapBody.addClientOrSenderFault(replyMessage.getFaultString(),252 replyMessage.getLocale());253 } else if (soapBody instanceof Soap11Body) {254 Soap11Body soap11Body = (Soap11Body) soapBody;255 soapFault = soap11Body.addFault(replyMessage.getFaultCodeQName(),256 replyMessage.getFaultString(),257 replyMessage.getLocale());258 } else if (soapBody instanceof Soap12Body) {259 Soap12Body soap12Body = (Soap12Body) soapBody;260 Soap12Fault soap12Fault = soap12Body.addServerOrReceiverFault(replyMessage.getFaultString(),261 replyMessage.getLocale());262 soap12Fault.addFaultSubcode(replyMessage.getFaultCodeQName());263 264 soapFault = soap12Fault;265 } else {266 throw new CitrusRuntimeException("Found unsupported SOAP implementation. Use SOAP 1.1 or SOAP 1.2.");267 }268 269 if (replyMessage.getFaultActor() != null) {270 soapFault.setFaultActorOrRole(replyMessage.getFaultActor());271 }272 273 List<String> soapFaultDetails = replyMessage.getFaultDetails();274 if (!soapFaultDetails.isEmpty()) {275 TransformerFactory transformerFactory = TransformerFactory.newInstance();276 Transformer transformer = transformerFactory.newTransformer();277 278 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");279 280 SoapFaultDetail faultDetail = soapFault.addFaultDetail();281 for (int i = 0; i < soapFaultDetails.size(); i++) {282 transformer.transform(new StringSource(soapFaultDetails.get(i)), faultDetail.getResult());283 }284 }285 }286 287 /**288 * Get the message payload object as {@link Source}, supported payload types are289 * {@link Source}, {@link Document} and {@link String}.290 * @param replyPayload payload object291 * @return {@link Source} representation of the payload292 */293 private Source getPayloadAsSource(Object replyPayload) {294 if (replyPayload instanceof Source) {295 return (Source) replyPayload;296 } else if (replyPayload instanceof Document) {297 return new DOMSource((Document) replyPayload);298 } else if (replyPayload instanceof String) {299 return new StringSource((String) replyPayload);300 } else {301 throw new CitrusRuntimeException("Unknown type for reply message payload (" + replyPayload.getClass().getName() + ") " +302 "Supported types are " + "'" + Source.class.getName() + "', " + "'" + Document.class.getName() + "'" + 303 ", or '" + String.class.getName() + "'");304 }305 }306 /**307 * Get the default QName from local part.308 * @param localPart309 * @return310 */311 private QName getDefaultQName(String localPart) {312 if (StringUtils.hasText(defaultNamespaceUri)) {313 return new QName(defaultNamespaceUri, localPart, defaultPrefix);314 } else {315 throw new SoapHeaderException("Failed to add SOAP header '" + localPart + "', " +316 "because neither valid QName nor default namespace-uri is set!");317 }318 }319 /**320 * Gets the endpoint adapter.321 * @return...
Source:AssertSoapFault.java
...80 log.debug("Validating SOAP fault ...");81 82 SoapFault controlFault = constructControlFault(context);83 84 validator.validateSoapFault(SoapFault.from(soapFaultException.getSoapFault()), controlFault, context, validationContext);85 86 log.debug("Asserted SOAP fault as expected: " + soapFaultException.getFaultCode() + ": " + soapFaultException.getFaultStringOrReason());87 log.info("Assert SOAP fault validation successful");88 89 return;90 } catch (RuntimeException e) {91 throw new ValidationException("SOAP fault validation failed for asserted exception type - expected: '" + 92 SoapFaultClientException.class + "' but was: '" + e.getClass().getName() + "'", e);93 } catch (Exception e) {94 throw new ValidationException("SOAP fault validation failed for asserted exception type - expected: '" + 95 SoapFaultClientException.class + "' but was: '" + e.getClass().getName() + "'", e);96 }97 98 throw new ValidationException("SOAP fault validation failed! Missing asserted SOAP fault exception");99 }100 /**101 * Constructs the control soap fault holding all expected fault information102 * like faultCode, faultString and faultDetail.103 * 104 * @return the constructed SoapFault instance.105 */106 private SoapFault constructControlFault(TestContext context) {107 SoapFault controlFault= new SoapFault();108 if (StringUtils.hasText(faultActor)) {109 controlFault.faultActor(context.replaceDynamicContentInString(faultActor));110 }111 controlFault.faultCode(context.replaceDynamicContentInString(faultCode));112 controlFault.faultString(context.replaceDynamicContentInString(faultString));113 for (String faultDetail : faultDetails) {114 controlFault.addFaultDetail(context.replaceDynamicContentInString(faultDetail));115 }116 try {117 for (String faultDetailPath : faultDetailResourcePaths) {118 String resourcePath = context.replaceDynamicContentInString(faultDetailPath);119 controlFault.addFaultDetail(context.replaceDynamicContentInString(FileUtils.readToString(FileUtils.getFileResource(resourcePath, context), FileUtils.getCharset(resourcePath))));120 }121 } catch (IOException e) {122 throw new CitrusRuntimeException("Failed to create SOAP fault detail from file resource", e);123 }124 return controlFault;125 }126 /**127 * Set the nested test action.128 * @param action the action to set129 */130 public AssertSoapFault setAction(TestAction action) {131 addTestAction(action);132 return this;133 }134 /**135 * Set the fault code.136 * @param faultCode the faultCode to set...
Source:SendSoapFaultAction.java
...71 String resourcePath = context.replaceDynamicContentInString(faultDetailPath);72 soapFault.addFaultDetail(context.replaceDynamicContentInString(FileUtils.readToString(FileUtils.getFileResource(resourcePath, context), FileUtils.getCharset(resourcePath))));73 }74 } catch (IOException e) {75 throw new CitrusRuntimeException("Failed to create SOAP fault detail from file resource", e);76 }77 return soapFault;78 }79 /**80 * Set the fault code QName string. This can be either81 * a fault code in {@link org.springframework.ws.soap.server.endpoint.SoapFaultDefinition}82 * or a custom QName like {http://www.consol.de/citrus}citrus:TEC-100083 *84 * @param faultCode the faultCode to set85 */86 public SendSoapFaultAction setFaultCode(String faultCode) {87 this.faultCode = faultCode;88 return this;89 }...
from
Using AI Code Generation
1public static SoapFaultBuilder soapFault() {2 return new SoapFaultBuilder();3}4public static SoapFaultBuilder soapFault() {5 return new SoapFaultBuilder();6}7public static SoapFaultBuilder soapFault() {8 return new SoapFaultBuilder();9}10public static SoapFaultBuilder soapFault() {11 return new SoapFaultBuilder();12}13public static SoapFaultBuilder soapFault() {14 return new SoapFaultBuilder();15}16public static SoapFaultBuilder soapFault() {17 return new SoapFaultBuilder();18}19public static SoapFaultBuilder soapFault() {20 return new SoapFaultBuilder();21}22public static SoapFaultBuilder soapFault() {23 return new SoapFaultBuilder();24}25public static SoapFaultBuilder soapFault() {26 return new SoapFaultBuilder();27}28public static SoapFaultBuilder soapFault() {29 return new SoapFaultBuilder();30}31public static SoapFaultBuilder soapFault() {32 return new SoapFaultBuilder();33}34public static SoapFaultBuilder soapFault() {35 return new SoapFaultBuilder();36}37public static SoapFaultBuilder soapFault() {38 return new SoapFaultBuilder();39}40public static SoapFaultBuilder soapFault() {41 return new SoapFaultBuilder();42}43public static SoapFaultBuilder soapFault() {44 return new SoapFaultBuilder();45}46public static SoapFaultBuilder soapFault() {47 return new SoapFaultBuilder();48}49public static SoapFaultBuilder soapFault() {50 return new SoapFaultBuilder();51}52public static SoapFaultBuilder soapFault() {53 return new SoapFaultBuilder();54}55public static SoapFaultBuilder soapFault() {56 return new SoapFaultBuilder();57}58public static SoapFaultBuilder soapFault() {59 return new SoapFaultBuilder();60}61public static SoapFaultBuilder soapFault() {62 return new SoapFaultBuilder();63}
from
Using AI Code Generation
1SoapFault soapFault = new SoapFault();2soapFault.setFaultCode("Server");3soapFault.setFaultString("SOAP Fault");4soapFault.setFaultDetail("detail");5soapFault.setFaultReason("SOAP Fault Reason");6SoapMessage soapMessage = new SoapMessage();7soapMessage.setFault(soapFault);8soapMessage.setFaultDetail("detail");9soapMessage.setFaultReason("SOAP Fault Reason");10soapMessage.setFaultString("SOAP Fault");11soapMessage.setFaultCode("Server");12soapMessage.setFaultDetail("detail");13soapMessage.setFaultReason("SOAP Fault Reason");14soapMessage.setFaultString("SOAP Fault");15soapMessage.setFaultCode("Server");16soapMessage.setFaultDetail("detail");17soapMessage.setFaultReason("SOAP Fault Reason");18soapMessage.setFaultString("SOAP Fault");19soapMessage.setFaultCode("Server");20soapMessage.setFaultDetail("detail");21soapMessage.setFaultReason("
from
Using AI Code Generation
1SoapFault soapFault = new SoapFault();2soapFault.setFaultCode(SoapFault.FaultCode.SERVER);3soapFault.setFaultString("Error: Invalid input");4soapFault.setDetail("<detail>Input value is invalid</detail>");5context.setVariable("soapFault", soapFault);6SoapMessage soapMessage = new SoapMessage();7soapMessage.setFault(soapFault);8context.setVariable("soapMessage", soapMessage);9SendSoapFaultAction sendSoapFaultAction = new SendSoapFaultAction();10sendSoapFaultAction.setFaultCode(SoapFault.FaultCode.SERVER);11sendSoapFaultAction.setFaultString("Error: Invalid input");12sendSoapFaultAction.setDetail("<detail>Input value is invalid</detail>");13sendSoapFaultAction.setFaultDetail("<detail>Input value is invalid</detail>");14sendSoapFaultAction.setFaultDetailElementName("detail");15sendSoapFaultAction.setFaultStringOrReason("Error: Invalid input");16sendSoapFaultAction.setFaultStringOrReasonElementName("FaultString");17sendSoapFaultAction.setFaultCode(SoapFault.FaultCode.SERVER);18sendSoapFaultAction.setFaultCodeElementName("FaultCode");19sendSoapFaultAction.setFaultCodeValue("Server");
from
Using AI Code Generation
1public class 3 {2 public static void main(String[] args) {3 SoapFault soapFault = new SoapFault();4 soapFault.setFaultCode("faultcode");5 soapFault.setFaultActor("faultactor");6 soapFault.setFaultString("faultstring");7 System.out.println("faultcode: " + soapFault.getFaultCode());8 System.out.println("faultactor: " + soapFault.getFaultActor());9 System.out.println("faultstring: " + soapFault.getFaultString());10 }11}
from
Using AI Code Generation
1SoapFault soapFault = new SoapFault("Server", "Client", "Invalid request");2soapFault.setFaultString("Invalid request");3soapFault.setFaultDetail("<detail>Invalid request</detail>");4soapFault.setFaultCode("soap:Server");5soapFault.setFaultCodeWithPrefix("soap:Server");6SoapFault soapFault = new SoapFault("Server", "Client", "Invalid request");7soapFault.setFaultString("Invalid request");8soapFault.setFaultDetail("<detail>Invalid request</detail>");9soapFault.setFaultCode("soap:Server");10soapFault.setFaultCodeWithPrefix("soap:Server");11soapFault.setFaultCodeWithPrefix("soap:Server");12soapFault.setFaultCode("soap:Server");13SoapFault soapFault = new SoapFault("Server", "Client", "Invalid request");14soapFault.setFaultString("Invalid request");15soapFault.setFaultDetail("<detail>Invalid request</detail>");16soapFault.setFaultCode("soap:Server");17soapFault.setFaultCodeWithPrefix("soap:Server");18soapFault.setFaultCodeWithPrefix("soap:Server");19soapFault.setFaultCode("soap:Server");20soapFault.setFaultCodeWithPrefix("soap:Server");21SoapFault soapFault = new SoapFault("Server", "Client", "Invalid request");22soapFault.setFaultString("Invalid request");23soapFault.setFaultDetail("<detail>Invalid request</detail>");24soapFault.setFaultCode("soap:Server");25soapFault.setFaultCodeWithPrefix("soap:Server");26soapFault.setFaultCodeWithPrefix("soap:Server");27soapFault.setFaultCode("soap:Server");28soapFault.setFaultCodeWithPrefix("soap:Server");29soapFault.setFaultCodeWithPrefix("soap:Server");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!