[#12] Extend methods logic #13
2 changed files with 2 additions and 43 deletions
client/src/main/java/info/frostfs/sdk/utils
models/src/test/java/info/frostfs/sdk/mappers/object
|
@ -1,42 +0,0 @@
|
||||||
package info.frostfs.sdk.utils;
|
|
||||||
|
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
|
||||||
|
|
||||||
import javax.validation.ConstraintViolation;
|
|
||||||
import javax.validation.Validation;
|
|
||||||
import javax.validation.ValidationException;
|
|
||||||
import javax.validation.Validator;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static java.util.Objects.isNull;
|
|
||||||
|
|
||||||
public class ValidatorUtils {
|
|
||||||
private static final String OBJECT_IS_NULL = "object is null";
|
|
||||||
private static final String ERROR_PROPERTY_TEMPLATE = "property %s with value %s %s";
|
|
||||||
|
|
||||||
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
||||||
|
|
||||||
|
|
||||||
public static <T> void validate(T object, Class<?>... classes) {
|
|
||||||
if (isNull(object)) {
|
|
||||||
throw new ValidationException(OBJECT_IS_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<ConstraintViolation<T>> violations = validator.validate(object, classes);
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(violations)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] errors = (String[]) violations.stream()
|
|
||||||
.map(violation -> String.format(
|
|
||||||
ERROR_PROPERTY_TEMPLATE,
|
|
||||||
violation.getPropertyPath().toString(),
|
|
||||||
violation.getInvalidValue(),
|
|
||||||
violation.getMessage()
|
|
||||||
))
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
throw new ValidationException(String.join(",", errors));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,6 +8,7 @@ import info.frostfs.sdk.dto.object.ObjectAttribute;
|
||||||
import info.frostfs.sdk.dto.object.ObjectHeader;
|
import info.frostfs.sdk.dto.object.ObjectHeader;
|
||||||
import info.frostfs.sdk.dto.object.OwnerId;
|
import info.frostfs.sdk.dto.object.OwnerId;
|
||||||
import info.frostfs.sdk.enums.ObjectType;
|
import info.frostfs.sdk.enums.ObjectType;
|
||||||
|
import info.frostfs.sdk.mappers.response.MetaHeaderMapper;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.EnumSource;
|
import org.junit.jupiter.params.provider.EnumSource;
|
||||||
|
@ -55,7 +56,7 @@ public class ObjectHeaderMapperTest {
|
||||||
@Test
|
@Test
|
||||||
void toGrpcMessage_null() {
|
void toGrpcMessage_null() {
|
||||||
//When + Then
|
//When + Then
|
||||||
assertNull(ObjectHeaderMapper.toGrpcMessage(null));
|
assertThrows(IllegalArgumentException.class, () -> ObjectHeaderMapper.toGrpcMessage(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue