Browse Source

nhu fixed the vote test :)

Trinh Tong 6 years ago
parent
commit
df8c220377

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java View File

@@ -23,6 +23,8 @@ public class VoteController {
23 23
     public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
24 24
             vote) {
25 25
         vote = voteRepository.save(vote);
26
+
27
+        System.out.println("votes is " + vote);
26 28
         // Set the headers for the newly created resource
27 29
         HttpHeaders responseHeaders = new HttpHeaders();
28 30
         responseHeaders.setLocation(ServletUriComponentsBuilder.

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java View File

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3 3
 import javax.persistence.*;
4
+import java.util.Objects;
4 5
 
5 6
 @Entity
6 7
 public class Option {
@@ -32,4 +33,18 @@ public class Option {
32 33
         return "{\"id\":" + id
33 34
                 + ",\"value\":\"" + value + "\"}";
34 35
     }
36
+
37
+    @Override
38
+    public boolean equals(Object o) {
39
+        if (this == o) return true;
40
+        if (!(o instanceof Option)) return false;
41
+        Option option = (Option) o;
42
+        return Objects.equals(getId(), option.getId()) &&
43
+                Objects.equals(getValue(), option.getValue());
44
+    }
45
+
46
+    @Override
47
+    public int hashCode() {
48
+        return Objects.hash(getId(), getValue());
49
+    }
35 50
 }

+ 16
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

@@ -4,6 +4,7 @@ import org.hibernate.validator.constraints.NotEmpty;
4 4
 
5 5
 import javax.persistence.*;
6 6
 import javax.validation.constraints.Size;
7
+import java.util.Objects;
7 8
 import java.util.Set;
8 9
 
9 10
 @Entity
@@ -55,4 +56,19 @@ public class Poll {
55 56
                 + ",\"question\":\"" + question + "\""
56 57
                 + ",\"options\":" + options + "}";
57 58
     }
59
+
60
+    @Override
61
+    public boolean equals(Object o) {
62
+        if (this == o) return true;
63
+        if (!(o instanceof Poll)) return false;
64
+        Poll poll = (Poll) o;
65
+        return Objects.equals(getId(), poll.getId()) &&
66
+                Objects.equals(getQuestion(), poll.getQuestion()) &&
67
+                Objects.equals(getOptions(), poll.getOptions());
68
+    }
69
+
70
+    @Override
71
+    public int hashCode() {
72
+        return Objects.hash(getId(), getQuestion(), getOptions());
73
+    }
58 74
 }

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java View File

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3 3
 import javax.persistence.*;
4
+import java.util.Objects;
4 5
 
5 6
 @Entity
6 7
 public class Vote {
@@ -34,4 +35,18 @@ public class Vote {
34 35
         return "{\"id\":" + id
35 36
                 + ",\"option\":" + option + "}";
36 37
     }
38
+
39
+    @Override
40
+    public boolean equals(Object o) {
41
+        if (this == o) return true;
42
+        if (!(o instanceof Vote)) return false;
43
+        Vote vote = (Vote) o;
44
+        return Objects.equals(getId(), vote.getId()) &&
45
+                Objects.equals(getOption(), vote.getOption());
46
+    }
47
+
48
+    @Override
49
+    public int hashCode() {
50
+        return Objects.hash(getId(), getOption());
51
+    }
37 52
 }

+ 2
- 3
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java View File

@@ -155,13 +155,12 @@ public class PollControllerTest {
155 155
     public void testCreatePollSuccess() throws Exception {
156 156
         given(pollRepository.save(testPoll2)).willReturn(testPoll2);
157 157
 
158
-        MockHttpServletResponse response = mvc.perform(post("/polls/")
158
+        mvc.perform(post("/polls/")
159 159
                 .content(asJsonString(testPoll2))
160 160
                 .contentType(MediaType.APPLICATION_JSON)
161 161
                 .accept(MediaType.APPLICATION_JSON))
162
-                .andReturn().getResponse();
162
+                .andExpect(status().isCreated());
163 163
 
164
-        Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
165 164
     }
166 165
 
167 166
     // =================== Update Poll =================== //

+ 10
- 2
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java View File

@@ -75,6 +75,8 @@ public class VoteControllerTest {
75 75
 
76 76
         testPoll = new Poll();
77 77
         testPoll.setId(TEST_ID);
78
+
79
+        testPoll.setQuestion("Do you like cheese?");
78 80
         options1 = new HashSet<>();
79 81
 
80 82
         option1 = new Option();
@@ -126,7 +128,7 @@ public class VoteControllerTest {
126 128
                 .willThrow(new ResourceNotFoundException());
127 129
 
128 130
         MockHttpServletResponse response = mvc.perform(
129
-                get("polls/votesBADBAD")
131
+                get("/polls/votesBADBAD")
130 132
                     .accept(MediaType.APPLICATION_JSON))
131 133
                     .andReturn().getResponse();
132 134
 
@@ -138,11 +140,17 @@ public class VoteControllerTest {
138 140
     public void testCreateVoteSuccess() throws Exception {
139 141
         Vote testVote2 = new Vote();
140 142
         testVote2.setId(TEST_ID);
143
+        testVote2.setOption(option1);
144
+
145
+        /* Fixed by adding an equals method and hashcode to each  object.
146
+         * during mocking the 'save' method is checking the absolute equality
147
+          * and since it's technically a different object, we have to override the equals method
148
+          * to properly compare the objects. */
141 149
 
142 150
         given(pollRepository.findOne(TEST_ID)).willReturn(testPoll);
143 151
         given(voteRepository.save(testVote2)).willReturn(testVote2);
144 152
 
145
-        mvc.perform(post("/polls/1/votes/1")
153
+        mvc.perform(post("/polls/1/votes")
146 154
                 .content(asJsonString(testVote2))
147 155
                 .contentType(MediaType.APPLICATION_JSON)
148 156
                 .accept(MediaType.APPLICATION_JSON))