-
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.
* refactor CardService and start writing tests * autowire mongotemplate * complete CategoryServiceIT * solve testcontainer issue and start of Controller-IT * finish CategoryControllerIT * add Mapper test - category package complete now
- Loading branch information
1 parent
29e6cb0
commit 7a9a6bf
Showing
15 changed files
with
505 additions
and
105 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
51 changes: 51 additions & 0 deletions
51
...va/org/hyperskill/community/flashcards/category/CategoryControllerValidationUnitTest.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,51 @@ | ||
package org.hyperskill.community.flashcards.category; | ||
|
||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import org.hyperskill.community.flashcards.category.request.CategoryCreateRequest; | ||
import org.hyperskill.community.flashcards.category.request.CategoryUpdateRequest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class CategoryControllerValidationUnitTest { | ||
|
||
Validator validator; | ||
|
||
@BeforeEach | ||
void setup() { | ||
try (var validatorFactory = Validation.buildDefaultValidatorFactory()) { | ||
this.validator = validatorFactory.getValidator(); | ||
} | ||
} | ||
|
||
@Test | ||
void whenNullName_ValidationError() { | ||
var createRequest = new CategoryCreateRequest(null); | ||
assertFalse(validator.validate(createRequest).isEmpty()); | ||
var updateRequest = new CategoryUpdateRequest(null); | ||
assertFalse(validator.validate(updateRequest).isEmpty()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"", " ", " "}) | ||
void whenBlankName_ValidationError(String name) { | ||
var createRequest = new CategoryCreateRequest(name); | ||
assertFalse(validator.validate(createRequest).isEmpty()); | ||
var updateRequest = new CategoryUpdateRequest(name); | ||
assertFalse(validator.validate(updateRequest).isEmpty()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"a", "category", "a+-3§"}) | ||
void whenNonBlankName_ValidationOk(String name) { | ||
var createRequest = new CategoryCreateRequest(name); | ||
assertTrue(validator.validate(createRequest).isEmpty()); | ||
var updateRequest = new CategoryUpdateRequest(name); | ||
assertTrue(validator.validate(updateRequest).isEmpty()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
server/src/test/java/org/hyperskill/community/flashcards/category/CategoryMapperTest.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,48 @@ | ||
package org.hyperskill.community.flashcards.category; | ||
|
||
import org.hyperskill.community.flashcards.category.mapper.CategoryMapper; | ||
import org.hyperskill.community.flashcards.category.model.Category; | ||
import org.hyperskill.community.flashcards.category.model.CategoryAccess; | ||
import org.hyperskill.community.flashcards.common.response.ActionType; | ||
import org.hyperskill.community.flashcards.common.response.PermittedAction; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class CategoryMapperTest { | ||
|
||
CategoryMapper mapper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mapper = new CategoryMapper("user"); | ||
} | ||
|
||
@Test | ||
void permittedUser_mapsCorrectly() { | ||
var category = createCategory("user", "rwd"); | ||
var dto = mapper.categoryToCategoryDto(category); | ||
assertEquals(category.id(), dto.id()); | ||
assertEquals(category.name(), dto.name()); | ||
var expectedPermissions = Set.of( | ||
new PermittedAction(ActionType.READ, "/api/categories/12345"), | ||
new PermittedAction(ActionType.WRITE, "/api/categories/12345"), | ||
new PermittedAction(ActionType.DELETE, "/api/categories/12345")); | ||
assertEquals(expectedPermissions, dto.actions()); | ||
} | ||
|
||
@Test | ||
void notPermittedUser_mapThrows() { | ||
var category = createCategory("other", "rw"); | ||
assertThrows(IllegalStateException.class, () -> mapper.categoryToCategoryDto(category)); | ||
} | ||
|
||
private Category createCategory( String username, String permissions) { | ||
return new Category("12345", "test", Set.of(new CategoryAccess(username, permissions))); | ||
} | ||
|
||
} |
Oops, something went wrong.