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

Commit 6e4b62b

Browse files
committed
Separate exceptions for username and id, update changelog and readme
1 parent 6e8eb8e commit 6e4b62b

9 files changed

Lines changed: 53 additions & 17 deletions

File tree

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.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ 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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public CommentedRant getRant(int id) {
9191
*/
9292
public User getUser(String username) {
9393
Integer result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username))
94-
.getValue().orElseThrow(() -> new NoSuchUserException(username));
94+
.getValue().orElseThrow(() -> new NoSuchUsernameException(username));
9595

9696
return getUser(result);
9797
}
@@ -104,7 +104,7 @@ public User getUser(String username) {
104104
*/
105105
public User getUser(int id) {
106106
User user = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class)
107-
.getValue().orElseThrow(() -> new NoSuchUserException(id));
107+
.getValue().orElseThrow(() -> new NoSuchUserIdException(id));
108108

109109
// Set the id, as that is not part of the response.
110110
user.setId(id);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.scorpiac.javarant;
22

33
public class NoSuchRantException extends RuntimeException {
4+
private final int id;
5+
46
public NoSuchRantException(int id) {
57
super("A rant with id " + id + " does not exist.");
8+
this.id = id;
9+
}
10+
11+
public int getId() {
12+
return id;
613
}
714
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.scorpiac.javarant;
22

3-
public class NoSuchUserException extends RuntimeException {
4-
public NoSuchUserException(String username) {
5-
super("The user '" + username + "' does not exist.");
6-
}
7-
8-
public NoSuchUserException(int id) {
9-
super("A user with id " + id + " does not exist.");
3+
public abstract class NoSuchUserException extends RuntimeException {
4+
public NoSuchUserException(String message) {
5+
super(message);
106
}
117
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.scorpiac.javarant;
2+
3+
public class NoSuchUserIdException extends NoSuchUserException {
4+
private final int id;
5+
6+
public NoSuchUserIdException(int id) {
7+
super("A user with id " + id + " does not exist.");
8+
this.id = id;
9+
}
10+
11+
public int getId() {
12+
return id;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.scorpiac.javarant;
2+
3+
public class NoSuchUsernameException extends NoSuchUserException {
4+
private final String username;
5+
6+
public NoSuchUsernameException(String username) {
7+
super("The user '" + username + "' does not exist.");
8+
this.username = username;
9+
}
10+
11+
public String getUsername() {
12+
return username;
13+
}
14+
}

src/test/java/com/scorpiac/javarant/DevRantIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testGetUserByUsername() throws IOException {
8181
);
8282
}
8383

84-
@Test(expectedExceptions = NoSuchUserException.class, expectedExceptionsMessageRegExp = ".*'not-a-name'.*")
84+
@Test(expectedExceptions = NoSuchUsernameException.class, expectedExceptionsMessageRegExp = ".*'not-a-name'.*")
8585
public void testGetUserByUsernameInvalid() throws IOException {
8686
server.stubFor(stubGet(
8787
get(urlPathEqualTo("/api/get-user-id"))
@@ -92,7 +92,7 @@ public void testGetUserByUsernameInvalid() throws IOException {
9292
devRant.getUser("not-a-name");
9393
}
9494

95-
@Test(expectedExceptions = NoSuchUserException.class, expectedExceptionsMessageRegExp = ".*123.*")
95+
@Test(expectedExceptions = NoSuchUserIdException.class, expectedExceptionsMessageRegExp = ".*123.*")
9696
public void testGetUserInvalid() throws IOException {
9797
server.stubFor(stubGet(
9898
get(urlPathEqualTo("/api/users/123")),

src/test/java/com/scorpiac/javarant/DevRantSmokeTestIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public void testGetUserByUsername() {
4242
assertTrue(user.getRantsCount() > 0);
4343
}
4444

45-
@Test(expectedExceptions = NoSuchUserException.class, expectedExceptionsMessageRegExp = ".*'This-is-a-non-existing-username.*'.*")
45+
@Test(expectedExceptions = NoSuchUsernameException.class, expectedExceptionsMessageRegExp = ".*'This-is-a-non-existing-username.*'.*")
4646
public void testGetUserByUsernameInvalid() {
4747
// Add some randomness in case someone wants to fuck this test by registering this username.
4848
devRant.getUser("This-is-a-non-existing-username-" + UUID.randomUUID().toString());
4949
}
5050

51-
@Test(expectedExceptions = NoSuchUserException.class, expectedExceptionsMessageRegExp = ".*123.*")
51+
@Test(expectedExceptions = NoSuchUserIdException.class, expectedExceptionsMessageRegExp = ".*123.*")
5252
public void testGetUserInvalid() {
5353
devRant.getUser(123);
5454
}

0 commit comments

Comments
 (0)