From 1909e4ebfc44dff1f57ea57f98b4be882e26c9f3 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Thu, 22 Mar 2012 11:36:20 +0100 Subject: [PATCH] Updating test to fit api change --- .gitignore | 2 + .../iron/ironworker/client/APIClientTest.java | 118 ++++++++++++++---- 2 files changed, 98 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index c38fa4e..9fbe57a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea *.iml +**/target/* +.gitignore.swp diff --git a/client/src/test/java/io/iron/ironworker/client/APIClientTest.java b/client/src/test/java/io/iron/ironworker/client/APIClientTest.java index 3222992..24d69e3 100644 --- a/client/src/test/java/io/iron/ironworker/client/APIClientTest.java +++ b/client/src/test/java/io/iron/ironworker/client/APIClientTest.java @@ -3,13 +3,17 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import org.apache.commons.io.IOUtils; import org.junit.BeforeClass; import org.junit.Test; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static java.util.UUID.randomUUID; +import static org.junit.Assert.*; /** @@ -21,13 +25,16 @@ public class APIClientTest { //System environment variables for iron worker. public static String IRON_IO_TOKEN = "IRON_IO_TOKEN"; public static String IRON_IO_PROJECT_ID = "IRON_IO_PROJECT_ID"; + public static Map IRON_IO_PARAMS = null; //Default worker - public static final String UNIT_TEST_WORKER = "UnitTestWorker"; - public static final String SIMPLE_WORKER_ZIP_FILE = "hello_world_worker.zip"; + public static final String WORKER_NAME = randomUUID().toString(); + public static final String WORKER_FILE = "runner.rb"; + public static final String WORKER_FILE_ZIP = "hello_world_worker.zip"; public static final String WORKER_RUNTIME = "ruby"; //Token and project private static String _ironToken; private static String _ironProjectId; + private static String _codeId; @BeforeClass public static void init() { @@ -44,41 +51,108 @@ public void shouldHaveEnvironmentVariable() { @Test(expected = APIException.class) public void shouldConnexionFail() throws APIException { APIClient client = new APIClient("badToken", "badProject"); - client.codesList(null); + client.codesList(IRON_IO_PARAMS); } @Test public void shouldCodesCreate() throws APIException { APIClient client = new APIClient(_ironToken, _ironProjectId); - URL resource = getClass().getClassLoader().getResource(SIMPLE_WORKER_ZIP_FILE); + URL resource = getClass().getClassLoader().getResource(WORKER_FILE_ZIP); JsonObject codeCreate = client.codesCreate( - UNIT_TEST_WORKER, resource.getFile(), WORKER_RUNTIME, SIMPLE_WORKER_ZIP_FILE); + WORKER_NAME, resource.getFile(), WORKER_RUNTIME, WORKER_FILE); assertEquals("Upload successful.", codeCreate.get("msg").getAsString()); } @Test public void shouldCodesList() throws APIException { APIClient client = new APIClient(_ironToken, _ironProjectId); - JsonObject codesList = client.codesList(null); + JsonObject codesList = client.codesList(IRON_IO_PARAMS); assertNotNull("Codes list could not be null", codesList); JsonArray codes = codesList.getAsJsonArray("codes"); - //Search for recently created code package - JsonElement previousUploadedCode = null; + JsonObject code = fetchCodeFromName(codes, WORKER_NAME); + _codeId = checkCodePackageValidity(code); + } + + @Test + public void shouldCodesGet() throws APIException { + APIClient client = new APIClient(_ironToken, _ironProjectId); + JsonObject code = client.codesGet(_codeId); + checkCodePackageValidity(code); + } + + @Test + public void shouldCodesRevisions() throws APIException { + APIClient client = new APIClient(_ironToken, _ironProjectId); + JsonObject codeRevisions = client.codesRevisions(_codeId, IRON_IO_PARAMS); + JsonArray revisions = codeRevisions.getAsJsonArray("revisions"); + assertEquals(1, revisions.size()); + checkCodeRevisionValidity(revisions.iterator().next().getAsJsonObject()); + } + + @Test + public void shouldCodesDownload() throws APIException, IOException { + APIClient client = new APIClient(_ironToken, _ironProjectId); + byte[] byteFromIron = client.codesDownload(_codeId, IRON_IO_PARAMS); + InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(WORKER_FILE); + byte[] bytesFromClient = IOUtils.toByteArray(resourceAsStream); + assertArrayEquals(bytesFromClient, byteFromIron); + } + + @Test + public void shouldCodesDelete() throws APIException { + APIClient client = new APIClient(_ironToken, _ironProjectId); + JsonObject deleteMessage = client.codesDelete(_codeId); + assertNotNull("Delete message could not be null", deleteMessage); + assertEquals("Deleted", deleteMessage.get("msg").getAsString()); + } + + /** + * Check the validity of revision object. + * + * @param revision the json object that represent a revision + */ + private void checkCodeRevisionValidity(JsonObject revision) { + assertNotNull("Revision object could not be null", revision); + assertNotNull("Revision id could not be null", revision.get("id").getAsString()); + assertNotNull("Code id could not be null", revision.get("code_id").getAsString()); + assertNotNull("Project id could not be null", revision.get("project_id").getAsString()); + assertEquals("1", revision.get("rev").getAsString()); + assertEquals(WORKER_RUNTIME, revision.get("runtime").getAsString()); + assertEquals(WORKER_NAME, revision.get("name").getAsString()); + assertEquals(WORKER_FILE, revision.get("file_name").getAsString()); + } + + /** + * Check the validity of code object. + * + * @param code the json object that represent a code package + * @return id of the code + */ + private String checkCodePackageValidity(JsonObject code) { + assertNotNull("Code object could not be null", code); + String id = code.get("id").getAsString(); + assertNotNull("Code id could not be null", id); + assertNotNull("Project id could not be null", code.get("project_id").getAsString()); + assertNotNull("Lastest change could not be null", code.get("latest_change").getAsString()); + return id; + } + + /** + * Find a code by name. + * + * @param codes the list of available codes + * @param name the name of the code to find + * @return the json object code or IllegalArgumentException + */ + private JsonObject fetchCodeFromName(JsonArray codes, String name) { for (JsonElement code : codes) { - String name = code.getAsJsonObject().get("name").getAsString(); - assertNotNull("Worker name could not be null", name); - if (name.equals(UNIT_TEST_WORKER)) { - previousUploadedCode = code; - break; + String codeName = code.getAsJsonObject().get("name").getAsString(); + assertNotNull("Worker name could not be null", codeName); + if (codeName.equals(name)) { + return code.getAsJsonObject(); } } - assertNotNull("Can't find previously uploaded worker", previousUploadedCode); - String id = previousUploadedCode.getAsJsonObject().get("id").getAsString(); - assertNotNull("Code id could not be null", id); - String project_id = previousUploadedCode.getAsJsonObject().get("project_id").getAsString(); - assertNotNull("Project id could not be null", project_id); - String latest_change = previousUploadedCode.getAsJsonObject().get("latest_change").getAsString(); - assertNotNull("Lastest change could not be null", latest_change); + throw new IllegalArgumentException("Can't find previously uploaded worker" + name); } }