Skip to content

Commit

Permalink
Containers.all() + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Oct 23, 2018
1 parent b7b567c commit d216ea5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/amihaiemil/docker/Containers.java
Expand Up @@ -27,6 +27,7 @@

import javax.json.JsonObject;
import java.io.IOException;
import java.util.Iterator;

/**
* Containers API.
Expand Down Expand Up @@ -72,6 +73,13 @@ Container create(
*/
Container create(final JsonObject container) throws IOException;

/**
* Return all the Containers, not only the running ones (simply iterating
* over this Containers instance will fetch only the running ones).
* @return Iterator over all the containers.
*/
Iterator<Container> all();

/**
* Return the Docker engine where these Containers came from.
* @return Docker.
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/amihaiemil/docker/RtContainers.java
Expand Up @@ -139,12 +139,28 @@ public Container create(
}
}

@Override
public Iterator<Container> all() {
return new ResourcesIterator<>(
this.client,
new HttpGet(this.baseUri.toString().concat("/json?all=true")),
json -> new RtContainer(
json,
this.client,
URI.create(
this.baseUri.toString() + "/" + json.getString("Id")
),
this.docker
)
);
}

@Override
public Iterator<Container> iterator() {
return new ResourcesIterator<>(
this.client,
new HttpGet(this.baseUri.toString().concat("/json")),
json-> new RtContainer(
json -> new RtContainer(
json,
this.client,
URI.create(
Expand Down
47 changes: 45 additions & 2 deletions src/test/java/com/amihaiemil/docker/RtContainersTestCase.java
Expand Up @@ -76,7 +76,7 @@ public void returnsDocker() {
* @throws Exception If an error occurs.
*/
@Test
public void iteratesContainers() throws Exception {
public void returnsAllContainers() throws Exception {
final AtomicInteger count = new AtomicInteger();
new RtContainers(
new AssertRequest(
Expand All @@ -90,8 +90,51 @@ public void iteratesContainers() throws Exception {
Json.createObjectBuilder()
.add("Id", "sha256:3e314f95dcace0f5e")
).build().toString()
),
new Condition(
"Resource path must be /json",
req -> req.getRequestLine().getUri().endsWith(
"/json?all=true"
)
)
), URI.create("http://localhost"), Mockito.mock(Docker.class)
),
URI.create("http://localhost/containers/"),
Mockito.mock(Docker.class)
).all().forEachRemaining(container -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Matchers.is(2)
);
}

/**
* Must return the same number of containers as there are elements in the
* json array returned by the service.
* @throws Exception If an error occurs.
*/
@Test
public void iteratesRunningContainers() throws Exception {
final AtomicInteger count = new AtomicInteger();
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add("Id", "sha256:e216a057b1cb1efc1")
).add(
Json.createObjectBuilder()
.add("Id", "sha256:3e314f95dcace0f5e")
).build().toString()
),
new Condition(
"Resource path must be /json",
req -> req.getRequestLine().getUri().endsWith("/json")
)
),
URI.create("http://localhost/containers/json"),
Mockito.mock(Docker.class)
).forEach(container -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Expand Down

0 comments on commit d216ea5

Please sign in to comment.