-
Notifications
You must be signed in to change notification settings - Fork 2
test: add unit tests for PetPolicy #163
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
6 commits
Select commit
Hold shift + click to select a range
8050de9
test: setup test fixtures for PetPolicy
lindaeskilsson 60aebd9
test: canCreate
lindaeskilsson 79bcb0f
test: canView
lindaeskilsson 37f3174
test: canViewOwnerPets
lindaeskilsson 61701c9
test: canDelete
lindaeskilsson d758635
test: canDelete
lindaeskilsson 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
170 changes: 170 additions & 0 deletions
170
src/test/java/org/example/vet1177/policy/PetPolicyTest.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,170 @@ | ||
| package org.example.vet1177.policy; | ||
|
|
||
| import org.example.vet1177.entities.Pet; | ||
| import org.example.vet1177.entities.Role; | ||
| import org.example.vet1177.entities.User; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| class PetPolicyTest { | ||
|
|
||
| private PetPolicy policy; | ||
|
|
||
| private User admin; | ||
| private User owner; | ||
| private User otherOwner; | ||
| private User vet; | ||
|
|
||
| private Pet pet; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| policy = new PetPolicy(); | ||
|
|
||
| admin = new User("Admin Adminsson", "admin@vet.se", "hash", Role.ADMIN); | ||
| setPrivateField(admin, "id", UUID.randomUUID()); | ||
|
|
||
| owner = new User("Anna Ägare", "anna@mail.se", "hash", Role.OWNER); | ||
| setPrivateField(owner, "id", UUID.randomUUID()); | ||
|
|
||
| otherOwner = new User("Karin Annan", "karin@mail.se", "hash", Role.OWNER); | ||
| setPrivateField(otherOwner, "id", UUID.randomUUID()); | ||
|
|
||
| vet = new User("Dr. Erik Vet", "erik@vet.se", "hash", Role.VET); | ||
| setPrivateField(vet, "id", UUID.randomUUID()); | ||
|
|
||
| pet = new Pet(); | ||
| pet.setOwner(owner); | ||
| } | ||
|
|
||
| //helper | ||
| private static void setPrivateField(Object target, String fieldName, Object value) throws Exception { | ||
| Field field = target.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| field.set(target, value); | ||
| } | ||
|
|
||
| //Test canCreate | ||
|
|
||
| @Test | ||
| void canCreate_owner_shouldReturnTrue() { | ||
| assertThat(policy.canCreate(owner)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canCreate_admin_shouldReturnTrue() { | ||
| assertThat(policy.canCreate(admin)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canCreate_vet_shouldReturnFalse() { | ||
| assertThat(policy.canCreate(vet)).isFalse(); | ||
| } | ||
|
|
||
| // canView | ||
| @Test | ||
| void canView_admin_shouldReturnTrue() { | ||
| assertThat(policy.canView(admin, pet)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canView_ownerOfPet_shouldReturnTrue() { | ||
| assertThat(policy.canView(owner, pet)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canView_ownerOfOtherPet_shouldReturnFalse() { | ||
| assertThat(policy.canView(otherOwner, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canView_vet_shouldReturnFalse() { | ||
| assertThat(policy.canView(vet, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canView_ownerWithNullPetOwner_shouldReturnFalse() { | ||
| pet.setOwner(null); | ||
|
|
||
| assertThat(policy.canView(owner, pet)).isFalse(); | ||
| } | ||
|
|
||
| //Can viewOwnerPets | ||
| @Test | ||
| void canViewOwnerPets_admin_shouldReturnTrue() { | ||
| assertThat(policy.canViewOwnerPets(admin, UUID.randomUUID())).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canViewOwnerPets_ownerViewingOwnPets_shouldReturnTrue() { | ||
| assertThat(policy.canViewOwnerPets(owner, owner.getId())).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canViewOwnerPets_ownerViewingOthersPets_shouldReturnFalse() { | ||
| assertThat(policy.canViewOwnerPets(owner, otherOwner.getId())).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canViewOwnerPets_vet_shouldReturnFalse() { | ||
| assertThat(policy.canViewOwnerPets(vet, owner.getId())).isFalse(); | ||
| } | ||
|
|
||
| // canDelete | ||
|
|
||
| @Test | ||
| void canDelete_admin_shouldReturnTrue() { | ||
| assertThat(policy.canDelete(admin, pet)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canDelete_ownerOfPet_shouldReturnTrue() { | ||
| assertThat(policy.canDelete(owner, pet)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canDelete_ownerOfOtherPet_shouldReturnFalse() { | ||
| assertThat(policy.canDelete(otherOwner, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canDelete_vet_shouldReturnFalse() { | ||
| assertThat(policy.canDelete(vet, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canDelete_ownerWithNullPetOwner_shouldReturnFalse() { | ||
| pet.setOwner(null); | ||
|
|
||
| assertThat(policy.canDelete(owner, pet)).isFalse(); | ||
| } | ||
|
|
||
| //canUpdate | ||
| @Test | ||
| void canUpdate_ownerOfPet_shouldReturnTrue() { | ||
| assertThat(policy.canUpdate(owner, pet)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void canUpdate_ownerOfOtherPet_shouldReturnFalse() { | ||
| assertThat(policy.canUpdate(otherOwner, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canUpdate_vet_shouldReturnFalse() { | ||
| assertThat(policy.canUpdate(vet, pet)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void canUpdate_ownerWithNullPetOwner_shouldReturnFalse() { | ||
| pet.setOwner(null); | ||
|
|
||
| assertThat(policy.canUpdate(owner, pet)).isFalse(); | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.