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
81 changes: 58 additions & 23 deletions src/main/java/com/amihaiemil/docker/RtNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@

import java.io.IOException;
import java.net.URI;
import javax.json.Json;
import javax.json.JsonObject;

import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

/**
* Runtime {@link Network}.
Expand Down Expand Up @@ -75,47 +81,76 @@ final class RtNetwork extends JsonResource implements Network {
@Override
public JsonObject inspect()
throws IOException, UnexpectedResponseException {
throw new UnsupportedOperationException(
String.join(" ",
"Network.inspect() is not yet implemented.",
"If you can contribute please",
"do it here: https://www.github.com/amihaiemil/docker-java-api"
)
);
return new Inspection(this.client, this.baseUri.toString());
}

@Override
public void remove() throws IOException, UnexpectedResponseException {
throw new UnsupportedOperationException(
String.join(" ",
"Network.remove() is not yet implemented.",
"If you can contribute please",
"do it here: https://www.github.com/amihaiemil/docker-java-api"
)
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
this.baseUri.toString()
);
final HttpDelete delete = new HttpDelete(
uri.build()
);
try {
this.client.execute(
delete,
new MatchStatus(delete.getURI(), HttpStatus.SC_NO_CONTENT)
);
} finally {
delete.releaseConnection();
}
}

@Override
public void connect(final String containerId)
throws IOException, UnexpectedResponseException {
throw new UnsupportedOperationException(
String.join(" ",
"Network.connect() is not yet implemented.",
"If you can contribute please",
"do it here: https://www.github.com/amihaiemil/docker-java-api"
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
this.baseUri.toString() + "/connect"
);
final HttpPost post = new HttpPost(
uri.build()
);
post.setEntity(
new StringEntity(
Copy link
Contributor

Choose a reason for hiding this comment

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

@paulodamaso Please use JsonObject here instead of composing it with String.format. This will make the code reusable when adding more methods with optional parameters. For example, in the next step, new connect method can be added to support optional EndpointConfig parameter, like this: connect(final String containerId, final JsonObject endpointConfig)

Json.createObjectBuilder().add("Container", containerId)
.build().toString()
)
);
try {
this.client.execute(
post,
new MatchStatus(post.getURI(), HttpStatus.SC_OK)
);
} finally {
post.releaseConnection();
}
}

@Override
public void disconnect(final String containerId)
throws IOException, UnexpectedResponseException {
throw new UnsupportedOperationException(
String.join(" ",
"Network.disconnect() is not yet implemented.",
"If you can contribute please",
"do it here: https://www.github.com/amihaiemil/docker-java-api"
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
this.baseUri.toString() + "/disconnect"
);
final HttpPost post = new HttpPost(
uri.build()
);
post.setEntity(
new StringEntity(
Json.createObjectBuilder()
.add("Container", containerId)
.add("Force", "true")
.build().toString()
)
);
try {
this.client.execute(
post,
new MatchStatus(post.getURI(), HttpStatus.SC_OK)
);
} finally {
post.releaseConnection();
}
}
}
71 changes: 44 additions & 27 deletions src/test/java/com/amihaiemil/docker/RtNetworkTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import javax.json.Json;
import javax.json.JsonObject;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsEmptyIterable;
import org.hamcrest.core.IsEqual;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;

Expand All @@ -60,7 +62,6 @@ public final class RtNetworkTestCase {
* @throws Exception If something else goes wrong.
*/
@Test
@Ignore
public void inspectsItself() throws Exception {
final Network network = new RtNetwork(
Json.createObjectBuilder().build(),
Expand Down Expand Up @@ -114,12 +115,11 @@ public void inspectsItself() throws Exception {
* @throws Exception If something goes wrong.
*/
@Test
@Ignore
public void removeSendsCorrectRequest() throws Exception {
new RtNetwork(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Response(HttpStatus.SC_NO_CONTENT),
new Condition(
"remove() must send a DELETE HTTP request",
req -> "DELETE".equals(req.getRequestLine().getMethod())
Expand All @@ -142,7 +142,6 @@ public void removeSendsCorrectRequest() throws Exception {
* @throws IOException If something goes wrong.
*/
@Test
@Ignore
public void connectContainer() throws IOException {
final Network network = new RtNetwork(
Json.createObjectBuilder().build(),
Expand All @@ -153,21 +152,31 @@ public void connectContainer() throws IOException {
req -> "POST".equals(req.getRequestLine().getMethod())
),
new Condition(
"connect() must send the request to the network url",
req -> "http://localhost/network/id1".equals(
req.getRequestLine().getUri()
)
"Resource path must be /connect",
req -> req.getRequestLine().getUri().endsWith("/connect")
),
new Condition(
"connect() must send the the container in the request body",
req -> {
boolean condition = false;
try{
condition =
EntityUtils.toString(
((HttpEntityEnclosingRequest) req)
.getEntity(),
Charset.defaultCharset()
).contains("containerId");
} catch (final IOException error){
condition = false;
}
return condition;
}
)
),
URI.create("http://localhost/network/id1"),
DOCKER
);
network.connect("containerId");
MatcherAssert.assertThat(
"could not create container",
network.inspect().getJsonArray("Container"),
new IsEmptyIterable<>()
);
}

/**
Expand All @@ -176,32 +185,40 @@ public void connectContainer() throws IOException {
* @throws IOException If something goes wrong.
*/
@Test
@Ignore
public void disconnectContainer() throws IOException {
final Network network = new RtNetwork(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Condition(
"connect() must send a POST HTTP request",
"disconnect() must send a POST HTTP request",
req -> "POST".equals(req.getRequestLine().getMethod())
),
new Condition(
"connect() must send the request to the network url",
req -> "http://localhost/network/id1".equals(
req.getRequestLine().getUri()
)
"Resource path must be /disconnect",
req -> req.getRequestLine().getUri().endsWith("/disconnect")
),
new Condition(
"disconnect() must send the container in the request body",
req -> {
boolean condition = false;
try{
condition =
EntityUtils.toString(
((HttpEntityEnclosingRequest) req)
.getEntity(),
Charset.defaultCharset()
).contains("containerId");
} catch (final IOException error){
condition = false;
}
return condition;
}
)
),
URI.create("http://localhost/network/id1"),
DOCKER
);
network.connect("containerId");
network.disconnect("containerId");
MatcherAssert.assertThat(
"could not create container",
network.inspect().getJsonArray("Container").size(),
new IsEqual<>(0)
);
}
}