Search in sources :

Example 6 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class Value method parseHeaderInformation.

static void parseHeaderInformation(CharSequence text, String type, CSVHeaderInformation info) {
    Matcher mapMatcher = MAP_PATTERN.matcher(text);
    String errorMessage = format("Failed to parse %s value: '%s'", type, text);
    if (!(mapMatcher.find() && mapMatcher.groupCount() == 1)) {
        throw new InvalidArgumentException(errorMessage);
    }
    String mapContents = mapMatcher.group(1);
    if (mapContents.isEmpty()) {
        throw new InvalidArgumentException(errorMessage);
    }
    Matcher matcher = KEY_VALUE_PATTERN.matcher(mapContents);
    if (!(matcher.find())) {
        throw new InvalidArgumentException(errorMessage);
    }
    do {
        String key = matcher.group("k");
        if (key != null) {
            String value = matcher.group("v");
            if (value != null) {
                info.assign(key, value);
            }
        }
    } while (matcher.find());
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) Matcher(java.util.regex.Matcher)

Example 7 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class DurationValueTest method assertConstructorThrows.

private static void assertConstructorThrows(long months, long days, long seconds, long nanos) {
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> duration(months, days, seconds, nanos));
    assertThat(e.getMessage()).contains("Invalid value for duration").contains("months=" + months).contains("days=" + days).contains("seconds=" + seconds).contains("nanos=" + nanos);
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException)

Example 8 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class UserSerialization method deserializeCredentials.

private static Credential deserializeCredentials(String part, int lineNumber) throws FormatException {
    String[] split = part.split(CREDENTIAL_SEPARATOR, -1);
    String algorithm = split[0];
    int iterations;
    String hasherVersion;
    if (split.length == 4) {
        iterations = Integer.parseInt(split[3]);
    } else if (split.length == 3) {
        iterations = LegacyCredential.ITERATIONS;
    } else {
        throw new FormatException(format("wrong number of credential fields [line %d]", lineNumber));
    }
    try {
        hasherVersion = SecureHasherConfigurations.getVersionForConfiguration(algorithm, iterations);
    } catch (InvalidArgumentException e) {
        throw new FormatException(format("unknown digest \"%s\" [line %d]:", part, lineNumber));
    }
    if (hasherVersion.equals("0")) {
        byte[] decodedPassword = HexString.decodeHexString(split[1]);
        byte[] decodedSalt = HexString.decodeHexString(split[2]);
        return new LegacyCredential(decodedSalt, decodedPassword);
    } else {
        return SystemGraphCredential.deserialize(part, new SecureHasher(hasherVersion));
    }
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) SecureHasher(org.neo4j.cypher.internal.security.SecureHasher) HexString(org.neo4j.string.HexString) FormatException(org.neo4j.cypher.internal.security.FormatException)

Example 9 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class TimeValue method builder.

private static TimeBuilder<TimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new TimeBuilder<>(defaultZone) {

        @Override
        protected boolean supportsTimeZone() {
            return true;
        }

        @Override
        public TimeValue buildInternal() {
            boolean selectingTime = fields.containsKey(TemporalFields.time);
            boolean selectingTimeZone;
            OffsetTime result;
            if (selectingTime) {
                AnyValue time = fields.get(TemporalFields.time);
                if (!(time instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct time from: %s", time));
                }
                TemporalValue t = (TemporalValue) time;
                result = t.getTimePart(defaultZone);
                selectingTimeZone = t.supportsTimeZone();
            } else {
                ZoneId timezone = timezone();
                if (!(timezone instanceof ZoneOffset)) {
                    timezone = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                }
                result = defaultTime(timezone);
                selectingTimeZone = false;
            }
            result = assignAllFields(result);
            if (timezone != null) {
                ZoneOffset currentOffset = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                if (selectingTime && selectingTimeZone) {
                    result = result.withOffsetSameInstant(currentOffset);
                } else {
                    result = result.withOffsetSameLocal(currentOffset);
                }
            }
            return time(result);
        }

        @Override
        protected TimeValue selectTime(AnyValue temporal) {
            if (!(temporal instanceof TemporalValue)) {
                throw new InvalidArgumentException(String.format("Cannot construct time from: %s", temporal));
            }
            if (temporal instanceof TimeValue && timezone == null) {
                return (TimeValue) temporal;
            }
            TemporalValue v = (TemporalValue) temporal;
            OffsetTime time = v.getTimePart(defaultZone);
            if (timezone != null) {
                ZoneOffset currentOffset = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                time = time.withOffsetSameInstant(currentOffset);
            }
            return time(time);
        }
    };
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZoneId(java.time.ZoneId) OffsetTime(java.time.OffsetTime) AnyValue(org.neo4j.values.AnyValue) ZoneOffset(java.time.ZoneOffset)

Example 10 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class DateValueTest method shouldEnforceStrictWeekRanges.

@Test
void shouldEnforceStrictWeekRanges() {
    LocalDate localDate = weekDate(2017, 52, 7).temporal();
    assertEquals(DayOfWeek.SUNDAY, localDate.getDayOfWeek(), "Sunday is the seventh day of the week.");
    assertEquals(52, localDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR));
    assertEquals(localDate, date(2017, 12, 31).temporal());
    InvalidArgumentException expected = assertThrows(InvalidArgumentException.class, () -> weekDate(2017, 53, 1), "2017 does not have 53 weeks.");
    assertEquals("Year 2017 does not contain 53 weeks.", expected.getMessage());
    assertEquals(date(2016, 1, 1), weekDate(2015, 53, 5));
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidArgumentException (org.neo4j.exceptions.InvalidArgumentException)17 LocalDate (java.time.LocalDate)4 AnyValue (org.neo4j.values.AnyValue)4 LocalTime (java.time.LocalTime)3 ZoneId (java.time.ZoneId)3 LocalDateTime (java.time.LocalDateTime)2 ZoneOffset (java.time.ZoneOffset)2 Test (org.junit.jupiter.api.Test)2 TemporalParseException (org.neo4j.exceptions.TemporalParseException)2 UnsupportedTemporalUnitException (org.neo4j.exceptions.UnsupportedTemporalUnitException)2 NumberValue.safeCastFloatingPoint (org.neo4j.values.storable.NumberValue.safeCastFloatingPoint)2 OffsetTime (java.time.OffsetTime)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)1 ZoneRulesException (java.time.zone.ZoneRulesException)1 Matcher (java.util.regex.Matcher)1 FormatException (org.neo4j.cypher.internal.security.FormatException)1 SecureHasher (org.neo4j.cypher.internal.security.SecureHasher)1 CypherTypeException (org.neo4j.exceptions.CypherTypeException)1