Skip to content

Commit 761b49b

Browse files
author
George Aristy
committed
(#85) RtNetworks, RtNetwork
1 parent e2ec92f commit 761b49b

File tree

6 files changed

+234
-3
lines changed

6 files changed

+234
-3
lines changed

src/main/java/com/amihaiemil/docker/Docker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
* @author Mihai Andronache ([email protected])
3333
* @version $Id$
3434
* @since 0.0.1
35-
* @todo #71:30min Continue implementing the rest of the Docker API (except
36-
* for Swarm and Images, which are being handled in another ticket).
35+
* @todo #85:30min Continue implementing the rest of the Docker API (except
36+
* for Network, Swarm and Images, which are being handled in other tickets).
3737
*/
3838
public interface Docker {
3939

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import javax.json.JsonObject;
29+
30+
/**
31+
* A docker network to which containers can be attached.
32+
*
33+
* @author George Aristy ([email protected])
34+
* @version $Id$
35+
* @see <a href="https://docs.docker.com/engine/api/v1.35/#tag/Network">Docker API v1.35</a>
36+
* @since 0.0.4
37+
*/
38+
public interface Network extends JsonObject {
39+
40+
}

src/main/java/com/amihaiemil/docker/Networks.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* @author Mihai Andronache ([email protected])
3131
* @version $Id$
3232
* @since 0.0.1
33+
* @todo #83:30min Implement Networks and Network interfaces. See
34+
* https://docs.docker.com/engine/api/v1.35/#tag/Network.
3335
*/
34-
public interface Networks {
36+
public interface Networks extends Iterable<Network> {
3537
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import javax.json.JsonObject;
29+
30+
/**
31+
* Runtime {@link Network}.
32+
*
33+
* @author George Aristy ([email protected])
34+
* @version $Id$
35+
* @since 0.0.4
36+
*/
37+
final class RtNetwork extends JsonResource implements Network {
38+
/**
39+
* Ctor.
40+
* @param json Network JSON
41+
*/
42+
RtNetwork(final JsonObject json) {
43+
super(() -> json);
44+
}
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import java.net.URI;
29+
import java.util.Iterator;
30+
import java.util.function.Supplier;
31+
import org.apache.http.client.HttpClient;
32+
import org.apache.http.client.methods.HttpGet;
33+
34+
/**
35+
* Runtime {@link Networks}.
36+
*
37+
* @author George Aristy ([email protected])
38+
* @version $Id$
39+
* @since 0.0.4
40+
*/
41+
final class RtNetworks implements Networks {
42+
/**
43+
* Network iterators.
44+
*/
45+
private final Supplier<Iterator<Network>> iter;
46+
47+
/**
48+
* Ctor.
49+
* @param http Http client
50+
* @param baseUri Docker API URI
51+
*/
52+
RtNetworks(final HttpClient http, final URI baseUri) {
53+
this.iter = () -> new ResourcesIterator<>(
54+
http,
55+
new HttpGet(baseUri.toString().concat("/networks")),
56+
RtNetwork::new
57+
);
58+
}
59+
60+
@Override
61+
public Iterator<Network> iterator() {
62+
return this.iter.get();
63+
}
64+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import com.amihaiemil.docker.mock.AssertRequest;
29+
import com.amihaiemil.docker.mock.Response;
30+
import java.net.URI;
31+
import org.hamcrest.MatcherAssert;
32+
import org.hamcrest.collection.IsIterableWithSize;
33+
import org.hamcrest.core.IsEqual;
34+
import org.junit.Test;
35+
36+
/**
37+
* Tests for {@link RtNetworks}.
38+
*
39+
* @author George Aristy ([email protected])
40+
* @version $Id$
41+
* @since 0.0.4
42+
*/
43+
public final class RtNetworksTest {
44+
/**
45+
* RtNetworks must iterate all networks returned by docker's API.
46+
*/
47+
@Test
48+
public void iterateNetworks() {
49+
MatcherAssert.assertThat(
50+
"Cannot iterate networks in JSON",
51+
new RtNetworks(
52+
new AssertRequest(
53+
new Response(
54+
200,
55+
// @checkstyle LineLength (20 lines)
56+
"[\n"
57+
+ " {\n"
58+
+ " \"Name\": \"bridge\",\n"
59+
+ " \"Id\": \"f2de39df4171b0dc801e8002d1d999b77256983dfc63041c0f34030aa3977566\",\n"
60+
+ " \"Created\": \"2016-10-19T06:21:00.416543526Z\"\n"
61+
+ " },\n"
62+
+ " {\n"
63+
+ " \"Name\": \"none\",\n"
64+
+ " \"Id\": \"e086a3893b05ab69242d3c44e49483a3bbbd3a26b46baa8f61ab797c1088d794\",\n"
65+
+ " \"Created\": \"0001-01-01T00:00:00Z\"\n"
66+
+ " },\n"
67+
+ " {\n"
68+
+ " \"Name\": \"host\",\n"
69+
+ " \"Id\": \"13e871235c677f196c4e1ecebb9dc733b9b2d2ab589e30c539efeda84a24215e\",\n"
70+
+ " \"Created\": \"0001-01-01T00:00:00Z\"\n"
71+
+ " }\n"
72+
+ "]"
73+
)
74+
),
75+
URI.create("http://test.docker.com")
76+
),
77+
new IsIterableWithSize<>(new IsEqual<>(3))
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)