Skip to content
This repository has been archived by the owner on Jun 3, 2019. It is now read-only.

Commit

Permalink
All Service Tests & Coverage added + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nscharrenberg committed Mar 16, 2019
1 parent f01a345 commit e5da16d
Show file tree
Hide file tree
Showing 6 changed files with 938 additions and 243 deletions.
Binary file added docs/images/testcoverage.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
<version>2.3</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/domain/Tweet.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public User getAuthor() {

public void setAuthor(User author) {
this.author = author;
if(author != null) {
author.addTweet(this);
}
}

public Date getCreatedAt() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public void setTweets(Set<Tweet> tweets) {

public void addTweet(Tweet tweet) {
this.tweets.add(tweet);
tweet.setAuthor(this);
}

public void removeTweet(Tweet tweet) {
Expand All @@ -195,7 +194,6 @@ private void addFollower(User user) {

public void removeFollowing(User user) {
this.following.remove(user);
user.removeFollower(this);
}

private void removeFollower(User user) {
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/service/TweetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public ObjectResponse<List<Tweet>> getByAuthorId(int id) {
return new ObjectResponse<>(getUserByIdResponse.getCode(), getUserByIdResponse.getMessage());
}

ObjectResponse<List<Tweet>> getTweetsByAuthorIdResponse = getByAuthorId(id);
List<Tweet> getTweetsByAuthorIdResponse = tr.getByAuthorId(id);

if(getTweetsByAuthorIdResponse.getObject() == null || getTweetsByAuthorIdResponse.getObject().isEmpty()) {
return getTweetsByAuthorIdResponse;
if(getTweetsByAuthorIdResponse == null) {
return new ObjectResponse<>(HttpStatusCodes.INTERNAL_SERVER_ERROR, "List of tweet from author " + getUserByIdResponse.getObject().getUsername() + " is never instantiated.");
}

return new ObjectResponse<>(HttpStatusCodes.OK, getTweetsByAuthorIdResponse.getObject().size() + " tweets from " + getUserByIdResponse.getObject().getUsername() + " loaded", getTweetsByAuthorIdResponse.getObject());
return new ObjectResponse<>(HttpStatusCodes.OK, getTweetsByAuthorIdResponse.size() + " tweets from " + getUserByIdResponse.getObject().getUsername() + " loaded", getTweetsByAuthorIdResponse);
}

/**
Expand Down Expand Up @@ -136,8 +136,14 @@ public ObjectResponse<Tweet> create(Tweet tweet) {
return new ObjectResponse<>(HttpStatusCodes.NOT_ACCEPTABLE, "Tweet must have a message");
}

if(tweet.getAuthor() == null || ur.getById(tweet.getAuthor().getId()) == null) {
return new ObjectResponse<>(HttpStatusCodes.NOT_FOUND, "Author not found");
if(tweet.getAuthor() == null) {
return new ObjectResponse<>(HttpStatusCodes.NOT_ACCEPTABLE, "Tweet must have an author");
}

ObjectResponse<User> getUserById = ur.getById(tweet.getAuthor().getId());

if(getUserById.getObject() == null) {
return new ObjectResponse<>(getUserById.getCode(), getUserById.getMessage());
}

if(tweet.getMessage().length() > 140) {
Expand All @@ -155,7 +161,7 @@ public ObjectResponse<Tweet> create(Tweet tweet) {
Tweet created = tr.create(tweet);

if(created != null) {
return new ObjectResponse<>(HttpStatusCodes.OK, "Tweet with message: " + tweet.getMessage() + " created", tweet);
return new ObjectResponse<>(HttpStatusCodes.CREATED, "Tweet with message: " + tweet.getMessage() + " created", tweet);
} else {
return new ObjectResponse<>(HttpStatusCodes.INTERNAL_SERVER_ERROR, "Could not create a new tweet due to an unknown error");
}
Expand Down Expand Up @@ -185,6 +191,14 @@ public ObjectResponse<Tweet> update(Tweet tweet) {
return getByIdResponse;
}

if(tweet.getAuthor() == null) {
return new ObjectResponse<>(HttpStatusCodes.NOT_ACCEPTABLE, "Author can not be null, must meet original author with id " + getByIdResponse.getObject().getAuthor().getId());
}

if(tweet.getAuthor().getId() != getByIdResponse.getObject().getAuthor().getId()) {
return new ObjectResponse<>(HttpStatusCodes.NOT_ACCEPTABLE, "Author can not be changed!");
}

ObjectResponse<Set<User>> getMentionsByMessageResponse = getMentionsByMessage(tweet.getMessage());

if(getMentionsByMessageResponse.getObject() == null) {
Expand Down Expand Up @@ -245,7 +259,7 @@ public ObjectResponse<Tweet> like(Tweet tweet, User user) {
return new ObjectResponse<>(getUserByIdResponse.getCode(), getUserByIdResponse.getMessage());
}

ObjectResponse<Tweet> getTweetByIdResponse = getById(user.getId());
ObjectResponse<Tweet> getTweetByIdResponse = getById(tweet.getId());

if(getTweetByIdResponse.getObject() == null) {
return getTweetByIdResponse;
Expand All @@ -268,11 +282,8 @@ public ObjectResponse<Tweet> like(Tweet tweet, User user) {
* @param tweet - the tweet
* @param user - the user that unlikes the tweet
* @return the tweet
* @throws InvalidContentException
* @throws ActionForbiddenException
* @throws NotFoundException
*/
public ObjectResponse<Tweet> unlike(Tweet tweet, User user) throws ActionForbiddenException, InvalidContentException, NotFoundException {
public ObjectResponse<Tweet> unlike(Tweet tweet, User user) {
if(user.getId() <= 0) {
return new ObjectResponse<>(HttpStatusCodes.NOT_ACCEPTABLE, "Invalid User ID");
}
Expand All @@ -287,7 +298,7 @@ public ObjectResponse<Tweet> unlike(Tweet tweet, User user) throws ActionForbidd
return new ObjectResponse<>(getUserByIdResponse.getCode(), getUserByIdResponse.getMessage());
}

ObjectResponse<Tweet> getTweetByIdResponse = getById(user.getId());
ObjectResponse<Tweet> getTweetByIdResponse = getById(tweet.getId());

if(getTweetByIdResponse.getObject() == null) {
return getTweetByIdResponse;
Expand Down
Loading

0 comments on commit e5da16d

Please sign in to comment.