forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
177 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,27 @@ | ||
package ru.javawebinar.topjava.web; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Locale; | ||
|
||
@Component | ||
public class MessageUtil { | ||
|
||
private final MessageSource messageSource; | ||
|
||
@Autowired | ||
public MessageUtil(MessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
public String getMessage(String code, Locale locale) { | ||
return messageSource.getMessage(code, null, locale); | ||
} | ||
|
||
public String getMessage(String code) { | ||
return getMessage(code, LocaleContextHolder.getLocale()); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -4,20 +4,24 @@ | |
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import ru.javawebinar.topjava.model.Role; | ||
import ru.javawebinar.topjava.model.User; | ||
import ru.javawebinar.topjava.util.exception.ErrorType; | ||
import ru.javawebinar.topjava.web.AbstractControllerTest; | ||
import ru.javawebinar.topjava.web.json.JsonUtil; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static ru.javawebinar.topjava.TestUtil.readFromJson; | ||
import static ru.javawebinar.topjava.TestUtil.userHttpBasic; | ||
import static ru.javawebinar.topjava.UserTestData.*; | ||
import static ru.javawebinar.topjava.util.exception.ErrorType.VALIDATION_ERROR; | ||
import static ru.javawebinar.topjava.web.ExceptionInfoHandler.EXCEPTION_DUPLICATE_EMAIL; | ||
|
||
class AdminRestControllerTest extends AbstractControllerTest { | ||
|
||
|
@@ -139,7 +143,7 @@ void createInvalid() throws Exception { | |
.with(userHttpBasic(ADMIN)) | ||
.content(JsonUtil.writeValue(expected))) | ||
.andExpect(status().isUnprocessableEntity()) | ||
.andExpect(jsonPath("$.type").value(ErrorType.VALIDATION_ERROR.name())) | ||
.andExpect(errorType(VALIDATION_ERROR)) | ||
.andDo(print()); | ||
} | ||
|
||
|
@@ -153,7 +157,35 @@ void updateInvalid() throws Exception { | |
.content(JsonUtil.writeValue(updated))) | ||
.andExpect(status().isUnprocessableEntity()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.type").value(ErrorType.VALIDATION_ERROR.name())) | ||
.andExpect(errorType(VALIDATION_ERROR)) | ||
.andDo(print()); | ||
} | ||
@Test | ||
@Transactional(propagation = Propagation.NEVER) | ||
void updateDuplicate() throws Exception { | ||
User updated = new User(USER); | ||
updated.setEmail("[email protected]"); | ||
mockMvc.perform(MockMvcRequestBuilders.put(REST_URL + USER_ID) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.with(userHttpBasic(ADMIN)) | ||
.content(jsonWithPassword(updated, "password"))) | ||
.andExpect(status().isConflict()) | ||
.andExpect(errorType(VALIDATION_ERROR)) | ||
.andExpect(detailMessage(EXCEPTION_DUPLICATE_EMAIL)) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@Transactional(propagation = Propagation.NEVER) | ||
void createDuplicate() throws Exception { | ||
User expected = new User(null, "New", "[email protected]", "newPass", 2300, Role.ROLE_USER, Role.ROLE_ADMIN); | ||
mockMvc.perform(MockMvcRequestBuilders.post(REST_URL) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.with(userHttpBasic(ADMIN)) | ||
.content(jsonWithPassword(expected, "newPass"))) | ||
.andExpect(status().isConflict()) | ||
.andExpect(errorType(VALIDATION_ERROR)) | ||
.andExpect(detailMessage(EXCEPTION_DUPLICATE_EMAIL)); | ||
|
||
} | ||
} |
Oops, something went wrong.