From efc18dc5806c0dbaf64327427fd69fef9946e0cc Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 18 Dec 2018 09:29:43 -0200 Subject: [PATCH] For #225: - Added test methods for Network.connect and Network.disconnect --- .../java/com/amihaiemil/docker/Network.java | 5 +- .../amihaiemil/docker/RtNetworkTestCase.java | 104 ++++++++++-------- 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/Network.java b/src/main/java/com/amihaiemil/docker/Network.java index bb9303dc..24638cc3 100644 --- a/src/main/java/com/amihaiemil/docker/Network.java +++ b/src/main/java/com/amihaiemil/docker/Network.java @@ -35,9 +35,10 @@ * @version $Id$ * @see Docker API v1.35 * @since 0.0.4 - * @todo #210:30min Implement Network methods: inspect, remove, connect and + * @todo #225:30min Implement Network methods: inspect, remove, connect and * disconnect like specified at: - * https://docs.docker.com/engine/api/v1.35/#tag/Network. + * https://docs.docker.com/engine/api/v1.35/#tag/Network. Then remove @Ignore + * annotaton from all network tests in RtNetworkTestCase. */ public interface Network extends JsonObject { diff --git a/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java b/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java index 2268e71a..c6806d5d 100644 --- a/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java @@ -28,11 +28,14 @@ import com.amihaiemil.docker.mock.AssertRequest; import com.amihaiemil.docker.mock.Condition; import com.amihaiemil.docker.mock.Response; + +import java.io.IOException; import java.net.URI; import javax.json.Json; import javax.json.JsonObject; import org.apache.http.HttpStatus; import org.hamcrest.MatcherAssert; +import org.hamcrest.collection.IsEmptyIterable; import org.hamcrest.core.IsEqual; import org.junit.Ignore; import org.junit.Test; @@ -137,66 +140,71 @@ public void removeSendsCorrectRequest() throws Exception { } /** - * RtNetwork throws UnsupportedOperationException for Inspect. - * @throws Exception If something else goes wrong. - */ - @Test(expected = UnsupportedOperationException.class) - public void unsupportedOperationInspect() throws Exception { - new RtNetwork( - Json.createObjectBuilder().build(), - new AssertRequest( - new Response(HttpStatus.SC_OK) - ), - URI.create("http://localhost/networks/id1"), - DOCKER - ).inspect(); - } - - /** - * RtNetwork throws UnsupportedOperationException for Remove. - * @throws Exception If something else goes wrong. - */ - @Test(expected = UnsupportedOperationException.class) - public void unsupportedOperationRemove() throws Exception { - new RtNetwork( - Json.createObjectBuilder().build(), - new AssertRequest( - new Response(HttpStatus.SC_OK) - ), - URI.create("http://localhost/networks/id1"), - DOCKER - ).remove(); - } - - /** - * RtNetwork throws UnsupportedOperationException for Connect. - * @throws Exception If something else goes wrong. + * RtNetwork.connect() must send a POST request to the correct url and + * connect the desired container to the network. + * @throws IOException If something goes wrong. */ - @Test(expected = UnsupportedOperationException.class) - public void unsupportedOperationConnect() throws Exception { - new RtNetwork( + @Test + @Ignore + public void connectContainer() throws IOException { + final Network network = new RtNetwork( Json.createObjectBuilder().build(), new AssertRequest( - new Response(HttpStatus.SC_OK) + new Response(HttpStatus.SC_OK), + new Condition( + "connect() 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() + ) + ) ), - URI.create("http://localhost/networks/id1"), + URI.create("http://localhost/network/id1"), DOCKER - ).connect("containerId"); + ); + network.connect("containerId"); + MatcherAssert.assertThat( + "could not create container", + network.inspect().getJsonArray("Container"), + new IsEmptyIterable<>() + ); } /** - * RtNetwork throws UnsupportedOperationException for Disconnect. - * @throws Exception If something else goes wrong. + * RtNetwork.disconnect() must send a POST request to the correct url and + * disconnect the desired container from the network. + * @throws IOException If something goes wrong. */ - @Test(expected = UnsupportedOperationException.class) - public void unsupportedOperationDisconnect() throws Exception { - new RtNetwork( + @Test + @Ignore + public void disconnectContainer() throws IOException { + final Network network = new RtNetwork( Json.createObjectBuilder().build(), new AssertRequest( - new Response(HttpStatus.SC_OK) + new Response(HttpStatus.SC_OK), + new Condition( + "connect() 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() + ) + ) ), - URI.create("http://localhost/networks/id1"), + URI.create("http://localhost/network/id1"), DOCKER - ).disconnect("containerId"); + ); + network.connect("containerId"); + network.disconnect("containerId"); + MatcherAssert.assertThat( + "could not create container", + network.inspect().getJsonArray("Container").size(), + new IsEqual<>(0) + ); } }