Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updating test to fit api change
  • Loading branch information
richard-julien committed Mar 22, 2012
1 parent de3d723 commit 1909e4e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
.idea .idea
*.iml *.iml
**/target/*
.gitignore.swp
118 changes: 96 additions & 22 deletions client/src/test/java/io/iron/ironworker/client/APIClientTest.java
Expand Up @@ -3,13 +3,17 @@
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.apache.commons.io.IOUtils;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Map;


import static org.junit.Assert.assertEquals; import static java.util.UUID.randomUUID;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.*;




/** /**
Expand All @@ -21,13 +25,16 @@ public class APIClientTest {
//System environment variables for iron worker. //System environment variables for iron worker.
public static String IRON_IO_TOKEN = "IRON_IO_TOKEN"; public static String IRON_IO_TOKEN = "IRON_IO_TOKEN";
public static String IRON_IO_PROJECT_ID = "IRON_IO_PROJECT_ID"; public static String IRON_IO_PROJECT_ID = "IRON_IO_PROJECT_ID";
public static Map<String, Object> IRON_IO_PARAMS = null;
//Default worker //Default worker
public static final String UNIT_TEST_WORKER = "UnitTestWorker"; public static final String WORKER_NAME = randomUUID().toString();
public static final String SIMPLE_WORKER_ZIP_FILE = "hello_world_worker.zip"; 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"; public static final String WORKER_RUNTIME = "ruby";
//Token and project //Token and project
private static String _ironToken; private static String _ironToken;
private static String _ironProjectId; private static String _ironProjectId;
private static String _codeId;


@BeforeClass @BeforeClass
public static void init() { public static void init() {
Expand All @@ -44,41 +51,108 @@ public void shouldHaveEnvironmentVariable() {
@Test(expected = APIException.class) @Test(expected = APIException.class)
public void shouldConnexionFail() throws APIException { public void shouldConnexionFail() throws APIException {
APIClient client = new APIClient("badToken", "badProject"); APIClient client = new APIClient("badToken", "badProject");
client.codesList(null); client.codesList(IRON_IO_PARAMS);
} }


@Test @Test
public void shouldCodesCreate() throws APIException { public void shouldCodesCreate() throws APIException {
APIClient client = new APIClient(_ironToken, _ironProjectId); 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( 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()); assertEquals("Upload successful.", codeCreate.get("msg").getAsString());
} }


@Test @Test
public void shouldCodesList() throws APIException { public void shouldCodesList() throws APIException {
APIClient client = new APIClient(_ironToken, _ironProjectId); 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); assertNotNull("Codes list could not be null", codesList);
JsonArray codes = codesList.getAsJsonArray("codes"); JsonArray codes = codesList.getAsJsonArray("codes");
//Search for recently created code package JsonObject code = fetchCodeFromName(codes, WORKER_NAME);
JsonElement previousUploadedCode = null; _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) { for (JsonElement code : codes) {
String name = code.getAsJsonObject().get("name").getAsString(); String codeName = code.getAsJsonObject().get("name").getAsString();
assertNotNull("Worker name could not be null", name); assertNotNull("Worker name could not be null", codeName);
if (name.equals(UNIT_TEST_WORKER)) { if (codeName.equals(name)) {
previousUploadedCode = code; return code.getAsJsonObject();
break;
} }
} }
assertNotNull("Can't find previously uploaded worker", previousUploadedCode); throw new IllegalArgumentException("Can't find previously uploaded worker" + name);
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);
} }
} }


0 comments on commit 1909e4e

Please sign in to comment.