hosting-asset-validation-baseline #56

Merged
hsh-michaelhoennig merged 7 commits from hosting-asset-validation-baseline into master 2024-05-30 10:45:13 +02:00
14 changed files with 132 additions and 62 deletions
Showing only changes of commit 6d286d18a9 - Show all commits

View File

@ -11,6 +11,7 @@ import lombok.NoArgsConstructor;
import lombok.Setter;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationEntity;
import net.hostsharing.hsadminng.hs.validation.Validatable;
import net.hostsharing.hsadminng.mapper.PatchableMapWrapper;
import net.hostsharing.hsadminng.rbac.rbacdef.RbacView;
import net.hostsharing.hsadminng.rbac.rbacdef.RbacView.SQL;
@ -65,7 +66,7 @@ import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class HsBookingItemEntity implements Stringifyable, RbacObject {
public class HsBookingItemEntity implements Stringifyable, RbacObject, Validatable<HsBookingItemEntity, HsBookingItemType> {
private static Stringify<HsBookingItemEntity> stringify = stringify(HsBookingItemEntity.class)
.withProp(HsBookingItemEntity::getDebitor)
@ -142,6 +143,11 @@ public class HsBookingItemEntity implements Stringifyable, RbacObject {
":" + caption;
}
@Override
public Map<String, Object> getProperties() {
return resources;
}
public static RbacView rbac() {
return rbacViewFor("bookingItem", HsBookingItemEntity.class)
.withIdentityView(SQL.query("""

View File

@ -0,0 +1,40 @@
package net.hostsharing.hsadminng.hs.booking.item;
import lombok.experimental.UtilityClass;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import jakarta.validation.ValidationException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.stream;
@UtilityClass
public class HsBookingItemEntityValidators {
private static final Map<Enum<HsBookingItemType>, HsEntityValidator<HsBookingItemEntity, HsBookingItemType>> validators = new HashMap<>();
public static void register(final Enum<HsBookingItemType> type, final HsEntityValidator<HsBookingItemEntity, HsBookingItemType> validator) {
stream(validator.propertyValidators).forEach( entry -> {
entry.verifyConsistency(Map.entry(type, validator));
});
validators.put(type, validator);
}
public static HsEntityValidator<HsBookingItemEntity, HsBookingItemType> forType(final Enum<HsBookingItemType> type) {
return validators.get(type);
}
public static Set<? extends Enum<?>> types() {
return validators.keySet();
}
static HsBookingItemEntity valid(final HsBookingItemEntity entityToSave) {
final var violations = HsBookingItemEntityValidators.forType(entityToSave.getType()).validate(entityToSave);
if (!violations.isEmpty()) {
throw new ValidationException(violations.toString());
}
return entityToSave;
}
}

View File

@ -8,10 +8,10 @@ import static net.hostsharing.hsadminng.hs.validation.EnumerationPropertyValidat
import static net.hostsharing.hsadminng.hs.validation.IntegerPropertyValidator.integerProperty;
@Component
class HsCloudServerAssetValidator extends HsEntityValidator {
class HsCloudServerAssetValidator extends HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType> {
static {
HsEntityValidator.register(CLOUD_SERVER, new HsCloudServerAssetValidator());
HsHostingAssetEntityValidators.register(CLOUD_SERVER, new HsCloudServerAssetValidator());
}
public HsCloudServerAssetValidator() {

View File

@ -1,6 +1,5 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.api.HsHostingAssetsApi;
import net.hostsharing.hsadminng.context.Context;
@ -17,11 +16,11 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ValidationException;
import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntityValidators.valid;
@RestController
public class HsHostingAssetController implements HsHostingAssetsApi {
@ -118,19 +117,11 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
new HsHostingAssetEntityPatcher(current).apply(body);
final var saved = assetRepo.save(current);
final var saved = assetRepo.save(valid(current));
final var mapped = mapper.map(saved, HsHostingAssetResource.class);
return ResponseEntity.ok(mapped);
}
private HsHostingAssetEntity valid(final HsHostingAssetEntity entityToSave) {
final var violations = HsEntityValidator.forType(entityToSave.getType()).validate(entityToSave);
if (!violations.isEmpty()) {
throw new ValidationException(violations.toString());
}
return entityToSave;
}
final BiConsumer<HsHostingAssetInsertResource, HsHostingAssetEntity> RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.putConfig(KeyValueMap.from(resource.getConfig()));
if (resource.getParentAssetUuid() != null) {

View File

@ -8,6 +8,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
import net.hostsharing.hsadminng.hs.validation.Validatable;
import net.hostsharing.hsadminng.mapper.PatchableMapWrapper;
import net.hostsharing.hsadminng.rbac.rbacdef.RbacView;
import net.hostsharing.hsadminng.rbac.rbacdef.RbacView.SQL;
@ -60,7 +61,7 @@ import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class HsHostingAssetEntity implements Stringifyable, RbacObject {
public class HsHostingAssetEntity implements Stringifyable, RbacObject, Validatable<HsHostingAssetEntity, HsHostingAssetType> {
private static Stringify<HsHostingAssetEntity> stringify = stringify(HsHostingAssetEntity.class)
.withProp(HsHostingAssetEntity::getType)
@ -113,6 +114,11 @@ public class HsHostingAssetEntity implements Stringifyable, RbacObject {
PatchableMapWrapper.of(configWrapper, (newWrapper) -> {configWrapper = newWrapper; }, config).assign(newConfg);
}
@Override
public Map<String, Object> getProperties() {
return config;
}
@Override
public String toString() {
return stringify.apply(this);

View File

@ -0,0 +1,41 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import lombok.experimental.UtilityClass;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import jakarta.validation.ValidationException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.stream;
@UtilityClass
public class HsHostingAssetEntityValidators {
private static final Map<Enum<HsHostingAssetType>, HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType>> validators = new HashMap<>();
public static void register(final Enum<HsHostingAssetType> type, final HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType> validator) {
stream(validator.propertyValidators).forEach( entry -> {
entry.verifyConsistency(Map.entry(type, validator));
});
validators.put(type, validator);
}
public static HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType> forType(final Enum<HsHostingAssetType> type) {
return validators.get(type);
}
public static Set<? extends Enum<?>> types() {
return validators.keySet();
}
static HsHostingAssetEntity valid(final HsHostingAssetEntity entityToSave) {
final var violations = HsHostingAssetEntityValidators.forType(entityToSave.getType()).validate(entityToSave);
if (!violations.isEmpty()) {
throw new ValidationException(violations.toString());
}
return entityToSave;
}
}

View File

@ -1,6 +1,5 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.api.HsHostingAssetPropsApi;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetTypeResource;
import org.springframework.http.ResponseEntity;
@ -15,7 +14,7 @@ public class HsHostingAssetPropsController implements HsHostingAssetPropsApi {
@Override
public ResponseEntity<List<String>> listAssetTypes() {
final var resource = HsEntityValidator.types().stream()
final var resource = HsHostingAssetEntityValidators.types().stream()
.map(Enum::name)
.toList();
return ResponseEntity.ok(resource);
@ -25,7 +24,7 @@ public class HsHostingAssetPropsController implements HsHostingAssetPropsApi {
public ResponseEntity<List<Object>> listAssetTypeProps(
final HsHostingAssetTypeResource assetType) {
final var propValidators = HsEntityValidator.forType(HsHostingAssetType.of(assetType));
final var propValidators = HsHostingAssetEntityValidators.forType(HsHostingAssetType.of(assetType));
final List<Map<String, Object>> resource = propValidators.properties();
return ResponseEntity.ok(toListOfObjects(resource));
}

View File

@ -9,10 +9,10 @@ import static net.hostsharing.hsadminng.hs.validation.EnumerationPropertyValidat
import static net.hostsharing.hsadminng.hs.validation.IntegerPropertyValidator.integerProperty;
@Component
class HsManagedServerAssetValidator extends HsEntityValidator {
class HsManagedServerAssetValidator extends HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType> {
static {
HsEntityValidator.register(MANAGED_SERVER, new HsManagedServerAssetValidator());
HsHostingAssetEntityValidators.register(MANAGED_SERVER, new HsManagedServerAssetValidator());
}
public HsManagedServerAssetValidator() {

View File

@ -11,10 +11,10 @@ import static net.hostsharing.hsadminng.hs.validation.EnumerationPropertyValidat
import static net.hostsharing.hsadminng.hs.validation.IntegerPropertyValidator.integerProperty;
@Component
class HsManagedWebspaceAssetValidator extends HsEntityValidator {
class HsManagedWebspaceAssetValidator extends HsEntityValidator<HsHostingAssetEntity, HsHostingAssetType> {
static {
HsEntityValidator.register(MANAGED_WEBSPACE, new HsManagedWebspaceAssetValidator());
HsHostingAssetEntityValidators.register(MANAGED_WEBSPACE, new HsManagedWebspaceAssetValidator());
}
public HsManagedWebspaceAssetValidator() {

View File

@ -3,54 +3,31 @@ package net.hostsharing.hsadminng.hs.validation;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.stream;
import static net.hostsharing.hsadminng.hs.validation.EnumerationPropertyValidator.enumerationProperty;
import static net.hostsharing.hsadminng.hs.validation.HsPropertyValidator.defType;
import static net.hostsharing.hsadminng.hs.validation.IntegerPropertyValidator.integerProperty;
public class HsEntityValidator {
public class HsEntityValidator<E extends Validatable<E, T>, T extends Enum<T>> {
private static final Map<Enum<?>, HsEntityValidator> validators = new HashMap<>();
private final HsPropertyValidator<?>[] propertyValidators;
public static void register(final Enum<?> type, final HsEntityValidator validator) {
stream(validator.propertyValidators).forEach( entry -> {
entry.verifyConsistency(Map.entry(type, validator));
});
validators.put(type, validator);
}
public static HsEntityValidator forType(final HsHostingAssetType type) {
return validators.get(type);
}
public final HsPropertyValidator<?>[] propertyValidators;
public HsEntityValidator(final HsPropertyValidator<?>... validators) {
propertyValidators = validators;
}
public static Set<? extends Enum<?>> types() {
return validators.keySet();
}
public List<String> validate(final HsHostingAssetEntity assetEntity) {
public List<String> validate(final E assetEntity) {
final var result = new ArrayList<String>();
assetEntity.getConfig().keySet().forEach( givenPropName -> {
assetEntity.getProperties().keySet().forEach( givenPropName -> {
if (stream(propertyValidators).map(pv -> pv.propertyName).noneMatch(propName -> propName.equals(givenPropName))) {
result.add("'config." + givenPropName + "' is not expected but is '" +assetEntity.getConfig().get(givenPropName) + "'");
result.add("'config." + givenPropName + "' is not expected but is '" +assetEntity.getProperties().get(givenPropName) + "'");
}
});
stream(propertyValidators).forEach(pv -> {
result.addAll(pv.validate(assetEntity.getConfig()));
result.addAll(pv.validate(assetEntity.getProperties()));
});
return result;
}

View File

@ -2,7 +2,6 @@ package net.hostsharing.hsadminng.hs.validation;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
@ -52,7 +51,7 @@ public abstract class HsPropertyValidator<T> {
protected abstract void validate(final ArrayList<String> result, final T propValue, final Map<String, Object> props);
public void verifyConsistency(final Map.Entry<? extends Enum<?>, HsEntityValidator> typeDef) {
public void verifyConsistency(final Map.Entry<? extends Enum<?>, ?> typeDef) {
if (required == null ) {
throw new IllegalStateException(typeDef.getKey() + "[" + propertyName + "] not fully initialized, please call either .required() or .optional()" );
}

View File

@ -0,0 +1,11 @@
package net.hostsharing.hsadminng.hs.validation;
import java.util.Map;
public interface Validatable<E, T extends Enum<T>> {
Map<String, Object> getProperties();
Enum<T> getType();
}

View File

@ -1,6 +1,5 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import org.junit.jupiter.api.Test;
import java.util.Map;
@ -14,7 +13,7 @@ class HsManagedServerValidatorUnitTest {
@Test
void validatesDependentProperties() {
// given
final var validator = HsEntityValidator.forType(MANAGED_SERVER);
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_SERVER);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_SERVER)
.config(Map.ofEntries(

View File

@ -1,7 +1,6 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntity;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import org.junit.jupiter.api.Test;
@ -15,8 +14,10 @@ import static org.assertj.core.api.Assertions.assertThat;
class HsManagedWebspaceAssetValidatorUnitTest {
@SuppressWarnings("unused") // just to make sure the class is loaded
HsEntityValidator validator = new HsManagedWebspaceAssetValidator();
// just to make sure the class is loaded
static {
new HsManagedWebspaceAssetValidator();
}
final HsBookingItemEntity managedServerBookingItem = HsBookingItemEntity.builder()
.debitor(HsOfficeDebitorEntity.builder().defaultPrefix("abc").build()
@ -35,7 +36,7 @@ class HsManagedWebspaceAssetValidatorUnitTest {
@Test
void validatesIdentifier() {
// given
final var validator = HsEntityValidator.forType(MANAGED_WEBSPACE);
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.parentAsset(mangedServerAssetEntity)
@ -58,7 +59,7 @@ class HsManagedWebspaceAssetValidatorUnitTest {
@Test
void validatesMissingProperties() {
// given
final var validator = HsEntityValidator.forType(MANAGED_WEBSPACE);
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.parentAsset(mangedServerAssetEntity)
@ -79,7 +80,7 @@ class HsManagedWebspaceAssetValidatorUnitTest {
@Test
void validatesUnknownProperties() {
// given
final var validator = HsEntityValidator.forType(MANAGED_WEBSPACE);
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.parentAsset(mangedServerAssetEntity)
@ -102,7 +103,7 @@ class HsManagedWebspaceAssetValidatorUnitTest {
@Test
void validatesValidProperties() {
// given
final var validator = HsEntityValidator.forType(MANAGED_WEBSPACE);
final var validator = HsHostingAssetEntityValidators.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.parentAsset(mangedServerAssetEntity)