Skip to content

Commit

Permalink
(#154) Filters for ListedImages
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Sep 29, 2018
1 parent 761b49b commit 1b73a18
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/java/com/amihaiemil/docker/ListedImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import java.net.URI;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;

/**
* Listed images, which may have a filter applied.
Expand All @@ -41,23 +46,57 @@
* filters and one without filters.
*/
final class ListedImages extends RtImages {
/**
* Image filters.
*/
private final Map<String, Iterable<String>> filters;

/**
* Ctor.
* @param client The http client.
* @param uri The URI for this Images API.
* @param dkr The docker entry point.
* @checkstyle ParameterNumber (10 lines)
* @checkstyle ParameterNumber (2 lines)
*/
ListedImages(final HttpClient client, final URI uri, final Docker dkr) {
this(client, uri, dkr, Collections.emptyMap());
}

/**
* Ctor.
* @param client The http client.
* @param uri The URI for this Images API.
* @param dkr The docker entry point.
* @param filters Image filter
* @checkstyle ParameterNumber (2 lines)
*/
ListedImages(
final HttpClient client, final URI uri,
final Docker dkr, final Map<String, Iterable<String>> filters
) {
super(client, uri, dkr);
this.filters = filters;
}

@Override
public Iterator<Image> iterator() {
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
super.baseUri().toString().concat("/json")
);
if (!this.filters.isEmpty()) {
final JsonObjectBuilder json = Json.createObjectBuilder();
this.filters.forEach(
(name, values) -> {
final JsonArrayBuilder array = Json.createArrayBuilder();
values.forEach(array::add);
json.add(name, array);
}
);
uri.addParameter("filters", json.build().toString());
}
return new ResourcesIterator<>(
super.client(),
new HttpGet(super.baseUri().toString().concat("/json")),
new HttpGet(uri.build()),
img -> new RtImage(
img,
super.client(),
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
import org.junit.Test;
import org.mockito.Mockito;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;

/**
* Unit tests for {@link ListedImages}.
Expand Down Expand Up @@ -82,4 +87,45 @@ public void iterateAll() {

}

/**
* {@link ListedImages} can include filters in request to fetch images.
*/
@Test
public void includeFiltersInRequest() {
final Map<String, Iterable<String>> filters = new HashMap<>();
filters.put(
"label",
Arrays.asList(
"maintainer=john@doe.org",
"randomLabel=test"
)
);
new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
"[{\"Id\": \"abc1\"}, {\"Id\":\"cde2\"}]"
),
new Condition(
// @checkstyle LineLength (11 lines)
"iterate() query parameters must include the filters provided",
req -> {
final List<NameValuePair> params = new UncheckedUriBuilder(
req.getRequestLine().getUri()
).getQueryParams();
// @checkstyle BooleanExpressionComplexity (5 lines)
return params.size() == 1
&& "filters".equals(params.get(0).getName())
&& params.get(0).getValue().contains("label")
&& params.get(0).getValue().contains("\"maintainer=john@doe.org\"")
&& params.get(0).getValue().contains("\"randomLabel=test\"");
}
)
),
URI.create("http://localhost/images"),
Mockito.mock(Docker.class),
filters
).iterator();
}

}

0 comments on commit 1b73a18

Please sign in to comment.