refactoring for implicit creation of dependend hosting-assets #108

Merged
hsh-michaelhoennig merged 22 commits from refactoring-for-implicit-creation-of-dependend-hosting-assets into master 2024-09-26 10:51:30 +02:00
3 changed files with 29 additions and 8 deletions
Showing only changes of commit 3035208818 - Show all commits

View File

@ -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<HsBookingItemRbacEntity, HsBookingItemResource> RBAC_ENTITY_TO_RESOURCE_POSTMAPPER = ITEM_TO_RESOURCE_POSTMAPPER::accept;
final BiConsumer<HsBookingItemInsertResource, HsBookingItemRbacEntity> 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()));
};

View File

@ -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);

View File

@ -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(