use @PersistenceContext for EntityManager, not @Autowired

This commit is contained in:
Michael Hoennig 2022-10-27 11:00:12 +02:00
parent 1d0d2372ea
commit dc1a17e915
42 changed files with 160 additions and 67 deletions

View File

@ -32,6 +32,7 @@ public class Context {
"content-length",
"host",
"user-agent");
@PersistenceContext
private EntityManager em;

View File

@ -35,17 +35,17 @@ class CustomErrorResponse {
private final String path;
private final int status;
private final int statusCode;
private final String error;
private final String statusPhrase;
private final String message;
CustomErrorResponse(final String path, final HttpStatus status, final String message) {
this.timestamp = LocalDateTime.now();
this.path = path;
this.status = status.value();
this.error = status.getReasonPhrase();
this.statusCode = status.value();
this.statusPhrase = status.getReasonPhrase();
this.message = message;
}
}

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.UUID;
@ -29,7 +30,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
@Autowired
private HsOfficeDebitorRepository debitorRepo;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
@ -31,7 +32,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
@Autowired
private HsOfficeMembershipRepository membershipRepo;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.UUID;
@ -30,7 +31,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
@Autowired
private HsOfficePartnerRepository partnerRepo;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
@ -38,7 +39,7 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
@Autowired
private HsOfficeContactRepository contactRepo;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
@ -33,7 +34,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
@Autowired
private HsOfficeSepaMandateRepository SepaMandateRepo;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -7,6 +7,7 @@ import org.springframework.util.ReflectionUtils;
import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.validation.ValidationException;
import java.lang.reflect.Field;
import java.util.List;
@ -18,7 +19,7 @@ import java.util.stream.Collectors;
*/
public class Mapper extends ModelMapper {
@Autowired
@PersistenceContext
EntityManager em;
public Mapper() {

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.UUID;
@ -26,7 +27,7 @@ public class RbacGrantController implements RbacGrantsApi {
@Autowired
private RbacGrantRepository rbacGrantRepository;
@Autowired
@PersistenceContext
private EntityManager em;
@Override

View File

@ -16,6 +16,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -43,7 +44,7 @@ class HsOfficeBankAccountControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -75,8 +75,8 @@ class HsOfficeBankAccountControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage())));
}
@ -122,8 +122,8 @@ class HsOfficeBankAccountControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage())));
}
}

View File

@ -22,6 +22,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.junit.jupiter.Container;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
@ -46,7 +47,7 @@ class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -18,6 +18,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -48,7 +49,7 @@ class HsOfficeContactControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -20,6 +20,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
@ -45,7 +46,7 @@ class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -19,6 +19,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate;
import java.util.UUID;
@ -36,7 +37,7 @@ import static org.hamcrest.Matchers.startsWith;
class HsOfficeCoopAssetsTransactionControllerAcceptanceTest {
@LocalServerPort
private Integer port;
Integer port;
@Autowired
Context context;
@ -50,7 +51,7 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested
@ -225,8 +226,8 @@ class HsOfficeCoopAssetsTransactionControllerAcceptanceTest {
.contentType(ContentType.JSON)
.body("", lenientlyEquals("""
{
"status": 400,
"error": "Bad Request",
"statusCode": 400,
"statusPhrase": "Bad Request",
"message": "ERROR: [400] coop assets transaction would result in a negative balance of assets"
}
""")); // @formatter:on

View File

@ -122,8 +122,8 @@ class HsOfficeCoopAssetsTransactionControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage)));
}

View File

@ -20,6 +20,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
@ -48,7 +49,7 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -17,6 +17,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate;
import java.util.UUID;
@ -32,16 +33,19 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
@Autowired
Context context;
@Autowired
Context contextMock;
@Autowired
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
@Autowired
HsOfficeMembershipRepository membershipRepo;
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@LocalServerPort
private Integer port;
@ -175,8 +179,8 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
}
""".formatted(givenMembership.getUuid())).port(port).when().post("http://localhost/api/hs/office/coopsharestransactions").then().log().all().assertThat().statusCode(400).contentType(ContentType.JSON).body("", lenientlyEquals("""
{
"status": 400,
"error": "Bad Request",
"statusCode": 400,
"statusPhrase": "Bad Request",
"message": "ERROR: [400] coop shares transaction would result in a negative number of shares"
}
""")); // @formatter:on

View File

@ -118,8 +118,8 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage)));
}

View File

@ -19,6 +19,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Arrays;
@ -46,7 +47,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -20,6 +20,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -59,7 +60,7 @@ class HsOfficeDebitorControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -24,6 +24,7 @@ import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@ -54,7 +55,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -20,6 +20,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate;
import java.util.UUID;
@ -57,7 +58,7 @@ class HsOfficeMembershipControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
private static int tempMemberNumber = 20010;

View File

@ -5,9 +5,14 @@ import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransact
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
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.orm.jpa.AutoConfigureTestEntityManager;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
@ -17,9 +22,13 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SynchronizationType;
import java.util.Map;
import java.util.UUID;
import static org.hamcrest.Matchers.is;
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.jsonPath;
@ -27,7 +36,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(HsOfficeMembershipController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
public class HsOfficeMembershipControllerRestTest {
@Autowired
@ -36,15 +44,26 @@ public class HsOfficeMembershipControllerRestTest {
@MockBean
Context contextMock;
@MockBean
EntityManager em;
@MockBean
HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;
@MockBean
HsOfficeMembershipRepository membershipRepo;
@Mock
EntityManager em;
@MockBean
EntityManagerFactory emf;
@BeforeEach
void init() {
when(emf.createEntityManager()).thenReturn(em);
when(emf.createEntityManager(any(Map.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class), any(Map.class))).thenReturn(em);
}
@Test
void respondBadRequest_ifPartnerUuidIsMissing() throws Exception {
@ -65,8 +84,8 @@ public class HsOfficeMembershipControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("[partnerUuid must not be null but is \"null\"]")));
}
@ -90,8 +109,8 @@ public class HsOfficeMembershipControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("[mainDebitorUuid must not be null but is \"null\"]")));
}
@ -121,8 +140,8 @@ public class HsOfficeMembershipControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("Unable to find Partner with uuid " + givenPartnerUuid)));
}
@ -152,8 +171,8 @@ public class HsOfficeMembershipControllerRestTest {
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("Unable to find Debitor with uuid " + givenMainDebitorUuid)));
}
}

View File

@ -22,6 +22,7 @@ import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.*;
@ -51,7 +52,7 @@ class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -18,6 +18,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -54,7 +55,7 @@ class HsOfficePartnerControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -20,6 +20,7 @@ import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@ -48,7 +49,7 @@ class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -17,6 +17,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
@ -46,7 +47,7 @@ class HsOfficePersonControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -18,6 +18,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
@ -43,7 +44,7 @@ class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -18,6 +18,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@ -45,7 +46,7 @@ class HsOfficeRelationshipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RawRbacGrantRepository rawGrantRepo;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -20,6 +20,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate;
import java.util.UUID;
@ -56,7 +57,7 @@ class HsOfficeSepaMandateControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -21,6 +21,7 @@ import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Arrays;
@ -54,7 +55,7 @@ class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@MockBean

View File

@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.UUID;
@ -41,7 +42,7 @@ class RbacGrantControllerAcceptanceTest extends ContextBasedTest {
@LocalServerPort
Integer port;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -20,6 +20,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;
@ -50,7 +51,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
RbacRoleRepository rbacRoleRepository;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -11,6 +11,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import static org.hamcrest.Matchers.*;
@ -24,7 +25,7 @@ class RbacRoleControllerAcceptanceTest {
@LocalServerPort
private Integer port;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -2,8 +2,10 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
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.mock.mockito.MockBean;
@ -14,11 +16,16 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SynchronizationType;
import java.util.Map;
import static java.util.Arrays.asList;
import static net.hostsharing.hsadminng.rbac.rbacrole.TestRbacRole.*;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -37,9 +44,20 @@ class RbacRoleControllerRestTest {
@MockBean
RbacRoleRepository rbacRoleRepository;
@MockBean
@Mock
EntityManager em;
@MockBean
EntityManagerFactory emf;
@BeforeEach
void init() {
when(emf.createEntityManager()).thenReturn(em);
when(emf.createEntityManager(any(Map.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class), any(Map.class))).thenReturn(em);
}
@Test
void apiCustomersWillReturnCustomersFromRepository() throws Exception {

View File

@ -14,6 +14,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@ -29,7 +30,7 @@ class RbacUserControllerAcceptanceTest {
@LocalServerPort
private Integer port;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired

View File

@ -2,8 +2,10 @@ package net.hostsharing.hsadminng.rbac.rbacuser;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
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.mock.mockito.MockBean;
@ -14,12 +16,17 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SynchronizationType;
import java.util.Map;
import java.util.UUID;
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -37,9 +44,20 @@ class RbacUserControllerRestTest {
@MockBean
RbacUserRepository rbacUserRepository;
@MockBean
@Mock
EntityManager em;
@MockBean
EntityManagerFactory emf;
@BeforeEach
void init() {
when(emf.createEntityManager()).thenReturn(em);
when(emf.createEntityManager(any(Map.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class), any(Map.class))).thenReturn(em);
}
@Test
void createUserUsesGivenUuid() throws Exception {
// given

View File

@ -17,6 +17,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;
@ -34,7 +35,7 @@ class RbacUserRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@MockBean

View File

@ -15,6 +15,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,7 +44,7 @@ class TestCustomerControllerAcceptanceTest {
@Autowired
JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em;
@Nested

View File

@ -11,6 +11,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@ -26,7 +27,7 @@ class TestCustomerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
TestCustomerRepository testCustomerRepository;
@Autowired
@PersistenceContext
EntityManager em;
@MockBean

View File

@ -14,6 +14,7 @@ import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@ -29,7 +30,7 @@ class TestPackageRepositoryIntegrationTest {
@Autowired
TestPackageRepository testPackageRepository;
@Autowired
@PersistenceContext
EntityManager em;
@Autowired