DockerClientFactory { @Nonnull public DockerClient createClientWithAPINegotiation(DockerClientConfig config) { DockerCloudUtils.requireNonNull(config, "Client configuration cannot be null."); DockerClient client = createClient(config); DockerAPIVersion targetVersion = client.getApiVersion(); negotiate(client, targetVersion); return client; }  @Nonnull static DockerClientFactory getDefault(); @Nonnull DockerClient createClientWithAPINegotiation(DockerClientConfig config); @Nonnull abstract DockerClient createClient(DockerClientConfig config);  }
@Test public void apiNegotiationWithDefaultVersionRequested() { TestFactory clientFactory = createTestFactory(); clientFactory.fixture = clt -> clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")); DockerClientConfig config = new DockerClientConfig(TestDockerClient.TEST_CLIENT_URI, DockerAPIVersion.DEFAULT); TestDockerClient client = (TestDockerClient) clientFactory.createClientWithAPINegotiation(config); assertThat(client.getApiVersion()).isEqualTo(DockerAPIVersion.parse("9.99")); }
@Test public void apiNegotiationWithMatchingVersionProvided() { TestFactory clientFactory = createTestFactory(); DockerAPIVersion targetVersion = DockerAPIVersion.parse("9.99"); clientFactory.fixture = clt -> clt.setSupportedAPIVersion(targetVersion); DockerClientConfig config = new DockerClientConfig(TestDockerClient.TEST_CLIENT_URI, targetVersion); TestDockerClient client = (TestDockerClient) clientFactory.createClientWithAPINegotiation(config); assertThat(client.getApiVersion()).isEqualTo(targetVersion); }
@Test public void apiNegotiationTargetVersionInRange() { TestFactory clientFactory = createTestFactory(); DockerAPIVersion targetVersion = DockerAPIVersion.parse("8.5"); List<Consumer<TestDockerClient>> fixtures = Arrays.asList( clt -> { clt.setMinAPIVersion(DockerAPIVersion.parse("8.0")); clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")); }, clt -> { clt.setMinAPIVersion(DockerAPIVersion.parse("8.5")); clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")); }, clt -> { clt.setMinAPIVersion(DockerAPIVersion.parse("8.0")); clt.setSupportedAPIVersion(DockerAPIVersion.parse("8.5")); } ); DockerClientConfig config = new DockerClientConfig(TestDockerClient.TEST_CLIENT_URI, targetVersion); fixtures.forEach(fixture -> { clientFactory.fixture = fixture; TestDockerClient client = (TestDockerClient) clientFactory.createClientWithAPINegotiation(config); assertThat(client.getApiVersion()).isEqualTo(targetVersion); }); }
@Test public void apiNegotiationTargetVersionTooNew() { TestFactory clientFactory = createTestFactory(); DockerClientConfig config = new DockerClientConfig(TestDockerClient.TEST_CLIENT_URI, DockerAPIVersion.parse("10.0")); Arrays.<Consumer<TestDockerClient>>asList( clt -> { clt.setMinAPIVersion(DockerAPIVersion.parse("8.0")); clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")); }, clt -> clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")) ).forEach(fixture -> { clientFactory.fixture = fixture; TestDockerClient client = (TestDockerClient) clientFactory.createClientWithAPINegotiation(config); assertThat(client.getApiVersion()).isEqualTo(DockerAPIVersion.parse("9.99")); }); }
@Test public void apiNegotiationTargetVersionTooOld() { TestFactory clientFactory = createTestFactory(); DockerClientConfig config = new DockerClientConfig(TestDockerClient.TEST_CLIENT_URI, DockerAPIVersion.parse("7.0")); clientFactory.fixture = clt -> { clt.setMinAPIVersion(DockerAPIVersion.parse("8.0")); clt.setSupportedAPIVersion(DockerAPIVersion.parse("9.99")); }; TestDockerClient client = (TestDockerClient) clientFactory.createClientWithAPINegotiation(config); assertThat(client.getApiVersion()).isEqualTo(DockerAPIVersion.parse("8.0")); }