aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidator.java47
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidatorTest.java50
2 files changed, 51 insertions, 46 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidator.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidator.java
index f2745c95ea..53bfc1ee3c 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidator.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidator.java
@@ -19,18 +19,25 @@
*/
package org.openecomp.sdc.be.model.tosca.validators;
+import java.math.BigInteger;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
import org.openecomp.sdc.be.model.DataTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class IntegerValidator implements PropertyTypeValidator {
- private static IntegerValidator integerValidator = new IntegerValidator();
- private PatternBase base8Pattern = new PatternBase(Pattern.compile("([-+])?0o([0-7]+)"), 8);
- private PatternBase base10Pattern = new PatternBase(Pattern.compile("([-+])?(0|[1-9][0-9]*)"), 10);
- private PatternBase base16Pattern = new PatternBase(Pattern.compile("([-+])?0x([0-9a-fA-F]+)"), 16);
- private PatternBase[] patterns = {base10Pattern, base8Pattern, base16Pattern};
+ private static final Logger log = LoggerFactory.getLogger(IntegerValidator.class);
+
+ private static final IntegerValidator integerValidator = new IntegerValidator();
+ private final PatternBase base8Pattern = new PatternBase(Pattern.compile("([-+])?0o([0-7]+)"), 8);
+ private final PatternBase base10Pattern = new PatternBase(Pattern.compile("([-+])?(0|[1-9][0-9]*)"), 10);
+ private final PatternBase base16Pattern = new PatternBase(Pattern.compile("([-+])?0x([0-9a-fA-F]+)"), 16);
+ private final PatternBase[] patterns = {base10Pattern, base8Pattern, base16Pattern};
private IntegerValidator() {
}
@@ -40,21 +47,19 @@ public class IntegerValidator implements PropertyTypeValidator {
}
@Override
- public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
+ public boolean isValid(final String value, final String innerType, final Map<String, DataTypeDefinition> allDataTypes) {
+
if (value == null || value.isEmpty()) {
return true;
}
- for (PatternBase patternBase : patterns) {
- Matcher matcher = patternBase.pattern.matcher(value);
- Long parsed = null;
+ for (final PatternBase patternBase : patterns) {
+ final Matcher matcher = patternBase.getPattern().matcher(value);
if (matcher.matches()) {
try {
- parsed = Long.parseLong(matcher.group(2), patternBase.base);
- if (matcher.group(1) != null && matcher.group(1).compareTo("-") == 0) {
- parsed *= -1;
- }
- return (Integer.MIN_VALUE <= parsed && parsed <= (Integer.MAX_VALUE)) ? true : false;
- } catch (NumberFormatException e) {
+ new BigInteger(matcher.group(2), patternBase.base);
+ return true;
+ } catch (Exception e) {
+ log.warn("Failed to build BigInteger {}", value, e);
return false;
}
}
@@ -63,18 +68,16 @@ public class IntegerValidator implements PropertyTypeValidator {
}
@Override
- public boolean isValid(String value, String innerType) {
+ public boolean isValid(final String value, final String innerType) {
return isValid(value, innerType, null);
}
+ @Getter
+ @AllArgsConstructor
private class PatternBase {
- Pattern pattern;
- Integer base;
+ private final Pattern pattern;
+ private final Integer base;
- public PatternBase(Pattern pattern, Integer base) {
- this.pattern = pattern;
- this.base = base;
- }
}
}
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidatorTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidatorTest.java
index b33f1d2310..597ed25443 100644
--- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidatorTest.java
+++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/validators/IntegerValidatorTest.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,16 +20,17 @@
package org.openecomp.sdc.be.model.tosca.validators;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.junit.jupiter.api.Test;
-public class IntegerValidatorTest {
- private static IntegerValidator validator = IntegerValidator.getInstance();
+class IntegerValidatorTest {
+
+ private static final IntegerValidator validator = IntegerValidator.getInstance();
@Test
- public void testIntegerValidatorDecimal() {
+ void testIntegerValidatorDecimal() {
assertTrue(validator.isValid(null, null));
assertTrue(validator.isValid("", null));
assertTrue(validator.isValid("0", null));
@@ -37,33 +38,34 @@ public class IntegerValidatorTest {
assertTrue(validator.isValid("-0", null));
assertTrue(validator.isValid("+65465", null));
assertTrue(validator.isValid("-65465", null));
- assertTrue(validator.isValid("2147483647", null));
- assertFalse(validator.isValid("2147483648", null));
- assertTrue(validator.isValid("-2147483648", null));
- assertFalse(validator.isValid("-2147483649", null));
+ assertTrue(validator.isValid("9223372036854775807", null));
+ assertTrue(validator.isValid("92233720368547758079223372036854775807", null));
+ assertTrue(validator.isValid("-9223372036854775808", null));
+ assertTrue(validator.isValid("-92233720368547758089223372036854775808", null));
}
@Test
- public void testIntegerValidatorHexa() {
+ void testIntegerValidatorHexa() {
assertTrue(validator.isValid("-0xadc", null));
assertTrue(validator.isValid("+0xadf", null));
- assertTrue(validator.isValid("0x7FFFFFFF", null));
- assertFalse(validator.isValid("0x80000000", null));
- assertTrue(validator.isValid("-0x80000000", null));
- assertFalse(validator.isValid("-0x80000001", null));
+ assertTrue(validator.isValid("0x7FFFFFFFFFFFFFFF", null));
+ assertTrue(validator.isValid("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", null));
+ assertTrue(validator.isValid("-0x8000000000000000", null));
+ assertTrue(validator.isValid("-0x8000000000000000000000000000000", null));
}
- public void testIntegerValidatorOctal() {
- assertTrue(validator.isValid("0o545435", null));
+ @Test
+ void testIntegerValidatorOctal() {
+ assertTrue(validator.isValid("+0o545435", null));
assertTrue(validator.isValid("-0o545435", null));
- assertTrue(validator.isValid("0o17777777777", null));
- assertFalse(validator.isValid("0o20000000000", null));
- assertTrue(validator.isValid("-0o20000000000", null));
- assertFalse(validator.isValid("-0o20000000001", null));
+ assertTrue(validator.isValid("0o777777777777777777777", null));
+ assertTrue(validator.isValid("0o777777777777777777777777777777777777777777", null));
+ assertTrue(validator.isValid("-0o1000000000000000000000", null));
+ assertTrue(validator.isValid("-0o1000000000000000000000000000000000000000000", null));
}
@Test
- public void testIntegerValidatorIncorrect() {
+ void testIntegerValidatorIncorrect() {
assertFalse(validator.isValid("-2.147483649", null));
assertFalse(validator.isValid("dsfasf342342", null));
}