-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionsControllerIntegrationTest.java
More file actions
145 lines (120 loc) · 5.81 KB
/
ConnectionsControllerIntegrationTest.java
File metadata and controls
145 lines (120 loc) · 5.81 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.premtsd.linkedin.connectionservice.integration;
import com.premtsd.linkedin.connectionservice.entity.Person;
import com.premtsd.linkedin.connectionservice.exception.BusinessRuleViolationException;
import com.premtsd.linkedin.connectionservice.service.ConnectionsService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.premtsd.linkedin.connectionservice.controller.ConnectionsController;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Arrays;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(ConnectionsController.class)
@AutoConfigureMockMvc(addFilters = false)
@ActiveProfiles("test")
class ConnectionsControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ConnectionsService connectionsService;
private static final String X_USER_ID_HEADER = "X-User-Id";
@Test
void completeConnectionFlow_ShouldWorkWithMockedService() throws Exception {
// Given
List<Person> emptyConnections = Arrays.asList();
List<Person> oneConnection = Arrays.asList(
Person.builder().id(1L).userId(2L).name("Bob").build()
);
// Mock the service calls
when(connectionsService.getFirstDegreeConnections())
.thenReturn(emptyConnections) // First call - no connections
.thenReturn(oneConnection); // Second call - one connection
when(connectionsService.sendConnectionRequest(2L)).thenReturn(true);
when(connectionsService.acceptConnectionRequest(1L)).thenReturn(true);
// Step 1: Initially no connections
mockMvc.perform(get("/core/first-degree")
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(0));
// Step 2: Send connection request
mockMvc.perform(post("/core/request/{userId}", 2L)
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isOk())
.andExpect(content().string("true"));
// Step 3: Accept connection request
mockMvc.perform(post("/core/accept/{userId}", 1L)
.header(X_USER_ID_HEADER, "2"))
.andExpect(status().isOk())
.andExpect(content().string("true"));
// Step 4: Verify connection exists
mockMvc.perform(get("/core/first-degree")
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1))
.andExpect(jsonPath("$[0].userId").value(2))
.andExpect(jsonPath("$[0].name").value("Bob"));
}
@Test
void businessRuleViolations_ShouldReturnBadRequest() throws Exception {
// Given
when(connectionsService.sendConnectionRequest(any()))
.thenThrow(new BusinessRuleViolationException("Connection request already exists"));
// When & Then
mockMvc.perform(post("/core/request/{userId}", 2L)
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("Connection request already exists"))
.andExpect(jsonPath("$.status").value(400));
}
@Test
void rejectConnectionRequest_ShouldWork() throws Exception {
// Given
when(connectionsService.rejectConnectionRequest(1L)).thenReturn(true);
// When & Then
mockMvc.perform(post("/core/reject/{userId}", 1L)
.header(X_USER_ID_HEADER, "2"))
.andExpect(status().isOk())
.andExpect(content().string("true"));
}
@Test
void getAllEndpoints_ShouldBeAccessible() throws Exception {
// Given
when(connectionsService.getFirstDegreeConnections()).thenReturn(Arrays.asList());
when(connectionsService.sendConnectionRequest(any())).thenReturn(true);
when(connectionsService.acceptConnectionRequest(any())).thenReturn(true);
when(connectionsService.rejectConnectionRequest(any())).thenReturn(true);
// Test all endpoints
mockMvc.perform(get("/core/first-degree")
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isOk());
mockMvc.perform(post("/core/request/{userId}", 2L)
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isOk());
mockMvc.perform(post("/core/accept/{userId}", 1L)
.header(X_USER_ID_HEADER, "2"))
.andExpect(status().isOk());
mockMvc.perform(post("/core/reject/{userId}", 1L)
.header(X_USER_ID_HEADER, "2"))
.andExpect(status().isOk());
}
@Test
void errorHandling_ShouldReturnProperStatusCodes() throws Exception {
// Given
when(connectionsService.sendConnectionRequest(any()))
.thenThrow(new RuntimeException("Database error"));
// When & Then
mockMvc.perform(post("/core/request/{userId}", 2L)
.header(X_USER_ID_HEADER, "1"))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value("An unexpected error occurred"))
.andExpect(jsonPath("$.status").value(500));
}
}