diff --git a/src/main/java/com/amihaiemil/docker/RtNetwork.java b/src/main/java/com/amihaiemil/docker/RtNetwork.java index d920b0a2..3eeffd7e 100644 --- a/src/main/java/com/amihaiemil/docker/RtNetwork.java +++ b/src/main/java/com/amihaiemil/docker/RtNetwork.java @@ -27,8 +27,14 @@ import java.io.IOException; import java.net.URI; +import javax.json.Json; 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 +81,76 @@ 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( + Json.createObjectBuilder().add("Container", containerId) + .build().toString() ) ); + 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( + Json.createObjectBuilder() + .add("Container", containerId) + .add("Force", "true") + .build().toString() ) ); + 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) - ); } }