Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit d91c0b2

Browse files
authored
Merge pull request #16 from LucaScorpion/rework-exception-model
Rework exception model, add smoke tests
2 parents 62ab2c3 + 6e4b62b commit d91c0b2

28 files changed

Lines changed: 284 additions & 252 deletions

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.1.0
4+
5+
Add an exception model and change the return values of all API methods.
6+
All methods now simply return the value, or throw an exception if something went wrong.
7+
38
## 2.0.0
49

5-
Initial version (since rework).
10+
Initial version.

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ pipeline {
2222

2323
def mvn(String goals) {
2424
def mvnHome = tool 'Maven 3'
25-
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore $goals"
25+
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore -Djavarant.test.smoke $goals"
2626
}

README.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,33 @@ A devRant API wrapper for Java.
55
[![Jenkins](https://img.shields.io/jenkins/s/https/jenkins.scorpiac.com/job/JavaRant/job/master.svg)](https://jenkins.scorpiac.com/job/JavaRant/)
66
[![Jenkins tests](https://img.shields.io/jenkins/t/https/jenkins.scorpiac.com/job/JavaRant/job/master.svg)](https://jenkins.scorpiac.com/job/JavaRant/job/master/lastCompletedBuild/testReport/)
77

8-
## Using JavaRant
8+
## Maven
99
JavaRant is available on [Maven](http://mvnrepository.com/artifact/com.scorpiac.javarant/javarant), simply add this dependency to your `pom.xml` file:
1010

1111
```xml
1212
<dependency>
1313
<groupId>com.scorpiac.javarant</groupId>
1414
<artifactId>javarant</artifactId>
15-
<version>2.0.0</version>
15+
<version>2.1.0</version>
1616
</dependency>
1717
```
1818

19-
## Getting started
19+
## Using JavaRant
2020

2121
To access devRant simply create a new `DevRant` object:
2222

2323
```
2424
DevRant devRant = new DevRant();
2525
```
2626

27-
Most object that are returned from `DevRant` are wrapped in a `Result` object.
28-
This will contain an optional result value, along with an error message.
29-
If the optional result is empty, then an error occurred and the error message will be set.
30-
For example:
31-
32-
```
33-
Result<CommentedRant> result = devRant.getRant(832125);
34-
35-
if (!result.getValue().isPresent()) {
36-
System.out.println("An error occurred: " + result.getError());
37-
} else {
38-
CommentedRant rant = result.getValue().get();
39-
System.out.println(rant.getUser().getUsername() + '\n' + rant.getText());
40-
}
41-
```
42-
4327
The `DevRant` class itself can be used to get specific rants and users.
4428

4529
```
4630
// Get a specific rant.
47-
Result<CommentedRant> rant = devRant.getRant(686001);
31+
CommentedRant rant = devRant.getRant(686001);
4832
4933
// Get a user by username.
50-
Result<User> me = devRant.getUser("LucaScorpion");
34+
User me = devRant.getUser("LucaScorpion");
5135
```
5236

5337
The `DevRant` class contains 2 methods for getting to specific parts of the api.
@@ -56,13 +40,13 @@ This is used to access the rant and collab feeds.
5640

5741
```
5842
// Get the 10 latest rants.
59-
Result<List<Rant>> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0);
43+
List<Rant> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0);
6044
6145
// Get the 10 best stories.
62-
Result<List<Rant>> stories = devRant.getFeed().getStories(Sort.TOP, 0);
46+
List<Rant> stories = devRant.getFeed().getStories(Sort.TOP, 0);
6347
6448
// Get 10 collabs.
65-
Result<List<Collab>> collabs = devRant.getFeed().getCollabs(10);
49+
List<Collab> collabs = devRant.getFeed().getCollabs(10);
6650
```
6751

6852
Second, `getAuth()` which returns a `DevRantAuth` object, which is used to access user functionality.

pom.xml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.scorpiac.javarant</groupId>
88
<artifactId>javarant</artifactId>
9-
<version>2.0.0</version>
9+
<version>2.1.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>JavaRant</name>
@@ -55,40 +55,48 @@
5555
</properties>
5656

5757
<dependencies>
58+
<!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
5859
<dependency>
5960
<groupId>com.fasterxml.jackson.core</groupId>
6061
<artifactId>jackson-databind</artifactId>
6162
<version>${jackson.version}</version>
6263
</dependency>
64+
<!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/fluent-hc -->
6365
<dependency>
6466
<groupId>org.apache.httpcomponents</groupId>
6567
<artifactId>fluent-hc</artifactId>
6668
<version>${fluent.version}</version>
6769
</dependency>
68-
<dependency>
69-
<groupId>org.slf4j</groupId>
70-
<artifactId>slf4j-simple</artifactId>
71-
<version>${slf4j.version}</version>
72-
</dependency>
70+
<!-- http://mvnrepository.com/artifact/com.google.inject/guice -->
7371
<dependency>
7472
<groupId>com.google.inject</groupId>
7573
<artifactId>guice</artifactId>
7674
<version>${guice.version}</version>
7775
</dependency>
7876

7977
<!-- Test dependencies -->
78+
79+
<!-- http://mvnrepository.com/artifact/org.testng/testng -->
8080
<dependency>
8181
<groupId>org.testng</groupId>
8282
<artifactId>testng</artifactId>
8383
<version>${testng.version}</version>
8484
<scope>test</scope>
8585
</dependency>
86+
<!-- http://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
8687
<dependency>
8788
<groupId>com.github.tomakehurst</groupId>
8889
<artifactId>wiremock</artifactId>
8990
<version>${wiremock.version}</version>
9091
<scope>test</scope>
9192
</dependency>
93+
<!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
94+
<dependency>
95+
<groupId>org.slf4j</groupId>
96+
<artifactId>slf4j-simple</artifactId>
97+
<version>${slf4j.version}</version>
98+
<scope>test</scope>
99+
</dependency>
92100
</dependencies>
93101

94102
<!-- Distribution management for OSSRH -->
@@ -105,6 +113,7 @@
105113

106114
<build>
107115
<plugins>
116+
<!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
108117
<plugin>
109118
<groupId>org.apache.maven.plugins</groupId>
110119
<artifactId>maven-compiler-plugin</artifactId>
@@ -114,6 +123,7 @@
114123
<target>${java.version}</target>
115124
</configuration>
116125
</plugin>
126+
<!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-failsafe-plugin -->
117127
<plugin>
118128
<groupId>org.apache.maven.plugins</groupId>
119129
<artifactId>maven-failsafe-plugin</artifactId>
@@ -143,6 +153,7 @@
143153
</activation>
144154
<build>
145155
<plugins>
156+
<!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-source-plugin -->
146157
<plugin>
147158
<groupId>org.apache.maven.plugins</groupId>
148159
<artifactId>maven-source-plugin</artifactId>
@@ -156,6 +167,7 @@
156167
</execution>
157168
</executions>
158169
</plugin>
170+
<!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-javadoc-plugin -->
159171
<plugin>
160172
<groupId>org.apache.maven.plugins</groupId>
161173
<artifactId>maven-javadoc-plugin</artifactId>
@@ -172,6 +184,7 @@
172184
<quiet>true</quiet>
173185
</configuration>
174186
</plugin>
187+
<!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-gpg-plugin -->
175188
<plugin>
176189
<groupId>org.apache.maven.plugins</groupId>
177190
<artifactId>maven-gpg-plugin</artifactId>

src/main/java/com/scorpiac/javarant/CommentedRant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.scorpiac.javarant;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
35
import java.util.Collections;
46
import java.util.List;
57

68
public class CommentedRant extends Rant {
9+
@JsonProperty
710
private List<Comment> comments;
811

912
/**

src/main/java/com/scorpiac/javarant/DevRant.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DevRant {
1515
private final DevRantAuth devRantAuth;
1616

1717
private RequestHandler requestHandler;
18-
Auth auth;
18+
private Auth auth;
1919

2020
static {
2121
INJECTOR = Guice.createInjector();
@@ -40,6 +40,14 @@ RequestHandler getRequestHandler() {
4040
return requestHandler;
4141
}
4242

43+
Auth getAuthObject() {
44+
return auth;
45+
}
46+
47+
void setAuthObject(Auth auth) {
48+
this.auth = auth;
49+
}
50+
4351
/**
4452
* Access the devRant feed.
4553
*
@@ -70,8 +78,9 @@ public DevRantAuth getAuth() {
7078
* @param id The id of the rant.
7179
* @return The rant.
7280
*/
73-
public Result<CommentedRant> getRant(int id) {
74-
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CommentedRantResponse.class);
81+
public CommentedRant getRant(int id) {
82+
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CommentedRantResponse.class)
83+
.getValue().orElseThrow(() -> new NoSuchRantException(id));
7584
}
7685

7786
/**
@@ -80,16 +89,11 @@ public Result<CommentedRant> getRant(int id) {
8089
* @param username The username of the user.
8190
* @return The user.
8291
*/
83-
public Result<User> getUser(String username) {
84-
Result<Integer> result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username));
92+
public User getUser(String username) {
93+
Integer result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username))
94+
.getValue().orElseThrow(() -> new NoSuchUsernameException(username));
8595

86-
// Check the result.
87-
if (!result.getValue().isPresent()) {
88-
// When the username is invalid, no error message is returned by the API.
89-
return new Result<>("Invalid username specified.");
90-
}
91-
92-
return getUser(result.getValue().get());
96+
return getUser(result);
9397
}
9498

9599
/**
@@ -98,28 +102,23 @@ public Result<User> getUser(String username) {
98102
* @param id The id of the user.
99103
* @return The user.
100104
*/
101-
public Result<User> getUser(int id) {
102-
Result<User> result = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class);
103-
104-
// Check the result.
105-
if (!result.getValue().isPresent()) {
106-
// When the user id is invalid, no error message is returned by the API.
107-
return new Result<>("Invalid user id specified.");
108-
}
105+
public User getUser(int id) {
106+
User user = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class)
107+
.getValue().orElseThrow(() -> new NoSuchUserIdException(id));
109108

110109
// Set the id, as that is not part of the response.
111-
result.getValue().get().setId(id);
110+
user.setId(id);
112111

113-
return result;
112+
return user;
114113
}
115114

116115
/**
117116
* Get a random rant.
118117
*
119118
* @return A random rant.
120119
*/
121-
public Result<Rant> getSurprise() {
122-
return requestHandler.get(ApiEndpoint.SURPRISE, RantResponse.class);
120+
public Rant getSurprise() {
121+
return requestHandler.get(ApiEndpoint.SURPRISE, RantResponse.class).getValueOrError();
123122
}
124123

125124
/**
@@ -128,8 +127,9 @@ public Result<Rant> getSurprise() {
128127
* @param id The id of the collab.
129128
* @return The collab.
130129
*/
131-
public Result<Collab> getCollab(int id) {
132-
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CollabResponse.class);
130+
public Collab getCollab(int id) {
131+
return requestHandler.get(ApiEndpoint.RANTS.toString() + '/' + id, CollabResponse.class)
132+
.getValue().orElseThrow(() -> new NoSuchRantException(id));
133133
}
134134

135135
/**
@@ -143,17 +143,17 @@ public Result<Collab> getCollab(int id) {
143143
public boolean login(String username, char[] password) {
144144
logout();
145145

146-
Result<Auth> response = requestHandler.post(ApiEndpoint.AUTH_TOKEN, AuthResponse.class,
146+
Auth response = requestHandler.post(ApiEndpoint.AUTH_TOKEN, AuthResponse.class,
147147
new BasicNameValuePair("username", username),
148148
new BasicNameValuePair("password", String.valueOf(password))
149-
);
149+
).getValue().orElseThrow(() -> new DevRantException("Could not log in."));
150150

151151
// Clear the password.
152152
for (int i = 0; i < password.length; i++) {
153153
password[i] = 0;
154154
}
155155

156-
response.getValue().ifPresent(r -> auth = r);
156+
auth = response;
157157
return isLoggedIn();
158158
}
159159

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.scorpiac.javarant;
2+
3+
public class DevRantApiException extends RuntimeException {
4+
public DevRantApiException(String message) {
5+
super("A devRant API exception occurred: " + message);
6+
}
7+
}

src/main/java/com/scorpiac/javarant/DevRantAuth.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public class DevRantAuth {
2121
* @param vote The vote to cast.
2222
* @return The rant.
2323
*/
24-
public Result<Rant> voteRant(int id, Vote vote) {
24+
public Rant voteRant(int id, Vote vote) {
2525
return devRant.getRequestHandler().post(
2626
ApiEndpoint.RANTS.toString() + '/' + id + '/' + ApiEndpoint.VOTE.toString(),
2727
RantResponse.class,
2828
getParameters(vote.getOptions())
29-
);
29+
).getValueOrError();
3030
}
3131

3232
/**
@@ -36,19 +36,19 @@ public Result<Rant> voteRant(int id, Vote vote) {
3636
* @param vote The vote to cast.
3737
* @return The comment.
3838
*/
39-
public Result<Comment> voteComment(int id, Vote vote) {
39+
public Comment voteComment(int id, Vote vote) {
4040
return devRant.getRequestHandler().post(
4141
ApiEndpoint.COMMENTS.toString() + '/' + id + '/' + ApiEndpoint.VOTE.toString(),
4242
CommentResponse.class,
4343
getParameters(vote.getOptions())
44-
);
44+
).getValueOrError();
4545
}
4646

4747
private NameValuePair[] getParameters(List<NameValuePair> params) {
4848
// Add the auth parameters.
49-
params.add(new BasicNameValuePair("token_id", devRant.auth.getId()));
50-
params.add(new BasicNameValuePair("token_key", devRant.auth.getKey()));
51-
params.add(new BasicNameValuePair("user_id", devRant.auth.getUserId()));
49+
params.add(new BasicNameValuePair("token_id", devRant.getAuthObject().getId()));
50+
params.add(new BasicNameValuePair("token_key", devRant.getAuthObject().getKey()));
51+
params.add(new BasicNameValuePair("user_id", devRant.getAuthObject().getUserId()));
5252

5353
return params.toArray(new NameValuePair[0]);
5454
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.scorpiac.javarant;
2+
3+
public class DevRantException extends RuntimeException {
4+
public DevRantException(String message) {
5+
super(message);
6+
}
7+
8+
public DevRantException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
}

0 commit comments

Comments
 (0)