From 7855b1b087da3887bc8393c9f3e03c58f9dffd59 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 11 Jun 2010 12:23:34 -0700 Subject: [PATCH] Issue 86: add get messages to azurequeue --- .../storage/queue/AzureQueueAsyncClient.java | 14 +- .../azure/storage/queue/AzureQueueClient.java | 18 +- .../storage/queue/domain/QueueMessage.java | 162 ++++++++++++++++++ .../storage/queue/options/GetOptions.java | 87 ++++++++++ .../queue/xml/QueueMessagesListHandler.java | 104 +++++++++++ .../queue/AzureQueueAsyncClientTest.java | 41 +++++ .../queue/AzureQueueClientLiveTest.java | 28 ++- .../storage/queue/options/GetOptionsTest.java | 75 ++++++++ .../xml/QueueMessagesListHandlerTest.java | 68 ++++++++ .../resources/queue/test_get_messages.xml | 21 +++ 10 files changed, 609 insertions(+), 9 deletions(-) create mode 100644 azure/src/main/java/org/jclouds/azure/storage/queue/domain/QueueMessage.java create mode 100644 azure/src/main/java/org/jclouds/azure/storage/queue/options/GetOptions.java create mode 100644 azure/src/main/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandler.java create mode 100644 azure/src/test/java/org/jclouds/azure/storage/queue/options/GetOptionsTest.java create mode 100644 azure/src/test/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandlerTest.java create mode 100644 azure/src/test/resources/queue/test_get_messages.xml diff --git a/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClient.java b/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClient.java index e5377d7d83..6df584e865 100644 --- a/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClient.java +++ b/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClient.java @@ -18,6 +18,7 @@ */ package org.jclouds.azure.storage.queue; +import java.util.Set; import java.util.concurrent.ExecutionException; import javax.ws.rs.DELETE; @@ -33,9 +34,12 @@ import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication; import org.jclouds.azure.storage.options.CreateOptions; import org.jclouds.azure.storage.options.ListOptions; import org.jclouds.azure.storage.queue.binders.BindToXmlStringPayload; +import org.jclouds.azure.storage.queue.domain.QueueMessage; import org.jclouds.azure.storage.queue.domain.QueueMetadata; +import org.jclouds.azure.storage.queue.options.GetOptions; import org.jclouds.azure.storage.queue.options.PutMessageOptions; import org.jclouds.azure.storage.queue.xml.AccountNameEnumerationResultsHandler; +import org.jclouds.azure.storage.queue.xml.QueueMessagesListHandler; import org.jclouds.azure.storage.reference.AzureStorageHeaders; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.Endpoint; @@ -64,7 +68,7 @@ import com.google.common.util.concurrent.ListenableFuture; * during processing will be wrapped in an {@link ExecutionException} as documented in * {@link ListenableFuture#get()}. * - * @see + * @see * @author Adrian Cole */ @SkipEncoding('/') @@ -89,6 +93,14 @@ public interface AzureQueueAsyncClient { @Path("{queue}") ListenableFuture createQueue(@PathParam("queue") String queue, CreateOptions... options); + /** + * @see AzureQueueClient#getMessages + */ + @GET + @Path("{queue}/messages") + @XMLResponseParser(QueueMessagesListHandler.class) + ListenableFuture> getMessages(@PathParam("queue") String queue, GetOptions... options); + /** * @see AzureQueueClient#deleteQueue */ diff --git a/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueClient.java b/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueClient.java index 9d08a62ada..851c09ca70 100644 --- a/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueClient.java +++ b/azure/src/main/java/org/jclouds/azure/storage/queue/AzureQueueClient.java @@ -18,15 +18,19 @@ */ package org.jclouds.azure.storage.queue; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.jclouds.azure.storage.domain.BoundedSet; import org.jclouds.azure.storage.options.CreateOptions; import org.jclouds.azure.storage.options.ListOptions; +import org.jclouds.azure.storage.queue.domain.QueueMessage; import org.jclouds.azure.storage.queue.domain.QueueMetadata; +import org.jclouds.azure.storage.queue.options.GetOptions; import org.jclouds.azure.storage.queue.options.PutMessageOptions; import org.jclouds.concurrent.Timeout; +import org.jclouds.http.HttpResponseException; import com.google.common.util.concurrent.ListenableFuture; @@ -106,7 +110,8 @@ public interface AzureQueueClient { * it is added to the time it is retrieved and deleted. If a message is not retrieved before the * time-to-live interval expires, the message is removed from the queue. * - * If the message is too large, the service returns status code 400 (Bad Request). + * @throws HttpResponseException + * If the message is too large, the service returns status code 400 (Bad Request). * */ void putMessage(String queue, String message, PutMessageOptions... options); @@ -123,4 +128,15 @@ public interface AzureQueueClient { */ @Timeout(duration = 10, timeUnit = TimeUnit.MINUTES) void clearMessages(String queue); + + /** + * The Get Messages operation retrieves one or more messages from the front of the queue. + * + * @param queue + * the name of the queue to retrieve messages from + * @param options + * controls the number of messages to receive and the visibility window + */ + Set getMessages(String queue, GetOptions... options); + } diff --git a/azure/src/main/java/org/jclouds/azure/storage/queue/domain/QueueMessage.java b/azure/src/main/java/org/jclouds/azure/storage/queue/domain/QueueMessage.java new file mode 100644 index 0000000000..f69d47cbed --- /dev/null +++ b/azure/src/main/java/org/jclouds/azure/storage/queue/domain/QueueMessage.java @@ -0,0 +1,162 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. = 0, "dequeueCount not set"); + this.popReceipt = checkNotNull(popReceipt, "popReceipt"); + this.timeNextVisible = checkNotNull(timeNextVisible, "timeNextVisible"); + this.messageText = checkNotNull(messageText, "messageText"); + } + + /** + * The MessageID element is a GUID value that identifies the message in the queue. This value is + * assigned to the message by the Queue service and is opaque to the client. This value may be + * used together with the value of the PopReceipt element to delete a message from the queue + * after it has been retrieved with the Get Messages operation. + * + * + */ + public String getMessageId() { + return messageId; + } + + public Date getInsertionTime() { + return insertionTime; + } + + public Date getExpirationTime() { + return expirationTime; + } + + /** + * DequeueCount element has a value of 1 the first time the message is dequeued. This value is + * incremented each time the message is subsequently dequeued. + * */ + public int getDequeueCount() { + return dequeueCount; + } + + /** + * The value of PopReceipt is opaque to the client; its only purpose is to ensure that a message + * may be deleted with the Delete Message operation. + */ + public String getPopReceipt() { + return popReceipt; + } + + public Date getTimeNextVisible() { + return timeNextVisible; + } + + public String getMessageText() { + return messageText; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + dequeueCount; + result = prime * result + ((expirationTime == null) ? 0 : expirationTime.hashCode()); + result = prime * result + ((insertionTime == null) ? 0 : insertionTime.hashCode()); + result = prime * result + ((messageId == null) ? 0 : messageId.hashCode()); + result = prime * result + ((messageText == null) ? 0 : messageText.hashCode()); + result = prime * result + ((popReceipt == null) ? 0 : popReceipt.hashCode()); + result = prime * result + ((timeNextVisible == null) ? 0 : timeNextVisible.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + QueueMessage other = (QueueMessage) obj; + if (dequeueCount != other.dequeueCount) + return false; + if (expirationTime == null) { + if (other.expirationTime != null) + return false; + } else if (!expirationTime.equals(other.expirationTime)) + return false; + if (insertionTime == null) { + if (other.insertionTime != null) + return false; + } else if (!insertionTime.equals(other.insertionTime)) + return false; + if (messageId == null) { + if (other.messageId != null) + return false; + } else if (!messageId.equals(other.messageId)) + return false; + if (messageText == null) { + if (other.messageText != null) + return false; + } else if (!messageText.equals(other.messageText)) + return false; + if (popReceipt == null) { + if (other.popReceipt != null) + return false; + } else if (!popReceipt.equals(other.popReceipt)) + return false; + if (timeNextVisible == null) { + if (other.timeNextVisible != null) + return false; + } else if (!timeNextVisible.equals(other.timeNextVisible)) + return false; + return true; + } + + @Override + public String toString() { + return "QueueMessage [dequeueCount=" + dequeueCount + ", expirationTime=" + expirationTime + + ", insertionTime=" + insertionTime + ", messageId=" + messageId + ", messageText=" + + messageText + ", popReceipt=" + popReceipt + ", timeNextVisible=" + + timeNextVisible + "]"; + } + +} diff --git a/azure/src/main/java/org/jclouds/azure/storage/queue/options/GetOptions.java b/azure/src/main/java/org/jclouds/azure/storage/queue/options/GetOptions.java new file mode 100644 index 0000000000..53714cc407 --- /dev/null +++ b/azure/src/main/java/org/jclouds/azure/storage/queue/options/GetOptions.java @@ -0,0 +1,87 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.jclouds.azure.storage.queue.options; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.util.Collections; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Contains common options supported in the REST API for the GET operation.

+ * Usage

The recommended way to instantiate a GetOptions object is to statically import + * GetOptions.* and invoke a static creation method followed by an instance mutator (if needed): + *

+ * + * import static org.jclouds.azure.storage.queue.options.GetOptions.Builder.* + * import org.jclouds.azure.storage.queue.AzureQueueClient; + *

+ * AzureQueueClient connection = // get connection + * messages = connection.getMessages("queueName", maxMessages(3)); + * * + * + * @see + * @author Adrian Cole + */ +public class GetOptions extends BaseHttpRequestOptions { + public static final GetOptions NONE = new GetOptions(); + + /** + * A nonzero integer value that specifies the number of messages to retrieve from the queue, up + * to a maximum of 32. By default, a single message is retrieved from the queue with this + * operation. + * + */ + public GetOptions maxMessages(int count) { + checkArgument(count > 0&& count <= 32, "count must be a positive number; max 32"); + queryParameters.replaceValues("numofmessages", Collections.singletonList(count + "")); + return this; + } + + /** + * An integer value that specifies the message's visibility timeout in seconds. The maximum value + * is 2 hours. The default message visibility timeout is 30 seconds. + */ + public GetOptions visibilityTimeout(int timeout) { + checkArgument(timeout > 0 && timeout <= 2 * 60 * 60, + "timeout is in seconds; must be positive and maximum 2 hours"); + queryParameters.replaceValues("visibilitytimeout", Collections.singletonList(timeout + "")); + return this; + } + + public static class Builder { + + /** + * @see GetOptions#maxMessages(int) + */ + public static GetOptions maxMessages(int count) { + GetOptions options = new GetOptions(); + return options.maxMessages(count); + } + + /** + * @see GetOptions#visibilityTimeout(int) + */ + public static GetOptions visibilityTimeout(int visibilityTimeout) { + GetOptions options = new GetOptions(); + return options.visibilityTimeout(visibilityTimeout); + } + } +} diff --git a/azure/src/main/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandler.java b/azure/src/main/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandler.java new file mode 100644 index 0000000000..3af92e0eba --- /dev/null +++ b/azure/src/main/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandler.java @@ -0,0 +1,104 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.jclouds.azure.storage.queue.xml; + +import java.util.Date; +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.azure.storage.queue.domain.QueueMessage; +import org.jclouds.date.DateService; +import org.jclouds.http.functions.ParseSax; + +import com.google.common.collect.Sets; + +/** + * Parses the following XML document: + *

+ * QueueMessagesList + * + * @see + * @author Adrian Cole + */ +public class QueueMessagesListHandler extends ParseSax.HandlerWithResult> { + + private Set messages = Sets.newLinkedHashSet(); + + private String messageId; + private Date insertionTime; + private Date expirationTime; + private int dequeueCount; + private String popReceipt; + private Date timeNextVisible; + private String messageText; + + private StringBuilder currentText = new StringBuilder(); + + private final DateService dateService; + + @Inject + public QueueMessagesListHandler(DateService dateService) { + this.dateService = dateService; + } + + public Set getResult() { + return messages; + } + + public void endElement(String uri, String name, String qName) { + if (qName.equals("MessageId")) { + this.messageId = currentText.toString().trim(); + } else if (qName.equals("InsertionTime")) { + this.insertionTime = parseDate(); + } else if (qName.equals("ExpirationTime")) { + this.expirationTime = parseDate(); + } else if (qName.equals("DequeueCount")) { + this.dequeueCount = Integer.parseInt(currentText.toString().trim()); + } else if (qName.equals("PopReceipt")) { + this.popReceipt = currentText.toString().trim(); + } else if (qName.equals("TimeNextVisible")) { + this.timeNextVisible = parseDate(); + } else if (qName.equals("MessageText")) { + // TODO: figure out why we need to do trim. excess leading whitespace seems to be from + // outside the element + this.messageText = currentText.toString().trim(); + } else if (qName.equals("QueueMessage")) { + messages.add(new QueueMessage(messageId, insertionTime, expirationTime, dequeueCount, + popReceipt, timeNextVisible, messageText)); + messageId = null; + insertionTime = null; + expirationTime = null; + dequeueCount = -1; + popReceipt = null; + timeNextVisible = null; + messageText = null; + } + currentText = new StringBuilder(); + } + + private Date parseDate() { + return dateService.rfc822DateParse(currentText.toString().trim()); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } + +} diff --git a/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClientTest.java b/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClientTest.java index d70579b684..d54d433204 100644 --- a/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClientTest.java +++ b/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueAsyncClientTest.java @@ -20,6 +20,7 @@ package org.jclouds.azure.storage.queue; import static org.jclouds.azure.storage.options.CreateOptions.Builder.withMetadata; import static org.jclouds.azure.storage.options.ListOptions.Builder.maxResults; +import static org.jclouds.azure.storage.queue.options.GetOptions.Builder.maxMessages; import static org.jclouds.azure.storage.queue.options.PutMessageOptions.Builder.withTTL; import static org.testng.Assert.assertEquals; @@ -31,8 +32,10 @@ import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication; import org.jclouds.azure.storage.options.CreateOptions; import org.jclouds.azure.storage.options.ListOptions; import org.jclouds.azure.storage.queue.config.AzureQueueRestClientModule; +import org.jclouds.azure.storage.queue.options.GetOptions; import org.jclouds.azure.storage.queue.options.PutMessageOptions; import org.jclouds.azure.storage.queue.xml.AccountNameEnumerationResultsHandler; +import org.jclouds.azure.storage.queue.xml.QueueMessagesListHandler; import org.jclouds.http.functions.CloseContentAndReturn; import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ReturnTrueIf2xx; @@ -55,6 +58,44 @@ import com.google.inject.name.Names; @Test(groups = "unit", testName = "azurequeue.AzureQueueAsyncClientTest") public class AzureQueueAsyncClientTest extends RestClientTest { + public void testGetMessages() throws SecurityException, NoSuchMethodException, IOException { + Method method = AzureQueueAsyncClient.class.getMethod("getMessages", String.class, + GetOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, + "myqueue"); + + assertRequestLineEquals(httpRequest, + "GET https://myaccount.queue.core.windows.net/myqueue/messages HTTP/1.1"); + assertHeadersEqual(httpRequest, "x-ms-version: 2009-09-19\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseSax.class); + assertSaxResponseParserClassEquals(method, QueueMessagesListHandler.class); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + + public void testGetMessagesOptions() throws SecurityException, NoSuchMethodException, + IOException { + Method method = AzureQueueAsyncClient.class.getMethod("getMessages", String.class, + GetOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, + "myqueue", maxMessages(1).visibilityTimeout(30)); + + assertRequestLineEquals( + httpRequest, + "GET https://myaccount.queue.core.windows.net/myqueue/messages?numofmessages=1&visibilitytimeout=30 HTTP/1.1"); + assertHeadersEqual(httpRequest, "x-ms-version: 2009-09-19\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseSax.class); + assertSaxResponseParserClassEquals(method, QueueMessagesListHandler.class); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + public void testListQueues() throws SecurityException, NoSuchMethodException, IOException { Method method = AzureQueueAsyncClient.class.getMethod("listQueues", ListOptions[].class); GeneratedHttpRequest httpRequest = processor.createRequest(method); diff --git a/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueClientLiveTest.java b/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueClientLiveTest.java index df1b7678cd..e09d1e751c 100644 --- a/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueClientLiveTest.java +++ b/azure/src/test/java/org/jclouds/azure/storage/queue/AzureQueueClientLiveTest.java @@ -18,22 +18,26 @@ */ package org.jclouds.azure.storage.queue; +import static org.jclouds.azure.storage.options.ListOptions.Builder.prefix; +import static org.jclouds.azure.storage.queue.options.GetOptions.Builder.maxMessages; +import static org.jclouds.azure.storage.queue.options.PutMessageOptions.Builder.withTTL; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import java.security.SecureRandom; +import java.util.Set; import org.jclouds.azure.storage.domain.BoundedSet; import org.jclouds.azure.storage.options.CreateOptions; -import org.jclouds.azure.storage.options.ListOptions; +import org.jclouds.azure.storage.queue.domain.QueueMessage; import org.jclouds.azure.storage.queue.domain.QueueMetadata; -import org.jclouds.azure.storage.queue.options.PutMessageOptions; import org.jclouds.http.HttpResponseException; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Iterables; import com.google.inject.Injector; /** @@ -41,7 +45,7 @@ import com.google.inject.Injector; * * @author Adrian Cole */ -@Test(groups = "live", sequential = true, testName = "cloudservers.AzureQueueClientLiveTest") +@Test(groups = "live", sequential = true, testName = "azurequeue.AzureQueueClientLiveTest") public class AzureQueueClientLiveTest { String account; @@ -96,8 +100,8 @@ public class AzureQueueClientLiveTest { @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testCreateQueue" }) public void testListQueuesWithOptions() throws Exception { - BoundedSet response = connection.listQueues(ListOptions.Builder.prefix( - privateQueue).maxResults(1)); + BoundedSet response = connection + .listQueues(prefix(privateQueue).maxResults(1)); assert null != response; long initialQueueCount = response.size(); assertTrue(initialQueueCount >= 0); @@ -107,11 +111,21 @@ public class AzureQueueClientLiveTest { @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testCreateQueue" }) public void testPutMessage() throws Exception { - connection.putMessage(privateQueue, "holycow", PutMessageOptions.Builder.withTTL(4)); - // TODO loop for up to 30 seconds checking if they are really gone + connection.putMessage(privateQueue, "holycow", withTTL(4)); + connection.putMessage(privateQueue, "holymoo", withTTL(4)); } @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testPutMessage" }) + public void testGetMessages() throws Exception { + Set messages = connection.getMessages(privateQueue, maxMessages(2)); + QueueMessage m1 = Iterables.get(messages, 0); + assertEquals(m1.getMessageText(), "holycow"); + QueueMessage m2 = Iterables.get(messages, 1); + assertEquals(m2.getMessageText(), "holymoo"); + assertEquals(connection.getMessages(privateQueue).size(), 0); + } + + @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testGetMessages" }) public void testDeleteQueue() throws Exception { connection.clearMessages(privateQueue); connection.deleteQueue(privateQueue); diff --git a/azure/src/test/java/org/jclouds/azure/storage/queue/options/GetOptionsTest.java b/azure/src/test/java/org/jclouds/azure/storage/queue/options/GetOptionsTest.java new file mode 100644 index 0000000000..81915b8e3c --- /dev/null +++ b/azure/src/test/java/org/jclouds/azure/storage/queue/options/GetOptionsTest.java @@ -0,0 +1,75 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.jclouds.azure.storage.queue.options; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +/** + * Tests behavior of {@code GetOptions} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "azurequeue.GetOptionsTest") +public class GetOptionsTest { + + public void testMaxMessages() { + GetOptions options = new GetOptions().maxMessages(1); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("numofmessages")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMaxMessagesTooSmall() { + new GetOptions().maxMessages(0); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testMaxMessagesTooBig() { + new GetOptions().maxMessages(33); + } + + public void testMaxMessagesStatic() { + GetOptions options = GetOptions.Builder.maxMessages(1); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("numofmessages")); + } + + public void testVisibilityTimeout() { + GetOptions options = new GetOptions().visibilityTimeout(1); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("visibilitytimeout")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testVisibilityTimeoutTooSmall() { + new GetOptions().visibilityTimeout(0); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testVisibilityTimeoutTooBig() { + new GetOptions().visibilityTimeout((2 * 60 * 60) + 1); + } + + public void testVisibilityTimeoutStatic() { + GetOptions options = GetOptions.Builder.visibilityTimeout(1); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("visibilitytimeout")); + } + +} diff --git a/azure/src/test/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandlerTest.java b/azure/src/test/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandlerTest.java new file mode 100644 index 0000000000..8df82b0f15 --- /dev/null +++ b/azure/src/test/java/org/jclouds/azure/storage/queue/xml/QueueMessagesListHandlerTest.java @@ -0,0 +1,68 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.jclouds.azure.storage.queue.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Set; + +import org.jclouds.azure.storage.queue.domain.QueueMessage; +import org.jclouds.date.DateService; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.inject.internal.ImmutableSet; + +/** + * Tests behavior of {@code QueueMessagesListHandler} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "azurequeue.QueueMessagesListHandlerTest") +public class QueueMessagesListHandlerTest extends BaseHandlerTest { + private DateService dateService; + + @BeforeTest + protected void setUpInjector() { + super.setUpInjector(); + dateService = injector.getInstance(DateService.class); + } + + public void testApplyInputStream() { + InputStream is = getClass().getResourceAsStream("/queue/test_get_messages.xml"); + Set expected = ImmutableSet. of( + + new QueueMessage("43190737-06f4-4ccf-b600-28f410707df3", dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:08 GMT"), dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:13 GMT"), 1, + "AgAAAAEAAADZcwAADlwO5JQJywE=", dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:39 GMT"), "holycow"), + new QueueMessage("7b75a124-7efe-45a2-97e4-388664319718", dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:09 GMT"), dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:13 GMT"), 1, + "AgAAAAEAAADZcwAADlwO5JQJywE=", dateService + .rfc822DateParse("Fri, 11 Jun 2010 18:35:39 GMT"), "holymoo")); + + Set result = factory.create( + injector.getInstance(QueueMessagesListHandler.class)).parse(is); + assertEquals(result, expected); + } +} diff --git a/azure/src/test/resources/queue/test_get_messages.xml b/azure/src/test/resources/queue/test_get_messages.xml new file mode 100644 index 0000000000..d888d600e1 --- /dev/null +++ b/azure/src/test/resources/queue/test_get_messages.xml @@ -0,0 +1,21 @@ + + + + 43190737-06f4-4ccf-b600-28f410707df3 + Fri, 11 Jun 2010 18:35:08 GMT + Fri, 11 Jun 2010 18:35:13 GMT + 1 + AgAAAAEAAADZcwAADlwO5JQJywE= + Fri, 11 Jun 2010 18:35:39 GMT + holycow + + + 7b75a124-7efe-45a2-97e4-388664319718 + Fri, 11 Jun 2010 18:35:09 GMT + Fri, 11 Jun 2010 18:35:13 GMT + 1 + AgAAAAEAAADZcwAADlwO5JQJywE= + Fri, 11 Jun 2010 18:35:39 GMT + holymoo + + \ No newline at end of file