Skip to content

Commit 89ad623

Browse files
author
jetsaii
committed
Add unit tests
1 parent 4ae0036 commit 89ad623

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/main/java/leetcode/api/StatsResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,13 @@ public int getContributionPoints() {
8686
public int getReputation() {
8787
return reputation;
8888
}
89+
90+
public boolean equals(StatsResponse s) {
91+
// Compared with itself
92+
if (s == this) {
93+
return true;
94+
}
95+
96+
return status.equals(s.getStatus()) && message.equals(s.getMessage()) && totalSolved == s.getTotalSolved() && totalQuestions == s.getTotalQuestions() && easySolved == s.getEasySolved() && totalEasy == s.getTotalEasy() && mediumSolved == s.getMediumSolved() && totalMedium == s.getTotalMedium() && hardSolved == s.getHardSolved() && totalHard == s.getTotalHard() && acceptanceRate == s.getAcceptanceRate() && ranking == s.getRanking() && contributionPoints == s.getContributionPoints() && reputation == s.getReputation();
97+
}
8998
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package leetcode.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
@SpringBootTest
9+
public class StatsResponseTests {
10+
StatsResponse s = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
11+
12+
@Test
13+
void statusCorrect() {
14+
assertEquals("success", s.getStatus());
15+
}
16+
17+
@Test
18+
void messageCorrect() {
19+
assertEquals("retrieved", s.getMessage());
20+
}
21+
22+
@Test
23+
void totalSolvedCorrect() {
24+
assertEquals(1, s.getTotalSolved());
25+
}
26+
27+
@Test
28+
void totalQuestionsCorrect() {
29+
assertEquals(2, s.getTotalQuestions());
30+
}
31+
32+
@Test
33+
void easySolvedCorrect() {
34+
assertEquals(3, s.getEasySolved());
35+
}
36+
37+
@Test
38+
void totalEasyCorrect() {
39+
assertEquals(4, s.getTotalEasy());
40+
}
41+
42+
@Test
43+
void mediumSolvedCorrect() {
44+
assertEquals(5, s.getMediumSolved());
45+
}
46+
47+
@Test
48+
void totalMediumCorrect() {
49+
assertEquals(6, s.getTotalMedium());
50+
}
51+
52+
@Test
53+
void hardSolvedCorrect() {
54+
assertEquals(7, s.getHardSolved());
55+
}
56+
57+
@Test
58+
void totalHardCorrect() {
59+
assertEquals(8, s.getTotalHard());
60+
}
61+
62+
@Test
63+
void acceptanceCorrect() {
64+
assertEquals((float) 99.99, s.getAcceptanceRate());
65+
}
66+
67+
@Test
68+
void rankingCorrect() {
69+
assertEquals(10, s.getRanking());
70+
}
71+
72+
@Test
73+
void contributionPointsCorrect() {
74+
assertEquals(11, s.getContributionPoints());
75+
}
76+
77+
@Test
78+
void reputationCorrect() {
79+
assertEquals(12, s.getReputation());
80+
}
81+
82+
@Test
83+
void sameRefEqualCorrect() {
84+
assertEquals(s, s);
85+
}
86+
87+
@Test
88+
void sameValEqualCorrect() {
89+
StatsResponse copy = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
90+
assertTrue(s.equals(copy));
91+
}
92+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
package leetcode.api;
22

3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.mockito.Mockito.when;
6+
import org.springframework.test.web.servlet.MvcResult;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11+
import org.springframework.boot.test.mock.mockito.MockBean;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
14+
@WebMvcTest(UserController.class)
315
public class UserControllerTests {
16+
@Autowired
17+
private MockMvc mockMvc;
18+
19+
@MockBean
20+
private StatsService service;
21+
22+
23+
@Test
24+
void validUsername() throws Exception {
25+
StatsResponse mockResponse = new StatsResponse("success", "retrieved", 1, 2,3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
26+
when(service.getStats("user_exists")).thenReturn(mockResponse);
27+
28+
MvcResult result = mockMvc.perform(get("/user_exists")).andExpect(status().isOk()).andReturn();
29+
String resultStr = result.getResponse().getContentAsString();
30+
String expected = "{\"status\":\"success\",\"message\":\"retrieved\",\"totalSolved\":1,\"totalQuestions\":2,\"easySolved\":3,\"totalEasy\":4,\"mediumSolved\":5,\"totalMedium\":6,\"hardSolved\":7,\"totalHard\":8,\"acceptanceRate\":99.99,\"ranking\":10,\"contributionPoints\":11,\"reputation\":12}";
31+
assertEquals(resultStr, expected);
32+
}
33+
34+
@Test
35+
void noUsername() throws Exception {
36+
MvcResult result = mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn();
37+
String resultStr = result.getResponse().getContentAsString();
38+
String expected = "{\"status\":\"error\",\"message\":\"please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}";
39+
assertEquals(resultStr, expected);
40+
}
41+
42+
@Test
43+
void nonValidUsername() throws Exception {
44+
StatsResponse mockResponse = new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
45+
when(service.getStats("user_does_not_exist")).thenReturn(mockResponse);
46+
47+
MvcResult result = mockMvc.perform(get("/user_does_not_exist")).andExpect(status().isOk()).andReturn();
48+
String resultStr = result.getResponse().getContentAsString();
49+
String expected = "{\"status\":\"error\",\"message\":\"user does not exist\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}";
50+
assertEquals(resultStr, expected);
51+
}
452
}

0 commit comments

Comments
 (0)