Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/com/amihaiemil/docker/Containers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -95,10 +95,17 @@ Container create(
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerList">Docker API Docs</a>
*/
Containers filter(Map<String, Iterable<String>> filters);

/**
* Return the Docker engine where these Containers came from.
* @return Docker.
*/
Docker docker();

/**
* Get this container.<br><br>
* @param containerId Id of the Container
* @return This container object.
*/
Container get(final String containerId);
}
24 changes: 22 additions & 2 deletions src/main/java/com/amihaiemil/docker/RtContainers.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class RtContainers implements Containers {
* Docker API.
*/
private final Docker docker;

/**
* Ctor.
* @param client Given HTTP Client.
Expand Down Expand Up @@ -136,7 +136,7 @@ public Container create(
post.releaseConnection();
}
}

@Override
public Docker docker() {
return this.docker;
Expand All @@ -158,4 +158,24 @@ HttpClient client() {
URI baseUri() {
return this.baseUri;
}

/**
* Get this container.<br><br>
*
* @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
);
}
}
45 changes: 33 additions & 12 deletions src/test/java/com/amihaiemil/docker/RtContainersTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @checkstyle MethodName (500 lines)
*/
public final class RtContainersTestCase {

/**
* RtContainers can return its parent Docker.
*/
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public void createsWithPayloadForCreateJson() throws Exception {
)
),
URI.create("http://localhost/test"),
Mockito.mock(Docker.class)
Mockito.mock(Docker.class)
).create(json);
}

Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -499,12 +499,33 @@ 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()
).getString("Id"),
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")
);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackpan123 These 2 tests don't make sense. Why are you calling start and stop?

Just add a simple test only for the get(...) method and assert that a Container is being returned (not null) and that its JSON contains the ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What get(...) method wants to express is that users cant directly obtain a certain container instead of iteratively searching. When the user finds it, who can directly manipulate the internal method of the Container .

}