From 5a6b1cc379c30536549520c15cb85ac23156e0ae Mon Sep 17 00:00:00 2001 From: ry99 Date: Thu, 2 Apr 2020 17:40:45 -0400 Subject: [PATCH] added test case --- .../java/com/amihaiemil/docker/Container.java | 2 +- .../com/amihaiemil/docker/RtContainer.java | 2 +- .../docker/RtContainerTestCase.java | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/Container.java b/src/main/java/com/amihaiemil/docker/Container.java index 4d2ba7b3..f23aee08 100644 --- a/src/main/java/com/amihaiemil/docker/Container.java +++ b/src/main/java/com/amihaiemil/docker/Container.java @@ -161,7 +161,7 @@ void remove(final boolean volumes, final boolean force, final boolean link) * Wait Container. * @throws IOException If something goes wrong. * the expected one (200). - * @return the exit code of the container + * @return The exit code of the container */ int waitOn(String state) throws IOException; diff --git a/src/main/java/com/amihaiemil/docker/RtContainer.java b/src/main/java/com/amihaiemil/docker/RtContainer.java index a73ed77b..d2cd5c28 100644 --- a/src/main/java/com/amihaiemil/docker/RtContainer.java +++ b/src/main/java/com/amihaiemil/docker/RtContainer.java @@ -237,7 +237,7 @@ public int waitOn(final String state) throws IOException { UncheckedUriBuilder urib = new UncheckedUriBuilder( this.baseUri.toString().concat("/wait") ); - if(null == state || state.isEmpty()){ + if(!(null == state || state.isEmpty())){ urib.addParameter("condition", state); } diff --git a/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java b/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java index 5e1b1ccc..68292c4c 100644 --- a/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java @@ -37,6 +37,7 @@ import javax.json.Json; import javax.json.JsonObject; import java.net.URI; +import static org.junit.Assert.assertEquals; /** * Unit tests for RtContainer. @@ -627,4 +628,82 @@ public void getsLogs() { Matchers.notNullValue() ); } + + /** + * RtContainer can wait with no problem. + * @throws Exception If something goes wrong. + */ + @Test + public void waitsOk() throws Exception { + int retval = new RtContainer( + Json.createObjectBuilder().add("Id", "123").build(), + new AssertRequest( + new Response( + HttpStatus.SC_OK, + Json.createObjectBuilder() + .add("StatusCode", 0) + .build().toString() + ), + new Condition( + "Method should be a POST", + req -> req.getRequestLine().getMethod().equals("POST") + ), + new Condition( + "Resource path must be /123/wait", + req -> req.getRequestLine().getUri().endsWith("/wait") + ) + ), + URI.create("http://localhost:80/1.30/containers/123"), + Mockito.mock(Docker.class) + ).waitOn(null); + assertEquals(0, retval); + } + + /** + * RtContainer can wait with a condition. + * @throws Exception If something goes wrong. + */ + @Test + public void waitsOkWithCondition() throws Exception { + int retval = new RtContainer( + Json.createObjectBuilder().add("Id", "123").build(), + new AssertRequest( + new Response( + HttpStatus.SC_OK, + Json.createObjectBuilder() + .add("StatusCode", 0) + .build().toString() + ), + new Condition( + "Method should be a POST", + req -> req.getRequestLine().getMethod().equals("POST") + ), + new Condition( + "Resource path must be /123/wait", + req -> req.getRequestLine().getUri().endsWith("ition=next-exit") + ) + ), + URI.create("http://localhost:80/1.30/containers/123"), + Mockito.mock(Docker.class) + ).waitOn("next-exit"); + assertEquals(0, retval); + } + + /** + * RtContainer throws URE if it receives a server error on remove. + * @throws Exception If something goes wrong. + */ + @Test(expected = UnexpectedResponseException.class) + public void waitWithServerError() throws Exception { + new RtContainer( + Json.createObjectBuilder().build(), + new AssertRequest( + new Response( + HttpStatus.SC_INTERNAL_SERVER_ERROR + ) + ), + URI.create("http://localhost:80/1.30/containers/123"), + Mockito.mock(Docker.class) + ).waitOn(null); + } }