Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating interfaces for Client and Queue to facilitate unit testing #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/io/iron/ironmq/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/iron/ironmq/IClient.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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;
}
162 changes: 162 additions & 0 deletions src/main/java/io/iron/ironmq/IQueue.java
Original file line number Diff line number Diff line change
@@ -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;

}
6 changes: 3 additions & 3 deletions src/main/java/io/iron/ironmq/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/iron/ironmq/IronMQTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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");
}
Expand Down