diff --git a/README.md b/README.md index 1a72c83..4657639 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # zold-java-client +[![Managed by Zerocrat](https://www.0crat.com/badge/GFCHY7NQG.svg)](https://www.0crat.com/p/GFCHY7NQG) + [![Build Status](https://travis-ci.org/amihaiemil/zold-java-client.svg?branch=master)](https://travis-ci.org/amihaiemil/zold-java-client) [![Coverage Status](https://coveralls.io/repos/github/amihaiemil/zold-java-client/badge.svg?branch=master)](https://coveralls.io/github/amihaiemil/zold-java-client?branch=master) +[![Donate via Zerocracy](https://www.0crat.com/contrib-badge/GFCHY7NQG.svg)](https://www.0crat.com/contrib/GFCHY7NQG) [![DevOps By Rultor.com](http://www.rultor.com/b/amihaiemil/zold-java-client)](http://www.rultor.com/p/amihaiemil/zold-java-client) [![We recommend IntelliJ IDEA](http://amihaiemil.github.io/images/intellij-idea-recommend.svg)](https://www.jetbrains.com/idea/) @@ -34,3 +37,10 @@ Make sure the maven build: passes before making a PR. [Checkstyle](http://checkstyle.sourceforge.net/) will make sure you're following our code style and guidlines. + +This project is managed by [Zerocracy](http://www.zerocracy.com/), see the +[Policy](http://www.zerocracy.com/policy.html) for more details. + +Note that we do not have Zerocracy QAs, yet we still try to adhere to the [QA + rules](http://www.zerocracy.com/policy.html#42) as much as possible (we won't block any PRs for cosmetic stuff such + as commit messages, though). \ No newline at end of file diff --git a/src/main/java/com/amihaiemil/zold/RestfulZoldWts.java b/src/main/java/com/amihaiemil/zold/RestfulZoldWts.java new file mode 100644 index 0000000..a494c22 --- /dev/null +++ b/src/main/java/com/amihaiemil/zold/RestfulZoldWts.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2019, 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 zold-java-client 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.zold; + +import java.net.URI; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClients; + +/** + * RESTful Zold network entry point. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.1 + * @todo #11:30min implement body of interface methods. + */ +public final class RestfulZoldWts implements ZoldWts { + + /** + * Apache HttpClient which sends the requests. + */ + private final HttpClient client; + + /** + * Base URI. + */ + private final URI baseUri; + + /** + * Constructor. + * @param baseUri Base URI. + */ + public RestfulZoldWts(final URI baseUri) { + this( + HttpClients.custom() + .setMaxConnPerRoute(10) + .setMaxConnTotal(10) + .build(), + baseUri + ); + } + + /** + * Constructor. We recommend you to use the simple constructor + * and let us configure the HttpClient for you.

+ * Use this constructor only if you know what you're doing. + * + * @param client Given HTTP Client. + * @param baseUri Base URI. + */ + public RestfulZoldWts(final HttpClient client, final URI baseUri) { + this.client = client; + this.baseUri = baseUri; + } + + /** + * Pull the wallet from the network. + * @return Wallet object. + * @todo #11:30min should avoid using null + * (it's here for now till the body is implemented) + * maybe should throw an exception when pull fails too + */ + public Wallet pull() { + return null; + } +} diff --git a/src/main/java/com/amihaiemil/zold/UserAgentRequestHeader.java b/src/main/java/com/amihaiemil/zold/UserAgentRequestHeader.java new file mode 100644 index 0000000..39148a3 --- /dev/null +++ b/src/main/java/com/amihaiemil/zold/UserAgentRequestHeader.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2019, 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 zold-java-client 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.zold; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.Properties; +import org.apache.http.HttpHeaders; +import org.apache.http.client.protocol.RequestDefaultHeaders; +import org.apache.http.message.BasicHeader; + +/** + * User Agent Request Header Interceptor. + * @author Ammar Atef (a_atef45@yahoo.com) + * @version $Id$ + * @since 0.0.1 + * @todo #7:30min We should use this class wherever we are + * instantiating an HttpClient in order to send the User-Agent + * HTTP header. + */ +final class UserAgentRequestHeader extends RequestDefaultHeaders { + + /** + * Config properties file. + */ + private static final String CONFIG_FILE = "config.properties"; + + /** + * Version property key. + */ + private static final String VERSION_KEY = "build.version"; + + /** + * Ctor. + */ + UserAgentRequestHeader() { + super(Collections.singletonList( + new BasicHeader( + HttpHeaders.USER_AGENT, + String.join( + " ", + "zold-java-client /", + version(), + "See https://github.com/amihaiemil/zold-java-client" + ) + ) + )); + } + + /** + * Read current version from property file. + * @return Build version. + */ + private static String version() { + final ClassLoader loader = + Thread.currentThread().getContextClassLoader(); + final String version; + final Properties properties = new Properties(); + try (final InputStream inputStream = + loader.getResourceAsStream(CONFIG_FILE)){ + properties.load(inputStream); + version = properties.getProperty(VERSION_KEY); + } catch (final IOException exception) { + throw new RuntimeException( + String.format("Missing %s file.", CONFIG_FILE) + ); + } + return version; + } + +} diff --git a/src/main/java/com/amihaiemil/zold/Wallet.java b/src/main/java/com/amihaiemil/zold/Wallet.java new file mode 100644 index 0000000..2559c32 --- /dev/null +++ b/src/main/java/com/amihaiemil/zold/Wallet.java @@ -0,0 +1,33 @@ +package com.amihaiemil.zold; + +/** + * Zold Wallet. + * @author Ammar Atef (ammar.atef45@gmail.com) + * @version $Id$ + * @since 0.0.1 + */ +public interface Wallet { + /** + * Get the balance of the wallet. + * @return Balance + */ + double balance(); + + /** + * Pay to another wallet. + * @param keygap Sender keygap + * @param user Recipient user id + * @param amount Amount to be sent + * @param details The details of transfer + * @todo #11:30min solve checkstyle paramternumber error either by cahnging the + * method structure or supressing the warning + */ + void pay(String keygap, String user, double amount, String details); + + /** + * Finds all payments that match this query and returns. + * @param id Wallet id + * @param details Regex of payment details + */ + void find(String id, String details); +} diff --git a/src/main/java/com/amihaiemil/zold/ZoldWts.java b/src/main/java/com/amihaiemil/zold/ZoldWts.java new file mode 100644 index 0000000..503f1c2 --- /dev/null +++ b/src/main/java/com/amihaiemil/zold/ZoldWts.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2019, 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 zold-java-client 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.zold; + +/** + * Zold network entry point. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.1 + */ +public interface ZoldWts { + /** + * Pull the wallet from the network. + * @return Wallet object. + */ + Wallet pull(); + +} diff --git a/src/main/java/com/amihaiemil/zold/placeholder.md b/src/main/java/com/amihaiemil/zold/placeholder.md deleted file mode 100644 index 98e42ce..0000000 --- a/src/main/java/com/amihaiemil/zold/placeholder.md +++ /dev/null @@ -1 +0,0 @@ -placeholder for src/test/resources diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..9b2807d --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1 @@ +build.version=${project.version} diff --git a/src/test/java/com/amihaiemil/zold/RestfulWtsTestCase.java b/src/test/java/com/amihaiemil/zold/RestfulWtsTestCase.java new file mode 100644 index 0000000..f7d524f --- /dev/null +++ b/src/test/java/com/amihaiemil/zold/RestfulWtsTestCase.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2019, 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 zold-java-client 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.zold; + +import java.net.URI; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit tests for {@link RestfulZoldNet}. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.1 + */ +public final class RestfulWtsTestCase { + + /** + * {@link RestfulWts} can be instantiated. + */ + @Test + public void isInstantiated() { + MatcherAssert.assertThat( + new RestfulZoldWts(URI.create("localhost:8080/zold")), + Matchers.instanceOf(ZoldWts.class) + ); + } +} diff --git a/src/test/java/com/amihaiemil/zold/placeholder.md b/src/test/java/com/amihaiemil/zold/placeholder.md deleted file mode 100644 index 98e42ce..0000000 --- a/src/test/java/com/amihaiemil/zold/placeholder.md +++ /dev/null @@ -1 +0,0 @@ -placeholder for src/test/resources