Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 16, 2018
2 parents 6009d3c + 3a26da2 commit 7d8c998
Show file tree
Hide file tree
Showing 3 changed files with 543 additions and 3 deletions.
167 changes: 164 additions & 3 deletions src/test/java/com/amihaiemil/docker/RtContainersTestCase.java
Expand Up @@ -25,14 +25,175 @@
*/
package com.amihaiemil.docker;

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Condition;
import com.amihaiemil.docker.mock.PayloadOf;
import com.amihaiemil.docker.mock.Response;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpStatus;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

/**
* Unit tests for RtContainers.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @see <a href="https://docs.docker.com/engine/api/v1.30/#operation/ContainerCreate">Docker ContainerCreate API</a>
* @version $Id$
* @since 0.0.1
* @todo #26:30min Write unit tests for all the methods of RtContainers.
* AssertRequest should be passed in the ctor, with the right expected
* response and predicates.
* @checkstyle MethodName (500 lines)
* @todo #47:30min Implement tests on the rest of the methods of RtContainers.
* Currently only testing the create(String name) method.
* @todo #47:30min Impediment: once #45 is done, unignore the following tests
* and refactor accordingly if needed: ioErrorIfResponseIs400,
* ioErrorIfResponseIs404, ioErrorIfResponseIs406, ioErrorIfResponseIs409,
* ioErrorIfResponseIs500.
*/
public final class RtContainersTestCase {
/**
* The request should be welformed.
*
* @throws Exception unexpected
*/
@Test
public void welformedRequestForCreateContainerFromImage() throws Exception {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_CREATED, "{ \"Id\": \"df2419f4\" }"
),
new Condition(
"The 'Content-Type' header must be set.",
req -> req.getHeaders("Content-Type").length > 0
),
new Condition(
"Content-Type must be 'application/json'.",
// @checkstyle LineLength (1 line)
req -> "application/json".equals(req.getHeaders("Content-Type")[0].getValue())
),
new Condition(
"Resource path must be /create",
// @checkstyle LineLength (1 line)
req -> req.getRequestLine().getUri().endsWith("/create")
),
new Condition(
"The 'Image' attribute must be set in the payload.",
// @checkstyle LineLength (1 line)
req -> "some_image".equals(new PayloadOf(req).getString("Image"))
)
), URI.create("http://localhost/test")
).create("some_image");
}

/**
* Returns a container if the service call is successful.
*
* @throws Exception unexpected
*/
@Test
public void returnsContainerIfCallIsSuccessful() throws Exception {
MatcherAssert.assertThat(
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_CREATED,
"{ \"Id\": \"df2419f4\" }"
)
), URI.create("http://localhost/test")
).create("some_image"),
Matchers.notNullValue()
);
}

/**
* Must fail if docker responds with error code 400.
*
* @throws IOException due to code 400
*/
@Ignore
@Test(expected = IOException.class)
public void ioErrorIfResponseIs400() throws IOException {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_BAD_REQUEST,
""
)
), URI.create("http://localhost/test")
).create("some_image");
}

/**
* Must fail if docker responds with error code 404.
*
* @throws IOException due to code 404
*/
@Ignore
@Test(expected = IOException.class)
public void ioErrorIfResponseIs404() throws IOException {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_NOT_FOUND,
""
)
), URI.create("http://localhost/test")
).create("some_image");
}

/**
* Must fail if docker responds with error code 406.
*
* @throws IOException due to code 406
*/
@Ignore
@Test(expected = IOException.class)
public void ioErrorIfResponseIs406() throws IOException {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_NOT_ACCEPTABLE,
""
)
), URI.create("http://localhost/test")
).create("some_image");
}

/**
* Must fail if docker responds with error code 409.
*
* @throws IOException due to code 409
*/
@Ignore
@Test(expected = IOException.class)
public void ioErrorIfResponseIs409() throws IOException {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_CONFLICT,
""
)
), URI.create("http://localhost/test")
).create("some_image");
}

/**
* Must fail if docker responds with error code 500.
*
* @throws IOException due to code 500
*/
@Ignore
@Test(expected = IOException.class)
public void ioErrorIfResponseIs500() throws IOException {
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_INTERNAL_SERVER_ERROR,
""
)
), URI.create("http://localhost/test")
).create("some_image");
}
}
169 changes: 169 additions & 0 deletions src/test/java/com/amihaiemil/docker/mock/PayloadOf.java
@@ -0,0 +1,169 @@
package com.amihaiemil.docker.mock;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;

/**
* JSON payload of an HttpRequest.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class PayloadOf implements JsonObject {
/**
* The request's payload.
*/
private final JsonObject json;

/**
* Ctor.
*
* @param request The http request
* @throws IllegalStateException if the request's payload cannot be read
*/
public PayloadOf(final HttpRequest request) {
try {
if (request instanceof HttpEntityEnclosingRequest) {
this.json = Json.createReader(
((HttpEntityEnclosingRequest) request).getEntity()
.getContent()
).readObject();
} else {
this.json = Json.createObjectBuilder().build();
}
} catch (final IOException ex) {
throw new IllegalStateException("Cannot read request payload", ex);
}
}

@Override
public JsonArray getJsonArray(final String name) {
return this.json.getJsonArray(name);
}

@Override
public JsonObject getJsonObject(final String name) {
return this.json.getJsonObject(name);
}

@Override
public JsonNumber getJsonNumber(final String name) {
return this.json.getJsonNumber(name);
}

@Override
public JsonString getJsonString(final String name) {
return this.json.getJsonString(name);
}

@Override
public String getString(final String name) {
return this.json.getString(name);
}

@Override
public String getString(final String name, final String defaultValue) {
return this.json.getString(name, defaultValue);
}

@Override
public int getInt(final String name) {
return this.json.getInt(name);
}

@Override
public int getInt(final String name, final int defaultValue) {
return this.json.getInt(name, defaultValue);
}

@Override
public boolean getBoolean(final String name) {
return this.json.getBoolean(name);
}

@Override
public boolean getBoolean(final String name, final boolean defaultValue) {
return this.json.getBoolean(name, defaultValue);
}

@Override
public boolean isNull(final String name) {
return this.json.isNull(name);
}

@Override
public ValueType getValueType() {
return this.json.getValueType();
}

@Override
public int size() {
return this.json.size();
}

@Override
public boolean isEmpty() {
return this.json.isEmpty();
}

@Override
public boolean containsKey(final Object key) {
return this.json.containsKey(key);
}

@Override
public boolean containsValue(final Object value) {
return this.json.containsValue(value);
}

@Override
public JsonValue get(final Object key) {
return this.json.get(key);
}

@Override
public JsonValue put(final String key, final JsonValue value) {
return this.json.put(key, value);
}

@Override
public JsonValue remove(final Object key) {
return this.json.remove(key);
}

@Override
public void putAll(final Map<? extends String, ? extends JsonValue> map) {
this.json.putAll(map);
}

@Override
public void clear() {
this.json.clear();
}

@Override
public Set<String> keySet() {
return this.json.keySet();
}

@Override
public Collection<JsonValue> values() {
return this.json.values();
}

@Override
public Set<Entry<String, JsonValue>> entrySet() {
return this.json.entrySet();
}
}

3 comments on commit 7d8c998

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7d8c998 Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 26-a8980bc9 disappeared from src/test/java/com/amihaiemil/docker/RtContainersTestCase.java, that's why I closed #47. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7d8c998 Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 47-15f4a258 discovered in src/test/java/com/amihaiemil/docker/RtContainersTestCase.java and submitted as #53. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7d8c998 Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 47-dbe85f1e discovered in src/test/java/com/amihaiemil/docker/RtContainersTestCase.java and submitted as #54. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.