Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2016, TeamDev Ltd. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.spine3.server.validate;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Descriptors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spine3.base.FieldPath;
import org.spine3.validate.ConstraintViolation;

import java.util.List;

/**
* Validates fields of type {@link Boolean}.
*
* @author Dmitry Kashcheiev
*/
/* package */ class BooleanFieldValidator extends FieldValidator<Boolean> {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove excessive empty line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* Creates a new validator instance.
*
* @param descriptor a descriptor of the field to validate
* @param fieldValues values to validate
* @param rootFieldPath a path to the root field (if present)
*/
/*package*/ BooleanFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Boolean> fieldValues, FieldPath rootFieldPath) {
super(descriptor, fieldValues, rootFieldPath, false);
}

/**
* In Protobuf there is no way to tell if the value is {@code false} or was not set.
*
* @return false
*/
@Override
protected boolean isValueNotSet(Boolean value) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test this. Do you have a Codecov browser plug-in installed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

@Override
protected List<ConstraintViolation> validate() {
if (isRequiredField()) {
log().warn("'required' option not allowed for boolean field");
}
return super.validate();
}

private enum LogSingleton {
INSTANCE;
@SuppressWarnings("NonSerializableFieldInSerializableClass")
private final Logger value = LoggerFactory.getLogger(BooleanFieldValidator.class);
}

private static Logger log() {
return LogSingleton.INSTANCE.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void validateEntityId() {

@Override
protected boolean isValueNotSet(ByteString value) {
final boolean isNotSet = value.isEmpty();
return isNotSet;
final boolean result = value.isEmpty();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2016, TeamDev Ltd. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.spine3.server.validate;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Descriptors;
import org.spine3.base.FieldPath;
import org.spine3.validate.ConstraintViolation;

import java.util.List;

/**
* Validates fields of type {@link Descriptors.EnumValueDescriptor}.
*
* @author Dmitry Kashcheiev
*/
/* package */ class EnumFieldValidator extends FieldValidator<Descriptors.EnumValueDescriptor> {

/**
* Creates a new validator instance.
*
* @param descriptor a descriptor of the field to validate
* @param fieldValues values to validate
* @param rootFieldPath a path to the root field (if present)
*/
/*package*/ EnumFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Descriptors.EnumValueDescriptor> fieldValues, FieldPath rootFieldPath) {
super(descriptor, fieldValues, rootFieldPath, false);
}

@Override
protected boolean isValueNotSet(Descriptors.EnumValueDescriptor value) {
final int intValue = value.getNumber();
final boolean result = intValue == 0;
return result;
}

@Override
protected List<ConstraintViolation> validate() {
checkIfRequiredAndNotSet();
final List<ConstraintViolation> violations = super.validate();
return violations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* @param descr a descriptor of the field to validate
* @param values values to validate
* @param rootFieldPath a path to the root field (if present)
* @param strict if {@code true} the validator would assume that the field is required event
* @param strict if {@code true} the validator would assume that the field is required, even
* if corresponding field option is not present
*/
protected FieldValidator(FieldDescriptor descr, ImmutableList<V> values, FieldPath rootFieldPath, boolean strict) {
Expand Down Expand Up @@ -132,6 +132,14 @@ protected void validateEntityId() {
}
}

/**
* Returns {@code true} if the field has required attribute or validation is strict.
*/
protected boolean isRequiredField() {
final boolean result = requiredOption.getValue() || strict;
return result;
}

/**
* Checks if the field is required and not set and adds violations found.
*
Expand All @@ -140,7 +148,7 @@ protected void validateEntityId() {
* <p>It is required to override {@link #isValueNotSet(Object)} method to use this one.
*/
protected void checkIfRequiredAndNotSet() {
if (!requiredOption.getValue() && !strict) {
if (!isRequiredField()) {
return;
}
if (values.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
import com.google.protobuf.Message;
Expand Down Expand Up @@ -77,7 +78,11 @@ private static FieldValidator<?> create(FieldDescriptor descriptor,
final ImmutableList<ByteString> byteStrings = toValueList(fieldValue);
return new ByteStringFieldValidator(descriptor, byteStrings, rootFieldPath);
case BOOLEAN:
final ImmutableList<Boolean> booleans = toValueList(fieldValue);
return new BooleanFieldValidator(descriptor, booleans, rootFieldPath);
case ENUM:
final ImmutableList<Descriptors.EnumValueDescriptor> enums = toValueList(fieldValue);
return new EnumFieldValidator(descriptor, enums, rootFieldPath);
default:
throw fieldTypeIsNotSupported(descriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ protected List<ConstraintViolation> validate() {

@Override
protected boolean isValueNotSet(Message value) {
final boolean isNotSet = isDefault(value);
return isNotSet;
final boolean result = isDefault(value);
return result;
}

private void validateFieldsOfMessageIfNeeded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ protected List<ConstraintViolation> validate() {
@Override
protected boolean isValueNotSet(V value) {
final int intValue = value.intValue();
final boolean isNotSet = intValue == 0;
return isNotSet;
final boolean result = intValue == 0;
return result;
}

private void validateRangeOptions(V value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private ConstraintViolation newViolation(String fieldValue) {

@Override
protected boolean isValueNotSet(String value) {
final boolean isNotSet = value.isEmpty();
return isNotSet;
final boolean result = value.isEmpty();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2016, TeamDev Ltd. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.spine3.server.validate;


import com.google.common.collect.ImmutableList;
import com.google.protobuf.Any;
import org.junit.Test;
import org.spine3.base.FieldPath;

import static org.junit.Assert.assertFalse;

/**
* @author dmitry.kashcheiev
*/
public class BooleanFieldValidatorShould {

private final BooleanFieldValidator validator =
new BooleanFieldValidator(Any.getDescriptor().getFields().get(0), ImmutableList.of(false), FieldPath.getDefaultInstance());

@Test
public void convert_string_to_number() {
assertFalse(validator.isValueNotSet(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import com.google.protobuf.StringValue;
import org.junit.Test;
import org.spine3.base.FieldPath;
import org.spine3.test.validate.msg.AnnotatedEnumFieldValue;
import org.spine3.test.validate.msg.RequiredByteStringFieldValue;
import org.spine3.test.validate.msg.RequiredEnumFieldValue;
import org.spine3.test.validate.msg.RequiredMsgFieldValue;

import static com.google.protobuf.Descriptors.FieldDescriptor;
Expand Down Expand Up @@ -107,17 +107,21 @@ public void create_ByteString_field_validator() {
assertTrue(validator instanceof ByteStringFieldValidator);
}

@Test(expected = IllegalArgumentException.class)
public void throw_exception_if_pass_enum_field() {
final FieldDescriptor field = AnnotatedEnumFieldValue.getDescriptor().getFields().get(0);
@Test
public void create_Enum_field_validator() {
final FieldDescriptor field = RequiredEnumFieldValue.getDescriptor().getFields().get(0);

final FieldValidator validator = create(field, new Object(), FIELD_PATH);

create(field, new Object(), FIELD_PATH);
assertTrue(validator instanceof EnumFieldValidator);
}

@Test(expected = IllegalArgumentException.class)
public void throw_exception_if_pass_boolean_field() {
@Test
public void create_Boolean_field_validator() {
final FieldDescriptor field = BoolValue.getDescriptor().getFields().get(0);

create(field, new Object(), FIELD_PATH);
final FieldValidator validator = create(field, new Object(), FIELD_PATH);

assertTrue(validator instanceof BooleanFieldValidator);
}
}
Loading