diff --git a/src/main/java/com/spotify/docker/client/DefaultDockerClient.java b/src/main/java/com/spotify/docker/client/DefaultDockerClient.java index 4640389c6..a5c703a27 100644 --- a/src/main/java/com/spotify/docker/client/DefaultDockerClient.java +++ b/src/main/java/com/spotify/docker/client/DefaultDockerClient.java @@ -1988,11 +1988,11 @@ public List listServices(final Service.Criteria criteria) assertApiVersionIsAbove("1.24"); final Map> filters = new HashMap<>(); - if (criteria.serviceId() != null) { - filters.put("id", Collections.singletonList(criteria.serviceId())); + if (criteria.serviceIds() != null) { + filters.put("id", criteria.serviceIds()); } - if (criteria.serviceName() != null) { - filters.put("name", Collections.singletonList(criteria.serviceName())); + if (criteria.serviceNames() != null) { + filters.put("name", criteria.serviceNames()); } final List labels = new ArrayList<>(); diff --git a/src/main/java/com/spotify/docker/client/messages/swarm/Service.java b/src/main/java/com/spotify/docker/client/messages/swarm/Service.java index 21541f785..39b2b86e0 100644 --- a/src/main/java/com/spotify/docker/client/messages/swarm/Service.java +++ b/src/main/java/com/spotify/docker/client/messages/swarm/Service.java @@ -29,7 +29,9 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableMap; +import java.util.Collections; import java.util.Date; +import java.util.List; import java.util.Map; import javax.annotation.Nullable; @@ -67,13 +69,13 @@ public abstract static class Criteria { * Filter by service id. */ @Nullable - public abstract String serviceId(); + public abstract List serviceIds(); /** * Filter by service name. */ @Nullable - public abstract String serviceName(); + public abstract List serviceNames(); /** * Filter by label. @@ -88,25 +90,25 @@ public static Builder builder() { @AutoValue.Builder public abstract static class Builder { - public abstract Builder serviceId(final String serviceId); + public abstract Builder serviceIds(final List serviceIds); /** * @deprecated As of release 7.0.0, replaced by {@link #serviceId(String)}. */ @Deprecated public Builder withServiceId(final String serviceId) { - serviceId(serviceId); + serviceIds(Collections.singletonList(serviceId)); return this; } - public abstract Builder serviceName(final String serviceName); + public abstract Builder serviceNames(final List serviceNames); /** * @deprecated As of release 7.0.0, replaced by {@link #serviceName(String)}. */ @Deprecated public Builder withServiceName(final String serviceName) { - serviceName(serviceName); + serviceNames(Collections.singletonList(serviceName)); return this; } diff --git a/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java b/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java index 3539a11e7..745a8409c 100644 --- a/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java +++ b/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java @@ -63,6 +63,7 @@ import static java.lang.System.getenv; import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.toList; import static org.apache.commons.lang.StringUtils.containsIgnoreCase; import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.allOf; @@ -5378,28 +5379,38 @@ public void testListServices() throws Exception { } @Test - public void testListServicesFilterById() throws Exception { + public void testListServicesFilterByIds() throws Exception { requireDockerApiVersionAtLeast("1.24", "swarm support"); - final ServiceSpec spec = createServiceSpec(randomName()); - final ServiceCreateResponse response = sut.createService(spec); + final ServiceSpec spec1 = createServiceSpec(randomName()); + final ServiceSpec spec2 = createServiceSpec(randomName()); + final ServiceCreateResponse response1 = sut.createService(spec1); + final ServiceCreateResponse response2 = sut.createService(spec2); final List services = sut - .listServices(Service.find().serviceId(response.id()).build()); - assertThat(services.size(), is(1)); - assertThat(services.get(0).id(), is(response.id())); + .listServices(Service.find().serviceIds(Arrays.asList( + response1.id(), response2.id())).build()); + assertThat(services.size(), is(2)); + List serviceIds = services.stream().map(Service::id).collect(toList()); + assertThat(serviceIds, containsInAnyOrder(response1.id(), response2.id())); } @Test - public void testListServicesFilterByName() throws Exception { + public void testListServicesFilterByNames() throws Exception { requireDockerApiVersionAtLeast("1.24", "swarm support"); - final String serviceName = randomName(); - final ServiceSpec spec = createServiceSpec(serviceName); - sut.createService(spec); + final String serviceName1 = randomName(); + final ServiceSpec spec1 = createServiceSpec(serviceName1); + sut.createService(spec1); + final String serviceName2 = randomName(); + final ServiceSpec spec2 = createServiceSpec(serviceName2); + sut.createService(spec2); final List services = - sut.listServices(Service.find().serviceName(serviceName).build()); - assertThat(services.size(), is(1)); - assertThat(services.get(0).spec().name(), is(serviceName)); + sut.listServices(Service.find().serviceNames(Arrays.asList( + serviceName1, serviceName2)).build()); + assertThat(services.size(), is(2)); + List serviceNames = services.stream().map(service -> service.spec().name()) + .collect(toList()); + assertThat(serviceNames, containsInAnyOrder(serviceName1, serviceName2)); } @Test