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", "content-length",
"host", "host",
"user-agent"); "user-agent");
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;

View File

@ -35,17 +35,17 @@ class CustomErrorResponse {
private final String path; private final String path;
private final int status; private final int statusCode;
private final String error; private final String statusPhrase;
private final String message; private final String message;
CustomErrorResponse(final String path, final HttpStatus status, final String message) { CustomErrorResponse(final String path, final HttpStatus status, final String message) {
this.timestamp = LocalDateTime.now(); this.timestamp = LocalDateTime.now();
this.path = path; this.path = path;
this.status = status.value(); this.statusCode = status.value();
this.error = status.getReasonPhrase(); this.statusPhrase = status.getReasonPhrase();
this.message = message; 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 org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -29,7 +30,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
@Autowired @Autowired
private HsOfficeDebitorRepository debitorRepo; private HsOfficeDebitorRepository debitorRepo;
@Autowired @PersistenceContext
private EntityManager em; private EntityManager em;
@Override @Override

View File

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

View File

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

View File

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

View File

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

View File

@ -7,6 +7,7 @@ import org.springframework.util.ReflectionUtils;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.validation.ValidationException; import javax.validation.ValidationException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
@ -18,7 +19,7 @@ import java.util.stream.Collectors;
*/ */
public class Mapper extends ModelMapper { public class Mapper extends ModelMapper {
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
public Mapper() { 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 org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -26,7 +27,7 @@ public class RbacGrantController implements RbacGrantsApi {
@Autowired @Autowired
private RbacGrantRepository rbacGrantRepository; private RbacGrantRepository rbacGrantRepository;
@Autowired @PersistenceContext
private EntityManager em; private EntityManager em;
@Override @Override

View File

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

View File

@ -75,8 +75,8 @@ class HsOfficeBankAccountControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage()))); .andExpect(jsonPath("message", is(testCase.expectedErrorMessage())));
} }
@ -122,8 +122,8 @@ class HsOfficeBankAccountControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage()))); .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 org.testcontainers.junit.jupiter.Container;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -46,7 +47,7 @@ class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
RawRbacGrantRepository rawGrantRepo; RawRbacGrantRepository rawGrantRepo;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
@Autowired @Autowired

View File

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

View File

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

View File

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

View File

@ -122,8 +122,8 @@ class HsOfficeCoopAssetsTransactionControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage))); .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 org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDate; import java.time.LocalDate;
@ -48,7 +49,7 @@ class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBase
@Autowired @Autowired
RawRbacGrantRepository rawGrantRepo; RawRbacGrantRepository rawGrantRepo;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
@Autowired @Autowired

View File

@ -17,6 +17,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID; import java.util.UUID;
@ -32,16 +33,19 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
@Autowired @Autowired
Context context; Context context;
@Autowired
Context contextMock;
@Autowired @Autowired
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo; HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
@Autowired @Autowired
HsOfficeMembershipRepository membershipRepo; HsOfficeMembershipRepository membershipRepo;
@Autowired @Autowired
JpaAttempt jpaAttempt; JpaAttempt jpaAttempt;
@Autowired
@PersistenceContext
EntityManager em; EntityManager em;
@LocalServerPort @LocalServerPort
private Integer port; 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(""" """.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, "statusCode": 400,
"error": "Bad Request", "statusPhrase": "Bad Request",
"message": "ERROR: [400] coop shares transaction would result in a negative number of shares" "message": "ERROR: [400] coop shares transaction would result in a negative number of shares"
} }
""")); // @formatter:on """)); // @formatter:on

View File

@ -118,8 +118,8 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage))); .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 org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
@ -46,7 +47,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
@Autowired @Autowired
RawRbacGrantRepository rawGrantRepo; RawRbacGrantRepository rawGrantRepo;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
@Autowired @Autowired

View File

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

View File

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

View File

@ -20,6 +20,7 @@ import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID; import java.util.UUID;
@ -57,7 +58,7 @@ class HsOfficeMembershipControllerAcceptanceTest {
@Autowired @Autowired
JpaAttempt jpaAttempt; JpaAttempt jpaAttempt;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
private static int tempMemberNumber = 20010; 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.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity; import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import net.hostsharing.hsadminng.mapper.Mapper; import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired; 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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import; 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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SynchronizationType;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; 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.jsonPath;
@ -27,7 +36,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(HsOfficeMembershipController.class) @WebMvcTest(HsOfficeMembershipController.class)
@Import(Mapper.class) @Import(Mapper.class)
@RunWith(SpringRunner.class)
public class HsOfficeMembershipControllerRestTest { public class HsOfficeMembershipControllerRestTest {
@Autowired @Autowired
@ -36,15 +44,26 @@ public class HsOfficeMembershipControllerRestTest {
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean
EntityManager em;
@MockBean @MockBean
HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo; HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;
@MockBean @MockBean
HsOfficeMembershipRepository membershipRepo; 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 @Test
void respondBadRequest_ifPartnerUuidIsMissing() throws Exception { void respondBadRequest_ifPartnerUuidIsMissing() throws Exception {
@ -65,8 +84,8 @@ public class HsOfficeMembershipControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("[partnerUuid must not be null but is \"null\"]"))); .andExpect(jsonPath("message", is("[partnerUuid must not be null but is \"null\"]")));
} }
@ -90,8 +109,8 @@ public class HsOfficeMembershipControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("[mainDebitorUuid must not be null but is \"null\"]"))); .andExpect(jsonPath("message", is("[mainDebitorUuid must not be null but is \"null\"]")));
} }
@ -121,8 +140,8 @@ public class HsOfficeMembershipControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("Unable to find Partner with uuid " + givenPartnerUuid))); .andExpect(jsonPath("message", is("Unable to find Partner with uuid " + givenPartnerUuid)));
} }
@ -152,8 +171,8 @@ public class HsOfficeMembershipControllerRestTest {
// then // then
.andExpect(status().is4xxClientError()) .andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400))) .andExpect(jsonPath("statusCode", is(400)))
.andExpect(jsonPath("error", is("Bad Request"))) .andExpect(jsonPath("statusPhrase", is("Bad Request")))
.andExpect(jsonPath("message", is("Unable to find Debitor with uuid " + givenMainDebitorUuid))); .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 org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.*; import java.util.*;
@ -51,7 +52,7 @@ class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
RawRbacGrantRepository rawGrantRepo; RawRbacGrantRepository rawGrantRepo;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
@Autowired @Autowired

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,8 +2,10 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper; import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; 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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.SynchronizationType;
import java.util.Map;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static net.hostsharing.hsadminng.rbac.rbacrole.TestRbacRole.*; import static net.hostsharing.hsadminng.rbac.rbacrole.TestRbacRole.*;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; 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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -37,9 +44,20 @@ class RbacRoleControllerRestTest {
@MockBean @MockBean
RbacRoleRepository rbacRoleRepository; RbacRoleRepository rbacRoleRepository;
@MockBean @Mock
EntityManager em; 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 @Test
void apiCustomersWillReturnCustomersFromRepository() throws Exception { 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 org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.UUID; import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -29,7 +30,7 @@ class RbacUserControllerAcceptanceTest {
@LocalServerPort @LocalServerPort
private Integer port; private Integer port;
@Autowired @PersistenceContext
EntityManager em; EntityManager em;
@Autowired @Autowired

View File

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

View File

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

View File

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

View File

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

View File

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