diff --git a/src/main/java/com/amihaiemil/docker/RtVolumes.java b/src/main/java/com/amihaiemil/docker/RtVolumes.java index de4362ad..435dc1e0 100644 --- a/src/main/java/com/amihaiemil/docker/RtVolumes.java +++ b/src/main/java/com/amihaiemil/docker/RtVolumes.java @@ -25,9 +25,11 @@ */ package com.amihaiemil.docker; -import org.apache.http.client.HttpClient; - +import java.io.IOException; import java.net.URI; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; /** * Runtime {@link Volumes}. @@ -63,6 +65,21 @@ abstract class RtVolumes implements Volumes { this.docker = dkr; } + @Override + public void prune() throws IOException, UnexpectedResponseException { + final HttpPost prune = new HttpPost( + this.baseUri.toString().concat("/prune") + ); + try { + this.client.execute( + prune, + new MatchStatus(prune.getURI(), HttpStatus.SC_OK) + ); + } finally { + prune.releaseConnection(); + } + } + @Override public Docker docker() { return this.docker; diff --git a/src/main/java/com/amihaiemil/docker/Volumes.java b/src/main/java/com/amihaiemil/docker/Volumes.java index 84f589b1..f47a58f8 100644 --- a/src/main/java/com/amihaiemil/docker/Volumes.java +++ b/src/main/java/com/amihaiemil/docker/Volumes.java @@ -25,16 +25,25 @@ */ package com.amihaiemil.docker; +import java.io.IOException; + /** * Volumes API. * @author Mihai Andronache (amihaiemil@gmail.com) * @author Boris Kuzmic (boris.kuzmic@gmail.com) * @version $Id$ * @since 0.0.1 - * @todo #180:30min Continue implementing prune volumes operation. */ public interface Volumes extends Iterable { + /** + * Deletes unused volumes. + * @throws IOException If an I/O error occurs. + * @throws UnexpectedResponseException If the API responds with an + * unexpected status. + */ + void prune() throws IOException, UnexpectedResponseException; + /** * Return the Docker engine where these Images came from. * @return Docker. diff --git a/src/test/java/com/amihaiemil/docker/RtVolumesTestCase.java b/src/test/java/com/amihaiemil/docker/RtVolumesTestCase.java new file mode 100644 index 00000000..545f483c --- /dev/null +++ b/src/test/java/com/amihaiemil/docker/RtVolumesTestCase.java @@ -0,0 +1,89 @@ +package com.amihaiemil.docker; + +import com.amihaiemil.docker.mock.AssertRequest; +import com.amihaiemil.docker.mock.Condition; +import com.amihaiemil.docker.mock.Response; +import java.net.URI; +import javax.json.Json; +import org.apache.http.HttpStatus; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.mockito.Mockito; + + +/** + * Unit tests for {@link RtVolumes}. + * @author Boris Kuzmic (boris.kuzmic@gmail.com) + * @since 0.0.7 + * @checkstyle MethodName (500 lines) + */ +public final class RtVolumesTestCase { + + /** + * Mock docker. + */ + private static final Docker DOCKER = Mockito.mock(Docker.class); + + /** + * RtVolumes.prune() sends correct request and exist successfully on + * response code 200. + * @throws Exception If an error occurs. + */ + @Test + public void prunesOk() throws Exception { + new ListedVolumes( + new AssertRequest( + new Response(HttpStatus.SC_OK), + new Condition( + "prune() must send a POST request", + req -> "POST".equals(req.getRequestLine().getMethod()) + ), + new Condition( + "prune() resource URL must be '/volumes/prune'", + req -> req.getRequestLine() + .getUri().endsWith("/volumes/prune") + ) + ), + URI.create("http://localhost/volumes"), + DOCKER + ).prune(); + } + + /** + * RtVolumes.prune() must throw UnexpectedResponseException if service + * responds with 500. + * @throws Exception The UnexpectedResponseException. + */ + @Test(expected = UnexpectedResponseException.class) + public void pruneThrowsErrorOnResponse500() throws Exception { + new ListedVolumes( + new AssertRequest( + new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR) + ), + URI.create("http://localhost/volumes"), + DOCKER + ).prune(); + } + + /** + * RtVolumes can return its Docker parent. + */ + @Test + public void returnsDocker() { + MatcherAssert.assertThat( + new ListedVolumes( + new AssertRequest( + new Response( + HttpStatus.SC_OK, + Json.createArrayBuilder().build().toString() + ) + ), + URI.create("http://localhost"), + DOCKER + ).docker(), + new IsEqual<>(DOCKER) + ); + } + +}