-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitQuickControllerTest.java
More file actions
68 lines (52 loc) · 2.69 KB
/
CommitQuickControllerTest.java
File metadata and controls
68 lines (52 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pl.commit.craft.quick;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(CommitQuickController.class)
class CommitQuickControllerTest {
@Autowired
private MockMvc mockMvc;
@MockitoBean
private CommitQuickService commitQuickService;
private ObjectMapper objectMapper;
@BeforeEach
void setUp() {
objectMapper = new ObjectMapper();
}
@Test
void testGenerateCommitSuccess() throws Exception {
String topicScope = "fix";
boolean isGitCommand = true;
CommitQuickRequest commitQuickRequest = new CommitQuickRequest(topicScope, null, isGitCommand);
String expectedCommitMessage = "git commit -m \"fix: fixed a bug\"";
when(commitQuickService.generateQuickCommit(topicScope, null, isGitCommand)).thenReturn(expectedCommitMessage);
mockMvc.perform(post("/api/v1/commit-quick/craft")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(commitQuickRequest)))
.andExpect(status().isOk())
.andExpect(content().string(expectedCommitMessage));
verify(commitQuickService, times(1)).generateQuickCommit(topicScope, null, isGitCommand);
}
@Test
void testGenerateCommitFailure() throws Exception {
String topicScope = "invalidTopic";
boolean isGitCommand = false;
CommitQuickRequest commitQuickRequest = new CommitQuickRequest(topicScope, null, isGitCommand);
when(commitQuickService.generateQuickCommit(topicScope, null, isGitCommand)).thenReturn("Invalid commit");
mockMvc.perform(post("/api/v1/commit-quick/craft")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(commitQuickRequest)))
.andExpect(status().isOk())
.andExpect(content().string("Invalid commit"));
verify(commitQuickService, times(1)).generateQuickCommit(topicScope, null, isGitCommand);
}
}