Skip to content

Commit d98b386

Browse files
author
Boris Kuzmic
committed
Fixing LocalDockerITCase for iterating volumes.
1 parent ea1edc1 commit d98b386

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

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

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
*/
2626
package com.amihaiemil.docker;
2727

28+
import java.io.IOException;
2829
import java.net.URI;
2930
import java.util.Collections;
3031
import java.util.Iterator;
3132
import java.util.Map;
33+
import java.util.stream.Collectors;
34+
import javax.json.JsonObject;
35+
import org.apache.http.HttpStatus;
3236
import org.apache.http.client.HttpClient;
3337
import org.apache.http.client.methods.HttpGet;
3438

@@ -74,22 +78,36 @@ public Iterator<Volume> iterator() {
7478
final FilteredUriBuilder uri = new FilteredUriBuilder(
7579
new UncheckedUriBuilder(super.baseUri().toString()), this.filters
7680
);
77-
return new ResourcesIterator<>(
78-
super.client(),
79-
new HttpGet(
80-
uri.build()
81-
),
82-
volume -> new RtVolume(
83-
volume,
84-
super.client(),
85-
URI.create(
86-
String.format("%s/%s",
87-
super.baseUri().toString(),
88-
volume.getString("Name")
81+
final HttpGet get = new HttpGet(uri.build());
82+
try {
83+
final JsonObject obj = super.client().execute(
84+
get,
85+
new ReadJsonObject(
86+
new MatchStatus(get.getURI(), HttpStatus.SC_OK)
87+
)
88+
);
89+
return obj.getJsonArray("Volumes").stream()
90+
.map(json -> (JsonObject) json)
91+
.map(
92+
volume -> (Volume) new RtVolume(
93+
volume,
94+
super.client(),
95+
URI.create(
96+
String.format("%s/%s",
97+
super.baseUri().toString(),
98+
volume.getString("Name")
99+
)
100+
),
101+
super.docker()
89102
)
90-
),
91-
super.docker()
92-
)
93-
);
103+
).collect(Collectors.toList())
104+
.iterator();
105+
} catch (final IOException err) {
106+
throw new RuntimeException(
107+
String.format("Error executing GET on %s", super.baseUri())
108+
);
109+
} finally {
110+
get.releaseConnection();
111+
}
94112
}
95113
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void iterateAll() {
5959
new AssertRequest(
6060
new Response(
6161
HttpStatus.SC_OK,
62-
"[{\"Name\": \"abc1\"}, {\"Name\":\"cde2\"}]"
62+
"{\"Volumes\":[{\"Name\": \"abc1\"}, {\"Name\":\"cde2\"}]}"
6363
),
6464
new Condition(
6565
"iterate() must send a GET request",
@@ -105,7 +105,7 @@ public void iterateWithFilters() throws IOException {
105105
new Response(
106106
HttpStatus.SC_OK,
107107
//@checkstyle LineLength (1 line)
108-
"[{\"Name\": \"abc1\"}, {\"Name\": \"def2\"}, {\"Name\": \"ghi3\"}, {\"Name\":\"jkl4\"}]"
108+
"{\"Volumes\":[{\"Name\": \"abc1\"}, {\"Name\": \"def2\"}, {\"Name\": \"ghi3\"}, {\"Name\":\"jkl4\"}]}"
109109
),
110110
new Condition(
111111
"iterate() must send a GET request",

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,18 @@
2525
*/
2626
package com.amihaiemil.docker;
2727

28+
import java.io.File;
29+
import java.nio.file.Paths;
2830
import org.hamcrest.MatcherAssert;
2931
import org.hamcrest.Matchers;
3032
import org.hamcrest.collection.IsIterableWithSize;
31-
import org.junit.Ignore;
3233
import org.junit.Test;
3334
import org.mockito.internal.matchers.GreaterOrEqual;
3435

35-
import java.io.File;
36-
import java.nio.file.Paths;
37-
3836
/**
3937
* Integration tests for LocalDocker.
4038
* @author Mihai Andronache (amihaiemil@gmail.com)
4139
* @version $Id$
42-
* @todo #241:30min Fix Volumes object return problem. Volumes is returning an
43-
* JsonObject instead of JsonArray. Its return must be according its
44-
* documentation:
45-
* https://docs.docker.com/engine/api/v1.30/#operation/VolumeList and what is
46-
* defined in #241.
4740
* @since 0.0.1
4841
*/
4942
public final class LocalDockerITCase {
@@ -65,7 +58,6 @@ public void pingsDocker() throws Exception {
6558
* @throws Exception If something goes wrong.
6659
*/
6760
@Test
68-
@Ignore
6961
public void listVolumes() throws Exception {
7062
final Docker docker = new LocalDocker(
7163
Paths.get("/var/run/docker.sock").toFile()

0 commit comments

Comments
 (0)