Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 26, 2018
2 parents f15e6d0 + 3574c4c commit 7995bd6
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/main/java/com/amihaiemil/docker/RemoteDocker.java
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import java.net.URI;
import org.apache.http.client.HttpClient;

/**
* Use this to communicate with a remote Docker API.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class RemoteDocker extends RtDocker {
/**
* Remote Docker engine.
* @param uri Remote Docker URI.
*/
public RemoteDocker(final URI uri) {
this(new SslHttpClient(), uri);
}

/**
* Remote Docker engine.
* @param client The http client to use.
* @param uri Remote Docker URI.
*/
public RemoteDocker(final HttpClient client, final URI uri) {
this(client, uri, "v1.35");
}

/**
* Remote Docker engine.
* @param client The http client to use.
* @param uri Remote Docker URI.
* @param version API version (eg. v1.35).
*/
public RemoteDocker(final HttpClient client, final URI uri,
final String version) {
super(client, uri);
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/amihaiemil/docker/RtDocker.java
Expand Up @@ -37,8 +37,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #11:30min Implement RemoteDocker which will make the requests over
* a tcp socket and TLS if certificates are provided.
*/
abstract class RtDocker implements Docker {

Expand Down
142 changes: 142 additions & 0 deletions src/main/java/com/amihaiemil/docker/SslHttpClient.java
@@ -0,0 +1,142 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

/**
* An HttpClient that works over a normal network socket.
*
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle ParameterNumber (150 lines)
* @todo #23:30min Add path for certificates as a parameter and, with those
* certificates, register the http/https protocols, similar to how "unix" is
* registered in UnixHttpClient.
*/
final class SslHttpClient implements HttpClient {
/**
* Decorated HttpClient.
*/
private final HttpClient origin;

/**
* Ctor.
*/
SslHttpClient() {
this(
HttpClients.custom()
.setMaxConnPerRoute(10)
.setMaxConnTotal(10)
.build()
);
}

/**
* Ctor.
* @param client Decorated HttpClient.
*/
SslHttpClient(final HttpClient client) {
this.origin = client;
}

@Override
public HttpParams getParams() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public ClientConnectionManager getConnectionManager() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public HttpResponse execute(final HttpUriRequest request)
throws IOException, ClientProtocolException {
return this.origin.execute(request);
}

@Override
public HttpResponse execute(final HttpUriRequest request,
final HttpContext context)
throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public HttpResponse execute(final HttpHost target,
final HttpRequest request)
throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public HttpResponse execute(final HttpHost target,
final HttpRequest request, final HttpContext context)
throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T> T execute(final HttpUriRequest request,
final ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T> T execute(final HttpUriRequest request,
final ResponseHandler<? extends T> responseHandler,
final HttpContext context) throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T> T execute(final HttpHost target, final HttpRequest request,
final ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T> T execute(final HttpHost target, final HttpRequest request,
final ResponseHandler<? extends T> responseHandler,
final HttpContext context) throws IOException, ClientProtocolException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
79 changes: 79 additions & 0 deletions src/test/java/com/amihaiemil/docker/RemoteDockerTestCase.java
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Response;
import java.net.URI;
import org.apache.http.HttpStatus;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Unit tests for {@link RemoteDocker}.
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle MethodName (500 lines)
* @todo #23:30min RemoteDocker: implement the rest of the test cases
* (including integration tests) for RemoteDocker.
*/
public final class RemoteDockerTestCase {
/**
* Ping must be TRUE if response is OK.
* @throws Exception If an error occurs.
*/
@Test
public void pingTrueIfResponseIsOk() throws Exception {
MatcherAssert.assertThat(
new RemoteDocker(
new AssertRequest(
new Response(HttpStatus.SC_OK, "")
),
URI.create("http://remotedocker")
).ping(),
Matchers.is(true)
);
}

/**
* Ping must be False if response is not OK.
* @throws Exception If an error occurs.
*/
@Test
public void pingFalseIfResponseIsNotOk() throws Exception {
MatcherAssert.assertThat(
new RemoteDocker(
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND, "")
),
URI.create("http://remotedocker")
).ping(),
Matchers.is(false)
);
}
}
58 changes: 58 additions & 0 deletions src/test/java/com/amihaiemil/docker/SslHttpClientTestCase.java
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Response;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Unit tests for {@link SslHttpClient}.
* @author George Aristy (george.aristy@gmail.com)
* @version $Id$
* @since 0.0.1
* @checkstyle MethodName (500 lines)
*/
public final class SslHttpClientTestCase {
/**
* DefaultHttpClient can execute the request and return the response.
* @throws Exception If an error occurs.
*/
@Test
public void executesRequestAndReturnsResponse() throws Exception {
final Response response = new Response(HttpStatus.SC_OK, "");
MatcherAssert.assertThat(
new SslHttpClient(
new AssertRequest(response)
).execute(new HttpGet()),
Matchers.is(response)
);
}
}

3 comments on commit 7995bd6

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7995bd6 Mar 26, 2018

Choose a reason for hiding this comment

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

Puzzle 11-6dd03fe4 disappeared from src/main/java/com/amihaiemil/docker/RtDocker.java, that's why I closed #23. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7995bd6 Mar 26, 2018

Choose a reason for hiding this comment

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

Puzzle 23-d9c71050 discovered in src/main/java/com/amihaiemil/docker/SslHttpClient.java and submitted as #67. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7995bd6 Mar 26, 2018

Choose a reason for hiding this comment

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

Puzzle 23-6815c522 discovered in src/test/java/com/amihaiemil/docker/RemoteDockerTestCase.java and submitted as #68. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.