hosting-asset-validation-baseline (#56)
Co-authored-by: Michael Hoennig <michael@hoennig.de> Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/56 Reviewed-by: Timotheus Pokorra <timotheus.pokorra@hostsharing.net>
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_WEBSPACE;
|
||||
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
@@ -69,37 +70,42 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
.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 PrivateCloud",
|
||||
"validFrom": "2024-04-01",
|
||||
"validTo": null,
|
||||
"resources": {
|
||||
"CPU": 10,
|
||||
"HDD": 10240,
|
||||
"SDD": 10240,
|
||||
"extra": 42
|
||||
}
|
||||
}
|
||||
]
|
||||
"type": "MANAGED_SERVER",
|
||||
"caption": "some ManagedServer",
|
||||
"validFrom": "2022-10-01",
|
||||
"validTo": null,
|
||||
"resources": {
|
||||
"RAM": 8,
|
||||
"SDD": 512,
|
||||
"CPUs": 2,
|
||||
"Traffic": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CLOUD_SERVER",
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": {
|
||||
"HDD": 1024,
|
||||
"RAM": 4,
|
||||
"CPUs": 2,
|
||||
"Traffic": 42
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "PRIVATE_CLOUD",
|
||||
"caption": "some PrivateCloud",
|
||||
"validFrom": "2024-04-01",
|
||||
"validTo": null,
|
||||
"resources": {
|
||||
"HDD": 10240,
|
||||
"SDD": 10240,
|
||||
"CPUs": 10,
|
||||
"Traffic": 42
|
||||
}
|
||||
}
|
||||
]
|
||||
"""));
|
||||
// @formatter:on
|
||||
}
|
||||
@@ -123,7 +129,7 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"debitorUuid": "%s",
|
||||
"type": "MANAGED_SERVER",
|
||||
"caption": "some new booking",
|
||||
"resources": { "CPU": 12, "extra": 42 },
|
||||
"resources": { "CPUs": 12, "RAM": 4, "SSD": 100, "Traffic": 250 },
|
||||
"validFrom": "2022-10-13"
|
||||
}
|
||||
""".formatted(givenDebitor.getUuid()))
|
||||
@@ -139,7 +145,7 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"caption": "some new booking",
|
||||
"validFrom": "2022-10-13",
|
||||
"validTo": null,
|
||||
"resources": { "CPU": 12 }
|
||||
"resources": { "CPUs": 12, "SSD": 100, "Traffic": 250 }
|
||||
}
|
||||
"""))
|
||||
.header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/booking/items/[^/]*"))
|
||||
@@ -177,7 +183,12 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": { CPU: 2, HDD: 1024 }
|
||||
"resources": {
|
||||
"HDD": 1024,
|
||||
"RAM": 4,
|
||||
"CPUs": 2,
|
||||
"Traffic": 42
|
||||
}
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
@@ -222,7 +233,12 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"caption": "some CloudServer",
|
||||
"validFrom": "2023-01-15",
|
||||
"validTo": "2024-04-14",
|
||||
"resources": { CPU: 2, HDD: 1024 }
|
||||
"resources": {
|
||||
"HDD": 1024,
|
||||
"RAM": 4,
|
||||
"CPUs": 2,
|
||||
"Traffic": 42
|
||||
}
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
@@ -234,7 +250,8 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void globalAdmin_canPatchAllUpdatablePropertiesOfBookingItem() {
|
||||
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
final var givenBookingItem = givenSomeBookingItem(1000111, MANAGED_WEBSPACE,
|
||||
resource("HDD", 100), resource("SSD", 50), resource("Traffic", 250));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -245,9 +262,9 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"validFrom": "2020-06-05",
|
||||
"validTo": "2022-12-31",
|
||||
"resources": {
|
||||
"CPU": "4",
|
||||
"Traffic": 500,
|
||||
"HDD": null,
|
||||
"SSD": "4096"
|
||||
"SSD": 100
|
||||
}
|
||||
}
|
||||
""")
|
||||
@@ -263,9 +280,8 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"validFrom": "2022-11-01",
|
||||
"validTo": "2022-12-31",
|
||||
"resources": {
|
||||
"CPU": "4",
|
||||
"SSD": "4096",
|
||||
"something": 1
|
||||
"Traffic": 500,
|
||||
"SSD": 100
|
||||
}
|
||||
}
|
||||
""")); // @formatter:on
|
||||
@@ -288,7 +304,8 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void globalAdmin_canDeleteArbitraryBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
final var givenBookingItem = givenSomeBookingItem(1000111, MANAGED_WEBSPACE,
|
||||
resource("HDD", 100), resource("SSD", 50), resource("Traffic", 250));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -306,7 +323,8 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void normalUser_canNotDeleteUnrelatedBookingItem() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItem = givenSomeTemporaryBookingItemForDebitorNumber(1000111, entry("something", 1));
|
||||
final var givenBookingItem = givenSomeBookingItem(1000111, MANAGED_WEBSPACE,
|
||||
resource("HDD", 100), resource("SSD", 50), resource("Traffic", 250));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -322,15 +340,16 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
}
|
||||
}
|
||||
|
||||
private HsBookingItemEntity givenSomeTemporaryBookingItemForDebitorNumber(final int debitorNumber,
|
||||
final Map.Entry<String, Integer> resources) {
|
||||
@SafeVarargs
|
||||
private HsBookingItemEntity givenSomeBookingItem(final int debitorNumber,
|
||||
final HsBookingItemType hsBookingItemType, final Map.Entry<String, Object>... 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)
|
||||
.type(HsBookingItemType.MANAGED_WEBSPACE)
|
||||
.type(hsBookingItemType)
|
||||
.caption("some test-booking")
|
||||
.resources(Map.ofEntries(resources))
|
||||
.validity(Range.closedOpen(
|
||||
@@ -340,4 +359,8 @@ class HsBookingItemControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
return bookingItemRepo.save(newBookingItem);
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
|
||||
private Map.Entry<String, Object> resource(final String key, final Object value) {
|
||||
return entry(key, value);
|
||||
}
|
||||
}
|
||||
|
@@ -167,9 +167,9 @@ class HsBookingItemRepositoryIntegrationTest extends ContextBasedTestWithCleanup
|
||||
// then
|
||||
allTheseBookingItemsAreReturned(
|
||||
result,
|
||||
"HsBookingItemEntity(D-1000212, MANAGED_SERVER, [2022-10-01,), some ManagedServer, { CPU: 2, SDD: 512, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, CLOUD_SERVER, [2023-01-15,2024-04-15), some CloudServer, { CPU: 2, HDD: 1024, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, PRIVATE_CLOUD, [2024-04-01,), some PrivateCloud, { CPU: 10, HDD: 10240, SDD: 10240, extra: 42 })");
|
||||
"HsBookingItemEntity(D-1000212, MANAGED_SERVER, [2022-10-01,), some ManagedServer, { CPUs: 2, RAM: 8, SDD: 512, Traffic: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, CLOUD_SERVER, [2023-01-15,2024-04-15), some CloudServer, { CPUs: 2, HDD: 1024, RAM: 4, Traffic: 42 })",
|
||||
"HsBookingItemEntity(D-1000212, PRIVATE_CLOUD, [2024-04-01,), some PrivateCloud, { CPUs: 10, HDD: 10240, SDD: 10240, Traffic: 42 })");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -184,9 +184,9 @@ class HsBookingItemRepositoryIntegrationTest extends ContextBasedTestWithCleanup
|
||||
// then:
|
||||
exactlyTheseBookingItemsAreReturned(
|
||||
result,
|
||||
"HsBookingItemEntity(D-1000111, MANAGED_SERVER, [2022-10-01,), some ManagedServer, { CPU: 2, SDD: 512, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, CLOUD_SERVER, [2023-01-15,2024-04-15), some CloudServer, { CPU: 2, HDD: 1024, extra: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, PRIVATE_CLOUD, [2024-04-01,), some PrivateCloud, { CPU: 10, HDD: 10240, SDD: 10240, extra: 42 })");
|
||||
"HsBookingItemEntity(D-1000111, MANAGED_SERVER, [2022-10-01,), some ManagedServer, { CPUs: 2, RAM: 8, SDD: 512, Traffic: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, CLOUD_SERVER, [2023-01-15,2024-04-15), some CloudServer, { CPUs: 2, HDD: 1024, RAM: 4, Traffic: 42 })",
|
||||
"HsBookingItemEntity(D-1000111, PRIVATE_CLOUD, [2024-04-01,), some PrivateCloud, { CPUs: 10, HDD: 10240, SDD: 10240, Traffic: 42 })");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,44 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.validation.ValidationException;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_WEBSPACE;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.validators.HsBookingItemEntityValidators.valid;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.CLOUD_SERVER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
class HsBookingItemEntityValidatorsUnitTest {
|
||||
|
||||
@Test
|
||||
void validThrowsException() {
|
||||
// given
|
||||
final var cloudServerBookingItemEntity = HsBookingItemEntity.builder()
|
||||
.type(CLOUD_SERVER)
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = catchThrowable( ()-> valid(cloudServerBookingItemEntity) );
|
||||
|
||||
// then
|
||||
assertThat(result).isInstanceOf(ValidationException.class)
|
||||
.hasMessageContaining(
|
||||
"'resources.CPUs' is required but missing",
|
||||
"'resources.RAM' is required but missing",
|
||||
"'resources.SSD' is required but missing",
|
||||
"'resources.Traffic' is required but missing");
|
||||
}
|
||||
|
||||
@Test
|
||||
void listsTypes() {
|
||||
// when
|
||||
final var result = HsBookingItemEntityValidators.types();
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(CLOUD_SERVER, MANAGED_SERVER, MANAGED_WEBSPACE);
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.validators.HsBookingItemEntityValidators.forType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsCloudServerBookingItemValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesProperties() {
|
||||
// given
|
||||
final var validator = HsBookingItemEntityValidators.forType(CLOUD_SERVER);
|
||||
final var cloudServerBookingItemEntity = HsBookingItemEntity.builder()
|
||||
.type(CLOUD_SERVER)
|
||||
.resources(Map.ofEntries(
|
||||
entry("CPUs", 2),
|
||||
entry("RAM", 25),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250),
|
||||
entry("SLA-EMail", true)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(cloudServerBookingItemEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'resources.SLA-EMail' is not expected but is set to 'true'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsAllValidations() {
|
||||
// when
|
||||
final var validator = forType(CLOUD_SERVER);
|
||||
|
||||
// then
|
||||
assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder(
|
||||
"{type=integer, propertyName=CPUs, required=true, unit=null, min=1, max=32, step=null}",
|
||||
"{type=integer, propertyName=RAM, required=true, unit=GB, min=1, max=128, step=null}",
|
||||
"{type=integer, propertyName=SSD, required=true, unit=GB, min=25, max=1000, step=25}",
|
||||
"{type=integer, propertyName=HDD, required=false, unit=GB, min=0, max=4000, step=250}",
|
||||
"{type=integer, propertyName=Traffic, required=true, unit=GB, min=250, max=10000, step=250}",
|
||||
"{type=enumeration, propertyName=SLA-Infrastructure, required=false, values=[BASIC, EXT8H, EXT4H, EXT2H]}");
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.validators.HsBookingItemEntityValidators.forType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsManagedServerBookingItemValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesProperties() {
|
||||
// given
|
||||
final var validator = HsBookingItemEntityValidators.forType(MANAGED_SERVER);
|
||||
final var mangedServerBookingItemEntity = HsBookingItemEntity.builder()
|
||||
.type(MANAGED_SERVER)
|
||||
.resources(Map.ofEntries(
|
||||
entry("CPUs", 2),
|
||||
entry("RAM", 25),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250),
|
||||
entry("SLA-EMail", true)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedServerBookingItemEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'resources.SLA-EMail' is expected to be false because resources.SLA-Platform=BASIC but is true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsAllValidations() {
|
||||
// when
|
||||
final var validator = forType(MANAGED_SERVER);
|
||||
|
||||
// then
|
||||
assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder(
|
||||
"{type=integer, propertyName=CPUs, required=true, unit=null, min=1, max=32, step=null}",
|
||||
"{type=integer, propertyName=RAM, required=true, unit=GB, min=1, max=128, step=null}",
|
||||
"{type=integer, propertyName=SSD, required=true, unit=GB, min=25, max=1000, step=25}",
|
||||
"{type=integer, propertyName=HDD, required=false, unit=GB, min=0, max=4000, step=250}",
|
||||
"{type=integer, propertyName=Traffic, required=true, unit=GB, min=250, max=10000, step=250}",
|
||||
"{type=enumeration, propertyName=SLA-Platform, required=false, values=[BASIC, EXT8H, EXT4H, EXT2H]}",
|
||||
"{type=boolean, propertyName=SLA-EMail, required=false, falseIf={SLA-Platform=BASIC}}",
|
||||
"{type=boolean, propertyName=SLA-Maria, required=false, falseIf={SLA-Platform=BASIC}}",
|
||||
"{type=boolean, propertyName=SLA-PgSQL, required=false, falseIf={SLA-Platform=BASIC}}",
|
||||
"{type=boolean, propertyName=SLA-Office, required=false, falseIf={SLA-Platform=BASIC}}",
|
||||
"{type=boolean, propertyName=SLA-Web, required=false, falseIf={SLA-Platform=BASIC}}");
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package net.hostsharing.hsadminng.hs.booking.item.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemType.MANAGED_WEBSPACE;
|
||||
import static net.hostsharing.hsadminng.hs.booking.item.validators.HsBookingItemEntityValidators.forType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsManagedWebspaceBookingItemValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesProperties() {
|
||||
// given
|
||||
final var mangedServerBookingItemEntity = HsBookingItemEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.resources(Map.ofEntries(
|
||||
entry("CPUs", 2),
|
||||
entry("RAM", 25),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250),
|
||||
entry("SLA-EMail", true)
|
||||
))
|
||||
.build();
|
||||
final var validator = forType(mangedServerBookingItemEntity.getType());
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedServerBookingItemEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'resources.CPUs' is not expected but is set to '2'",
|
||||
"'resources.SLA-EMail' is not expected but is set to 'true'",
|
||||
"'resources.RAM' is not expected but is set to '25'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsAllValidations() {
|
||||
// when
|
||||
final var validator = forType(MANAGED_WEBSPACE);
|
||||
|
||||
// then
|
||||
assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder(
|
||||
"{type=integer, propertyName=SSD, required=true, unit=GB, min=1, max=100, step=1}",
|
||||
"{type=integer, propertyName=HDD, required=false, unit=GB, min=0, max=250, step=10}",
|
||||
"{type=integer, propertyName=Traffic, required=true, unit=GB, min=10, max=1000, step=10}",
|
||||
"{type=enumeration, propertyName=SLA-Platform, required=false, values=[BASIC, EXT24H]}",
|
||||
"{type=integer, propertyName=Daemons, required=false, unit=null, min=0, max=10, step=null}",
|
||||
"{type=boolean, propertyName=Online Office Server, required=false, falseIf=null}");
|
||||
}
|
||||
}
|
@@ -19,6 +19,8 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
@@ -113,7 +115,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/hosting/assets?type=" + HsHostingAssetType.MANAGED_SERVER)
|
||||
.get("http://localhost/api/hs/hosting/assets?type=" + MANAGED_SERVER)
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
@@ -159,7 +161,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
class AddServer {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canAddAsset() {
|
||||
void globalAdmin_canAddBookedAsset() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenBookingItem = givenBookingItem("First", "some PrivateCloud");
|
||||
@@ -173,7 +175,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"bookingItemUuid": "%s",
|
||||
"type": "MANAGED_SERVER",
|
||||
"identifier": "vm1400",
|
||||
"caption": "some new CloudServer",
|
||||
"caption": "some new ManagedServer",
|
||||
"config": { "CPUs": 2, "RAM": 100, "SSD": 300, "Traffic": 250 }
|
||||
}
|
||||
""".formatted(givenBookingItem.getUuid()))
|
||||
@@ -187,7 +189,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
{
|
||||
"type": "MANAGED_SERVER",
|
||||
"identifier": "vm1400",
|
||||
"caption": "some new CloudServer",
|
||||
"caption": "some new ManagedServer",
|
||||
"config": { "CPUs": 2, "RAM": 100, "SSD": 300, "Traffic": 250 }
|
||||
}
|
||||
"""))
|
||||
@@ -200,6 +202,48 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parentAssetAgent_canAddSubAsset() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenParentAsset = givenParentAsset("First", MANAGED_SERVER);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "person-FirbySusan@example.com")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"parentAssetUuid": "%s",
|
||||
"type": "MANAGED_WEBSPACE",
|
||||
"identifier": "fir90",
|
||||
"caption": "some new ManagedWebspace in client's ManagedServer",
|
||||
"config": { "SSD": 100, "Traffic": 250 }
|
||||
}
|
||||
""".formatted(givenParentAsset.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/hosting/assets")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"type": "MANAGED_WEBSPACE",
|
||||
"identifier": "fir90",
|
||||
"caption": "some new ManagedWebspace in client's ManagedServer",
|
||||
"config": { "SSD": 100, "Traffic": 250 }
|
||||
}
|
||||
"""))
|
||||
.header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/hosting/assets/[^/]*"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new asset can be accessed under the generated UUID
|
||||
final var newUserUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void additionalValidationsArePerformend_whenAddingAsset() {
|
||||
|
||||
@@ -215,7 +259,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"bookingItemUuid": "%s",
|
||||
"type": "MANAGED_SERVER",
|
||||
"identifier": "vm1400",
|
||||
"caption": "some new CloudServer",
|
||||
"caption": "some new ManagedServer",
|
||||
"config": { "CPUs": 0, "extra": 42 }
|
||||
}
|
||||
""".formatted(givenBookingItem.getUuid()))
|
||||
@@ -228,14 +272,14 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"statusPhrase": "Bad Request",
|
||||
"message": "['extra' is not expected but is '42', 'CPUs' is expected to be >= 1 but is 0, 'RAM' is required but missing, 'SSD' is required but missing, 'Traffic' is required but missing]"
|
||||
"message": "['config.extra' is not expected but is set to '42', 'config.CPUs' is expected to be >= 1 but is 0, 'config.RAM' is required but missing, 'config.SSD' is required but missing, 'config.Traffic' is required but missing]"
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class GetASset {
|
||||
class GetAsset {
|
||||
|
||||
@Test
|
||||
void globalAdmin_canGetArbitraryAsset() {
|
||||
@@ -321,7 +365,8 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void globalAdmin_canPatchAllUpdatablePropertiesOfAsset() {
|
||||
|
||||
final var givenAsset = givenSomeTemporaryAssetForDebitorNumber("2001", entry("something", 1));
|
||||
final var givenAsset = givenSomeTemporaryHostingAsset("2001", CLOUD_SERVER,
|
||||
config("CPUs", 4), config("RAM", 100), config("HDD", 100), config("Traffic", 2000));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -330,9 +375,9 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
.body("""
|
||||
{
|
||||
"config": {
|
||||
"CPU": "4",
|
||||
"CPUs": 2,
|
||||
"HDD": null,
|
||||
"SSD": "4096"
|
||||
"SSD": 250
|
||||
}
|
||||
}
|
||||
""")
|
||||
@@ -348,9 +393,9 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
"identifier": "vm2001",
|
||||
"caption": "some test-asset",
|
||||
"config": {
|
||||
"CPU": "4",
|
||||
"SSD": "4096",
|
||||
"something": 1
|
||||
"CPUs": 2,
|
||||
"RAM": 100,
|
||||
"SSD": 250
|
||||
}
|
||||
}
|
||||
""")); // @formatter:on
|
||||
@@ -359,7 +404,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
assertThat(assetRepo.findByUuid(givenAsset.getUuid())).isPresent().get()
|
||||
.matches(asset -> {
|
||||
assertThat(asset.toString()).isEqualTo("HsHostingAssetEntity(CLOUD_SERVER, vm2001, some test-asset, D-1000111:some CloudServer, { CPU: 4, SSD: 4096, something: 1 })");
|
||||
assertThat(asset.toString()).isEqualTo("HsHostingAssetEntity(CLOUD_SERVER, vm2001, some test-asset, D-1000111:some CloudServer, { CPUs: 2, RAM: 100, SSD: 250, Traffic: 2000 })");
|
||||
return true;
|
||||
});
|
||||
}
|
||||
@@ -371,7 +416,8 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void globalAdmin_canDeleteArbitraryAsset() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAsset = givenSomeTemporaryAssetForDebitorNumber("2002", entry("something", 1));
|
||||
final var givenAsset = givenSomeTemporaryHostingAsset("2002", CLOUD_SERVER,
|
||||
config("CPUs", 4), config("RAM", 100), config("HDD", 100), config("Traffic", 2000));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -389,7 +435,8 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
@Test
|
||||
void normalUser_canNotDeleteUnrelatedAsset() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAsset = givenSomeTemporaryAssetForDebitorNumber("2003", entry("something", 1));
|
||||
final var givenAsset = givenSomeTemporaryHostingAsset("2003", CLOUD_SERVER,
|
||||
config("CPUs", 4), config("RAM", 100), config("HDD", 100), config("Traffic", 2000));
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -412,14 +459,22 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
.findAny().orElseThrow();
|
||||
}
|
||||
|
||||
private HsHostingAssetEntity givenSomeTemporaryAssetForDebitorNumber(final String identifierSuffix,
|
||||
final Map.Entry<String, Integer> resources) {
|
||||
HsHostingAssetEntity givenParentAsset(final String debitorName, final HsHostingAssetType assetType) {
|
||||
final var givenDebitor = debitorRepo.findDebitorByOptionalNameLike(debitorName).stream().findAny().orElseThrow();
|
||||
final var givenAsset = assetRepo.findAllByCriteria(givenDebitor.getUuid(), null, assetType).stream().findAny().orElseThrow();
|
||||
return givenAsset;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private HsHostingAssetEntity givenSomeTemporaryHostingAsset(final String identifierSuffix,
|
||||
final HsHostingAssetType hostingAssetType,
|
||||
final Map.Entry<String, Object>... resources) {
|
||||
return jpaAttempt.transacted(() -> {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var newAsset = HsHostingAssetEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.bookingItem(givenBookingItem("First", "some CloudServer"))
|
||||
.type(HsHostingAssetType.CLOUD_SERVER)
|
||||
.type(hostingAssetType)
|
||||
.identifier("vm" + identifierSuffix)
|
||||
.caption("some test-asset")
|
||||
.config(Map.ofEntries(resources))
|
||||
@@ -428,4 +483,8 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
|
||||
return assetRepo.save(newAsset);
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
|
||||
private Map.Entry<String, Object> config(final String key, final Object value) {
|
||||
return entry(key, value);
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ class HsHostingAssetPropsControllerAcceptanceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalAdmin_canListPropertiesOfGivenAssetType() {
|
||||
void anyone_canListPropertiesOfGivenAssetType() {
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
@@ -52,101 +52,50 @@ class HsHostingAssetPropsControllerAcceptanceTest {
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
[
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "CPUs",
|
||||
"required": true,
|
||||
"unit": null,
|
||||
"min": 1,
|
||||
"max": 32,
|
||||
"step": null
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "RAM",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 1,
|
||||
"max": 128,
|
||||
"step": null
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "SSD",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 25,
|
||||
"max": 1000,
|
||||
"step": 25
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "HDD",
|
||||
"required": false,
|
||||
"unit": "GB",
|
||||
"min": 0,
|
||||
"max": 4000,
|
||||
"step": 250
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "Traffic",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 250,
|
||||
"max": 10000,
|
||||
"step": 250
|
||||
},
|
||||
{
|
||||
"type": "enumeration",
|
||||
"propertyName": "SLA-Platform",
|
||||
"required": false,
|
||||
"values": [
|
||||
"BASIC",
|
||||
"EXT8H",
|
||||
"EXT4H",
|
||||
"EXT2H"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"propertyName": "SLA-EMail",
|
||||
"required": false,
|
||||
"falseIf": {
|
||||
"SLA-Platform": "BASIC"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"propertyName": "SLA-Maria",
|
||||
"required": false,
|
||||
"falseIf": {
|
||||
"SLA-Platform": "BASIC"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"propertyName": "SLA-PgSQL",
|
||||
"required": false,
|
||||
"falseIf": {
|
||||
"SLA-Platform": "BASIC"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"propertyName": "SLA-Office",
|
||||
"required": false,
|
||||
"falseIf": {
|
||||
"SLA-Platform": "BASIC"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"propertyName": "SLA-Web",
|
||||
"required": false,
|
||||
"falseIf": {
|
||||
"SLA-Platform": "BASIC"
|
||||
}
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "CPUs",
|
||||
"required": true,
|
||||
"unit": null,
|
||||
"min": 1,
|
||||
"max": 32,
|
||||
"step": null
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "RAM",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 1,
|
||||
"max": 128,
|
||||
"step": null
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "SSD",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 25,
|
||||
"max": 1000,
|
||||
"step": 25
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "HDD",
|
||||
"required": false,
|
||||
"unit": "GB",
|
||||
"min": 0,
|
||||
"max": 4000,
|
||||
"step": 250
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"propertyName": "Traffic",
|
||||
"required": true,
|
||||
"unit": "GB",
|
||||
"min": 250,
|
||||
"max": 10000,
|
||||
"step": 250
|
||||
}
|
||||
]
|
||||
"""));
|
||||
|
@@ -1,97 +0,0 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.hosting.asset.validator.HsHostingAssetValidator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsHostingAssetValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesMissingProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.config(emptyMap())
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'SSD' is required but missing",
|
||||
"'Traffic' is required but missing"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesUnknownProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 0),
|
||||
entry("SSD", 1),
|
||||
entry("Traffic", 10),
|
||||
entry("unknown", "some value")
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'unknown' is not expected but is 'some value'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesDependentProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetValidator.forType(MANAGED_SERVER);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_SERVER)
|
||||
.config(Map.ofEntries(
|
||||
entry("CPUs", 2),
|
||||
entry("RAM", 25),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250),
|
||||
entry("SLA-EMail", true)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'SLA-EMail' is expected to be false because SLA-Platform=BASIC but is true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesValidProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 200),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidators.forType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsCloudServerHostingAssetValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesProperties() {
|
||||
// given
|
||||
final var cloudServerHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(CLOUD_SERVER)
|
||||
.config(Map.ofEntries(
|
||||
entry("RAM", 2000),
|
||||
entry("SSD", 256),
|
||||
entry("Traffic", "250"),
|
||||
entry("SLA-Platform", "xxx")
|
||||
))
|
||||
.build();
|
||||
final var validator = forType(cloudServerHostingAssetEntity.getType());
|
||||
|
||||
|
||||
// when
|
||||
final var result = validator.validate(cloudServerHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'config.SLA-Platform' is not expected but is set to 'xxx'",
|
||||
"'config.CPUs' is required but missing",
|
||||
"'config.RAM' is expected to be <= 128 but is 2000",
|
||||
"'config.SSD' is expected to be multiple of 25 but is 256",
|
||||
"'config.Traffic' is expected to be of type class java.lang.Integer, but is of type 'String'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsAllValidations() {
|
||||
// when
|
||||
final var validator = forType(CLOUD_SERVER);
|
||||
|
||||
// then
|
||||
assertThat(validator.properties()).map(Map::toString).containsExactlyInAnyOrder(
|
||||
"{type=integer, propertyName=CPUs, required=true, unit=null, min=1, max=32, step=null}",
|
||||
"{type=integer, propertyName=RAM, required=true, unit=GB, min=1, max=128, step=null}",
|
||||
"{type=integer, propertyName=SSD, required=true, unit=GB, min=25, max=1000, step=25}",
|
||||
"{type=integer, propertyName=HDD, required=false, unit=GB, min=0, max=4000, step=250}",
|
||||
"{type=integer, propertyName=Traffic, required=true, unit=GB, min=250, max=10000, step=250}");
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.validation.ValidationException;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidators.valid;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
class HsHostingAssetEntityValidatorsUnitTest {
|
||||
|
||||
@Test
|
||||
void validThrowsException() {
|
||||
// given
|
||||
final var cloudServerHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(CLOUD_SERVER)
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = catchThrowable( ()-> valid(cloudServerHostingAssetEntity) );
|
||||
|
||||
// then
|
||||
assertThat(result).isInstanceOf(ValidationException.class)
|
||||
.hasMessageContaining(
|
||||
"'config.CPUs' is required but missing",
|
||||
"'config.RAM' is required but missing",
|
||||
"'config.SSD' is required but missing",
|
||||
"'config.Traffic' is required but missing");
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidators.forType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsManagedServerHostingAssetValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void validatesProperties() {
|
||||
// given
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_SERVER)
|
||||
.config(Map.ofEntries(
|
||||
entry("RAM", 2000),
|
||||
entry("SSD", 256),
|
||||
entry("Traffic", "250"),
|
||||
entry("SLA-Platform", "xxx")
|
||||
))
|
||||
.build();
|
||||
final var validator = forType(mangedWebspaceHostingAssetEntity.getType());
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'config.SLA-Platform' is not expected but is set to 'xxx'",
|
||||
"'config.CPUs' is required but missing",
|
||||
"'config.RAM' is expected to be <= 128 but is 2000",
|
||||
"'config.SSD' is expected to be multiple of 25 but is 256",
|
||||
"'config.Traffic' is expected to be of type class java.lang.Integer, but is of type 'String'");
|
||||
}
|
||||
}
|
@@ -0,0 +1,120 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset.validators;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
|
||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Map.entry;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
|
||||
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsManagedWebspaceHostingAssetValidatorUnitTest {
|
||||
|
||||
final HsBookingItemEntity managedServerBookingItem = HsBookingItemEntity.builder()
|
||||
.debitor(HsOfficeDebitorEntity.builder().defaultPrefix("abc").build()
|
||||
)
|
||||
.build();
|
||||
final HsHostingAssetEntity mangedServerAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_SERVER)
|
||||
.bookingItem(managedServerBookingItem)
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 0),
|
||||
entry("SSD", 1),
|
||||
entry("Traffic", 10)
|
||||
))
|
||||
.build();
|
||||
|
||||
@Test
|
||||
void validatesIdentifier() {
|
||||
// given
|
||||
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.parentAsset(mangedServerAssetEntity)
|
||||
.identifier("xyz00")
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 0),
|
||||
entry("SSD", 1),
|
||||
entry("Traffic", 10)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'identifier' expected to match '^abc[0-9][0-9]$', but is 'xyz00'");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void validatesMissingProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.parentAsset(mangedServerAssetEntity)
|
||||
.identifier("abc00")
|
||||
.config(emptyMap())
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactlyInAnyOrder(
|
||||
"'config.SSD' is required but missing",
|
||||
"'config.Traffic' is required but missing"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesUnknownProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.parentAsset(mangedServerAssetEntity)
|
||||
.identifier("abc00")
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 0),
|
||||
entry("SSD", 1),
|
||||
entry("Traffic", 10),
|
||||
entry("unknown", "some value")
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).containsExactly("'config.unknown' is not expected but is set to 'some value'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesValidProperties() {
|
||||
// given
|
||||
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
|
||||
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
|
||||
.type(MANAGED_WEBSPACE)
|
||||
.parentAsset(mangedServerAssetEntity)
|
||||
.identifier("abc00")
|
||||
.config(Map.ofEntries(
|
||||
entry("HDD", 200),
|
||||
entry("SSD", 25),
|
||||
entry("Traffic", 250)
|
||||
))
|
||||
.build();
|
||||
|
||||
// when
|
||||
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
|
||||
|
||||
// then
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user