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
15 changes: 12 additions & 3 deletions src/main/java/com/amihaiemil/docker/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
* @version $Id$
* @see <a href="https://docs.docker.com/engine/api/v1.35/#tag/Image">Docker Images API</a>
* @since 0.0.1
* @todo #86:30min Continue implementing the operations that affect a single
* docker image. See the link referenced above.
* @todo #96:30min Finish implementing the operations that affect a single
* docker image (I think `tag` is the only one remaining). See the link
* referenced above.
*/
public interface Image {

Expand All @@ -55,5 +56,13 @@ public interface Image {
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ImageHistory">Image History</a>
*/
Images history();


/**
* The parent {@link Images}.
* @throws IOException If something goes wrong.
* @throws UnexpectedResponseException If the status response is not
* the expected one (200 OK).
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ImageDelete">Remove an image</a>
*/
void delete() throws IOException, UnexpectedResponseException;
}
19 changes: 19 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.io.IOException;
import java.net.URI;
import javax.json.JsonObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;

/**
* Runtime {@link Image}.
Expand Down Expand Up @@ -69,4 +71,21 @@ public Images history() {
this.client, URI.create(this.baseUri.toString() + "/history")
);
}

@Override
public void delete() throws IOException, UnexpectedResponseException {
final HttpDelete delete = new HttpDelete(this.baseUri);
try {
final int status = this.client.execute(delete)
.getStatusLine()
.getStatusCode();
if (status != HttpStatus.SC_OK) {
throw new UnexpectedResponseException(
delete.getRequestLine().getUri(), status, HttpStatus.SC_OK
);
}
} finally {
delete.releaseConnection();
}
}
}
70 changes: 70 additions & 0 deletions src/test/java/com/amihaiemil/docker/RtImageTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle MethodName (500 lines)
*/
public final class RtImageTestCase {

Expand Down Expand Up @@ -110,4 +111,73 @@ public void returnsHistory() {
)
);
}

/**
* RtImage.delete() must send a DELETE request to the image's url.
* @throws Exception If something goes wrong.
*/
@Test
public void deleteSendsCorrectRequest() throws Exception {
new RtImage(
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Condition(
"RtImages.delete() must send a DELETE HTTP request",
req -> "DELETE".equals(req.getRequestLine().getMethod())
),
new Condition(
"RtImages.delete() must send the request to the image url",
req -> "http://localhost/images/test".equals(
req.getRequestLine().getUri()
)
)
),
URI.create("http://localhost/images/test")
).delete();
}

/**
* RtImage.delete() must throw UnexpectedResponseException if service
* responds with 404.
* @throws Exception The UnexpectedResponseException
*/
@Test(expected = UnexpectedResponseException.class)
public void deleteErrorOn404() throws Exception {
new RtImage(
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
URI.create("http://localhost/images/test")
).delete();
}

/**
* RtImage.delete() must throw UnexpectedResponseException if service
* responds with 409.
* @throws Exception The UnexpectedResponseException
*/
@Test(expected = UnexpectedResponseException.class)
public void deleteErrorOn409() throws Exception {
new RtImage(
new AssertRequest(
new Response(HttpStatus.SC_CONFLICT)
),
URI.create("http://localhost/images/test")
).delete();
}

/**
* RtImage.delete() must throw UnexpectedResponseException if service
* responds with 500.
* @throws Exception The UnexpectedResponseException
*/
@Test(expected = UnexpectedResponseException.class)
public void deleteErrorOn500() throws Exception {
new RtImage(
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("http://localhost/images/test")
).delete();
}
}