From 767160231d0cf9810f33a39fe69f1b667fa65be1 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 26 Dec 2018 17:18:38 -0200 Subject: [PATCH 1/2] For #236: Added the rest of RtNetwork methods and fixed tests --- .../java/com/amihaiemil/docker/RtNetwork.java | 79 +++++++++++++------ .../amihaiemil/docker/RtNetworkTestCase.java | 71 ++++++++++------- 2 files changed, 100 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/RtNetwork.java b/src/main/java/com/amihaiemil/docker/RtNetwork.java index d920b0a2..60fdddff 100644 --- a/src/main/java/com/amihaiemil/docker/RtNetwork.java +++ b/src/main/java/com/amihaiemil/docker/RtNetwork.java @@ -28,7 +28,12 @@ import java.io.IOException; import java.net.URI; import javax.json.JsonObject; + +import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; /** * Runtime {@link Network}. @@ -75,47 +80,75 @@ final class RtNetwork extends JsonResource implements Network { @Override public JsonObject inspect() throws IOException, UnexpectedResponseException { - throw new UnsupportedOperationException( - String.join(" ", - "Network.inspect() is not yet implemented.", - "If you can contribute please", - "do it here: https://www.github.com/amihaiemil/docker-java-api" - ) - ); + return new Inspection(this.client, this.baseUri.toString()); } @Override public void remove() throws IOException, UnexpectedResponseException { - throw new UnsupportedOperationException( - String.join(" ", - "Network.remove() is not yet implemented.", - "If you can contribute please", - "do it here: https://www.github.com/amihaiemil/docker-java-api" - ) + final UncheckedUriBuilder uri = new UncheckedUriBuilder( + this.baseUri.toString() + ); + final HttpDelete delete = new HttpDelete( + uri.build() ); + try { + this.client.execute( + delete, + new MatchStatus(delete.getURI(), HttpStatus.SC_NO_CONTENT) + ); + } finally { + delete.releaseConnection(); + } } @Override public void connect(final String containerId) throws IOException, UnexpectedResponseException { - throw new UnsupportedOperationException( - String.join(" ", - "Network.connect() is not yet implemented.", - "If you can contribute please", - "do it here: https://www.github.com/amihaiemil/docker-java-api" + final UncheckedUriBuilder uri = new UncheckedUriBuilder( + this.baseUri.toString() + "/connect" + ); + final HttpPost post = new HttpPost( + uri.build() + ); + post.setEntity( + new StringEntity( + String.format("{\"Container\" : \"%s\"}", containerId) ) ); + try { + this.client.execute( + post, + new MatchStatus(post.getURI(), HttpStatus.SC_OK) + ); + } finally { + post.releaseConnection(); + } } @Override public void disconnect(final String containerId) throws IOException, UnexpectedResponseException { - throw new UnsupportedOperationException( - String.join(" ", - "Network.disconnect() is not yet implemented.", - "If you can contribute please", - "do it here: https://www.github.com/amihaiemil/docker-java-api" + final UncheckedUriBuilder uri = new UncheckedUriBuilder( + this.baseUri.toString() + "/disconnect" + ); + final HttpPost post = new HttpPost( + uri.build() + ); + post.setEntity( + new StringEntity( + String.format( + "{\"Container\" : \"%s\", \"Force\": \"true\"}", + containerId + ) ) ); + try { + this.client.execute( + post, + new MatchStatus(post.getURI(), HttpStatus.SC_OK) + ); + } finally { + post.releaseConnection(); + } } } diff --git a/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java b/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java index 4498f82e..8a78a1e9 100644 --- a/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java @@ -31,13 +31,15 @@ import java.io.IOException; import java.net.URI; +import java.nio.charset.Charset; import javax.json.Json; import javax.json.JsonObject; + +import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpStatus; +import org.apache.http.util.EntityUtils; import org.hamcrest.MatcherAssert; -import org.hamcrest.collection.IsEmptyIterable; import org.hamcrest.core.IsEqual; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; @@ -60,7 +62,6 @@ public final class RtNetworkTestCase { * @throws Exception If something else goes wrong. */ @Test - @Ignore public void inspectsItself() throws Exception { final Network network = new RtNetwork( Json.createObjectBuilder().build(), @@ -114,12 +115,11 @@ public void inspectsItself() throws Exception { * @throws Exception If something goes wrong. */ @Test - @Ignore public void removeSendsCorrectRequest() throws Exception { new RtNetwork( Json.createObjectBuilder().build(), new AssertRequest( - new Response(HttpStatus.SC_OK), + new Response(HttpStatus.SC_NO_CONTENT), new Condition( "remove() must send a DELETE HTTP request", req -> "DELETE".equals(req.getRequestLine().getMethod()) @@ -142,7 +142,6 @@ public void removeSendsCorrectRequest() throws Exception { * @throws IOException If something goes wrong. */ @Test - @Ignore public void connectContainer() throws IOException { final Network network = new RtNetwork( Json.createObjectBuilder().build(), @@ -153,21 +152,31 @@ public void connectContainer() throws IOException { req -> "POST".equals(req.getRequestLine().getMethod()) ), new Condition( - "connect() must send the request to the network url", - req -> "http://localhost/network/id1".equals( - req.getRequestLine().getUri() - ) + "Resource path must be /connect", + req -> req.getRequestLine().getUri().endsWith("/connect") + ), + new Condition( + "connect() must send the the container in the request body", + req -> { + boolean condition = false; + try{ + condition = + EntityUtils.toString( + ((HttpEntityEnclosingRequest) req) + .getEntity(), + Charset.defaultCharset() + ).contains("containerId"); + } catch (final IOException error){ + condition = false; + } + return condition; + } ) ), URI.create("http://localhost/network/id1"), DOCKER ); network.connect("containerId"); - MatcherAssert.assertThat( - "could not create container", - network.inspect().getJsonArray("Container"), - new IsEmptyIterable<>() - ); } /** @@ -176,32 +185,40 @@ public void connectContainer() throws IOException { * @throws IOException If something goes wrong. */ @Test - @Ignore public void disconnectContainer() throws IOException { final Network network = new RtNetwork( Json.createObjectBuilder().build(), new AssertRequest( new Response(HttpStatus.SC_OK), new Condition( - "connect() must send a POST HTTP request", + "disconnect() must send a POST HTTP request", req -> "POST".equals(req.getRequestLine().getMethod()) ), new Condition( - "connect() must send the request to the network url", - req -> "http://localhost/network/id1".equals( - req.getRequestLine().getUri() - ) + "Resource path must be /disconnect", + req -> req.getRequestLine().getUri().endsWith("/disconnect") + ), + new Condition( + "disconnect() must send the container in the request body", + req -> { + boolean condition = false; + try{ + condition = + EntityUtils.toString( + ((HttpEntityEnclosingRequest) req) + .getEntity(), + Charset.defaultCharset() + ).contains("containerId"); + } catch (final IOException error){ + condition = false; + } + return condition; + } ) ), URI.create("http://localhost/network/id1"), DOCKER ); - network.connect("containerId"); network.disconnect("containerId"); - MatcherAssert.assertThat( - "could not create container", - network.inspect().getJsonArray("Container").size(), - new IsEqual<>(0) - ); } } From 1e2f68475105db808b6f3da31f1b8bca19ec3299 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 2 Jan 2019 10:49:16 -0200 Subject: [PATCH 2/2] For #236: Revision corrections. --- src/main/java/com/amihaiemil/docker/RtNetwork.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/RtNetwork.java b/src/main/java/com/amihaiemil/docker/RtNetwork.java index 60fdddff..3eeffd7e 100644 --- a/src/main/java/com/amihaiemil/docker/RtNetwork.java +++ b/src/main/java/com/amihaiemil/docker/RtNetwork.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.net.URI; +import javax.json.Json; import javax.json.JsonObject; import org.apache.http.HttpStatus; @@ -112,7 +113,8 @@ public void connect(final String containerId) ); post.setEntity( new StringEntity( - String.format("{\"Container\" : \"%s\"}", containerId) + Json.createObjectBuilder().add("Container", containerId) + .build().toString() ) ); try { @@ -136,10 +138,10 @@ public void disconnect(final String containerId) ); post.setEntity( new StringEntity( - String.format( - "{\"Container\" : \"%s\", \"Force\": \"true\"}", - containerId - ) + Json.createObjectBuilder() + .add("Container", containerId) + .add("Force", "true") + .build().toString() ) ); try {