Browse files

First implementation of unit testing with JUnit

  • Loading branch information...
1 parent 562fb53 commit 268fa07a3e89dc91891cd4b6a56cb9f202ac786d @richard-julien committed Mar 22, 2012
View
2 .gitignore
@@ -1,2 +1,4 @@
.idea
*.iml
+.DS_Store
+**/target
View
5 client/pom.xml
@@ -34,5 +34,10 @@
<artifactId>joda-time</artifactId>
<version>[2.0,2.2)</version>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>[4.0,4.10)</version>
+ </dependency>
</dependencies>
</project>
View
159 client/src/test/java/io/iron/ironworker/client/APIClientTest.java
@@ -0,0 +1,159 @@
+package io.iron.ironworker.client;
+
+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.Ignore;
+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.*;
+
+
+/**
+ * User: Julien
+ * Date: 21/03/12
+ * Time: 23:42
+ */
+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<String, Object> IRON_IO_PARAMS = null;
+ //Default worker
+ public static final String WORKER_NAME = "TestUnitWorker";
+ 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() {
+ _ironToken = System.getenv(IRON_IO_TOKEN);
+ _ironProjectId = System.getenv(IRON_IO_PROJECT_ID);
+ }
+
+ @Test
+ public void shouldHaveEnvironmentVariable() {
+ assertNotNull(IRON_IO_PROJECT_ID + " environment variable not set", _ironProjectId);
+ assertNotNull(IRON_IO_TOKEN + " environment variable not set", _ironToken);
+ }
+
+ @Test(expected = APIException.class)
+ public void shouldConnexionFail() throws APIException {
+ APIClient client = new APIClient("badToken", "badProject");
+ client.codesList(IRON_IO_PARAMS);
+ }
+
+ @Test
+ public void shouldCodesCreate() throws APIException {
+ APIClient client = new APIClient(_ironToken, _ironProjectId);
+ URL resource = getClass().getClassLoader().getResource(WORKER_FILE_ZIP);
+ JsonObject codeCreate = client.codesCreate(
+ 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(IRON_IO_PARAMS);
+ assertNotNull(codesList);
+ JsonArray codes = codesList.getAsJsonArray("codes");
+ 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_ZIP);
+ byte[] bytesFromClient = IOUtils.toByteArray(resourceAsStream);
+ assertArrayEquals(bytesFromClient, byteFromIron);
+ }
+
+ @Test
+ @Ignore //TODO : activate test when REST API could re enable deleted worker
+ public void shouldCodesDelete() throws APIException {
+ APIClient client = new APIClient(_ironToken, _ironProjectId);
+ JsonObject deleteMessage = client.codesDelete(_codeId);
+ assertNotNull(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);
+ assertNotNull(revision.get("id").getAsString());
+ assertNotNull(revision.get("code_id").getAsString());
+ assertNotNull(revision.get("project_id").getAsString());
+ assertNotNull(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);
+ String id = code.get("id").getAsString();
+ assertNotNull(id);
+ assertNotNull(code.get("project_id").getAsString());
+ assertNotNull(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 codeName = code.getAsJsonObject().get("name").getAsString();
+ assertNotNull(codeName);
+ if (codeName.equals(name)) {
+ return code.getAsJsonObject();
+ }
+ }
+ throw new IllegalArgumentException("Can't find previously uploaded worker name : " + name);
+ }
+}
+
View
BIN client/src/test/resources/hello_world_worker.zip
Binary file not shown.

0 comments on commit 268fa07

Please sign in to comment.