Skip to content

topic4 group1 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kma.topic8.springrestsample;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
Expand All @@ -10,12 +12,14 @@
public class ExceptionController {

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<String> handle(final MissingServletRequestParameterException ex) {
public ResponseEntity<Map<String, String>> handle(final MissingServletRequestParameterException ex) {
System.out.println("handle exception: " + ex.getMessage());

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ex.getMessage());
.body(Map.of(
"error", ex.getMessage()
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class LoginDto {

private String login;
private String password;
private int age;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kma.topic8.springrestsample;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.server.LocalServerPort;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import kma.topic8.springrestsample.dto.LoginResponseDto;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class LoginControllerIT {

@MockBean
private UserService userService;

@LocalServerPort
void savePort(int port) {
RestAssured.port = port;
}

@Test
void shouldSendRequest() {
Mockito.when(userService.doLogin(ArgumentMatchers.any()))
.thenReturn(LoginResponseDto.of("other-login", "message"));

RestAssured
.given()
.queryParam("requiredField", "stejwhewhrge")
.contentType(ContentType.JSON)
.body(LoginControllerIT.class.getResourceAsStream("/request.json"))
.when()
.post("/login")
.then()
.statusCode(200)
.body("login", CoreMatchers.is("other-login"))
.body("message", CoreMatchers.is("message"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package kma.topic8.springrestsample;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

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.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import kma.topic8.springrestsample.dto.LoginResponseDto;

@WebMvcTest({
LoginController.class
})
class LoginControllerTest {

@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;

@Test
void shouldSendRequest() throws Exception {
when(userService.doLogin(any()))
.thenReturn(LoginResponseDto.of("login1", "message2"));

mockMvc.perform(
MockMvcRequestBuilders.post("/login")
.queryParam("requiredField", "asd")
.contentType(MediaType.APPLICATION_JSON)
.content(LoginControllerTest.class.getResourceAsStream("/request.json").readAllBytes())
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json(
new String(LoginControllerTest.class.getResourceAsStream("/response.json").readAllBytes())
))
.andExpect(MockMvcResultMatchers.header().string(HttpHeaders.AUTHORIZATION, "generated-jwt-token"));
}

@Test
void shouldHandleMissingParameter() throws Exception {
when(userService.doLogin(any()))
.thenReturn(LoginResponseDto.of("login1", "message2"));

mockMvc.perform(
MockMvcRequestBuilders.post("/login")
.contentType(MediaType.APPLICATION_JSON)
.content(LoginControllerTest.class.getResourceAsStream("/request.json").readAllBytes())
)
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.content().json(
new String(LoginControllerTest.class.getResourceAsStream("/errorMissingParameter.json").readAllBytes())
))
.andExpect(MockMvcResultMatchers.header().doesNotExist(HttpHeaders.AUTHORIZATION));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"error": "Required String parameter 'requiredField' is not present"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"login": "add",
"password": "gagw",
"age": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"login": "login1",
"message": "message2"
}