|
| 1 | +package com.github.dockerjava.api.model; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.github.dockerjava.api.model.Ports.Binding; |
| 11 | + |
| 12 | +public class PortsTest { |
| 13 | +private final ObjectMapper objectMapper = new ObjectMapper(); |
| 14 | + |
| 15 | +@Test |
| 16 | +public void deserializingPortWithMultipleBindingsReturnsFirstBinding() throws Exception { |
| 17 | +String json = "{\"80/tcp\":[{\"HostIp\":\"10.0.0.1\",\"HostPort\":\"80\"},{\"HostIp\":\"10.0.0.2\",\"HostPort\":\"80\"}]}"; |
| 18 | +Ports ports = objectMapper.readValue(json, Ports.class); |
| 19 | +Map<ExposedPort, Binding> bindings = ports.getBindings(); |
| 20 | +assertEquals(bindings.size(), 1); |
| 21 | + |
| 22 | +Binding binding = bindings.get(ExposedPort.tcp(80)); |
| 23 | +assertEquals(binding, new Binding("10.0.0.1", 80)); |
| 24 | + } |
| 25 | + |
| 26 | +@Test |
| 27 | +public void serializePort() throws Exception { |
| 28 | +Ports ports = new Ports(ExposedPort.tcp(80), new Binding("10.0.0.1", 80)); |
| 29 | +String expectedJson = "{\"80/tcp\":[{\"HostIp\":\"10.0.0.1\",\"HostPort\":\"80\"}]}"; |
| 30 | +assertEquals(objectMapper.writeValueAsString(ports), expectedJson); |
| 31 | + } |
| 32 | + |
| 33 | +} |