From 8050de93c3b3d73f9ad11ccbc46ac446b9da9154 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 10:15:19 +0200 Subject: [PATCH 1/6] test: setup test fixtures for PetPolicy --- .../example/vet1177/policy/PetPolicyTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/test/java/org/example/vet1177/policy/PetPolicyTest.java diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java new file mode 100644 index 00000000..59c29990 --- /dev/null +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -0,0 +1,48 @@ +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 java.lang.reflect.Field; +import java.util.UUID; + +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); + } + + 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); + } + +} From 60aebd975f543faef428aea7f0f704bb38af7886 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 10:29:57 +0200 Subject: [PATCH 2/6] test: canCreate --- .../example/vet1177/policy/PetPolicyTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java index 59c29990..3934207a 100644 --- a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -4,10 +4,13 @@ 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; @@ -45,4 +48,22 @@ private static void setPrivateField(Object target, String fieldName, Object valu 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(); + } + + } From 79bcb0fa2638c8e2e1bcfe7e0eff1388c6d128fc Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 10:46:11 +0200 Subject: [PATCH 3/6] test: canView --- .../example/vet1177/policy/PetPolicyTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java index 3934207a..84287d13 100644 --- a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -65,5 +65,32 @@ 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(); + } } From 37f31740e762ca63029d984c9772c70d411dc27b Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 11:05:20 +0200 Subject: [PATCH 4/6] test: canViewOwnerPets --- .../example/vet1177/policy/PetPolicyTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java index 84287d13..f8248fd7 100644 --- a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -93,4 +93,25 @@ void canView_ownerWithNullPetOwner_shouldReturnFalse() { 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(); + } + } From 61701c9403d15916dc212aa62e64ed75d2e85af3 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 11:28:02 +0200 Subject: [PATCH 5/6] test: canDelete --- .../example/vet1177/policy/PetPolicyTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java index f8248fd7..ae21b276 100644 --- a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -42,6 +42,7 @@ void setUp() throws Exception { 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); @@ -114,4 +115,33 @@ 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(); + } + } From d758635c7f00fbe748a597b756c93c88c3eb6e86 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 8 Apr 2026 11:42:47 +0200 Subject: [PATCH 6/6] test: canDelete --- .../example/vet1177/policy/PetPolicyTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java index ae21b276..5f802afd 100644 --- a/src/test/java/org/example/vet1177/policy/PetPolicyTest.java +++ b/src/test/java/org/example/vet1177/policy/PetPolicyTest.java @@ -144,4 +144,27 @@ void canDelete_ownerWithNullPetOwner_shouldReturnFalse() { 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(); + } + }