Skip to content

Commit 1c0fec1

Browse files
committed
Task 55 : Implement JUnit tests for auth service and user service
1 parent 6257b8c commit 1c0fec1

File tree

27 files changed

+2199
-9
lines changed

27 files changed

+2199
-9
lines changed

authservice/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@
135135
<artifactId>spring-cloud-starter-openfeign</artifactId>
136136
</dependency>
137137

138+
<dependency>
139+
<groupId>com.fasterxml.jackson.datatype</groupId>
140+
<artifactId>jackson-datatype-jsr310</artifactId>
141+
</dependency>
142+
143+
138144
</dependencies>
139145
<dependencyManagement>
140146
<dependencies>

authservice/src/main/java/com/springbootmicroservices/authservice/model/auth/dto/request/LoginRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.springbootmicroservices.authservice.model.auth.dto.request;
22

33
import jakarta.validation.constraints.NotBlank;
4-
import lombok.Builder;
5-
import lombok.Getter;
6-
import lombok.Setter;
4+
import lombok.*;
75

86
@Getter
97
@Setter
108
@Builder
9+
@NoArgsConstructor
10+
@AllArgsConstructor
1111
public class LoginRequest {
1212

1313
@NotBlank
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.springbootmicroservices.authservice.base;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
import org.mockito.junit.jupiter.MockitoExtension;
5+
import org.mockito.junit.jupiter.MockitoSettings;
6+
import org.mockito.quality.Strictness;
7+
8+
@ExtendWith(MockitoExtension.class)
9+
@MockitoSettings(strictness = Strictness.LENIENT)
10+
public abstract class AbstractBaseServiceTest {
11+
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.springbootmicroservices.authservice.base;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.web.servlet.MockMvc;
8+
9+
@SpringBootTest
10+
@AutoConfigureMockMvc
11+
public class AbstractRestControllerTest {
12+
13+
@Autowired
14+
protected MockMvc mockMvc;
15+
16+
@Autowired
17+
protected ObjectMapper objectMapper;
18+
19+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.springbootmicroservices.authservice.controller;
2+
3+
import com.springbootmicroservices.authservice.base.AbstractRestControllerTest;
4+
import com.springbootmicroservices.authservice.model.auth.dto.request.LoginRequest;
5+
import com.springbootmicroservices.authservice.model.auth.dto.request.RegisterRequest;
6+
import com.springbootmicroservices.authservice.model.auth.dto.request.TokenInvalidateRequest;
7+
import com.springbootmicroservices.authservice.model.auth.dto.request.TokenRefreshRequest;
8+
import com.springbootmicroservices.authservice.model.auth.dto.response.TokenResponse;
9+
import com.springbootmicroservices.authservice.model.common.dto.response.CustomResponse;
10+
import com.springbootmicroservices.authservice.service.LogoutService;
11+
import com.springbootmicroservices.authservice.service.RefreshTokenService;
12+
import com.springbootmicroservices.authservice.service.RegisterService;
13+
import com.springbootmicroservices.authservice.service.UserLoginService;
14+
import org.junit.jupiter.api.Test;
15+
import org.springframework.boot.test.mock.mockito.MockBean;
16+
import org.springframework.http.MediaType;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.*;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24+
25+
class AuthControllerTest extends AbstractRestControllerTest {
26+
27+
@MockBean
28+
private RegisterService registerService;
29+
30+
@MockBean
31+
private UserLoginService userLoginService;
32+
33+
@MockBean
34+
private RefreshTokenService refreshTokenService;
35+
36+
@MockBean
37+
private LogoutService logoutService;
38+
39+
@Test
40+
void registerAdmin_ValidRequest_ReturnsSuccess() throws Exception {
41+
42+
// Given
43+
RegisterRequest registerRequest = RegisterRequest.builder()
44+
45+
.password("validPassword123")
46+
.firstName("John")
47+
.lastName("Doe")
48+
.phoneNumber("1234567890100")
49+
.role("user")
50+
.build();
51+
52+
// When & Then
53+
mockMvc.perform(post("/api/v1/authentication/users/register")
54+
.contentType(MediaType.APPLICATION_JSON)
55+
.content(objectMapper.writeValueAsString(registerRequest)))
56+
.andExpect(status().isOk())
57+
.andExpect(jsonPath("$.isSuccess").value(true))
58+
.andExpect(jsonPath("$.httpStatus").value("OK"));
59+
60+
// Verify
61+
verify(registerService, times(1)).registerUser(any(RegisterRequest.class));
62+
63+
}
64+
65+
@Test
66+
void loginUser_ValidRequest_ReturnsTokenResponse() throws Exception {
67+
68+
// Given
69+
LoginRequest loginRequest = LoginRequest.builder()
70+
71+
.password("validPassword123")
72+
.build();
73+
74+
TokenResponse tokenResponse = TokenResponse.builder()
75+
.accessToken("newAccessToken")
76+
.accessTokenExpiresAt(System.currentTimeMillis() + 3600)
77+
.refreshToken("newRefreshToken")
78+
.build();
79+
80+
CustomResponse<TokenResponse> expectedResponse = CustomResponse.successOf(tokenResponse);
81+
82+
// When
83+
when(userLoginService.login(any(LoginRequest.class))).thenReturn(expectedResponse);
84+
85+
// Then
86+
mockMvc.perform(post("/api/v1/authentication/users/login")
87+
.contentType(MediaType.APPLICATION_JSON)
88+
.content(objectMapper.writeValueAsString(loginRequest)))
89+
.andExpect(status().isOk())
90+
.andExpect(jsonPath("$.isSuccess").value(true))
91+
.andExpect(jsonPath("$.httpStatus").value("OK"))
92+
.andExpect(jsonPath("$.response.accessToken").value("newAccessToken"));
93+
94+
// Verify
95+
verify(userLoginService, times(1)).login(any(LoginRequest.class));
96+
97+
}
98+
99+
@Test
100+
void refreshToken_ValidRequest_ReturnsTokenResponse() throws Exception {
101+
102+
// Given
103+
TokenRefreshRequest tokenRefreshRequest = TokenRefreshRequest.builder()
104+
.refreshToken("validRefreshToken")
105+
.build();
106+
107+
TokenResponse tokenResponse = TokenResponse.builder()
108+
.accessToken("newAccessToken")
109+
.accessTokenExpiresAt(System.currentTimeMillis() + 3600)
110+
.refreshToken("newRefreshToken")
111+
.build();
112+
113+
CustomResponse<TokenResponse> expectedResponse = CustomResponse.successOf(tokenResponse);
114+
115+
// When
116+
when(refreshTokenService.refreshToken(any(TokenRefreshRequest.class))).thenReturn(expectedResponse);
117+
118+
// Then
119+
mockMvc.perform(post("/api/v1/authentication/users/refresh-token")
120+
.contentType(MediaType.APPLICATION_JSON)
121+
.content(objectMapper.writeValueAsString(tokenRefreshRequest)))
122+
.andExpect(status().isOk())
123+
.andExpect(jsonPath("$.isSuccess").value(true))
124+
.andExpect(jsonPath("$.httpStatus").value("OK"))
125+
.andExpect(jsonPath("$.response.accessToken").value("newAccessToken"));
126+
127+
verify(refreshTokenService, times(1)).refreshToken(any(TokenRefreshRequest.class));
128+
}
129+
130+
@Test
131+
void logout_ValidRequest_ReturnsSuccess() throws Exception {
132+
133+
// Given
134+
TokenInvalidateRequest tokenInvalidateRequest = TokenInvalidateRequest.builder()
135+
.accessToken("validAccessToken")
136+
.refreshToken("validRefreshToken")
137+
.build();
138+
139+
// When & Then
140+
mockMvc.perform(post("/api/v1/authentication/users/logout")
141+
.contentType(MediaType.APPLICATION_JSON)
142+
.content(objectMapper.writeValueAsString(tokenInvalidateRequest)))
143+
.andExpect(status().isOk())
144+
.andExpect(jsonPath("$.isSuccess").value(true))
145+
.andExpect(jsonPath("$.httpStatus").value("OK"));
146+
147+
// Verify
148+
verify(logoutService, times(1)).logout(any(TokenInvalidateRequest.class));
149+
150+
}
151+
152+
153+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.springbootmicroservices.authservice.exception;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.http.HttpStatus;
9+
10+
import java.time.LocalDateTime;
11+
import java.util.Collections;
12+
13+
public class CustomErrorTest {
14+
15+
@Test
16+
void testCustomErrorBuilder_WithAllFields() {
17+
18+
LocalDateTime now = LocalDateTime.now();
19+
CustomError.CustomSubError subError = CustomError.CustomSubError.builder()
20+
.message("Sub error message")
21+
.field("field")
22+
.value("value")
23+
.type("type")
24+
.build();
25+
26+
CustomError customError = CustomError.builder()
27+
.time(now)
28+
.httpStatus(HttpStatus.BAD_REQUEST)
29+
.header(CustomError.Header.VALIDATION_ERROR.getName())
30+
.message("Main error message")
31+
.subErrors(Collections.singletonList(subError))
32+
.build();
33+
34+
assertNotNull(customError);
35+
assertEquals(now, customError.getTime());
36+
assertEquals(HttpStatus.BAD_REQUEST, customError.getHttpStatus());
37+
assertEquals(CustomError.Header.VALIDATION_ERROR.getName(), customError.getHeader());
38+
assertEquals("Main error message", customError.getMessage());
39+
assertFalse(customError.getIsSuccess());
40+
assertNotNull(customError.getSubErrors());
41+
assertEquals(1, customError.getSubErrors().size());
42+
assertEquals(subError, customError.getSubErrors().get(0));
43+
}
44+
45+
@Test
46+
void testCustomErrorBuilder_DefaultValues() {
47+
CustomError customError = CustomError.builder().build();
48+
49+
assertNotNull(customError);
50+
assertNotNull(customError.getTime());
51+
assertEquals(false, customError.getIsSuccess());
52+
assertNull(customError.getHttpStatus());
53+
assertNull(customError.getHeader());
54+
assertNull(customError.getMessage());
55+
assertNull(customError.getSubErrors());
56+
}
57+
58+
@Test
59+
void testCustomSubErrorBuilder_WithAllFields() {
60+
CustomError.CustomSubError subError = CustomError.CustomSubError.builder()
61+
.message("Sub error message")
62+
.field("field")
63+
.value("value")
64+
.type("type")
65+
.build();
66+
67+
assertNotNull(subError);
68+
assertEquals("Sub error message", subError.getMessage());
69+
assertEquals("field", subError.getField());
70+
assertEquals("value", subError.getValue());
71+
assertEquals("type", subError.getType());
72+
}
73+
74+
@Test
75+
void testCustomSubErrorBuilder_DefaultValues() {
76+
CustomError.CustomSubError subError = CustomError.CustomSubError.builder().build();
77+
78+
assertNotNull(subError);
79+
assertNull(subError.getMessage());
80+
assertNull(subError.getField());
81+
assertNull(subError.getValue());
82+
assertNull(subError.getType());
83+
}
84+
85+
@Test
86+
void testCustomErrorHeaderEnum() {
87+
assertEquals("API ERROR", CustomError.Header.API_ERROR.getName());
88+
assertEquals("ALREADY EXIST", CustomError.Header.ALREADY_EXIST.getName());
89+
assertEquals("NOT EXIST", CustomError.Header.NOT_FOUND.getName());
90+
assertEquals("VALIDATION ERROR", CustomError.Header.VALIDATION_ERROR.getName());
91+
assertEquals("DATABASE ERROR", CustomError.Header.DATABASE_ERROR.getName());
92+
assertEquals("PROCESS ERROR", CustomError.Header.PROCESS_ERROR.getName());
93+
assertEquals("AUTH ERROR", CustomError.Header.AUTH_ERROR.getName());
94+
}
95+
96+
}

0 commit comments

Comments
 (0)