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
14 changed files
with
164 additions
and
23 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
41 changes: 41 additions & 0 deletions
41
src/main/java/ru/javawebinar/topjava/util/exception/ApplicationException.java
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,41 @@ | ||
package ru.javawebinar.topjava.util.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ApplicationException extends RuntimeException { | ||
|
||
private final ErrorType type; | ||
private final String msgCode; | ||
private final HttpStatus httpStatus; | ||
private final String[] args; | ||
|
||
public ApplicationException(String msgCode, HttpStatus httpStatus) { | ||
this(ErrorType.APP_ERROR, msgCode, httpStatus); | ||
} | ||
|
||
public ApplicationException(ErrorType type, String msgCode, HttpStatus httpStatus, String... args) { | ||
super(String.format("type=%s, msgCode=%s, args=%s", type, msgCode, Arrays.toString(args))); | ||
this.type = type; | ||
this.msgCode = msgCode; | ||
this.httpStatus = httpStatus; | ||
this.args = args; | ||
} | ||
|
||
public ErrorType getType() { | ||
return type; | ||
} | ||
|
||
public String getMsgCode() { | ||
return msgCode; | ||
} | ||
|
||
public HttpStatus getHttpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
public String[] getArgs() { | ||
return args; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/ru/javawebinar/topjava/util/exception/ModificationRestrictionException.java
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,11 @@ | ||
package ru.javawebinar.topjava.util.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class ModificationRestrictionException extends ApplicationException { | ||
public static final String EXCEPTION_MODIFICATION_RESTRICTION = "exception.user.modificationRestriction"; | ||
|
||
public ModificationRestrictionException() { | ||
super(EXCEPTION_MODIFICATION_RESTRICTION, HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS); | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
src/main/java/ru/javawebinar/topjava/util/exception/NotFoundException.java
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package ru.javawebinar.topjava.util.exception; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class NotFoundException extends RuntimeException { | ||
public NotFoundException(String message) { | ||
super(message); | ||
public class NotFoundException extends ApplicationException { | ||
public static final String NOT_FOUND_EXCEPTION = "exception.common.notFound"; | ||
|
||
// http://stackoverflow.com/a/22358422/548473 | ||
public NotFoundException(String arg) { | ||
super(ErrorType.DATA_NOT_FOUND, NOT_FOUND_EXCEPTION, HttpStatus.UNPROCESSABLE_ENTITY, arg); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/test/java/ru/javawebinar/topjava/web/user/HerokuRestControllerTest.java
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,43 @@ | ||
package ru.javawebinar.topjava.web.user; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import ru.javawebinar.topjava.UserTestData; | ||
import ru.javawebinar.topjava.util.exception.ErrorType; | ||
import ru.javawebinar.topjava.web.AbstractControllerTest; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static ru.javawebinar.topjava.Profiles.HEROKU; | ||
import static ru.javawebinar.topjava.TestUtil.userHttpBasic; | ||
import static ru.javawebinar.topjava.UserTestData.*; | ||
import static ru.javawebinar.topjava.util.exception.ModificationRestrictionException.EXCEPTION_MODIFICATION_RESTRICTION; | ||
|
||
@ActiveProfiles({HEROKU}) | ||
public class HerokuRestControllerTest extends AbstractControllerTest { | ||
|
||
private static final String REST_URL = AdminRestController.REST_URL + '/'; | ||
|
||
@Test | ||
void delete() throws Exception { | ||
mockMvc.perform(MockMvcRequestBuilders.delete(REST_URL + USER_ID) | ||
.with(userHttpBasic(ADMIN))) | ||
.andDo(print()) | ||
.andExpect(errorType(ErrorType.APP_ERROR)) | ||
.andExpect(detailMessage(EXCEPTION_MODIFICATION_RESTRICTION)) | ||
.andExpect(status().isUnavailableForLegalReasons()); | ||
} | ||
|
||
@Test | ||
void update() throws Exception { | ||
mockMvc.perform(MockMvcRequestBuilders.put(REST_URL + USER_ID) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.with(userHttpBasic(ADMIN)) | ||
.content(UserTestData.jsonWithPassword(USER, "password"))) | ||
.andExpect(errorType(ErrorType.APP_ERROR)) | ||
.andExpect(detailMessage(EXCEPTION_MODIFICATION_RESTRICTION)) | ||
.andExpect(status().isUnavailableForLegalReasons()); | ||
} | ||
} |