Skip to content

Commit d216ea5

Browse files
committed
Containers.all() + unit test
1 parent b7b567c commit d216ea5

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/main/java/com/amihaiemil/docker/Containers.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.json.JsonObject;
2929
import java.io.IOException;
30+
import java.util.Iterator;
3031

3132
/**
3233
* Containers API.
@@ -72,6 +73,13 @@ Container create(
7273
*/
7374
Container create(final JsonObject container) throws IOException;
7475

76+
/**
77+
* Return all the Containers, not only the running ones (simply iterating
78+
* over this Containers instance will fetch only the running ones).
79+
* @return Iterator over all the containers.
80+
*/
81+
Iterator<Container> all();
82+
7583
/**
7684
* Return the Docker engine where these Containers came from.
7785
* @return Docker.

src/main/java/com/amihaiemil/docker/RtContainers.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,28 @@ public Container create(
139139
}
140140
}
141141

142+
@Override
143+
public Iterator<Container> all() {
144+
return new ResourcesIterator<>(
145+
this.client,
146+
new HttpGet(this.baseUri.toString().concat("/json?all=true")),
147+
json -> new RtContainer(
148+
json,
149+
this.client,
150+
URI.create(
151+
this.baseUri.toString() + "/" + json.getString("Id")
152+
),
153+
this.docker
154+
)
155+
);
156+
}
157+
142158
@Override
143159
public Iterator<Container> iterator() {
144160
return new ResourcesIterator<>(
145161
this.client,
146162
new HttpGet(this.baseUri.toString().concat("/json")),
147-
json-> new RtContainer(
163+
json -> new RtContainer(
148164
json,
149165
this.client,
150166
URI.create(

src/test/java/com/amihaiemil/docker/RtContainersTestCase.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void returnsDocker() {
7676
* @throws Exception If an error occurs.
7777
*/
7878
@Test
79-
public void iteratesContainers() throws Exception {
79+
public void returnsAllContainers() throws Exception {
8080
final AtomicInteger count = new AtomicInteger();
8181
new RtContainers(
8282
new AssertRequest(
@@ -90,8 +90,51 @@ public void iteratesContainers() throws Exception {
9090
Json.createObjectBuilder()
9191
.add("Id", "sha256:3e314f95dcace0f5e")
9292
).build().toString()
93+
),
94+
new Condition(
95+
"Resource path must be /json",
96+
req -> req.getRequestLine().getUri().endsWith(
97+
"/json?all=true"
98+
)
9399
)
94-
), URI.create("http://localhost"), Mockito.mock(Docker.class)
100+
),
101+
URI.create("http://localhost/containers/"),
102+
Mockito.mock(Docker.class)
103+
).all().forEachRemaining(container -> count.incrementAndGet());
104+
MatcherAssert.assertThat(
105+
count.get(),
106+
Matchers.is(2)
107+
);
108+
}
109+
110+
/**
111+
* Must return the same number of containers as there are elements in the
112+
* json array returned by the service.
113+
* @throws Exception If an error occurs.
114+
*/
115+
@Test
116+
public void iteratesRunningContainers() throws Exception {
117+
final AtomicInteger count = new AtomicInteger();
118+
new RtContainers(
119+
new AssertRequest(
120+
new Response(
121+
HttpStatus.SC_OK,
122+
Json.createArrayBuilder()
123+
.add(
124+
Json.createObjectBuilder()
125+
.add("Id", "sha256:e216a057b1cb1efc1")
126+
).add(
127+
Json.createObjectBuilder()
128+
.add("Id", "sha256:3e314f95dcace0f5e")
129+
).build().toString()
130+
),
131+
new Condition(
132+
"Resource path must be /json",
133+
req -> req.getRequestLine().getUri().endsWith("/json")
134+
)
135+
),
136+
URI.create("http://localhost/containers/json"),
137+
Mockito.mock(Docker.class)
95138
).forEach(container -> count.incrementAndGet());
96139
MatcherAssert.assertThat(
97140
count.get(),

0 commit comments

Comments
 (0)