Skip to content

Commit

Permalink
Merge branch 'master' of github.com:amihaiemil/zold-java-client
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jan 20, 2019
2 parents 77fea9b + 10d890e commit 5d9ef72
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 2 deletions.
10 changes: 10 additions & 0 deletions 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/)

Expand Down Expand Up @@ -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).
89 changes: 89 additions & 0 deletions 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. <br><br>
* 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;
}
}
95 changes: 95 additions & 0 deletions 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;
}

}
33 changes: 33 additions & 0 deletions 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);
}
41 changes: 41 additions & 0 deletions 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();

}
1 change: 0 additions & 1 deletion src/main/java/com/amihaiemil/zold/placeholder.md

This file was deleted.

1 change: 1 addition & 0 deletions src/main/resources/config.properties
@@ -0,0 +1 @@
build.version=${project.version}
52 changes: 52 additions & 0 deletions 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)
);
}
}
1 change: 0 additions & 1 deletion src/test/java/com/amihaiemil/zold/placeholder.md

This file was deleted.

0 comments on commit 5d9ef72

Please sign in to comment.