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
8 changes: 8 additions & 0 deletions src/main/java/com/amihaiemil/docker/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ public interface Container {
* expected.
*/
void stop() throws IOException, UnexpectedResponseException;

/**
* Restarts the container.
* @throws IOException If something goes wrong.
* @throws UnexpectedResponseException If the status response is not
* expected.
*/
void restart() throws IOException, UnexpectedResponseException;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llorllale UnexpectedResponseException is runtime exception, no need to declare it :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil checkstyle fails if we don't include it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llorllale strange, I don't remember checkstyle being that strict. Do you know how to add an exception for it quickly, or should we merge and I'll look into it later?

Copy link
Contributor Author

@llorllale llorllale Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil to be clear, checkstyle fails when I add the @throws tag without declaring the exception. What you want is to include @throws without declaring the exception in throws, correct?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llorllale you're right, let's leave it this way then.

}
21 changes: 20 additions & 1 deletion src/main/java/com/amihaiemil/docker/RtContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #57:30min Keep implementing the rest of the Container operations.
* @todo #76:30min Continue implementing the rest of the Container operations.
* See the Docker API Docs for reference:
* https://docs.docker.com/engine/api/v1.35/#tag/Container
* @todo #46:30min Once we have the CI environment properly setup with a Docker
Expand Down Expand Up @@ -113,4 +113,23 @@ public void stop() throws IOException {
stop.releaseConnection();
}
}

@Override
public void restart() throws IOException, UnexpectedResponseException {
final HttpPost stop = new HttpPost(
this.baseUri.toString() + "/restart"
);
try {
final int status = this.client.execute(stop)
.getStatusLine()
.getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT) {
throw new UnexpectedResponseException(
stop.getURI().toString(), status, HttpStatus.SC_NO_CONTENT
);
}
} finally {
stop.releaseConnection();
}
}
}
59 changes: 59 additions & 0 deletions src/test/java/com/amihaiemil/docker/RtContainerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,63 @@ public void stopsWithNotModified() throws Exception {
URI.create("http://localhost:80/1.30/containers/123")
).stop();
}

/**
* Wellformed request for the restart command.
* @throws Exception If something goes wrong.
*/
@Test
public void wellformedRestartRequest() throws Exception {
new RtContainer(
new AssertRequest(
new Response(
HttpStatus.SC_NO_CONTENT, ""
),
new Condition(
"Method should be a POST",
req -> req.getRequestLine().getMethod().equals("POST")
),
new Condition(
"Resource path must be /{id}/restart",
req -> req.getRequestLine()
.getUri().endsWith("/9403/restart")
)
),
URI.create("http://localhost:80/1.30/containers/9403")
).restart();
}

/**
* RtContainer throws UnexpectedResponseException if it receives server
* error 500 on restart.
* @throws Exception The UnexpectedResponseException.
*/
@Test(expected = UnexpectedResponseException.class)
public void restartWithServerError() throws Exception {
new RtContainer(
new AssertRequest(
new Response(
HttpStatus.SC_INTERNAL_SERVER_ERROR, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).restart();
}

/**
* RtContainer throws UnexpectedResponseException if it receives 404 error
* on restart.
* @throws Exception The UnexpectedResponseException.
*/
@Test(expected = UnexpectedResponseException.class)
public void restartsWithNotFound() throws Exception {
new RtContainer(
new AssertRequest(
new Response(
HttpStatus.SC_NOT_FOUND, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).restart();
}
}