-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: controller for handling overview information and respective se…
…rvice. Pull out into abstraction mutual error handling, from services, and then in the controllers as well. Created necessary tests, and fixed some broken ones.
- Loading branch information
1 parent
239af7f
commit 8b152a0
Showing
12 changed files
with
200 additions
and
39 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/com/minitwitter/controller/ExceptionHandlingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.minitwitter.controller; | ||
|
||
import com.minitwitter.domain.dto.ErrorMessage; | ||
import com.minitwitter.service.ExceptionHandlingService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
@RestController | ||
@Slf4j | ||
public abstract class ExceptionHandlingController { | ||
|
||
@ExceptionHandler | ||
@ResponseStatus(BAD_REQUEST) | ||
public ErrorMessage handleUnknownUsernameException(ExceptionHandlingService.UnknownUsernameException e) { | ||
log.warn("", e); | ||
return new ErrorMessage(String.format("Unknown user '%s'", e.getUsername())); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/minitwitter/controller/OverviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.minitwitter.controller; | ||
|
||
import com.minitwitter.domain.dto.UserOverviewDTO; | ||
import com.minitwitter.service.OverviewService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("overview") | ||
@Slf4j | ||
public class OverviewController extends ExceptionHandlingController{ | ||
|
||
private OverviewService overviewService; | ||
|
||
public OverviewController(OverviewService overviewService) { | ||
this.overviewService = overviewService; | ||
} | ||
|
||
@GetMapping(value = "{username}") | ||
public UserOverviewDTO followers(@PathVariable String username) { | ||
return overviewService.getUsersFollowers(username); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/minitwitter/domain/dto/UserOverviewDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.minitwitter.domain.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = PRIVATE) | ||
public class UserOverviewDTO { | ||
private int tweets; | ||
private int followers; | ||
private int following; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/minitwitter/service/ExceptionHandlingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.minitwitter.service; | ||
|
||
import com.minitwitter.domain.User; | ||
import com.minitwitter.repository.UserRepository; | ||
import lombok.Getter; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public abstract class ExceptionHandlingService { | ||
|
||
private UserRepository userRepository; | ||
|
||
public ExceptionHandlingService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
protected User getUser(String username) { | ||
User user = userRepository.findOneByUsername(username); | ||
if (user != null) { | ||
return user; | ||
} | ||
throw new UnknownUsernameException(username); | ||
} | ||
|
||
public static class UnknownUsernameException extends RuntimeException { | ||
@Getter | ||
private String username; | ||
|
||
private UnknownUsernameException(String username) { | ||
super(username); | ||
this.username = username; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/minitwitter/service/OverviewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.minitwitter.service; | ||
|
||
import com.minitwitter.domain.User; | ||
import com.minitwitter.domain.dto.UserOverviewDTO; | ||
import com.minitwitter.repository.TweetRepository; | ||
import com.minitwitter.repository.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
@Service | ||
public class OverviewService extends ExceptionHandlingService { | ||
|
||
private TweetRepository tweetRepository; | ||
|
||
public OverviewService(TweetRepository tweetRepository, UserRepository userRepository) { | ||
super(userRepository); | ||
this.tweetRepository = tweetRepository; | ||
} | ||
|
||
@Transactional | ||
public UserOverviewDTO getUsersFollowers(String username) { | ||
int tweets = this.tweetRepository.countByAuthorUsername(username); | ||
|
||
User user = this.getUser(username); | ||
int followers = user.getFollowers().size(); | ||
int following = user.getFollowing().size(); | ||
|
||
return new UserOverviewDTO(tweets, followers, following); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/com/minitwitter/controller/OverviewControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.minitwitter.controller; | ||
|
||
import com.minitwitter.domain.dto.ErrorMessage; | ||
import com.minitwitter.domain.dto.UserOverviewDTO; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class OverviewControllerIntegrationTest extends RestIntegrationTest { | ||
|
||
@Test | ||
public void overviewOfUser_onlyThatUserTweetsAreReturned() { | ||
ResponseEntity<UserOverviewDTO> response = withAuthTestRestTemplate().getForEntity("/overview/{username}", | ||
UserOverviewDTO.class, Collections.singletonMap("username", getUsernameOfAuthUser())); | ||
assertThat(response.getStatusCode().is2xxSuccessful(), is(true)); | ||
UserOverviewDTO overview = response.getBody(); | ||
assertThat(overview.getTweets(), equalTo(ownTweetsCount())); | ||
assertThat(overview.getFollowers(), equalTo(followersCount())); | ||
assertThat(overview.getFollowing(), equalTo(followingUsers().length)); | ||
} | ||
|
||
@Test | ||
public void overviewOfUserWithWrongUsername_badRequestReturned() { | ||
ResponseEntity<ErrorMessage> response = withAuthTestRestTemplate().getForEntity("/overview/{username}", | ||
ErrorMessage.class, Collections.singletonMap("username", "unknown")); | ||
assertThat(response.getStatusCode(), is(HttpStatus.BAD_REQUEST)); | ||
assertThat(response.getBody().getMessage(), containsString("unknown")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters