Skip to content

Commit

Permalink
Implement additional single-entity builders and run more spec tests (#…
Browse files Browse the repository at this point in the history
…9038)

Support the setup of the additional database entities so that many more of the JS REST API spec tests can be successfully executed. This PR is a progression toward getting all entity types support.
---------

Signed-off-by: Jeff Schmidt <[email protected]>
  • Loading branch information
Jeff Schmidt authored Aug 16, 2024
1 parent c3eea1a commit fdb7967
Show file tree
Hide file tree
Showing 21 changed files with 853 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import jakarta.inject.Named;
import java.nio.charset.StandardCharsets;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

@Named
@ConfigurationPropertiesBinding
public class ByteArrayFromStringConverter implements Converter<String, byte[]> {
@Override
public byte[] convert(String source) {
return StringUtils.hasLength(source) ? source.getBytes(StandardCharsets.UTF_8) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import com.hedera.mirror.common.domain.entity.EntityId;
import jakarta.inject.Named;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;

@Named
@ConfigurationPropertiesBinding
public class EntityIdFromIntegerConverter implements Converter<Integer, EntityId> {
@Override
public EntityId convert(Integer entityId) {
return entityId != null ? EntityId.of(entityId) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import jakarta.inject.Named;
import java.util.regex.Pattern;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

@Named
@ConfigurationPropertiesBinding
@SuppressWarnings("java:S5842") // Upper and lower bounds in regex may be empty and must still match.
public class RangeFromStringConverter implements Converter<String, Range<Long>> {
private static final String LOWER_CLOSED = "[";
private static final String UPPER_CLOSED = "]";

private static final String RANGE_REGEX = "^([\\[(])?(\\d*)?,(\\d*)?([])])$";
private static final Pattern RANGE_PATTERN = Pattern.compile(RANGE_REGEX);

@Override
public Range<Long> convert(String source) {
if (!StringUtils.hasText(source)) {
return null;
}

var matcher = RANGE_PATTERN.matcher(source);
if (!matcher.matches()) {
throw new IllegalArgumentException("Range string is not valid, '%s'".formatted(source));
}

var lowerValueStr = matcher.group(2);
var lowerValue = StringUtils.hasText(lowerValueStr) ? Long.parseLong(lowerValueStr) : null;

var upperValueStr = matcher.group(3);
var upperValue = StringUtils.hasText(upperValueStr) ? Long.parseLong(upperValueStr) : null;
var upperBoundType = UPPER_CLOSED.equals(matcher.group(4)) ? BoundType.CLOSED : BoundType.OPEN;

Range<Long> range;
if (lowerValue != null) {
var lowerBoundType = LOWER_CLOSED.equals(matcher.group(1)) ? BoundType.CLOSED : BoundType.OPEN;
range = upperValue != null
? Range.range(lowerValue, lowerBoundType, upperValue, upperBoundType)
: Range.downTo(lowerValue, lowerBoundType);
} else {
range = upperValue != null ? Range.upTo(upperValue, upperBoundType) : Range.all();
}

return range;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;

class ByteArrayFromStringConverterTest {

@ParameterizedTest(name = "Convert \"{0}\" to EntityId")
@MethodSource("provideTestCases")
void testConverter(String source, byte[] expected) {
var converter = new ByteArrayFromStringConverter();
assertThat(converter.convert(source)).isEqualTo(expected);
}

@ParameterizedTest(name = "Convert \"{0}\" to EntityId")
@NullAndEmptySource
void testInvalidSource(String source) {
var converter = new ByteArrayFromStringConverter();
assertThat(converter.convert(source)).isNull();
}

private static Stream<Arguments> provideTestCases() {
return Stream.of(
Arguments.of("0", new byte[] {0x30}),
Arguments.of("01", new byte[] {0x30, 0x31}),
Arguments.of("hashgraph", "hashgraph".getBytes(StandardCharsets.UTF_8)),
Arguments.of("", null),
Arguments.of(" ", new byte[] {0x20}),
Arguments.of("\t\n", new byte[] {0x09, 0x0a}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullSource;

class EntityIdFromIntegerConverterTest {

@ParameterizedTest(name = "Convert {0} to EntityId")
@CsvSource({"0, 0.0.0", "1, 0.0.1", "1001, 0.0.1001"})
void testConverter(Integer source, String expected) {
var converter = new EntityIdFromIntegerConverter();
assertThat(converter.convert(source)).hasToString(expected);
}

@ParameterizedTest(name = "Convert {0} to EntityId")
@NullSource
void testNullSource(Integer source) {
var converter = new EntityIdFromIntegerConverter();
assertThat(converter.convert(source)).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.restjava.converter;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.common.collect.Range;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

class RangeFromStringConverterTest {

@ParameterizedTest(name = "Convert \"{0}\" to Range")
@CsvSource(
delimiterString = "#",
textBlock =
"""
[1,100)# [1..100)
[1,100]# [1..100]
[1,)# [1..+∞)
(,100]# (-∞..100]
(,)# (-∞..+∞)
""")
void testConverter(String source, String expected) {
var converter = new RangeFromStringConverter();
Range<Long> range = converter.convert(source);
assertThat(range).hasToString(expected);
}

@ParameterizedTest(name = "Convert \"{0}\" to Range")
@NullAndEmptySource
void testInvalidSource(String source) {
var converter = new RangeFromStringConverter();
assertThat(converter.convert(source)).isNull();
}

@ParameterizedTest(name = "Fail to convert \"{0}\" to Range")
@ValueSource(strings = {"bad", "[1,100}", "[A,$)", "12,13", ",", "[)"})
void testConverterFailures(String source) {
var converter = new RangeFromStringConverter();
assertThrows(IllegalArgumentException.class, () -> converter.convert(source));
}
}
Loading

0 comments on commit fdb7967

Please sign in to comment.