Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static ExposedPorts fromPrimitive(Map<String, Object> object) {

@JsonValue
public Map<String, Object> toPrimitive() {
return Stream.of(exposedPorts).collect(Collectors.toMap(
return Stream.of(exposedPorts)
.distinct()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

instead of distinct(), we can use a merge strategy (3rd argument of toMap) that does not throw, but uses the previously set value

.collect(Collectors.toMap(
ExposedPort::toString,
__ -> new Object()
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.notNullValue;

public class ExposedPortsTest {
Expand Down Expand Up @@ -46,4 +49,18 @@ public void usesFromJson() throws Exception {
new ExposedPort(3868, InternetProtocol.SCTP)
));
}

@Test
public void usesFromJsonWithDuplicate() throws Exception {
ExposedPorts ports = new ExposedPorts(
new ExposedPort(80, InternetProtocol.UDP),
new ExposedPort(80),
new ExposedPort(80));

assertThat(ports, notNullValue());
assertThat(ports.getExposedPorts(), arrayWithSize(3));

Map<String, Object> map = ports.toPrimitive();
assertThat(map, aMapWithSize(2));
}
}