Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/java/com/amihaiemil/docker/Images.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.Map;

/**
* Images API.
Expand All @@ -37,10 +38,6 @@
* @since 0.0.1
* @todo #98:30min Continue implementing the rest of the operations for the
* Images interface. See the docs referenced above for more details.
* @todo #144:30min Add the filter(Map<String, String>) method which will
* filter the given instance of Images. If filter() is called more times,
* all of the specified filters should amount and be applied to the last
* resulting Images instance.
* @todo #152:30min Add Fake implementations of Images and Image, in order to
* unit test method save() and other future methods which may require more
* than 1 HTTP request. Currently, the unit testing infrastructure does
Expand Down Expand Up @@ -94,6 +91,14 @@ Image importImage(
*/
Reader save() throws IOException, UnexpectedResponseException;

/**
* Filter these images.
* @param filters Filters to apply.
* @return Filtered images.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ImageList">Docker API Docs</a>
*/
Images filter(Map<String, Iterable<String>> filters);

/**
* Return the Docker engine where these Images came from.
* @return Docker.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/amihaiemil/docker/ListedImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.http.client.methods.HttpGet;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.json.Json;
Expand Down Expand Up @@ -107,4 +108,18 @@ public Iterator<Image> iterator() {
)
);
}

@Override
public Images filter(final Map<String, Iterable<String>> fltrs) {
final Map<String, Iterable<String>> merged = new HashMap<>(
this.filters
);
merged.putAll(fltrs);
return new ListedImages(
super.client(),
this.baseUri(),
this.docker(),
merged
);
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,49 @@ public void includeFiltersInRequest() {
).iterator();
}

/**
* {@link ListedImages} can include filters added in filter(), in addition
* to those provided via ctor, in request to fetch images.
*/
@Test
public void includeAddedFiltersInRequest() {
final Map<String, Iterable<String>> initial = new HashMap<>();
initial.put(
"label",
Arrays.asList(
"maintainer=john@doe.org",
"randomLabel=test"
)
);
final Map<String, Iterable<String>> added = new HashMap<>();
added.put("dangling", Arrays.asList("true"));
new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
"[{\"Id\": \"abc1\"}, {\"Id\":\"cde2\"}]"
),
new Condition(
// @checkstyle LineLength (12 lines)
"iterate() query parameters must include the filters provided",
req -> {
final List<NameValuePair> params = new UncheckedUriBuilder(
req.getRequestLine().getUri()
).getQueryParams();
// @checkstyle BooleanExpressionComplexity (6 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\"")
&& params.get(0).getValue().contains("\"dangling\":[\"true\"]");
}
)
),
URI.create("http://localhost/images"),
Mockito.mock(Docker.class),
initial
).filter(added).iterator();
}

}
1 change: 1 addition & 0 deletions src/test/java/com/amihaiemil/docker/RtImagesITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #153:30min Add integration tests for filters.
*/
public final class RtImagesITCase {

Expand Down