Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. class VoteServiceTest {
  2.  
  3.     private VoteService voteService;
  4.     private ArticleVote updatedVote;
  5.     private ArticleVote vote;
  6.     private Article article;
  7.     private User user;
  8.  
  9.     @BeforeEach
  10.     void setUp() {
  11.         initObjects();
  12.         voteService = new VoteService();
  13.     }
  14.  
  15.     @Test
  16.     void addVoteUp() {
  17.         vote.setVoteType(VoteType.VOTE_UP.toString());
  18.         Assert.assertEquals(1, voteService.addVote(article, vote).getVotes());
  19.     }
  20.  
  21.     @Test
  22.     void addVoteDown() {
  23.         vote.setVoteType(VoteType.VOTE_DOWN.toString());
  24.         Assert.assertEquals(-1, voteService.addVote(article, vote).getVotes());
  25.     }
  26.  
  27.     @Test
  28.     void updateVoteFromUpToDown() {
  29.         vote.setVoteType(VoteType.VOTE_UP.toString());
  30.         article.setVotes(1);
  31.         user.getArticleVotes().add(vote);
  32.         updatedVote.setVoteType(VoteType.VOTE_DOWN.toString());
  33.  
  34.         Assert.assertEquals(-1, voteService.updateVote(article, updatedVote, vote).getVotes());
  35.     }
  36.  
  37.     @Test
  38.     void updateVoteFromDownToUp() {
  39.         vote.setVoteType(VoteType.VOTE_DOWN.toString());
  40.         article.setVotes(-1);
  41.         user.getArticleVotes().add(vote);
  42.         updatedVote.setVoteType(VoteType.VOTE_UP.toString());
  43.  
  44.         Assert.assertEquals(1, voteService.updateVote(article, updatedVote, vote).getVotes());
  45.     }
  46.  
  47.     @Test
  48.     void updateVoteFromUpToUp() {
  49.         vote.setVoteType(VoteType.VOTE_UP.toString());
  50.         article.setVotes(1);
  51.         user.getArticleVotes().add(vote);
  52.         updatedVote.setVoteType(VoteType.VOTE_UP.toString());
  53.  
  54.         Assert.assertEquals(1, voteService.updateVote(article, updatedVote, vote).getVotes());
  55.     }
  56.  
  57.     private void initObjects() {
  58.         user = new User();
  59.         user.setUsername("user");
  60.         user.setPassword("password");
  61.  
  62.         article = new Article();
  63.         article.setId(1L);
  64.         article.setTitle("Testing");
  65.         article.setVotes(0);
  66.         article.setUser(user);
  67.         user.getAddedArticles().add(article);
  68.  
  69.         vote = new ArticleVote();
  70.         vote.setUser(user);
  71.         vote.setArticle(article);
  72.  
  73.         updatedVote = new ArticleVote();
  74.         updatedVote.setArticle(article);
  75.         updatedVote.setUser(user);
  76.  
  77.     }
  78. }
  79.  
  80. @Service
  81. public class VoteService {
  82.  
  83.     public Activity addVote(Activity activity, Vote vote) {
  84.         if (vote.getVoteType().equals(VoteType.VOTE_UP.toString())) {
  85.             activity.setVotes(activity.getVotes() + 1);
  86.         } else {
  87.             activity.setVotes(activity.getVotes() - 1);
  88.         }
  89.         return activity;
  90.     }
  91.  
  92.     public Activity updateVote(Activity activity, Vote vote, Vote previousVote) {
  93.         if (previousVote.getVoteType().equals(vote.getVoteType())) {
  94.             return activity;
  95.         } else {
  96.             previousVote.setVoteType(vote.getVoteType());
  97.  
  98.             if (vote.getVoteType().equals(VoteType.VOTE_UP.toString())) {
  99.                 activity.setVotes(activity.getVotes() + 2);
  100.             } else {
  101.                 activity.setVotes(activity.getVotes() - 2);
  102.             }
  103.             return activity;
  104.         }
  105.  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement