public ReceiveMessageResult unmarshall(StaxUnmarshallerContext context) throws Exception { ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { XMLEvent xmlEvent = context.nextEvent(); if (xmlEvent.isEndDocument()) return receiveMessageResult; if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) { if (context.testExpression("Message", targetDepth)) { receiveMessageResult.withMessages(MessageStaxUnmarshaller.getInstance().unmarshall(context)); continue; } } else if (xmlEvent.isEndElement()) { if (context.getCurrentDepth() < originalDepth) { return receiveMessageResult; } } } }
@Test public void testException() { com.amazonaws.services.sqs.model.Message message = new com.amazonaws.services.sqs.model.Message().withMessageId("test") .withBody("") .withReceiptHandle("receiptHandle"); Answer<?> answer = (Answer<ReceiveMessageResult>) invocation -> new ReceiveMessageResult(); AmazonSQSClient client = mock(AmazonSQSClient.class); when(client.listQueues(any(ListQueuesRequest.class))).thenReturn(new ListQueuesResult().withQueueUrls("junit_queue_url")); when(client.receiveMessage(any(ReceiveMessageRequest.class))).thenThrow(new RuntimeException("Error in SQS communication")) .thenReturn(new ReceiveMessageResult().withMessages(message)) .thenAnswer(answer); SQSObservableQueue queue = new SQSObservableQueue.Builder() .withQueueName("junit") .withClient(client).build(); List<Message> found = new LinkedList<>(); Observable<Message> observable = queue.observe(); assertNotNull(observable); observable.subscribe(found::add); Uninterruptibles.sleepUninterruptibly(1000, TimeUnit.MILLISECONDS); assertEquals(1, found.size()); } }
public ReceiveMessageResult unmarshall(StaxUnmarshallerContext context) throws Exception { ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { int xmlEvent = context.nextEvent(); if (xmlEvent == XmlPullParser.END_DOCUMENT) break; if (xmlEvent == XmlPullParser.START_TAG) { if (context.testExpression("Message", targetDepth)) { receiveMessageResult.withMessages(MessageStaxUnmarshaller.getInstance() .unmarshall(context)); continue; } } else if (xmlEvent == XmlPullParser.END_TAG) { if (context.getCurrentDepth() < originalDepth) { break; } } } return receiveMessageResult; }
public ReceiveMessageResult unmarshall(StaxUnmarshallerContext context) throws Exception { ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { XMLEvent xmlEvent = context.nextEvent(); if (xmlEvent.isEndDocument()) return receiveMessageResult; if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) { if (context.testExpression("Message", targetDepth)) { receiveMessageResult.withMessages(MessageStaxUnmarshaller.getInstance().unmarshall(context)); continue; } } else if (xmlEvent.isEndElement()) { if (context.getCurrentDepth() < originalDepth) { return receiveMessageResult; } } } }
public ReceiveMessageResult unmarshall(StaxUnmarshallerContext context) throws Exception { ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { int xmlEvent = context.nextEvent(); if (xmlEvent == XmlPullParser.END_DOCUMENT) break; if (xmlEvent == XmlPullParser.START_TAG) { if (context.testExpression("Message", targetDepth)) { receiveMessageResult.withMessages(MessageStaxUnmarshaller.getInstance() .unmarshall(context)); continue; } } else if (xmlEvent == XmlPullParser.END_TAG) { if (context.getCurrentDepth() < originalDepth) { break; } } } return receiveMessageResult; }
@Bean public AmazonSQSAsync amazonSqs() { AmazonSQSAsync sqs = mock(AmazonSQSAsync.class); given(sqs.getQueueUrl(new GetQueueUrlRequest("testQueue"))) .willReturn(new GetQueueUrlResult().withQueueUrl("http://testQueue.amazonaws.com")); given(sqs.receiveMessage(new ReceiveMessageRequest("http://testQueue.amazonaws.com") .withAttributeNames("All") .withMessageAttributeNames("All") .withMaxNumberOfMessages(10) .withWaitTimeSeconds(20))) .willReturn(new ReceiveMessageResult() .withMessages(new Message().withBody("messageContent"), new Message().withBody("messageContent2"))) .willReturn(new ReceiveMessageResult()); given(sqs.getQueueAttributes(any(GetQueueAttributesRequest.class))) .willReturn(new GetQueueAttributesResult()); return sqs; }