Skip to content

Commit

Permalink
Merge pull request #253 from bkuzmic/issue-243-FilteredUriBuilder
Browse files Browse the repository at this point in the history
For #243: Adding filters to FilteredUriBuilder
  • Loading branch information
amihaiemil authored Jan 3, 2019
2 parents 91e4e75 + f1419be commit c50b36c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
48 changes: 40 additions & 8 deletions src/main/java/com/amihaiemil/docker/FilteredUriBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
*/
package com.amihaiemil.docker;

import org.apache.http.client.utils.URIBuilder;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

/**
* {@link URIBuilder} with filtering.
Expand All @@ -43,10 +48,6 @@ final class FilteredUriBuilder extends URIBuilder {
*/
private final URIBuilder origin;

/**
* Filters.
*/
private final Map<String, Iterable<String>> filters;

/**
* Constructor.
Expand All @@ -57,11 +58,42 @@ final class FilteredUriBuilder extends URIBuilder {
FilteredUriBuilder(final URIBuilder builder,
final Map<String, Iterable<String>> filters){
this.origin = builder;
this.filters = filters;
this.addFilters(filters);
}

@Override
public URI build() {
throw new UnsupportedOperationException("filters not implemented yet");
try {
return this.origin.build();
} catch (final URISyntaxException ex) {
throw new IllegalStateException(
"Unexpected error while building a URI!", ex
);
}
}

@Override
public List<NameValuePair> getQueryParams() {
return this.origin.getQueryParams();
}

/**
* Adds a JSON encoded `filters` parameter.
* @param filters Filters.
*/
private void addFilters(final Map<String, Iterable<String>> filters) {
if (filters != null && !filters.isEmpty()) {
final JsonObjectBuilder json = Json.createObjectBuilder();
filters.forEach(
(name, values) -> {
final JsonArrayBuilder array = Json.createArrayBuilder();
values.forEach(array::add);
json.add(name, array);
}
);
this.origin.addParameter("filters", json.build().toString());
}


}
}
13 changes: 7 additions & 6 deletions src/main/java/com/amihaiemil/docker/ListedImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
*/
package com.amihaiemil.docker;

import org.apache.http.client.HttpClient;
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 org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

/**
* Listed images, which may have a filter applied.
Expand Down Expand Up @@ -75,9 +74,11 @@ final class ListedImages extends RtImages {

@Override
public Iterator<Image> iterator() {
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
super.baseUri().toString().concat("/json")
).addFilters(this.filters);
final FilteredUriBuilder uri = new FilteredUriBuilder(
new UncheckedUriBuilder(
super.baseUri().toString().concat("/json")
),
this.filters);

return new ResourcesIterator<>(
super.client(),
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/amihaiemil/docker/ListedVolumes.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ final class ListedVolumes extends RtVolumes {

@Override
public Iterator<Volume> iterator() {
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
super.baseUri().toString()
).addFilters(this.filters);

final FilteredUriBuilder uri = new FilteredUriBuilder(
new UncheckedUriBuilder(super.baseUri().toString()), this.filters
);
return new ResourcesIterator<>(
super.client(),
new HttpGet(
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/amihaiemil/docker/UncheckedUriBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;

import org.apache.http.client.utils.URIBuilder;

/**
Expand Down Expand Up @@ -63,33 +57,6 @@ public UncheckedUriBuilder addParameter(
super.addParameter(name, value);
return this;
}

/**
* Adds a JSON encoded `filters` parameter.
*
* @param filters Value of the filters.
* @return A {@link UncheckedUriBuilder} instance.
*
* @todo #240:30min Finish FilteredUriBuilder implementation moving this
* method to that class and uncommenting test in FilteredUriBuilderTests
*/
public UncheckedUriBuilder addFilters(
final Map<String, Iterable<String>> filters
) {
if (filters != null && !filters.isEmpty()) {
final JsonObjectBuilder json = Json.createObjectBuilder();
filters.forEach(
(name, values) -> {
final JsonArrayBuilder array = Json.createArrayBuilder();
values.forEach(array::add);
json.add(name, array);
}
);
this.addParameter("filters", json.build().toString());
}

return this;
}

@Override
public URI build() {
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/amihaiemil/docker/FilteredUriBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
*/
package com.amihaiemil.docker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsIterableWithSize;
import org.hamcrest.core.IsEqual;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand All @@ -45,17 +46,22 @@ public class FilteredUriBuilderTest {
* Tests if the {@link FilteredUriBuilder} can add filter parameters.
*/
@Test
@Ignore
public final void addFilterParameters() {
final Map<String, Iterable<String>> filters = new HashMap<>();
filters.put(
"driver",
Arrays.asList(
"bridge"
)
);
MatcherAssert.assertThat(
"Did not added filter parameters to URI",
new FilteredUriBuilder(
new URIBuilder(), new HashMap<>()
new URIBuilder(), filters
).getQueryParams(),
new IsIterableWithSize<>(
new IsEqual<>(1)
)
);

}
}

0 comments on commit c50b36c

Please sign in to comment.