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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public interface SearchImagesCmd extends SyncDockerCmd<List<SearchItem>> {

@CheckForNull
String getTerm();
Integer getLimit();

SearchImagesCmd withTerm(@Nonnull String term);
SearchImagesCmd withLimit(@Nonnull Integer limit);

interface Exec extends DockerCmdSyncExec<SearchImagesCmd, List<SearchItem>> {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.dockerjava.core.command;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import com.github.dockerjava.api.command.SearchImagesCmd;
import com.github.dockerjava.api.model.SearchItem;

import javax.annotation.Nonnull;

/**
* Search images
*
Expand All @@ -16,7 +19,11 @@
*/
public class SearchImagesCmdImpl extends AbstrDockerCmd<SearchImagesCmd, List<SearchItem>> implements SearchImagesCmd {

private static final int MIN_LIMIT = 1;
private static final int MAX_LIMIT = 100;

private String term;
private Integer limit;

public SearchImagesCmdImpl(SearchImagesCmd.Exec exec, String term) {
super(exec);
Expand All @@ -35,4 +42,17 @@ public SearchImagesCmd withTerm(String term) {
return this;
}

@Override
public Integer getLimit() {
return limit;
}

@Override
public SearchImagesCmd withLimit(@Nonnull Integer limit) {
String errorMessage = String.format("Limit %s is outside the range of [%s, %s]", limit, MIN_LIMIT, MAX_LIMIT);
checkArgument(limit <= MAX_LIMIT, errorMessage);
checkArgument(limit >= MIN_LIMIT, errorMessage);
this.limit = limit;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public SearchImagesCmdExec(WebTarget baseResource, DockerClientConfig dockerClie

@Override
protected List<SearchItem> execute(SearchImagesCmd command) {
WebTarget webResource = getBaseResource().path("/images/search").queryParam("term", command.getTerm());
WebTarget webResource = getBaseResource().path("/images/search")
.queryParam("term", command.getTerm());

if (command.getLimit() != null) {
webResource = webResource.queryParam("limit", command.getLimit());
}

LOGGER.trace("GET: {}", webResource);
return webResource.request().accept(MediaType.APPLICATION_JSON).get(new TypeReference<List<SearchItem>>() {
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

import static ch.lambdaj.Lambda.filter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.testinfected.hamcrest.jpa.HasFieldWithValue.hasField;

public class SearchImagesCmdIT extends CmdIT {
Expand All @@ -30,4 +28,37 @@ public void searchImages() throws DockerException {
assertThat(filter(hasField("name", is("busybox")), dockerSearch).size(), equalTo(1));
}

@Test(expected = IllegalArgumentException.class)
public void searchImagesWithInvalidMinimumLimit() throws DockerException {
dockerRule.getClient().searchImagesCmd("busybox").withLimit(0).exec();
}

@Test(expected = IllegalArgumentException.class)
public void searchImagesWithInvalidMaximumLimit() throws DockerException {
dockerRule.getClient().searchImagesCmd("busybox").withLimit(101).exec();
}

@Test
public void searchImagesWithValidMinimumLimit() throws DockerException {
List<SearchItem> dockerSearch = dockerRule.getClient().searchImagesCmd("busybox").withLimit(1).exec();
LOG.info("Search returned {}", dockerSearch.toString());

Matcher matcher = hasItem(hasField("name", equalTo("busybox")));
assertThat(dockerSearch, matcher);

assertThat(filter(hasField("name", is("busybox")), dockerSearch).size(), equalTo(1));

assertThat(dockerSearch.size(), equalTo(1));
}

@Test
public void searchImagesWithValidMaximumLimit() throws DockerException {
List<SearchItem> dockerSearch = dockerRule.getClient().searchImagesCmd("busybox").withLimit(1).exec();
LOG.info("Search returned {}", dockerSearch.toString());

Matcher matcher = hasItem(hasField("name", equalTo("busybox")));
assertThat(dockerSearch, matcher);

assertThat(filter(hasField("name", is("busybox")), dockerSearch).size(), equalTo(1));
}
}