-
Notifications
You must be signed in to change notification settings - Fork 382
[MVC 구현하기 - 3단계] 새양(양경호) 미션 제출합니다. #819
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
68dff5f
feat: impl json view with jsonMapper
geoje 5641e18
test: json view rendering
geoje a98da0f
feat: add user controller on app
geoje b513c6c
feat: add argument handler exception resolver
geoje f9bc764
feat: use handler exception resolver
geoje 9f171bb
test: user controller valid and invalid
geoje f30adee
refactor: move all entries on manual handler mapping
geoje 426b030
test: get home dashboard
geoje 21f152c
refactor: add meaning to display name
geoje 52d9e54
refactor: combine exception on test
geoje 2ccac39
fix: change request method to post
geoje 9c723b3
test: login with post method or invalid request
geoje 6ea97f2
refactor: remove manual handler mapping
geoje b245f11
refactor: remove legacy asis
geoje 995c6ad
refactor: prepare to move dispatcher servlet to mvc package
geoje 4b37adc
refactor: remove test of manual handlers
geoje d1ca156
refactor: remove manual controller on test
geoje 27e63be
refactor: move dispatcher servlet to mvc
geoje 9f30cff
refactor: remove unused method on dispatcher servlet
geoje 9c7fe19
style: add eof for viewing good on github
geoje ac51700
feat: write only value if having single entry
geoje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/techcourse/controller/HomeController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
|
||
@Controller | ||
@RequestMapping | ||
public class HomeController { | ||
|
||
@RequestMapping(method = RequestMethod.GET) | ||
public ModelAndView getHome() { | ||
return new ModelAndView(new JspView("/index.jsp")); | ||
} | ||
} |
20 changes: 13 additions & 7 deletions
20
app/src/main/java/com/techcourse/controller/LoginController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 12 additions & 6 deletions
18
app/src/main/java/com/techcourse/controller/LoginViewController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import com.interface21.webmvc.servlet.mvc.asis.Controller; | ||
|
||
public class LoginViewController implements Controller { | ||
@Controller | ||
@RequestMapping("/login/view") | ||
public class LoginViewController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return UserSession.getUserFrom(req.getSession()) | ||
@RequestMapping(method = RequestMethod.GET) | ||
public ModelAndView getLoginView(final HttpServletRequest req) throws Exception { | ||
String viewName = UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
}) | ||
.orElse("/login.jsp"); | ||
return new ModelAndView(new JspView(viewName)); | ||
} | ||
} |
17 changes: 11 additions & 6 deletions
17
app/src/main/java/com/techcourse/controller/LogoutController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import com.interface21.webmvc.servlet.mvc.asis.Controller; | ||
|
||
public class LogoutController implements Controller { | ||
@Controller | ||
@RequestMapping("/logout") | ||
public class LogoutController { | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
@RequestMapping(method = RequestMethod.GET) | ||
public ModelAndView getLogout(final HttpServletRequest req) throws Exception { | ||
final var session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return "redirect:/"; | ||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/techcourse/controller/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JsonView; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserController.class); | ||
private static final String PARAM_NAME_ACCOUNT = "account"; | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView getUser(HttpServletRequest request, HttpServletResponse response) { | ||
if (!isValidRequest(request)) { | ||
throw new IllegalArgumentException("Please input account as parameter"); | ||
} | ||
|
||
final String account = request.getParameter(PARAM_NAME_ACCOUNT); | ||
log.debug("user id : {}", account); | ||
|
||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(() -> new IllegalArgumentException("User not found: " + account)); | ||
|
||
final ModelAndView modelAndView = new ModelAndView(new JsonView()); | ||
modelAndView.addObject("user", user); | ||
return modelAndView; | ||
} | ||
|
||
private boolean isValidRequest(HttpServletRequest request) { | ||
String account = request.getParameter(PARAM_NAME_ACCOUNT); | ||
return account != null && !account.isBlank(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/test/java/com/techcourse/controller/HomeControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.techcourse.TomcatStarter; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HomeControllerTest { | ||
|
||
private static final String URL = "http://localhost:8080"; | ||
|
||
private TomcatStarter tomcat; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
tomcat = new TomcatStarter("src/main/webapp/", 8080); | ||
tomcat.start(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
tomcat.stop(); | ||
} | ||
|
||
@Test | ||
@DisplayName("홈 화면을 요청시 대시보드 페이지를 응답한다.") | ||
void getHome() throws Exception { | ||
// given | ||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(URL)) | ||
.build(); | ||
|
||
// when | ||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); | ||
client.close(); | ||
|
||
// then | ||
assertThat(response.body()).contains("<title>대시보드</title>"); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
app/src/test/java/com/techcourse/controller/LoginControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import com.techcourse.TomcatStarter; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpRequest.BodyPublishers; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class LoginControllerTest { | ||
|
||
private static final String URL = "http://localhost:8080/login"; | ||
private static final String CONTENT_TYPE_URLENCODED = "application/x-www-form-urlencoded"; | ||
private static final String CONTENT_TYPE_NAME = "Content-Type"; | ||
|
||
private TomcatStarter tomcat; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
tomcat = new TomcatStarter("src/main/webapp/", 8080); | ||
tomcat.start(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
tomcat.stop(); | ||
} | ||
|
||
@Test | ||
@DisplayName("올바른 계정 정보로 로그인 요청 시 대시보드 페이지로 리디렉션을 응답한다.") | ||
void postLogin() throws Exception { | ||
// given | ||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(URL)) | ||
.header(CONTENT_TYPE_NAME, CONTENT_TYPE_URLENCODED) | ||
.POST(BodyPublishers.ofString("account=gugu&password=password")) | ||
.build(); | ||
|
||
// when | ||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); | ||
client.close(); | ||
|
||
// then | ||
assertAll( | ||
() -> assertThat(response.headers().firstValue("Location")).hasValue("/index.jsp"), | ||
() -> assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_FOUND) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 계정으로 요청 시 401 페이지로 리디렉션을 응답한다.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 역시 디테일한 테스트케이스 👍 |
||
void getLoginWithNonAccount() throws Exception { | ||
// given | ||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(URL)) | ||
.header(CONTENT_TYPE_NAME, CONTENT_TYPE_URLENCODED) | ||
.POST(BodyPublishers.ofString("account=seyang&password=password")) | ||
.build(); | ||
|
||
// when | ||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); | ||
client.close(); | ||
|
||
// then | ||
assertAll( | ||
() -> assertThat(response.headers().firstValue("Location")).hasValue("/401.jsp"), | ||
() -> assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_FOUND) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("틀린 비밀번호로 요청 시 401 페이지로 리디렉션을 응답한다.") | ||
void getLoginWithInvalidPassword() throws Exception { | ||
// given | ||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(URL)) | ||
.header(CONTENT_TYPE_NAME, CONTENT_TYPE_URLENCODED) | ||
.POST(BodyPublishers.ofString("account=gugu&password=pw")) | ||
.build(); | ||
|
||
// when | ||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); | ||
response.sslSession(); | ||
client.close(); | ||
|
||
// then | ||
assertAll( | ||
() -> assertThat(response.headers().firstValue("Location")).hasValue("/401.jsp"), | ||
() -> assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_FOUND) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 익숙한 Rest assured를 사용했는데 추가 의존성을 사용하지 않고 TomcatStarter와 HttpClient를 사용해주셨네요! 💯 배워갑니다 👍