From 1b73a183c287e6fcf1602f1fcbb03a3ab253569f Mon Sep 17 00:00:00 2001 From: George Aristy Date: Sat, 29 Sep 2018 11:33:42 -0400 Subject: [PATCH] (#154) Filters for ListedImages --- .../com/amihaiemil/docker/ListedImages.java | 43 ++++++++++++++++- .../docker/ListedImagesTestCase.java | 46 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/ListedImages.java b/src/main/java/com/amihaiemil/docker/ListedImages.java index a2e0406e..40bcf7b2 100644 --- a/src/main/java/com/amihaiemil/docker/ListedImages.java +++ b/src/main/java/com/amihaiemil/docker/ListedImages.java @@ -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. @@ -41,23 +46,57 @@ * filters and one without filters. */ final class ListedImages extends RtImages { + /** + * Image filters. + */ + private final Map> 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> filters + ) { super(client, uri, dkr); + this.filters = filters; } @Override public Iterator 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(), diff --git a/src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java b/src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java index 48e3ea59..54d6951b 100644 --- a/src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java +++ b/src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java @@ -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}. @@ -82,4 +87,45 @@ public void iterateAll() { } + /** + * {@link ListedImages} can include filters in request to fetch images. + */ + @Test + public void includeFiltersInRequest() { + final Map> 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 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(); + } + }