From dc1a17e9158cf86fcfc255d41bf5bfb236abf083 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 27 Oct 2022 11:00:12 +0200 Subject: [PATCH] use @PersistenceContext for EntityManager, not @Autowired --- .../hsadminng/context/Context.java | 1 + .../hsadminng/errors/CustomErrorResponse.java | 8 ++-- .../debitor/HsOfficeDebitorController.java | 3 +- .../HsOfficeMembershipController.java | 3 +- .../partner/HsOfficePartnerController.java | 3 +- .../HsOfficeRelationshipController.java | 3 +- .../HsOfficeSepaMandateController.java | 3 +- .../hostsharing/hsadminng/mapper/Mapper.java | 3 +- .../rbac/rbacgrant/RbacGrantController.java | 3 +- ...ceBankAccountControllerAcceptanceTest.java | 3 +- ...HsOfficeBankAccountControllerRestTest.java | 8 ++-- ...eBankAccountRepositoryIntegrationTest.java | 3 +- ...OfficeContactControllerAcceptanceTest.java | 3 +- ...fficeContactRepositoryIntegrationTest.java | 3 +- ...tsTransactionControllerAcceptanceTest.java | 9 ++-- ...opAssetsTransactionControllerRestTest.java | 4 +- ...sTransactionRepositoryIntegrationTest.java | 3 +- ...esTransactionControllerAcceptanceTest.java | 14 +++--- ...opSharesTransactionControllerRestTest.java | 4 +- ...sTransactionRepositoryIntegrationTest.java | 3 +- ...OfficeDebitorControllerAcceptanceTest.java | 3 +- ...fficeDebitorRepositoryIntegrationTest.java | 3 +- ...iceMembershipControllerAcceptanceTest.java | 3 +- .../HsOfficeMembershipControllerRestTest.java | 43 +++++++++++++------ ...ceMembershipRepositoryIntegrationTest.java | 3 +- ...OfficePartnerControllerAcceptanceTest.java | 3 +- ...fficePartnerRepositoryIntegrationTest.java | 3 +- ...sOfficePersonControllerAcceptanceTest.java | 3 +- ...OfficePersonRepositoryIntegrationTest.java | 3 +- ...RelationshipRepositoryIntegrationTest.java | 3 +- ...ceSepaMandateControllerAcceptanceTest.java | 3 +- ...eSepaMandateRepositoryIntegrationTest.java | 3 +- .../RbacGrantControllerAcceptanceTest.java | 3 +- .../RbacGrantRepositoryIntegrationTest.java | 3 +- .../RbacRoleControllerAcceptanceTest.java | 3 +- .../rbacrole/RbacRoleControllerRestTest.java | 20 ++++++++- .../RbacUserControllerAcceptanceTest.java | 3 +- .../rbacuser/RbacUserControllerRestTest.java | 20 ++++++++- .../RbacUserRepositoryIntegrationTest.java | 3 +- .../TestCustomerControllerAcceptanceTest.java | 3 +- ...TestCustomerRepositoryIntegrationTest.java | 3 +- .../TestPackageRepositoryIntegrationTest.java | 3 +- 42 files changed, 160 insertions(+), 67 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/context/Context.java b/src/main/java/net/hostsharing/hsadminng/context/Context.java index b0c63fb4..21424069 100644 --- a/src/main/java/net/hostsharing/hsadminng/context/Context.java +++ b/src/main/java/net/hostsharing/hsadminng/context/Context.java @@ -32,6 +32,7 @@ public class Context { "content-length", "host", "user-agent"); + @PersistenceContext private EntityManager em; diff --git a/src/main/java/net/hostsharing/hsadminng/errors/CustomErrorResponse.java b/src/main/java/net/hostsharing/hsadminng/errors/CustomErrorResponse.java index 97fc6ca0..2714b817 100644 --- a/src/main/java/net/hostsharing/hsadminng/errors/CustomErrorResponse.java +++ b/src/main/java/net/hostsharing/hsadminng/errors/CustomErrorResponse.java @@ -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; } } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java index c2f80e61..4be8d611 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java @@ -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 diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java index 3e9c4c22..c10ff273 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java @@ -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 diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java index a5a82da2..5f86164f 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerController.java @@ -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 diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipController.java index 41855c84..62b3fc9f 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipController.java @@ -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 diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java index fc5124ec..60983c1b 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java @@ -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 diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java index e625a874..42a63674 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java @@ -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() { diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantController.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantController.java index 7a11ff42..901e16e4 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantController.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantController.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java index ed6741a8..347976c9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerRestTest.java index 8705b95d..a54ca5c6 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountControllerRestTest.java @@ -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()))); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java index 10b0252c..703b4af5 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java index f832b245..6067a9cc 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java index 2c4bac6e..7bba6c18 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java index 6b768d3b..0f981266 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerRestTest.java index e30dfe00..043404dd 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionControllerRestTest.java @@ -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))); } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java index 44f5ea6b..e6610002 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java index aa7f24bb..85bdc5a5 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerRestTest.java index 8caa0e13..c1b4307b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerRestTest.java @@ -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))); } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java index 87a2f2dc..6bb53532 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java index 2867c6fa..5c3b08e9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java index 8be99fdc..da34ccc9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java index dd854262..bb54995b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java @@ -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; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java index b948d5f5..c848cb0a 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java @@ -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))); } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java index f54d5fa0..8cd5be18 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java index bc1f1e43..3e1a7797 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java index 0213c09b..af755021 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java index f92c18ff..b063932a 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java index ba46b8a4..edc5a63d 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipRepositoryIntegrationTest.java index e227ebc0..365b1be0 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/relationship/HsOfficeRelationshipRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java index 2f0c2754..343325ae 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java index 4a6a8d0f..818c7bc4 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java index 47d13956..ab230ef2 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java index 94374aa0..5f51fde1 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerAcceptanceTest.java index 0412b332..f24c1986 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerRestTest.java index f3ea4285..186a367a 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleControllerRestTest.java @@ -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 { diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerAcceptanceTest.java index b6aee5f0..cafa9780 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerRestTest.java index e24e2edd..282f8238 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserControllerRestTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserRepositoryIntegrationTest.java index 07e2921c..95e773c6 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacuser/RbacUserRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerControllerAcceptanceTest.java index 296fc29b..143e370a 100644 --- a/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerControllerAcceptanceTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerRepositoryIntegrationTest.java index 6d3471f1..ef421220 100644 --- a/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/test/cust/TestCustomerRepositoryIntegrationTest.java @@ -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 diff --git a/src/test/java/net/hostsharing/hsadminng/test/pac/TestPackageRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/test/pac/TestPackageRepositoryIntegrationTest.java index 075925fa..1b25cf7d 100644 --- a/src/test/java/net/hostsharing/hsadminng/test/pac/TestPackageRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/test/pac/TestPackageRepositoryIntegrationTest.java @@ -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