1
0

ModelMapper as Spring Bean

This commit is contained in:
Michael Hoennig
2022-10-23 09:58:52 +02:00
parent 0b0f57c176
commit 6f3c03e6b6
45 changed files with 329 additions and 280 deletions

View File

@@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.bankaccount;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.Accepts;
import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context;
@@ -36,9 +37,6 @@ class HsOfficeBankAccountControllerAcceptanceTest {
@Autowired
Context context;
@Autowired
Context contextMock;
@Autowired
HsOfficeBankAccountRepository bankAccountRepo;

View File

@@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.bankaccount;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +24,9 @@ class HsOfficeBankAccountControllerRestTest {
@MockBean
Context contextMock;
@MockBean
Mapper mapper;
@MockBean
HsOfficeBankAccountRepository bankAccountRepo;

View File

@@ -6,16 +6,19 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.junit.jupiter.Container;
import javax.persistence.EntityManager;
@@ -31,8 +34,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeBankAccountRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
@@ -311,7 +313,7 @@ class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
}
private HsOfficeBankAccountEntity givenSomeTemporaryBankAccount(final String createdByUser) {
final var random = RandomString.make(3);
final var random = RandomStringUtils.randomAlphabetic(3);
return givenSomeTemporaryBankAccount(createdByUser, () ->
hsOfficeBankAccount(
"some temp acc #" + random,

View File

@@ -6,15 +6,16 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container;
@@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeContactRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
@@ -305,7 +305,7 @@ class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest {
}
private HsOfficeContactEntity givenSomeTemporaryContact(final String createdByUser) {
final var random = RandomString.make(12);
final var random = RandomStringUtils.randomAlphabetic(12);
return givenSomeTemporaryContact(createdByUser, () ->
hsOfficeContact(
"some temporary contact #" + random,

View File

@@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.coopassets;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.JsonBuilder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
@@ -28,6 +29,9 @@ class HsOfficeCoopAssetsTransactionControllerRestTest {
@MockBean
Context contextMock;
@MockBean
Mapper mapper;
@MockBean
HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;

View File

@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
@@ -32,8 +33,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeCoopAssetsTransactionRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.coopshares;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.JsonBuilder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
@@ -28,6 +29,9 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
@MockBean
Context contextMock;
@MockBean
Mapper mapper;
@MockBean
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;

View File

@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
@@ -30,8 +31,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeCoopSharesTransactionRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -43,9 +43,6 @@ class HsOfficeDebitorControllerAcceptanceTest {
@Autowired
Context context;
@Autowired
Context contextMock;
@Autowired
HsOfficeDebitorRepository debitorRepo;

View File

@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -32,8 +33,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeDebitorRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -2,12 +2,17 @@ package net.hostsharing.hsadminng.hs.office.membership;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionRepository;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -22,6 +27,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HsOfficeMembershipController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
public class HsOfficeMembershipControllerRestTest {
@Autowired

View File

@@ -4,12 +4,14 @@ import com.vladmihalcea.hibernate.type.range.Range;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeReasonForTerminationResource;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.PatchUnitTestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManager;
import java.time.LocalDate;
@@ -31,7 +33,6 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
> {
private static final UUID INITIAL_MEMBERSHIP_UUID = UUID.randomUUID();
private static final UUID INITIAL_MAIN_DEBITOR_UUID = UUID.randomUUID();
private static final UUID PATCHED_MAIN_DEBITOR_UUID = UUID.randomUUID();
private static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-04-15");
private static final LocalDate PATCHED_VALID_TO = LocalDate.parse("2022-12-31");
@@ -39,6 +40,8 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
@Mock
private EntityManager em;
private Mapper mapper = new Mapper();
@BeforeEach
void initMocks() {
lenient().when(em.getReference(eq(HsOfficeDebitorEntity.class), any())).thenAnswer(invocation ->
@@ -63,8 +66,8 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
}
@Override
protected HsOfficeMembershipEntityPatcher createPatcher(final HsOfficeMembershipEntity Membership) {
return new HsOfficeMembershipEntityPatcher(em, Membership);
protected HsOfficeMembershipEntityPatcher createPatcher(final HsOfficeMembershipEntity membership) {
return new HsOfficeMembershipEntityPatcher(em, mapper, membership);
}
@Override

View File

@@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeMembershipRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -2,11 +2,11 @@ package net.hostsharing.hsadminng.hs.office.partner;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import net.hostsharing.test.Accepts;
import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
import net.hostsharing.test.Accepts;
import net.hostsharing.test.JpaAttempt;
import org.json.JSONException;
import org.junit.jupiter.api.AfterEach;
@@ -166,15 +166,16 @@ class HsOfficePartnerControllerAcceptanceTest {
.body("""
{
"contactUuid": "%s",
"personUuid": "%s"
"personUuid": "%s",
"details": {}
}
""".formatted(givenContactUuid, givenPerson.getUuid()))
.port(port)
.when()
.post("http://localhost/api/hs/office/partners")
.then().log().all().assertThat()
.statusCode(404)
.body("message", is("cannot find contact uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
.statusCode(400)
.body("message", is("Unable to find Contact with uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
// @formatter:on
}
@@ -193,19 +194,15 @@ class HsOfficePartnerControllerAcceptanceTest {
{
"contactUuid": "%s",
"personUuid": "%s",
"registrationOffice": "Registergericht Hamburg",
"registrationNumber": "123456",
"birthName": null,
"birthday": null,
"dateOfDeath": null
"details": {}
}
""".formatted(givenContact.getUuid(), givenPersonUuid))
.port(port)
.when()
.post("http://localhost/api/hs/office/partners")
.then().log().all().assertThat()
.statusCode(404)
.body("message", is("cannot find person uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
.statusCode(400)
.body("message", is("Unable to find Person with uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
// @formatter:on
}
}

View File

@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -29,8 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficePartnerRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -6,14 +6,15 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
@@ -30,8 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficePersonRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
@Autowired
@@ -304,7 +304,7 @@ class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
private HsOfficePersonEntity givenSomeTemporaryPerson(final String createdByUser) {
return givenSomeTemporaryPerson(createdByUser, () ->
hsOfficePerson("some temporary person #" + RandomString.make(12)));
hsOfficePerson("some temporary person #" + RandomStringUtils.random(12)));
}
void exactlyThesePersonsAreReturned(final List<HsOfficePersonEntity> actualResult, final String... personLabels) {

View File

@@ -1,5 +1,6 @@
package net.hostsharing.hsadminng.hs.office.relationship;
import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.context.ContextBasedTest;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
@@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -28,8 +30,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeRelationshipRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeRelationshipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeSepaMandateRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Propagation;
@@ -28,8 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { RbacGrantRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -1,11 +1,15 @@
package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -18,12 +22,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(RbacRoleController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
class RbacRoleControllerRestTest {
@Autowired
MockMvc mockMvc;
@MockBean
Context contextMock;
@MockBean
RbacRoleRepository rbacRoleRepository;
@@ -32,23 +40,23 @@ class RbacRoleControllerRestTest {
// given
when(rbacRoleRepository.findAll()).thenReturn(
asList(hostmasterRole, customerXxxOwner, customerXxxAdmin));
asList(hostmasterRole, customerXxxOwner, customerXxxAdmin));
// when
mockMvc.perform(MockMvcRequestBuilders
.get("/api/rbac/roles")
.header("current-user", "superuser-alex@hostsharing.net")
.accept(MediaType.APPLICATION_JSON))
.get("/api/rbac/roles")
.header("current-user", "superuser-alex@hostsharing.net")
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].roleName", is("global#global.admin")))
.andExpect(jsonPath("$[1].roleName", is("test_customer#xxx.owner")))
.andExpect(jsonPath("$[2].roleName", is("test_customer#xxx.admin")))
.andExpect(jsonPath("$[2].uuid", is(customerXxxAdmin.getUuid().toString())))
.andExpect(jsonPath("$[2].objectUuid", is(customerXxxAdmin.getObjectUuid().toString())))
.andExpect(jsonPath("$[2].objectTable", is(customerXxxAdmin.getObjectTable().toString())))
.andExpect(jsonPath("$[2].objectIdName", is(customerXxxAdmin.getObjectIdName().toString())));
// then
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].roleName", is("global#global.admin")))
.andExpect(jsonPath("$[1].roleName", is("test_customer#xxx.owner")))
.andExpect(jsonPath("$[2].roleName", is("test_customer#xxx.admin")))
.andExpect(jsonPath("$[2].uuid", is(customerXxxAdmin.getUuid().toString())))
.andExpect(jsonPath("$[2].objectUuid", is(customerXxxAdmin.getObjectUuid().toString())))
.andExpect(jsonPath("$[2].objectTable", is(customerXxxAdmin.getObjectTable().toString())))
.andExpect(jsonPath("$[2].objectIdName", is(customerXxxAdmin.getObjectIdName().toString())));
}
}

View File

@@ -2,12 +2,14 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
@@ -19,8 +21,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, RbacRoleRepository.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class RbacRoleRepositoryIntegrationTest {
@Autowired

View File

@@ -1,11 +1,15 @@
package net.hostsharing.hsadminng.rbac.rbacuser;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -19,12 +23,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(RbacUserController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
class RbacUserControllerRestTest {
@Autowired
MockMvc mockMvc;
@MockBean
Context contextMock;
@MockBean
RbacUserRepository rbacUserRepository;

View File

@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Propagation;
@@ -24,8 +25,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { RbacUserRepository.class, Context.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class RbacUserRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -2,12 +2,14 @@ package net.hostsharing.hsadminng.test.cust;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.context.ContextBasedTest;
import net.hostsharing.test.JpaAttempt;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager;
@@ -20,8 +22,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, TestCustomerRepository.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class TestCustomerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired

View File

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.test.annotation.DirtiesContext;
@@ -19,8 +20,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, TestCustomerRepository.class, JpaAttempt.class })
@DirtiesContext
@Import( { Context.class, JpaAttempt.class })
class TestPackageRepositoryIntegrationTest {
@Autowired

View File

@@ -7,16 +7,21 @@ import lombok.Setter;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;
class MapperUnitTest {
private Mapper mapper = new Mapper();
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class SourceBean {
private String a;
private String b;
}
@@ -26,6 +31,7 @@ class MapperUnitTest {
@NoArgsConstructor
@AllArgsConstructor
public static class TargetBean {
private String a;
private String b;
private String c;
@@ -34,14 +40,14 @@ class MapperUnitTest {
@Test
void mapsNullBeanToNull() {
final SourceBean givenSource = null;
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { fail("should not have been called"); });
final var result = mapper.map(givenSource, TargetBean.class, (s, t) -> {fail("should not have been called");});
assertThat(result).isNull();
}
@Test
void mapsBean() {
final SourceBean givenSource = new SourceBean("1234", "Text");
final var result = Mapper.map(givenSource, TargetBean.class, null);
final var result = mapper.map(givenSource, TargetBean.class);
assertThat(result).usingRecursiveComparison().isEqualTo(
new TargetBean("1234", "Text", null)
);
@@ -50,9 +56,38 @@ class MapperUnitTest {
@Test
void mapsBeanWithPostmapper() {
final SourceBean givenSource = new SourceBean("1234", "Text");
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { t.setC("Extra"); });
final var result = mapper.map(givenSource, TargetBean.class, (s, t) -> {t.setC("Extra");});
assertThat(result).usingRecursiveComparison().isEqualTo(
new TargetBean("1234", "Text", "Extra")
);
}
@Test
void mapsList() {
final var givenSource = List.of(
new SourceBean("111", "Text A"),
new SourceBean("222", "Text B"),
new SourceBean("333", "Text C"));
final var result = mapper.mapList(givenSource, TargetBean.class);
assertThat(result).usingRecursiveComparison().isEqualTo(
List.of(
new TargetBean("111", "Text A", null),
new TargetBean("222", "Text B", null),
new TargetBean("333", "Text C", null)));
}
@Test
void mapsListWithPostMapper() {
final var givenSource = List.of(
new SourceBean("111", "Text A"),
new SourceBean("222", "Text B"),
new SourceBean("333", "Text C"));
final var result = mapper.mapList(givenSource, TargetBean.class, (s, t) -> {t.setC("Extra");});
assertThat(result).usingRecursiveComparison().isEqualTo(
List.of(
new TargetBean("111", "Text A", "Extra"),
new TargetBean("222", "Text B", "Extra"),
new TargetBean("333", "Text C", "Extra")));
}
}