diff --git a/src/main/java/com/amihaiemil/docker/Containers.java b/src/main/java/com/amihaiemil/docker/Containers.java index 98eebf9..bd51060 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 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 029fee1..a31bfcc 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,24 @@ HttpClient client() { URI baseUri() { return this.baseUri; } + + /** + * Get this container.

+ * + * @param containerId Id of the Container + * @return This container object. + */ + @Override + public Container get(final String containerId) { + return new RtContainer( + Json.createObjectBuilder() + .add("Id", containerId) + .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 0a7de5b..6c87c1d 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,25 @@ 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 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") + ); + } }