Skip to content

Commit 472f927

Browse files
elliotsayesElliot Sayes
andauthored
SCTP Support (#1341)
* Add SCTP as InternetProtocol * Add SCTP as ExposedPort * Fix documentation in InternetProtocol * Change import format to how it was previously * Fixed test failing as JSON mapper uses alphabetical order. * Tests now use "arrayContainingInAnyOrder" instead of "arrayContaining". Casting test/expected JSON to Map for unordered comparison rather than strict text comparison * Changed to cast to generic JsonNode class, can then compare entries as arrays Co-authored-by: Elliot Sayes <Elliot.Sayes@mbie.govt.nz>
1 parent 906e1a2 commit 472f927

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/model/ExposedPort.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.github.dockerjava.api.model.InternetProtocol.TCP;
44
import static com.github.dockerjava.api.model.InternetProtocol.UDP;
5+
import static com.github.dockerjava.api.model.InternetProtocol.SCTP;
56

67
import java.io.Serializable;
78

@@ -50,7 +51,7 @@ public ExposedPort(int port) {
5051
* Creates an {@link ExposedPort} for the given parameters.
5152
*
5253
* @param scheme
53-
* the {@link #getScheme() scheme}, <code>tcp</code> or <code>udp</code>
54+
* the {@link #getScheme() scheme}, <code>tcp</code>, <code>udp</code> or <code>sctp</code>
5455
* @param port
5556
* the {@link #getPort() port number}
5657
* @deprecated use {@link #ExposedPort(int, InternetProtocol)}
@@ -68,7 +69,7 @@ public InternetProtocol getProtocol() {
6869
}
6970

7071
/**
71-
* @return the scheme (internet protocol), <code>tcp</code> or <code>udp</code>
72+
* @return the scheme (internet protocol), <code>tcp</code>, <code>udp</code> or <code>sctp</code>
7273
* @deprecated use {@link #getProtocol()}
7374
*/
7475
@Deprecated
@@ -97,6 +98,14 @@ public static ExposedPort udp(int port) {
9798
return new ExposedPort(port, UDP);
9899
}
99100

101+
/**
102+
* Creates an {@link ExposedPort} for {@link InternetProtocol#SCTP}. This is a shortcut for
103+
* <code>new ExposedPort(port, {@link InternetProtocol#SCTP})</code>
104+
*/
105+
public static ExposedPort sctp(int port) {
106+
return new ExposedPort(port, SCTP);
107+
}
108+
100109
/**
101110
* Parses a textual port specification (as used by the Docker CLI) to an {@link ExposedPort}.
102111
*

docker-java-api/src/main/java/com/github/dockerjava/api/model/InternetProtocol.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* @see #TCP
77
* @see #UDP
8+
* @see #SCTP
89
*/
910
public enum InternetProtocol {
1011
/**
@@ -15,7 +16,12 @@ public enum InternetProtocol {
1516
/**
1617
* The <i>User Datagram Protocol</i>
1718
*/
18-
UDP;
19+
UDP,
20+
21+
/**
22+
* The <i>Stream Control Transmission Protocol</i>
23+
*/
24+
SCTP;
1925

2026
/**
2127
* The default {@link InternetProtocol}: {@link #TCP}

docker-java-api/src/main/java/com/github/dockerjava/api/model/PortConfigProtocol.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public enum PortConfigProtocol {
1111
TCP,
1212

1313
@JsonProperty("udp")
14-
UDP
14+
UDP,
15+
16+
@JsonProperty("sctp")
17+
SCTP
1518

1619
}
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.github.dockerjava.test.serdes.JSONTestHelper;
5+
import org.apache.commons.compress.utils.Lists;
46
import org.junit.Test;
57

6-
import static org.hamcrest.Matchers.arrayContaining;
7-
import static org.hamcrest.Matchers.is;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import java.util.Map.Entry;
11+
12+
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
813
import static org.hamcrest.Matchers.notNullValue;
914
import static org.junit.Assert.assertThat;
1015

@@ -14,21 +19,32 @@ public class ExposedPortsTest {
1419
public void usesToJson() throws Exception {
1520
ExposedPorts ports = new ExposedPorts(
1621
new ExposedPort(80),
17-
new ExposedPort(123, InternetProtocol.UDP)
22+
new ExposedPort(123, InternetProtocol.UDP),
23+
new ExposedPort(3868, InternetProtocol.SCTP)
1824
);
1925
String json = JSONTestHelper.getMapper().writeValueAsString(ports);
26+
List<Entry<String, JsonNode>> jsonEntries = getJsonEntries(json);
27+
28+
String jsonExpected = "{\"80/tcp\":{},\"123/udp\":{},\"3868/sctp\":{}}";
29+
List<Entry<String, JsonNode>> jsonEntriesExpected = getJsonEntries(jsonExpected);
30+
31+
assertThat(jsonEntries.toArray(), arrayContainingInAnyOrder(jsonEntriesExpected.toArray()));
32+
}
2033

21-
assertThat(json, is("{\"80/tcp\":{},\"123/udp\":{}}"));
34+
private List<Entry<String, JsonNode>> getJsonEntries(String json) throws Exception {
35+
JsonNode jsonNode = JSONTestHelper.getMapper().readValue(json, JsonNode.class);
36+
return Lists.newArrayList(jsonNode.fields());
2237
}
2338

2439
@Test
2540
public void usesFromJson() throws Exception {
26-
ExposedPorts ports = JSONTestHelper.getMapper().readValue("{\"80/tcp\":{},\"123/udp\":{}}", ExposedPorts.class);
41+
ExposedPorts ports = JSONTestHelper.getMapper().readValue("{\"80/tcp\":{},\"123/udp\":{},\"3868/sctp\":{}}", ExposedPorts.class);
2742

2843
assertThat(ports, notNullValue());
29-
assertThat(ports.getExposedPorts(), arrayContaining(
44+
assertThat(ports.getExposedPorts(), arrayContainingInAnyOrder(
3045
new ExposedPort(80),
31-
new ExposedPort(123, InternetProtocol.UDP)
46+
new ExposedPort(123, InternetProtocol.UDP),
47+
new ExposedPort(3868, InternetProtocol.SCTP)
3248
));
3349
}
3450
}

0 commit comments

Comments
 (0)