From 303520881820d2b4850ce95bc83f5c467a81151d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 25 Sep 2024 12:13:10 +0200 Subject: [PATCH] use EntityManagerWrapper in HsBookingItemController to enable mocking --- .../booking/item/HsBookingItemController.java | 7 ++--- .../item/HsBookingItemControllerRestTest.java | 26 ++++++++++++++++--- .../HsHostingAssetControllerRestTest.java | 4 ++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java index 71080a2c..88f3a4e9 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java @@ -10,6 +10,7 @@ import net.hostsharing.hsadminng.hs.booking.item.validators.HsBookingItemEntityV import net.hostsharing.hsadminng.hs.booking.project.HsBookingProjectRealEntity; import net.hostsharing.hsadminng.mapper.KeyValueMap; import net.hostsharing.hsadminng.mapper.Mapper; +import net.hostsharing.hsadminng.persistence.EntityManagerWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; @@ -37,8 +38,8 @@ public class HsBookingItemController implements HsBookingItemsApi { @Autowired private HsBookingItemRbacRepository bookingItemRepo; - @PersistenceContext - private EntityManager em; + @Autowired + private EntityManagerWrapper em; @Override @Transactional(readOnly = true) @@ -141,7 +142,7 @@ public class HsBookingItemController implements HsBookingItemsApi { final BiConsumer RBAC_ENTITY_TO_RESOURCE_POSTMAPPER = ITEM_TO_RESOURCE_POSTMAPPER::accept; final BiConsumer RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { - entity.setProject(em.getReference(HsBookingProjectRealEntity.class, resource.getProjectUuid())); + entity.setProject(em.find(HsBookingProjectRealEntity.class, resource.getProjectUuid())); entity.setValidity(toPostgresDateRange(LocalDate.now(), resource.getValidTo())); entity.putResources(KeyValueMap.from(resource.getResources())); }; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerRestTest.java index e28f4d38..4e21df03 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemControllerRestTest.java @@ -1,17 +1,20 @@ package net.hostsharing.hsadminng.hs.booking.item; +import net.hostsharing.hsadminng.config.JsonObjectMapperConfiguration; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.hs.booking.project.HsBookingProjectRealEntity; import net.hostsharing.hsadminng.hs.booking.project.HsBookingProjectRealRepository; import net.hostsharing.hsadminng.mapper.Mapper; +import net.hostsharing.hsadminng.persistence.EntityManagerWrapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; @@ -28,13 +31,14 @@ import java.util.UUID; import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals; import static org.hamcrest.Matchers.matchesRegex; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(HsBookingItemController.class) -@Import(Mapper.class) +@Import({Mapper.class, JsonObjectMapperConfiguration.class}) @RunWith(SpringRunner.class) class HsBookingItemControllerRestTest { @@ -44,8 +48,12 @@ class HsBookingItemControllerRestTest { @MockBean Context contextMock; - @Mock - EntityManager em; + @Autowired + @SuppressWarnings("unused") // not used in test, but in controller class + Mapper mapper; + + @MockBean + EntityManagerWrapper em; @MockBean EntityManagerFactory emf; @@ -56,6 +64,16 @@ class HsBookingItemControllerRestTest { @MockBean HsBookingItemRbacRepository rbacBookingItemRepo; + @TestConfiguration + public static class TestConfig { + + @Bean + public EntityManager entityManager() { + return mock(EntityManager.class); + } + + } + @BeforeEach void init() { when(emf.createEntityManager()).thenReturn(em); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java index 0d9dd87b..b2726f48 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerRestTest.java @@ -63,10 +63,11 @@ public class HsHostingAssetControllerRestTest { Context contextMock; @Autowired + @SuppressWarnings("unused") // not used in test, but in controller class Mapper mapper; @MockBean - private EntityManagerWrapper em; + EntityManagerWrapper em; @MockBean EntityManagerFactory emf; @@ -90,6 +91,7 @@ public class HsHostingAssetControllerRestTest { } } + enum ListTestCases { CLOUD_SERVER( List.of(