From f8a34509e69741ad526ded64d3c0d5ab0e207d23 Mon Sep 17 00:00:00 2001 From: "Andy P. Frantz" Date: Thu, 13 Feb 2014 09:04:51 -0500 Subject: [PATCH] Creating interfaces for Client and Queue to facilitate unit testing --- src/main/java/io/iron/ironmq/Client.java | 10 +- src/main/java/io/iron/ironmq/IClient.java | 28 ++++ src/main/java/io/iron/ironmq/IQueue.java | 162 +++++++++++++++++++ src/main/java/io/iron/ironmq/Queue.java | 6 +- src/test/java/io/iron/ironmq/IronMQTest.java | 4 +- 5 files changed, 200 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/iron/ironmq/IClient.java create mode 100644 src/main/java/io/iron/ironmq/IQueue.java diff --git a/src/main/java/io/iron/ironmq/Client.java b/src/main/java/io/iron/ironmq/Client.java index 74f7760..ca68774 100644 --- a/src/main/java/io/iron/ironmq/Client.java +++ b/src/main/java/io/iron/ironmq/Client.java @@ -25,7 +25,7 @@ /** * The Client class provides access to the IronMQ service. */ -public class Client { +public class Client implements IClient { static final private String apiVersion = "1"; static final Random rand = new Random(); @@ -97,19 +97,19 @@ public Client(String projectId, String token, Cloud cloud) { * * @param name The name of the Queue to create. */ - public Queue queue(String name) { + public IQueue queue(String name) { return new Queue(this, name); } - Reader delete(String endpoint) throws IOException { + public Reader delete(String endpoint) throws IOException { return request("DELETE", endpoint, null); } - Reader get(String endpoint) throws IOException { + public Reader get(String endpoint) throws IOException { return request("GET", endpoint, null); } - Reader post(String endpoint, String body) throws IOException { + public Reader post(String endpoint, String body) throws IOException { return request("POST", endpoint, body); } diff --git a/src/main/java/io/iron/ironmq/IClient.java b/src/main/java/io/iron/ironmq/IClient.java new file mode 100644 index 0000000..bb327c5 --- /dev/null +++ b/src/main/java/io/iron/ironmq/IClient.java @@ -0,0 +1,28 @@ +package io.iron.ironmq; + +import java.io.IOException; +import java.io.Reader; +import java.util.Map; + +public interface IClient { + + /** + * Returns a Queue using the given name. + * The network is not accessed during this call. + * + * @param name The name of the Queue to create. + */ + public abstract IQueue queue(String name); + + public abstract Map getOptions(); + + public abstract Object getOption(String name); + + public abstract String getEnv(); + + Reader delete(String endpoint) throws IOException; + + Reader get(String endpoint) throws IOException; + + Reader post(String endpoint, String body) throws IOException; +} \ No newline at end of file diff --git a/src/main/java/io/iron/ironmq/IQueue.java b/src/main/java/io/iron/ironmq/IQueue.java new file mode 100644 index 0000000..338b42d --- /dev/null +++ b/src/main/java/io/iron/ironmq/IQueue.java @@ -0,0 +1,162 @@ +package io.iron.ironmq; + +import java.io.IOException; + +public interface IQueue { + + /** + * Retrieves a Message from the queue. If there are no items on the queue, an + * EmptyQueueException is thrown. + * + * @throws EmptyQueueException If the queue is empty. + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract Message get() throws IOException; + + /** + * Retrieves Messages from the queue. If there are no items on the queue, an + * EmptyQueueException is thrown. + * @param numberOfMessages The number of messages to receive. Max. is 100. + * @throws EmptyQueueException If the queue is empty. + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract Messages get(int numberOfMessages) throws IOException; + + /** + * Retrieves Messages from the queue. If there are no items on the queue, an + * EmptyQueueException is thrown. + * @param numberOfMessages The number of messages to receive. Max. is 100. + * @param timeout timeout in seconds. + * @throws EmptyQueueException If the queue is empty. + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract Messages get(int numberOfMessages, int timeout) + throws IOException; + + /** + * Peeking at a queue returns the next messages on the queue, but it does not reserve them. + * If there are no items on the queue, an EmptyQueueException is thrown. + * + * @throws EmptyQueueException If the queue is empty. + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract Message peek() throws IOException; + + /** + * Peeking at a queue returns the next messages on the queue, but it does not reserve them. + * + * @param numberOfMessages The number of messages to receive. Max. is 100. + * @throws EmptyQueueException If the queue is empty. + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract Messages peek(int numberOfMessages) throws IOException; + + /** + * Touching a reserved message extends its timeout to the duration specified when the message was created. + * + * @param id The ID of the message to delete. + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract void touchMessage(String id) throws IOException; + + /** + * Deletes a Message from the queue. + * + * @param id The ID of the message to delete. + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract void deleteMessage(String id) throws IOException; + + /** + * Destroy the queue. + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract void destroy() throws IOException; + + /** + * Deletes a Message from the queue. + * + * @param msg The message to delete. + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract void deleteMessage(Message msg) throws IOException; + + /** + * Pushes a message onto the queue. + * + * @param msg The body of the message to push. + * @return The new message's ID + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract String push(String msg) throws IOException; + + /** + * Pushes a message onto the queue. + * + * @param msg The body of the message to push. + * @param timeout The message's timeout in seconds. + * @return The new message's ID + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract String push(String msg, long timeout) throws IOException; + + /** + * Pushes a message onto the queue. + * + * @param msg The body of the message to push. + * @param timeout The message's timeout in seconds. + * @param delay The message's delay in seconds. + * @return The new message's ID + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract String push(String msg, long timeout, long delay) + throws IOException; + + /** + * Pushes a message onto the queue. + * + * @param msg The body of the message to push. + * @param timeout The message's timeout in seconds. + * @param delay The message's delay in seconds. + * @param expiresIn The message's expiration offset in seconds. + * @return The new message's ID + * + * @throws HTTPException If the IronMQ service returns a status other than 200 OK. + * @throws IOException If there is an error accessing the IronMQ server. + */ + public abstract String push(String msg, long timeout, long delay, + long expiresIn) throws IOException; + + /** + * Clears the queue off all messages + * @throws IOException + */ + public abstract void clear() throws IOException; + + /** + * @return the name of this queue + */ + public abstract String getName(); + + public abstract int getSize() throws IOException; + +} \ No newline at end of file diff --git a/src/main/java/io/iron/ironmq/Queue.java b/src/main/java/io/iron/ironmq/Queue.java index 9eb9be7..0db93f2 100644 --- a/src/main/java/io/iron/ironmq/Queue.java +++ b/src/main/java/io/iron/ironmq/Queue.java @@ -9,11 +9,11 @@ /** * The Queue class represents a specific IronMQ queue bound to a client. */ -public class Queue { - final private Client client; +public class Queue implements IQueue { + final private IClient client; final private String name; - public Queue(Client client, String name) { + public Queue(IClient client, String name) { this.client = client; this.name = name; } diff --git a/src/test/java/io/iron/ironmq/IronMQTest.java b/src/test/java/io/iron/ironmq/IronMQTest.java index f9952f5..2fe300a 100644 --- a/src/test/java/io/iron/ironmq/IronMQTest.java +++ b/src/test/java/io/iron/ironmq/IronMQTest.java @@ -10,7 +10,7 @@ public class IronMQTest { @Test public void testClient() throws IOException { Client c = new Client(); - Queue q = c.queue("test-queue"); + IQueue q = c.queue("test-queue"); q.clear(); @@ -31,7 +31,7 @@ public class IronMQTest { @Test(expected=HTTPException.class) public void testErrorResponse() throws IOException { // intentionally invalid project/token combination Client c = new Client("4444444444444", "aaaaaa", Cloud.ironAWSUSEast); - Queue q = c.queue("test-queue"); + IQueue q = c.queue("test-queue"); q.push("test"); }