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
4 changes: 2 additions & 2 deletions src/main/java/com/amihaiemil/docker/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #71:30min Continue implementing the rest of the Docker API (except
* for Swarm and Images, which are being handled in another ticket).
* @todo #85:30min Continue implementing the rest of the Docker API (except
* for Network, Swarm and Images, which are being handled in other tickets).
*/
public interface Docker {

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/amihaiemil/docker/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import javax.json.JsonObject;

/**
* A docker network to which containers can be attached.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @see <a href="https://docs.docker.com/engine/api/v1.35/#tag/Network">Docker API v1.35</a>
* @since 0.0.4
*/
public interface Network extends JsonObject {

}
4 changes: 3 additions & 1 deletion src/main/java/com/amihaiemil/docker/Networks.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #83:30min Implement Networks and Network interfaces. See
* https://docs.docker.com/engine/api/v1.35/#tag/Network.
*/
public interface Networks {
public interface Networks extends Iterable<Network> {
}
45 changes: 45 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtNetwork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import javax.json.JsonObject;

/**
* Runtime {@link Network}.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.4
*/
final class RtNetwork extends JsonResource implements Network {
/**
* Ctor.
* @param json Network JSON
*/
RtNetwork(final JsonObject json) {
super(() -> json);
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtNetworks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import java.net.URI;
import java.util.Iterator;
import java.util.function.Supplier;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

/**
* Runtime {@link Networks}.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.4
*/
final class RtNetworks implements Networks {
/**
* Network iterators.
*/
private final Supplier<Iterator<Network>> iter;

/**
* Ctor.
* @param http Http client
* @param baseUri Docker API URI
*/
RtNetworks(final HttpClient http, final URI baseUri) {
this.iter = () -> new ResourcesIterator<>(
http,
new HttpGet(baseUri.toString().concat("/networks")),
RtNetwork::new
);
Copy link
Owner

Choose a reason for hiding this comment

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

@llorllale This supplier is nice, I like it. I was actually struggling to find this solution at some point (can't remember where). I need to study Java 8 more, I guess...

}

@Override
public Iterator<Network> iterator() {
return this.iter.get();
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/amihaiemil/docker/RtNetworksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Response;
import java.net.URI;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsIterableWithSize;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
* Tests for {@link RtNetworks}.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.4
*/
public final class RtNetworksTest {
/**
* RtNetworks must iterate all networks returned by docker's API.
*/
@Test
public void iterateNetworks() {
MatcherAssert.assertThat(
"Cannot iterate networks in JSON",
new RtNetworks(
new AssertRequest(
new Response(
200,
// @checkstyle LineLength (20 lines)
"[\n"
+ " {\n"
+ " \"Name\": \"bridge\",\n"
+ " \"Id\": \"f2de39df4171b0dc801e8002d1d999b77256983dfc63041c0f34030aa3977566\",\n"
+ " \"Created\": \"2016-10-19T06:21:00.416543526Z\"\n"
+ " },\n"
+ " {\n"
+ " \"Name\": \"none\",\n"
+ " \"Id\": \"e086a3893b05ab69242d3c44e49483a3bbbd3a26b46baa8f61ab797c1088d794\",\n"
+ " \"Created\": \"0001-01-01T00:00:00Z\"\n"
+ " },\n"
+ " {\n"
+ " \"Name\": \"host\",\n"
+ " \"Id\": \"13e871235c677f196c4e1ecebb9dc733b9b2d2ab589e30c539efeda84a24215e\",\n"
+ " \"Created\": \"0001-01-01T00:00:00Z\"\n"
+ " }\n"
+ "]"
Copy link
Owner

Choose a reason for hiding this comment

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

@llorllale Isn't it possible to build a JsonArray and call toString?

Json.createArrayBuilder()
    .add(...)
    .add(...)
    .build()
    .toString()

Normally, passing a direct String is better (theory-wise), since you're not involving other classes in the unit test, but I think it's safe to trust the javax.json implementstiond :D

Copy link
Contributor Author

@llorllale llorllale Sep 20, 2018

Choose a reason for hiding this comment

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

@amihaiemil I passed a string in order to mirror the docs more closely: basically just copy-pasted the example from the docs (minus irrelevant bits). Least amount of work and hassle.

Still want me to build it?

)
),
URI.create("http://test.docker.com")
),
new IsIterableWithSize<>(new IsEqual<>(3))
);
}
}