From ef961ea1e7b7f264b0d166775a6da796ac224f86 Mon Sep 17 00:00:00 2001 From: jackpan <826647196@qq.com> Date: Wed, 5 Aug 2020 11:07:23 +0800 Subject: [PATCH 1/3] Add get method --- .../com/amihaiemil/docker/Containers.java | 11 +++- .../com/amihaiemil/docker/RtContainers.java | 22 ++++++- .../docker/RtContainersTestCase.java | 60 +++++++++++++++---- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/Containers.java b/src/main/java/com/amihaiemil/docker/Containers.java index 98eebf92..98138bdd 100644 --- a/src/main/java/com/amihaiemil/docker/Containers.java +++ b/src/main/java/com/amihaiemil/docker/Containers.java @@ -74,7 +74,7 @@ Container create( * @throws IOException If something goes wrong. */ Container create(final JsonObject container) throws IOException; - + /** * Return all Containers, not only the running ones. * @return Iterator over all the containers. @@ -95,10 +95,17 @@ Container create( * @see Docker API Docs */ Containers filter(Map> filters); - + /** * Return the Docker engine where these Containers came from. * @return Docker. */ Docker docker(); + + /** + * Get this container.

+ * @param containerId of the Container + * @return this container object. + */ + Container get(final String containerId); } diff --git a/src/main/java/com/amihaiemil/docker/RtContainers.java b/src/main/java/com/amihaiemil/docker/RtContainers.java index 029fee1f..b91cbff5 100644 --- a/src/main/java/com/amihaiemil/docker/RtContainers.java +++ b/src/main/java/com/amihaiemil/docker/RtContainers.java @@ -57,7 +57,7 @@ abstract class RtContainers implements Containers { * Docker API. */ private final Docker docker; - + /** * Ctor. * @param client Given HTTP Client. @@ -136,7 +136,7 @@ public Container create( post.releaseConnection(); } } - + @Override public Docker docker() { return this.docker; @@ -158,4 +158,22 @@ HttpClient client() { URI baseUri() { return this.baseUri; } + + /** + * Get this container.

+ * + * @param containerId of the Container + * @return this container object. + */ + @Override + public Container get(String containerId) { + return new RtContainer( + Json.createObjectBuilder().build(), + this.client, + URI.create( + this.baseUri.toString() + "/" + containerId + ), + this.docker + ); + } } diff --git a/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java b/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java index 0a7de5bd..60b2e12f 100644 --- a/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java @@ -48,7 +48,7 @@ * @checkstyle MethodName (500 lines) */ public final class RtContainersTestCase { - + /** * RtContainers can return its parent Docker. */ @@ -69,7 +69,7 @@ public void returnsDocker() { Matchers.is(parent) ); } - + /** * Must return the same number of containers as there are elements in the * json array returned by the service. @@ -106,7 +106,7 @@ public void returnsAllContainers() throws Exception { Matchers.is(2) ); } - + /** * Must return the same number of containers as there are elements in the * json array returned by the service. @@ -141,7 +141,7 @@ public void iteratesRunningContainers() throws Exception { Matchers.is(2) ); } - + /** * The iterator works when there are no containers. * @throws Exception If an error occurs. @@ -162,7 +162,7 @@ public void iteratesZeroContainers() throws Exception { Matchers.is(0) ); } - + /** * Must throw {@link UnexpectedResponseException} if response code is 500. * @throws Exception The UnexpectedException. @@ -177,7 +177,7 @@ public void iterateFailsIfResponseIs500() throws Exception { Mockito.mock(Docker.class) ).iterator(); } - + /** * Must throw {@link UnexpectedResponseException} if response code is 400. * @throws Exception The UnexpectedException. @@ -192,7 +192,7 @@ public void iterateFailsIfResponseIs400() throws Exception { Mockito.mock(Docker.class) ).iterator(); } - + /** * The request should be well-formed. * @throws Exception If something goes wrong. @@ -280,7 +280,7 @@ public void createsWith404() throws IOException { ) ), URI.create("http://localhost/test"), - Mockito.mock(Docker.class) + Mockito.mock(Docker.class) ).create("some_image"); } @@ -356,7 +356,7 @@ public void createsWithImageName() throws Exception { ) ), URI.create("http://localhost/test"), - Mockito.mock(Docker.class) + Mockito.mock(Docker.class) ).create("some_name", "some_image"); } @@ -394,7 +394,7 @@ public void createsWithPayloadForCreateJson() throws Exception { ) ), URI.create("http://localhost/test"), - Mockito.mock(Docker.class) + Mockito.mock(Docker.class) ).create(json); } @@ -456,7 +456,7 @@ public void createEscapesNameParameter() throws Exception { ) ), URI.create("http://localhost/docker"), - Mockito.mock(Docker.class) + Mockito.mock(Docker.class) ).create("Adrian Toomes", "some/image"); } @@ -499,7 +499,7 @@ public void createsContainerWithId() throws Exception { ) ), URI.create("http://localhost/test"), - Mockito.mock(Docker.class) + Mockito.mock(Docker.class) ).create( Json.createObjectBuilder() .add("Image", "ubuntu").build() @@ -507,4 +507,40 @@ public void createsContainerWithId() throws Exception { Matchers.is("df2419f4") ); } + + /** + * RtContainers.get() returns an RtContainer with the container id. + * @throws Exception If something goes wrong. + */ + @Test + public void getContainerWithIdStartsWithNotFound() throws Exception { + new ListedContainers( + new AssertRequest( + new Response( + HttpStatus.SC_NO_CONTENT, + "{ \"Id\": \"df2419f4\", \"Warnings\": [ ] }" + ) + ), + URI.create("http://localhost/test"), + Mockito.mock(Docker.class) + ).get("df2419f4").start(); + } + + /** + * RtContainers.get() returns an RtContainer with the container id. + * @throws Exception If something goes wrong. + */ + @Test + public void getContainerWithIdStopWithNotFound() throws Exception { + new ListedContainers( + new AssertRequest( + new Response( + HttpStatus.SC_NO_CONTENT, + "{ \"Id\": \"df2419f4\", \"Warnings\": [ ] }" + ) + ), + URI.create("http://localhost/test"), + Mockito.mock(Docker.class) + ).get("df2419f4").stop(); + } } From 9e280aec9a780257acfb0891f5f2cf8af5e1da16 Mon Sep 17 00:00:00 2001 From: jackpan <826647196@qq.com> Date: Wed, 5 Aug 2020 11:22:00 +0800 Subject: [PATCH 2/3] Add get method --- src/main/java/com/amihaiemil/docker/Containers.java | 4 ++-- src/main/java/com/amihaiemil/docker/RtContainers.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/Containers.java b/src/main/java/com/amihaiemil/docker/Containers.java index 98138bdd..bd510605 100644 --- a/src/main/java/com/amihaiemil/docker/Containers.java +++ b/src/main/java/com/amihaiemil/docker/Containers.java @@ -104,8 +104,8 @@ Container create( /** * Get this container.

- * @param containerId of the Container - * @return this container object. + * @param containerId Id of the Container + * @return This container object. */ Container get(final String containerId); } diff --git a/src/main/java/com/amihaiemil/docker/RtContainers.java b/src/main/java/com/amihaiemil/docker/RtContainers.java index b91cbff5..007a0934 100644 --- a/src/main/java/com/amihaiemil/docker/RtContainers.java +++ b/src/main/java/com/amihaiemil/docker/RtContainers.java @@ -162,11 +162,11 @@ URI baseUri() { /** * Get this container.

* - * @param containerId of the Container - * @return this container object. + * @param containerId Id of the Container + * @return This container object. */ @Override - public Container get(String containerId) { + public Container get(final String containerId) { return new RtContainer( Json.createObjectBuilder().build(), this.client, From 996d90455de1ca33a9832bf7f80f460fbc55a156 Mon Sep 17 00:00:00 2001 From: jackpan <826647196@qq.com> Date: Thu, 6 Aug 2020 13:07:12 +0800 Subject: [PATCH 3/3] Add the id to this JSON and delete 2 tests, and add a new test. --- .../com/amihaiemil/docker/RtContainers.java | 4 +- .../docker/RtContainersTestCase.java | 43 ++++++------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/RtContainers.java b/src/main/java/com/amihaiemil/docker/RtContainers.java index 007a0934..a31bfcc9 100644 --- a/src/main/java/com/amihaiemil/docker/RtContainers.java +++ b/src/main/java/com/amihaiemil/docker/RtContainers.java @@ -168,7 +168,9 @@ URI baseUri() { @Override public Container get(final String containerId) { return new RtContainer( - Json.createObjectBuilder().build(), + Json.createObjectBuilder() + .add("Id", containerId) + .build(), this.client, URI.create( this.baseUri.toString() + "/" + containerId diff --git a/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java b/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java index 60b2e12f..6c87c1dc 100644 --- a/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtContainersTestCase.java @@ -513,34 +513,19 @@ public void createsContainerWithId() throws Exception { * @throws Exception If something goes wrong. */ @Test - public void getContainerWithIdStartsWithNotFound() throws Exception { - new ListedContainers( - new AssertRequest( - new Response( - HttpStatus.SC_NO_CONTENT, - "{ \"Id\": \"df2419f4\", \"Warnings\": [ ] }" - ) - ), - URI.create("http://localhost/test"), - Mockito.mock(Docker.class) - ).get("df2419f4").start(); - } - - /** - * RtContainers.get() returns an RtContainer with the container id. - * @throws Exception If something goes wrong. - */ - @Test - public void getContainerWithIdStopWithNotFound() throws Exception { - new ListedContainers( - new AssertRequest( - new Response( - HttpStatus.SC_NO_CONTENT, - "{ \"Id\": \"df2419f4\", \"Warnings\": [ ] }" - ) - ), - URI.create("http://localhost/test"), - Mockito.mock(Docker.class) - ).get("df2419f4").stop(); + public void getContainerWithId() throws Exception { + MatcherAssert.assertThat( + new ListedContainers( + new AssertRequest( + new Response( + HttpStatus.SC_OK, + "{ \"Id\": \"df2419f4\", \"Warnings\": [ ] }" + ) + ), + URI.create("http://localhost/test"), + Mockito.mock(Docker.class) + ).get("df2419f4").getString("Id"), + Matchers.is("df2419f4") + ); } }