Skip to content

Commit f753879

Browse files
Merge pull request #190 from SpineEventEngine/vallidators-enum-boolean
Add Enum and Boolean fields validators
2 parents d3a3e86 + b934d1a commit f753879

File tree

12 files changed

+249
-69
lines changed

12 files changed

+249
-69
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.server.validate;
22+
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.protobuf.Descriptors;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.spine3.base.FieldPath;
28+
import org.spine3.validate.ConstraintViolation;
29+
30+
import java.util.List;
31+
32+
/**
33+
* Validates fields of type {@link Boolean}.
34+
*
35+
* @author Dmitry Kashcheiev
36+
*/
37+
/* package */ class BooleanFieldValidator extends FieldValidator<Boolean> {
38+
39+
/**
40+
* Creates a new validator instance.
41+
*
42+
* @param descriptor a descriptor of the field to validate
43+
* @param fieldValues values to validate
44+
* @param rootFieldPath a path to the root field (if present)
45+
*/
46+
/*package*/ BooleanFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Boolean> fieldValues, FieldPath rootFieldPath) {
47+
super(descriptor, fieldValues, rootFieldPath, false);
48+
}
49+
50+
/**
51+
* In Protobuf there is no way to tell if the value is {@code false} or was not set.
52+
*
53+
* @return false
54+
*/
55+
@Override
56+
protected boolean isValueNotSet(Boolean value) {
57+
return false;
58+
}
59+
60+
@Override
61+
protected List<ConstraintViolation> validate() {
62+
if (isRequiredField()) {
63+
log().warn("'required' option not allowed for boolean field");
64+
}
65+
return super.validate();
66+
}
67+
68+
private enum LogSingleton {
69+
INSTANCE;
70+
@SuppressWarnings("NonSerializableFieldInSerializableClass")
71+
private final Logger value = LoggerFactory.getLogger(BooleanFieldValidator.class);
72+
}
73+
74+
private static Logger log() {
75+
return LogSingleton.INSTANCE.value;
76+
}
77+
}

server/src/main/java/org/spine3/server/validate/ByteStringFieldValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected void validateEntityId() {
6767

6868
@Override
6969
protected boolean isValueNotSet(ByteString value) {
70-
final boolean isNotSet = value.isEmpty();
71-
return isNotSet;
70+
final boolean result = value.isEmpty();
71+
return result;
7272
}
7373
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.server.validate;
22+
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.protobuf.Descriptors;
25+
import org.spine3.base.FieldPath;
26+
import org.spine3.validate.ConstraintViolation;
27+
28+
import java.util.List;
29+
30+
/**
31+
* Validates fields of type {@link Descriptors.EnumValueDescriptor}.
32+
*
33+
* @author Dmitry Kashcheiev
34+
*/
35+
/* package */ class EnumFieldValidator extends FieldValidator<Descriptors.EnumValueDescriptor> {
36+
37+
/**
38+
* Creates a new validator instance.
39+
*
40+
* @param descriptor a descriptor of the field to validate
41+
* @param fieldValues values to validate
42+
* @param rootFieldPath a path to the root field (if present)
43+
*/
44+
/*package*/ EnumFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Descriptors.EnumValueDescriptor> fieldValues, FieldPath rootFieldPath) {
45+
super(descriptor, fieldValues, rootFieldPath, false);
46+
}
47+
48+
@Override
49+
protected boolean isValueNotSet(Descriptors.EnumValueDescriptor value) {
50+
final int intValue = value.getNumber();
51+
final boolean result = intValue == 0;
52+
return result;
53+
}
54+
55+
@Override
56+
protected List<ConstraintViolation> validate() {
57+
checkIfRequiredAndNotSet();
58+
final List<ConstraintViolation> violations = super.validate();
59+
return violations;
60+
}
61+
}

server/src/main/java/org/spine3/server/validate/FieldValidator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
* @param descr a descriptor of the field to validate
7070
* @param values values to validate
7171
* @param rootFieldPath a path to the root field (if present)
72-
* @param strict if {@code true} the validator would assume that the field is required event
72+
* @param strict if {@code true} the validator would assume that the field is required, even
7373
* if corresponding field option is not present
7474
*/
7575
protected FieldValidator(FieldDescriptor descr, ImmutableList<V> values, FieldPath rootFieldPath, boolean strict) {
@@ -132,6 +132,14 @@ protected void validateEntityId() {
132132
}
133133
}
134134

135+
/**
136+
* Returns {@code true} if the field has required attribute or validation is strict.
137+
*/
138+
protected boolean isRequiredField() {
139+
final boolean result = requiredOption.getValue() || strict;
140+
return result;
141+
}
142+
135143
/**
136144
* Checks if the field is required and not set and adds violations found.
137145
*
@@ -140,7 +148,7 @@ protected void validateEntityId() {
140148
* <p>It is required to override {@link #isValueNotSet(Object)} method to use this one.
141149
*/
142150
protected void checkIfRequiredAndNotSet() {
143-
if (!requiredOption.getValue() && !strict) {
151+
if (!isRequiredField()) {
144152
return;
145153
}
146154
if (values.isEmpty()) {

server/src/main/java/org/spine3/server/validate/FieldValidatorFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.google.common.collect.ImmutableList;
2424
import com.google.protobuf.ByteString;
25+
import com.google.protobuf.Descriptors;
2526
import com.google.protobuf.Descriptors.FieldDescriptor;
2627
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
2728
import com.google.protobuf.Message;
@@ -77,7 +78,11 @@ private static FieldValidator<?> create(FieldDescriptor descriptor,
7778
final ImmutableList<ByteString> byteStrings = toValueList(fieldValue);
7879
return new ByteStringFieldValidator(descriptor, byteStrings, rootFieldPath);
7980
case BOOLEAN:
81+
final ImmutableList<Boolean> booleans = toValueList(fieldValue);
82+
return new BooleanFieldValidator(descriptor, booleans, rootFieldPath);
8083
case ENUM:
84+
final ImmutableList<Descriptors.EnumValueDescriptor> enums = toValueList(fieldValue);
85+
return new EnumFieldValidator(descriptor, enums, rootFieldPath);
8186
default:
8287
throw fieldTypeIsNotSupported(descriptor);
8388
}

server/src/main/java/org/spine3/server/validate/MessageFieldValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ protected List<ConstraintViolation> validate() {
8484

8585
@Override
8686
protected boolean isValueNotSet(Message value) {
87-
final boolean isNotSet = isDefault(value);
88-
return isNotSet;
87+
final boolean result = isDefault(value);
88+
return result;
8989
}
9090

9191
private void validateFieldsOfMessageIfNeeded() {

server/src/main/java/org/spine3/server/validate/NumberFieldValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ protected List<ConstraintViolation> validate() {
101101
@Override
102102
protected boolean isValueNotSet(V value) {
103103
final int intValue = value.intValue();
104-
final boolean isNotSet = intValue == 0;
105-
return isNotSet;
104+
final boolean result = intValue == 0;
105+
return result;
106106
}
107107

108108
private void validateRangeOptions(V value) {

server/src/main/java/org/spine3/server/validate/StringFieldValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private ConstraintViolation newViolation(String fieldValue) {
9292

9393
@Override
9494
protected boolean isValueNotSet(String value) {
95-
final boolean isNotSet = value.isEmpty();
96-
return isNotSet;
95+
final boolean result = value.isEmpty();
96+
return result;
9797
}
9898
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.server.validate;
22+
23+
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.protobuf.Any;
26+
import org.junit.Test;
27+
import org.spine3.base.FieldPath;
28+
29+
import static org.junit.Assert.assertFalse;
30+
31+
/**
32+
* @author dmitry.kashcheiev
33+
*/
34+
public class BooleanFieldValidatorShould {
35+
36+
private final BooleanFieldValidator validator =
37+
new BooleanFieldValidator(Any.getDescriptor().getFields().get(0), ImmutableList.of(false), FieldPath.getDefaultInstance());
38+
39+
@Test
40+
public void convert_string_to_number() {
41+
assertFalse(validator.isValueNotSet(false));
42+
}
43+
}

server/src/test/java/org/spine3/server/validate/FieldValidatorFactoryShould.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import com.google.protobuf.StringValue;
2929
import org.junit.Test;
3030
import org.spine3.base.FieldPath;
31-
import org.spine3.test.validate.msg.AnnotatedEnumFieldValue;
3231
import org.spine3.test.validate.msg.RequiredByteStringFieldValue;
32+
import org.spine3.test.validate.msg.RequiredEnumFieldValue;
3333
import org.spine3.test.validate.msg.RequiredMsgFieldValue;
3434

3535
import static com.google.protobuf.Descriptors.FieldDescriptor;
@@ -107,17 +107,21 @@ public void create_ByteString_field_validator() {
107107
assertTrue(validator instanceof ByteStringFieldValidator);
108108
}
109109

110-
@Test(expected = IllegalArgumentException.class)
111-
public void throw_exception_if_pass_enum_field() {
112-
final FieldDescriptor field = AnnotatedEnumFieldValue.getDescriptor().getFields().get(0);
110+
@Test
111+
public void create_Enum_field_validator() {
112+
final FieldDescriptor field = RequiredEnumFieldValue.getDescriptor().getFields().get(0);
113+
114+
final FieldValidator validator = create(field, new Object(), FIELD_PATH);
113115

114-
create(field, new Object(), FIELD_PATH);
116+
assertTrue(validator instanceof EnumFieldValidator);
115117
}
116118

117-
@Test(expected = IllegalArgumentException.class)
118-
public void throw_exception_if_pass_boolean_field() {
119+
@Test
120+
public void create_Boolean_field_validator() {
119121
final FieldDescriptor field = BoolValue.getDescriptor().getFields().get(0);
120122

121-
create(field, new Object(), FIELD_PATH);
123+
final FieldValidator validator = create(field, new Object(), FIELD_PATH);
124+
125+
assertTrue(validator instanceof BooleanFieldValidator);
122126
}
123127
}

0 commit comments

Comments
 (0)