introduce-booking-module (#41)
Co-authored-by: Michael Hoennig <michael@hoennig.de> Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/41 Reviewed-by: Marc Sandlus <marc.sandlus@hostsharing.net>
This commit is contained in:
@ -50,6 +50,7 @@ public class ArchitectureTest {
|
||||
"..hs.office.person",
|
||||
"..hs.office.relation",
|
||||
"..hs.office.sepamandate",
|
||||
"..hs.booking.item",
|
||||
"..errors",
|
||||
"..mapper",
|
||||
"..ping",
|
||||
@ -123,11 +124,22 @@ public class ArchitectureTest {
|
||||
|
||||
@ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsAdminPackagesRule = classes()
|
||||
public static final ArchRule hsOfficePackageAccessRule = classes()
|
||||
.that().resideInAPackage("..hs.office.(*)..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage(
|
||||
"..hs.office.(*)..",
|
||||
"..hs.booking.(*)..",
|
||||
"..rbac.rbacgrant" // TODO.test: just because of RbacGrantsDiagramServiceIntegrationTest
|
||||
);
|
||||
|
||||
@ArchTest
|
||||
@SuppressWarnings("unused")
|
||||
public static final ArchRule hsBookingPackageAccessRule = classes()
|
||||
.that().resideInAPackage("..hs.booking.(*)..")
|
||||
.should().onlyBeAccessed().byClassesThat()
|
||||
.resideInAnyPackage(
|
||||
"..hs.booking.(*)..",
|
||||
"..rbac.rbacgrant" // TODO.test: just because of RbacGrantsDiagramServiceIntegrationTest
|
||||
);
|
||||
|
||||
|
@ -0,0 +1,340 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.range.Range;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository;
|
||||
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
|
||||
import net.hostsharing.hsadminng.rbac.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.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = { HsadminNgApplication.class, JpaAttempt.class }
|
||||
)
|
||||
@Transactional
|
||||
class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup {
|
||||
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Autowired
|
||||
HsBookingItemRepository bookingItemRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficeDebitorRepository debitorRepo;
|
||||
|
||||
@Autowired
|
||||
JpaAttempt jpaAttempt;
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
|
||||
@Nested
|
||||
class ListBookingItems {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canViewAllBookingItemsOfArbitraryDebitor() {
|
||||
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenDebitor = debitorRepo.findDebitorByDebitorNumber(1000111).get(0);
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/booking/items?debitorUuid=" + givenDebitor.getUuid())
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
[
|
||||
{
|
||||
"caption": "some ManagedServer",
|
||||
"validFrom": "2022-10-01",
|
||||
"validTo": null,
|
||||
"resources": {
|
||||
"CPU": 2,
|
||||
"SDD": 512,
|
||||
"extra": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": {
|
||||
"CPU": 2,
|
||||
"HDD": 1024,
|
||||
"extra": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"caption": "some Whatever",
|
||||
"validFrom": "2024-04-01",
|
||||
"validTo": null,
|
||||
"resources": {
|
||||
"CPU": 1,
|
||||
"HDD": 2048,
|
||||
"SDD": 512,
|
||||
"extra": 42
|
||||
}
|
||||
}
|
||||
]
|
||||
"""));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class AddBookingItem {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canAddBookingItem() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenDebitor = debitorRepo.findDebitorByDebitorNumber(1000111).get(0);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"debitorUuid": "%s",
|
||||
"caption": "some new booking",
|
||||
"resources": { "CPU": 12, "extra": 42 },
|
||||
"validFrom": "2022-10-13"
|
||||
}
|
||||
""".formatted(givenDebitor.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/booking/items")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"caption": "some new booking",
|
||||
"validFrom": "2022-10-13",
|
||||
"validTo": null,
|
||||
"resources": { "CPU": 12 }
|
||||
}
|
||||
"""))
|
||||
.header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/booking/items/[^/]*"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new bookingItem can be accessed under the generated UUID
|
||||
final var newUserUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetBookingItem {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canGetArbitraryBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItemUuid = bookingItemRepo.findAll().stream()
|
||||
.filter(bi -> bi.getDebitor().getDebitorNumber() == 1000111)
|
||||
.filter(item -> item.getCaption().equals("some CloudServer"))
|
||||
.findAny().orElseThrow().getUuid();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/booking/items/" + givenBookingItemUuid)
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": { CPU: 2, HDD: 1024 }
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalUser_canNotGetUnrelatedBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItemUuid = bookingItemRepo.findAll().stream()
|
||||
.filter(bi -> bi.getDebitor().getDebitorNumber() == 1000212)
|
||||
.map(HsBookingItemEntity::getUuid)
|
||||
.findAny().orElseThrow();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "selfregistered-user-drew@hostsharing.org")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/booking/items/" + givenBookingItemUuid)
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(404); // @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void debitorAgentUser_canGetRelatedBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItemUuid = bookingItemRepo.findAll().stream()
|
||||
.filter(bi -> bi.getDebitor().getDebitorNumber() == 1000313)
|
||||
.filter(item -> item.getCaption().equals("some CloudServer"))
|
||||
.findAny().orElseThrow().getUuid();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "person-TuckerJack@example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/booking/items/" + givenBookingItemUuid)
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": { CPU: 2, HDD: 1024 }
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class PatchBookingItem {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canPatchAllUpdatablePropertiesOfBookingItem() {
|
||||
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"validFrom": "2020-06-05",
|
||||
"validTo": "2022-12-31",
|
||||
"resources": {
|
||||
"CPU": "4",
|
||||
"HDD": null,
|
||||
"SSD": "4096"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.port(port)
|
||||
.when()
|
||||
.patch("http://localhost/api/hs/booking/items/" + givenBookingItem.getUuid())
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"caption": "some test-booking",
|
||||
"validFrom": "2022-11-01",
|
||||
"validTo": "2022-12-31",
|
||||
"resources": {
|
||||
"CPU": "4",
|
||||
"SSD": "4096",
|
||||
"something": 1
|
||||
}
|
||||
}
|
||||
""")); // @formatter:on
|
||||
|
||||
// finally, the bookingItem is actually updated
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
assertThat(bookingItemRepo.findByUuid(givenBookingItem.getUuid())).isPresent().get()
|
||||
.matches(mandate -> {
|
||||
assertThat(mandate.getDebitor().toString()).isEqualTo("debitor(D-1000111: rel(anchor='LP First GmbH', type='DEBITOR', holder='LP First GmbH'), fir)");
|
||||
assertThat(mandate.getValidFrom()).isEqualTo("2020-06-05");
|
||||
assertThat(mandate.getValidTo()).isEqualTo("2022-12-31");
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class DeleteBookingItem {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canDeleteArbitraryBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/booking/items/" + givenBookingItem.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(204); // @formatter:on
|
||||
|
||||
// then the given bookingItem is gone
|
||||
assertThat(bookingItemRepo.findByUuid(givenBookingItem.getUuid())).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalUser_canNotDeleteUnrelatedBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "selfregistered-user-drew@hostsharing.org")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/booking/items/" + givenBookingItem.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(404); // @formatter:on
|
||||
|
||||
// then the given bookingItem is still there
|
||||
assertThat(bookingItemRepo.findByUuid(givenBookingItem.getUuid())).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private HsBookingItemEntity givenSomeTemporaryBookingItemForDebitorNumber(final int debitorNumber,
|
||||
final Map.Entry<String, Integer> resources) {
|
||||
return jpaAttempt.transacted(() -> {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenDebitor = debitorRepo.findDebitorByDebitorNumber(debitorNumber).get(0);
|
||||
final var newBookingItem = HsBookingItemEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.debitor(givenDebitor)
|
||||
.caption("some test-booking")
|
||||
.resources(Map.ofEntries(resources))
|
||||
.validity(Range.closedOpen(
|
||||
LocalDate.parse("2022-11-01"), LocalDate.parse("2023-03-31")))
|
||||
.build();
|
||||
|
||||
return bookingItemRepo.save(newBookingItem);
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.range.Range;
|
||||
import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource;
|
||||
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
|
||||
import net.hostsharing.hsadminng.mapper.KeyValueMap;
|
||||
import net.hostsharing.hsadminng.rbac.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 jakarta.persistence.EntityManager;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR;
|
||||
import static net.hostsharing.hsadminng.mapper.PatchMap.entry;
|
||||
import static net.hostsharing.hsadminng.mapper.PatchMap.patchMap;
|
||||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
|
||||
@TestInstance(PER_CLASS)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HsBookingItemEntityPatcherUnitTest extends PatchUnitTestBase<
|
||||
HsBookingItemPatchResource,
|
||||
HsBookingItemEntity
|
||||
> {
|
||||
|
||||
private static final UUID INITIAL_BOOKING_ITEM_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");
|
||||
|
||||
private static final Map<String, Object> INITIAL_RESOURCES = patchMap(
|
||||
entry("CPU", 1),
|
||||
entry("HDD", 1024),
|
||||
entry("MEM", 64)
|
||||
);
|
||||
private static final Map<String, Object> PATCH_RESOURCES = patchMap(
|
||||
entry("CPU", 2),
|
||||
entry("HDD", null),
|
||||
entry("SDD", 256)
|
||||
);
|
||||
private static final Map<String, Object> PATCHED_RESOURCES = patchMap(
|
||||
entry("CPU", 2),
|
||||
entry("SDD", 256),
|
||||
entry("MEM", 64)
|
||||
);
|
||||
|
||||
private static final String INITIAL_CAPTION = "initial caption";
|
||||
private static final String PATCHED_CAPTION = "patched caption";
|
||||
|
||||
@Mock
|
||||
private EntityManager em;
|
||||
|
||||
@BeforeEach
|
||||
void initMocks() {
|
||||
lenient().when(em.getReference(eq(HsOfficeDebitorEntity.class), any())).thenAnswer(invocation ->
|
||||
HsOfficeDebitorEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||
lenient().when(em.getReference(eq(HsBookingItemEntity.class), any())).thenAnswer(invocation ->
|
||||
HsBookingItemEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsBookingItemEntity newInitialEntity() {
|
||||
final var entity = new HsBookingItemEntity();
|
||||
entity.setUuid(INITIAL_BOOKING_ITEM_UUID);
|
||||
entity.setDebitor(TEST_DEBITOR);
|
||||
entity.getResources().putAll(KeyValueMap.from(INITIAL_RESOURCES));
|
||||
entity.setCaption(INITIAL_CAPTION);
|
||||
entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsBookingItemPatchResource newPatchResource() {
|
||||
return new HsBookingItemPatchResource();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsBookingItemEntityPatcher createPatcher(final HsBookingItemEntity bookingItem) {
|
||||
return new HsBookingItemEntityPatcher(bookingItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Property> propertyTestDescriptors() {
|
||||
return Stream.of(
|
||||
new JsonNullableProperty<>(
|
||||
"caption",
|
||||
HsBookingItemPatchResource::setCaption,
|
||||
PATCHED_CAPTION,
|
||||
HsBookingItemEntity::setCaption),
|
||||
new SimpleProperty<>(
|
||||
"resources",
|
||||
HsBookingItemPatchResource::setResources,
|
||||
PATCH_RESOURCES,
|
||||
HsBookingItemEntity::putResources,
|
||||
PATCHED_RESOURCES)
|
||||
.notNullable(),
|
||||
new JsonNullableProperty<>(
|
||||
"validto",
|
||||
HsBookingItemPatchResource::setValidTo,
|
||||
PATCHED_VALID_TO,
|
||||
HsBookingItemEntity::setValidTo)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR;
|
||||
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsBookingItemEntityUnitTest {
|
||||
public static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-01-01");
|
||||
public static final LocalDate GIVEN_VALID_TO = LocalDate.parse("2030-12-31");
|
||||
|
||||
final HsBookingItemEntity givenBookingItem = HsBookingItemEntity.builder()
|
||||
.debitor(TEST_DEBITOR)
|
||||
.caption("some caption")
|
||||
.resources(Map.ofEntries(
|
||||
entry("CPUs", 2),
|
||||
entry("SSD-storage", 512),
|
||||
entry("HDD-storage", 2048)))
|
||||
.validity(toPostgresDateRange(GIVEN_VALID_FROM, GIVEN_VALID_TO))
|
||||
.build();
|
||||
|
||||
@Test
|
||||
void toStringContainsAllPropertiesAndResourcesSortedByKey() {
|
||||
final var result = givenBookingItem.toString();
|
||||
|
||||
assertThat(result).isEqualTo("HsBookingItemEntity(D-1000100, [2020-01-01,2031-01-01), some caption, { CPUs: 2, HDD-storage: 2048, SSD-storage: 512 })");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringContainsOnlyMemberNumberAndCaption() {
|
||||
final var result = givenBookingItem.toShortString();
|
||||
|
||||
assertThat(result).isEqualTo("D-1000100:some caption");
|
||||
}
|
||||
|
||||
@Test
|
||||
void settingValidFromKeepsValidTo() {
|
||||
givenBookingItem.setValidFrom(LocalDate.parse("2023-12-31"));
|
||||
assertThat(givenBookingItem.getValidFrom()).isEqualTo(LocalDate.parse("2023-12-31"));
|
||||
assertThat(givenBookingItem.getValidTo()).isEqualTo(GIVEN_VALID_TO);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void settingValidToKeepsValidFrom() {
|
||||
givenBookingItem.setValidTo(LocalDate.parse("2024-12-31"));
|
||||
assertThat(givenBookingItem.getValidFrom()).isEqualTo(GIVEN_VALID_FROM);
|
||||
assertThat(givenBookingItem.getValidTo()).isEqualTo(LocalDate.parse("2024-12-31"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,337 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.range.Range;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorRepository;
|
||||
import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
|
||||
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
|
||||
import net.hostsharing.hsadminng.rbac.test.Array;
|
||||
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
|
||||
import net.hostsharing.hsadminng.rbac.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.Import;
|
||||
import org.springframework.orm.jpa.JpaSystemException;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.distinctGrantDisplaysOf;
|
||||
import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.distinctRoleNamesOf;
|
||||
import static net.hostsharing.hsadminng.rbac.test.Array.fromFormatted;
|
||||
import static net.hostsharing.hsadminng.rbac.test.JpaAttempt.attempt;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DataJpaTest
|
||||
@Import({ Context.class, JpaAttempt.class })
|
||||
class HsBookingItemRepositoryIntegrationTest extends ContextBasedTestWithCleanup {
|
||||
|
||||
@Autowired
|
||||
HsBookingItemRepository bookingItemRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficeDebitorRepository debitorRepo;
|
||||
|
||||
@Autowired
|
||||
RawRbacRoleRepository rawRoleRepo;
|
||||
|
||||
@Autowired
|
||||
RawRbacGrantRepository rawGrantRepo;
|
||||
|
||||
@Autowired
|
||||
JpaAttempt jpaAttempt;
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
|
||||
@MockBean
|
||||
HttpServletRequest request;
|
||||
|
||||
@Nested
|
||||
class CreateBookingItem {
|
||||
|
||||
@Test
|
||||
public void testHostsharingAdmin_withoutAssumedRole_canCreateNewBookingItem() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var count = bookingItemRepo.count();
|
||||
final var givenDebitor = debitorRepo.findDebitorByOptionalNameLike("First").get(0);
|
||||
|
||||
// when
|
||||
final var result = attempt(em, () -> {
|
||||
final var newBookingItem = HsBookingItemEntity.builder()
|
||||
.debitor(givenDebitor)
|
||||
.caption("some new booking item")
|
||||
.validity(Range.closedOpen(
|
||||
LocalDate.parse("2020-01-01"), LocalDate.parse("2023-01-01")))
|
||||
.build();
|
||||
return toCleanup(bookingItemRepo.save(newBookingItem));
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(result.returnedValue()).isNotNull().extracting(HsBookingItemEntity::getUuid).isNotNull();
|
||||
assertThatBookingItemIsPersisted(result.returnedValue());
|
||||
assertThat(bookingItemRepo.count()).isEqualTo(count + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsAndGrantsRoles() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var initialRoleNames = distinctRoleNamesOf(rawRoleRepo.findAll());
|
||||
final var initialGrantNames = distinctGrantDisplaysOf(rawGrantRepo.findAll()).stream()
|
||||
.map(s -> s.replace("hs_office_", ""))
|
||||
.toList();
|
||||
|
||||
// when
|
||||
attempt(em, () -> {
|
||||
final var givenDebitor = debitorRepo.findDebitorByOptionalNameLike("First").get(0);
|
||||
final var newBookingItem = HsBookingItemEntity.builder()
|
||||
.debitor(givenDebitor)
|
||||
.caption("some new booking item")
|
||||
.validity(Range.closedOpen(
|
||||
LocalDate.parse("2020-01-01"), LocalDate.parse("2023-01-01")))
|
||||
.build();
|
||||
return toCleanup(bookingItemRepo.save(newBookingItem));
|
||||
});
|
||||
|
||||
// then
|
||||
final var all = rawRoleRepo.findAll();
|
||||
assertThat(distinctRoleNamesOf(all)).containsExactlyInAnyOrder(Array.from(
|
||||
initialRoleNames,
|
||||
"hs_booking_item#D-1000111:some new booking item:ADMIN",
|
||||
"hs_booking_item#D-1000111:some new booking item:OWNER",
|
||||
"hs_booking_item#D-1000111:some new booking item:TENANT"));
|
||||
assertThat(distinctGrantDisplaysOf(rawGrantRepo.findAll()))
|
||||
.map(s -> s.replace("hs_office_", ""))
|
||||
.containsExactlyInAnyOrder(fromFormatted(
|
||||
initialGrantNames,
|
||||
|
||||
// insert+delete
|
||||
"{ grant perm:hs_booking_item#D-1000111:some new booking item:DELETE to role:global#global:ADMIN by system and assume }",
|
||||
|
||||
// owner
|
||||
"{ grant perm:hs_booking_item#D-1000111:some new booking item:UPDATE to role:hs_booking_item#D-1000111:some new booking item:OWNER by system and assume }",
|
||||
"{ grant role:hs_booking_item#D-1000111:some new booking item:OWNER to role:relation#FirstGmbH-with-DEBITOR-FirstGmbH:AGENT by system and assume }",
|
||||
|
||||
// admin
|
||||
"{ grant role:hs_booking_item#D-1000111:some new booking item:ADMIN to role:hs_booking_item#D-1000111:some new booking item:OWNER by system and assume }",
|
||||
|
||||
// tenant
|
||||
"{ grant role:hs_booking_item#D-1000111:some new booking item:TENANT to role:hs_booking_item#D-1000111:some new booking item:ADMIN by system and assume }",
|
||||
"{ grant perm:hs_booking_item#D-1000111:some new booking item:SELECT to role:hs_booking_item#D-1000111:some new booking item:TENANT by system and assume }",
|
||||
"{ grant role:relation#FirstGmbH-with-DEBITOR-FirstGmbH:TENANT to role:hs_booking_item#D-1000111:some new booking item:TENANT by system and assume }",
|
||||
|
||||
null));
|
||||
}
|
||||
|
||||
private void assertThatBookingItemIsPersisted(final HsBookingItemEntity saved) {
|
||||
final var found = bookingItemRepo.findByUuid(saved.getUuid());
|
||||
assertThat(found).isNotEmpty().map(HsBookingItemEntity::toString).get().isEqualTo(saved.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class FindByDebitorUuid {
|
||||
|
||||
@Test
|
||||
public void globalAdmin_withoutAssumedRole_canViewAllBookingItemsOfArbitraryDebitor() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var debitorUuid = debitorRepo.findDebitorByDebitorNumber(1000212).stream()
|
||||
.findAny().orElseThrow().getUuid();
|
||||
|
||||
// when
|
||||
final var result = bookingItemRepo.findAllByDebitorUuid(debitorUuid);
|
||||
|
||||
// then
|
||||
allTheseBookingItemsAreReturned(
|
||||
result,
|
||||
"HsBookingItemEntity(D-1000212, [2022-10-01,), some ManagedServer, { CPU: 2, SDD: 512, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, [2023-01-15,2024-04-15), some CloudServer, { CPU: 2, HDD: 1024, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, [2024-04-01,), some Whatever, { CPU: 1, HDD: 2048, SDD: 512, extra: 42 })");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalUser_canViewOnlyRelatedBookingItems() {
|
||||
// given:
|
||||
context("person-FirbySusan@example.com");
|
||||
final var debitorUuid = debitorRepo.findDebitorByDebitorNumber(1000111).stream().findAny().orElseThrow().getUuid();
|
||||
|
||||
// when:
|
||||
final var result = bookingItemRepo.findAllByDebitorUuid(debitorUuid);
|
||||
|
||||
// then:
|
||||
exactlyTheseBookingItemsAreReturned(
|
||||
result,
|
||||
"HsBookingItemEntity(D-1000111, [2022-10-01,), some ManagedServer, { CPU: 2, SDD: 512, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, [2023-01-15,2024-04-15), some CloudServer, { CPU: 2, HDD: 1024, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, [2024-04-01,), some Whatever, { CPU: 1, HDD: 2048, SDD: 512, extra: 42 })");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class UpdateBookingItem {
|
||||
|
||||
@Test
|
||||
public void hostsharingAdmin_canUpdateArbitraryBookingItem() {
|
||||
// given
|
||||
final var givenBookingItemUuid = givenSomeTemporaryBookingItem(1000111).getUuid();
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var foundBookingItem = em.find(HsBookingItemEntity.class, givenBookingItemUuid);
|
||||
foundBookingItem.getResources().put("CPUs", 2);
|
||||
foundBookingItem.getResources().remove("SSD-storage");
|
||||
foundBookingItem.getResources().put("HSD-storage", 2048);
|
||||
foundBookingItem.setValidity(Range.closedOpen(
|
||||
LocalDate.parse("2019-05-17"), LocalDate.parse("2023-01-01")));
|
||||
return toCleanup(bookingItemRepo.save(foundBookingItem));
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
assertThatBookingItemActuallyInDatabase(result.returnedValue());
|
||||
}).assertSuccessful();
|
||||
}
|
||||
|
||||
private void assertThatBookingItemActuallyInDatabase(final HsBookingItemEntity saved) {
|
||||
final var found = bookingItemRepo.findByUuid(saved.getUuid());
|
||||
assertThat(found).isNotEmpty().get().isNotSameAs(saved)
|
||||
.extracting(Object::toString).isEqualTo(saved.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class DeleteByUuid {
|
||||
|
||||
@Test
|
||||
public void globalAdmin_withoutAssumedRole_canDeleteAnyBookingItem() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItem(1000111);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
bookingItemRepo.deleteByUuid(givenBookingItem.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(jpaAttempt.transacted(() -> {
|
||||
context("superuser-fran@hostsharing.net", null);
|
||||
return bookingItemRepo.findByUuid(givenBookingItem.getUuid());
|
||||
}).assertSuccessful().returnedValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonGlobalAdmin_canNotDeleteTheirRelatedBookingItem() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItem(1000111);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("person-FirbySusan@example.com");
|
||||
assertThat(bookingItemRepo.findByUuid(givenBookingItem.getUuid())).isPresent();
|
||||
|
||||
bookingItemRepo.deleteByUuid(givenBookingItem.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertExceptionWithRootCauseMessage(
|
||||
JpaSystemException.class,
|
||||
"[403] Subject ", " is not allowed to delete hs_booking_item");
|
||||
assertThat(jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
return bookingItemRepo.findByUuid(givenBookingItem.getUuid());
|
||||
}).assertSuccessful().returnedValue()).isPresent(); // still there
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletingABookingItemAlsoDeletesRelatedRolesAndGrants() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var initialRoleNames = Array.from(distinctRoleNamesOf(rawRoleRepo.findAll()));
|
||||
final var initialGrantNames = Array.from(distinctGrantDisplaysOf(rawGrantRepo.findAll()));
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItem(1000111);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
return bookingItemRepo.deleteByUuid(givenBookingItem.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(result.returnedValue()).isEqualTo(1);
|
||||
assertThat(distinctRoleNamesOf(rawRoleRepo.findAll())).containsExactlyInAnyOrder(initialRoleNames);
|
||||
assertThat(distinctGrantDisplaysOf(rawGrantRepo.findAll())).containsExactlyInAnyOrder(initialGrantNames);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void auditJournalLogIsAvailable() {
|
||||
// given
|
||||
final var query = em.createNativeQuery("""
|
||||
select currentTask, targetTable, targetOp
|
||||
from tx_journal_v
|
||||
where targettable = 'hs_booking_item';
|
||||
""");
|
||||
|
||||
// when
|
||||
@SuppressWarnings("unchecked") final List<Object[]> customerLogEntries = query.getResultList();
|
||||
|
||||
// then
|
||||
assertThat(customerLogEntries).map(Arrays::toString).contains(
|
||||
"[creating booking-item test-data 1000111, hs_booking_item, INSERT]",
|
||||
"[creating booking-item test-data 1000212, hs_booking_item, INSERT]",
|
||||
"[creating booking-item test-data 1000313, hs_booking_item, INSERT]");
|
||||
}
|
||||
|
||||
private HsBookingItemEntity givenSomeTemporaryBookingItem(final int debitorNumber) {
|
||||
return jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenDebitor = debitorRepo.findDebitorByDebitorNumber(debitorNumber).get(0);
|
||||
final var newBookingItem = HsBookingItemEntity.builder()
|
||||
.debitor(givenDebitor)
|
||||
.caption("some temp booking item")
|
||||
.validity(Range.closedOpen(
|
||||
LocalDate.parse("2020-01-01"), LocalDate.parse("2023-01-01")))
|
||||
.resources(Map.ofEntries(
|
||||
entry("CPUs", 1),
|
||||
entry("SSD-storage", 256)))
|
||||
.build();
|
||||
|
||||
return toCleanup(bookingItemRepo.save(newBookingItem));
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
|
||||
void exactlyTheseBookingItemsAreReturned(
|
||||
final List<HsBookingItemEntity> actualResult,
|
||||
final String... bookingItemNames) {
|
||||
assertThat(actualResult)
|
||||
.extracting(bookingItemEntity -> bookingItemEntity.toString())
|
||||
.containsExactlyInAnyOrder(bookingItemNames);
|
||||
}
|
||||
|
||||
void allTheseBookingItemsAreReturned(final List<HsBookingItemEntity> actualResult, final String... bookingItemNames) {
|
||||
assertThat(actualResult)
|
||||
.extracting(bookingItemEntity -> bookingItemEntity.toString())
|
||||
.contains(bookingItemNames);
|
||||
}
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
package net.hostsharing.hsadminng.hs.office.coopshares;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.TEST_MEMBERSHIP;
|
||||
|
@ -181,6 +181,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean
|
||||
.containsExactlyInAnyOrder(Array.fromFormatted(
|
||||
initialGrantNames,
|
||||
"{ grant perm:relation#FirstGmbH-with-DEBITOR-FourtheG:INSERT>sepamandate to role:relation#FirstGmbH-with-DEBITOR-FourtheG:ADMIN by system and assume }",
|
||||
"{ grant perm:relation#FirstGmbH-with-DEBITOR-FourtheG:INSERT>hs_booking_item to role:relation#FirstGmbH-with-DEBITOR-FourtheG:ADMIN by system and assume }",
|
||||
|
||||
// owner
|
||||
"{ grant perm:debitor#D-1000122:DELETE to role:relation#FirstGmbH-with-DEBITOR-FourtheG:OWNER by system and assume }",
|
||||
|
@ -613,6 +613,7 @@ public class ImportOfficeData extends ContextBasedTest {
|
||||
private void deleteTestDataFromHsOfficeTables() {
|
||||
jpaAttempt.transacted(() -> {
|
||||
context(rbacSuperuser);
|
||||
em.createNativeQuery("delete from hs_booking_item where true").executeUpdate();
|
||||
em.createNativeQuery("delete from hs_office_coopassetstransaction where true").executeUpdate();
|
||||
em.createNativeQuery("delete from hs_office_coopassetstransaction_legacy_id where true").executeUpdate();
|
||||
em.createNativeQuery("delete from hs_office_coopsharestransaction where true").executeUpdate();
|
||||
|
Reference in New Issue
Block a user