浏览代码

Refmt, begin e2e testing

vvmk 8 年前
父节点
当前提交
ce0761d4e2

+ 7
- 5
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/ComputeResultController.java 查看文件

@@ -1,9 +1,9 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controllers;
2 2
 
3
-import io.zipcoder.tc_spring_poll_application.domain.Option;
4 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5 4
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
6 5
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import io.zipcoder.tc_spring_poll_application.utils.ResultCalculator;
7 7
 import org.springframework.beans.factory.annotation.Autowired;
8 8
 import org.springframework.http.HttpStatus;
9 9
 import org.springframework.http.ResponseEntity;
@@ -38,12 +38,14 @@ public class ComputeResultController {
38 38
         return new ResponseEntity<>(voteResult, HttpStatus.OK);
39 39
     }
40 40
 
41
-    protected VoteResult computeResults(Iterable<Vote> votes) {
41
+    private VoteResult computeResults(Iterable<Vote> votes) {
42 42
         Iterator<Vote> voterator = votes.iterator();
43
-        VoteResult result = new VoteResult();
44
-        while(voterator.hasNext()) {
43
+
44
+        ResultCalculator rc = new ResultCalculator();
45
+        while (voterator.hasNext()) {
45 46
             Vote v = voterator.next();
47
+            rc.add(v.getOption());
46 48
         }
47
-        return result;
49
+        return rc.calculate();
48 50
     }
49 51
 }

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/VoteController.java 查看文件

@@ -52,12 +52,12 @@ public class VoteController {
52 52
         return new ResponseEntity<>(vote, responseHeaders, HttpStatus.CREATED);
53 53
     }
54 54
 
55
-    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
55
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.GET)
56 56
     public ResponseEntity<Iterable<Vote>> getVotes() {
57 57
         return new ResponseEntity<>(voteRepository.findAll(), HttpStatus.OK);
58 58
     }
59 59
 
60
-    @RequestMapping(value="/polls/{pollId}/votes/", method=RequestMethod.GET)
60
+    @RequestMapping(value = "/polls/{pollId}/votes/", method = RequestMethod.GET)
61 61
     public ResponseEntity<Iterable<Vote>> getVotesForPoll(@PathVariable Long pollId) {
62 62
         return new ResponseEntity<>(voteRepository.findVotesByPoll(pollId), HttpStatus.OK);
63 63
     }

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java 查看文件

@@ -16,10 +16,10 @@ public class Option {
16 16
 
17 17
     @Id
18 18
     @GeneratedValue
19
-    @Column(name="OPTION_ID")
19
+    @Column(name = "OPTION_ID")
20 20
     private Long Id;
21 21
 
22
-    @Column(name="OPTION_VALUE")
22
+    @Column(name = "OPTION_VALUE")
23 23
     private String value;
24 24
 
25 25
     public Option() {

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java 查看文件

@@ -12,6 +12,11 @@ public class VoteResult {
12 12
     private int totalVotes;
13 13
     private Collection<OptionCount> results;
14 14
 
15
+    public VoteResult(int totalVotes, Collection<OptionCount> results) {
16
+        this.totalVotes = totalVotes;
17
+        this.results = results;
18
+    }
19
+
15 20
     public int getTotalVotes() {
16 21
         return totalVotes;
17 22
     }

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java 查看文件

@@ -9,5 +9,5 @@ import org.springframework.data.repository.CrudRepository;
9 9
  * author: https://github.com/vvmk
10 10
  * date: 4/5/18
11 11
  */
12
-public interface OptionRepository extends CrudRepository<Option, Long>{
12
+public interface OptionRepository extends CrudRepository<Option, Long> {
13 13
 }

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/utils/ResultCalculator.java 查看文件

@@ -2,6 +2,7 @@ package io.zipcoder.tc_spring_poll_application.utils;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Option;
4 4
 import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
5
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5 6
 
6 7
 import java.util.AbstractCollection;
7 8
 import java.util.HashMap;
@@ -36,6 +37,10 @@ public class ResultCalculator extends AbstractCollection<OptionCount> {
36 37
         optionCounts.put(oId, workingCount);
37 38
     }
38 39
 
40
+    public VoteResult calculate() {
41
+        return new VoteResult(size(), optionCounts.values());
42
+    }
43
+
39 44
     public OptionCount getOptionCountById(Long id) {
40 45
         return optionCounts.get(id);
41 46
     }

+ 3
- 5
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/VoteControllerTest.java 查看文件

@@ -14,9 +14,7 @@ import java.util.List;
14 14
 import static junit.framework.TestCase.assertEquals;
15 15
 import static org.mockito.ArgumentMatchers.any;
16 16
 import static org.mockito.ArgumentMatchers.anyLong;
17
-import static org.mockito.Mockito.verify;
18
-import static org.mockito.Mockito.when;
19
-import static org.mockito.Mockito.mock;
17
+import static org.mockito.Mockito.*;
20 18
 
21 19
 /**
22 20
  * project: spring-demo
@@ -38,8 +36,8 @@ public class VoteControllerTest {
38 36
 
39 37
         //create vote
40 38
         when(voteRepo.save(any(Vote.class))).thenAnswer(inv -> inv.getArgument(0));
41
-        when(voteRepo.findAll()).thenReturn((Iterable<Vote>)mock(List.class));
42
-        when(voteRepo.findVotesByPoll(anyLong())).thenReturn((Iterable<Vote>)mock(List.class));
39
+        when(voteRepo.findAll()).thenReturn((Iterable<Vote>) mock(List.class));
40
+        when(voteRepo.findVotesByPoll(anyLong())).thenReturn((Iterable<Vote>) mock(List.class));
43 41
     }
44 42
 
45 43
     @Test