diff --git a/src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java b/src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java index 71974a95..eed86a5e 100644 --- a/src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java +++ b/src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java @@ -3,16 +3,19 @@ */ package com.arangodb.springframework.config; -import java.io.IOException; -import java.lang.annotation.Annotation; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; - +import com.arangodb.ArangoDB; +import com.arangodb.ArangoDBException; import com.arangodb.ContentType; +import com.arangodb.RequestContext; import com.arangodb.serde.ArangoSerde; import com.arangodb.serde.jackson.JacksonMapperProvider; +import com.arangodb.springframework.annotation.*; +import com.arangodb.springframework.core.ArangoOperations; +import com.arangodb.springframework.core.convert.*; +import com.arangodb.springframework.core.convert.resolver.*; +import com.arangodb.springframework.core.mapping.ArangoMappingContext; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; +import com.arangodb.springframework.core.template.ArangoTemplate; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; @@ -22,31 +25,11 @@ import org.springframework.data.mapping.model.FieldNamingStrategy; import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; -import com.arangodb.ArangoDB; -import com.arangodb.ArangoDBException; -import com.arangodb.springframework.annotation.Document; -import com.arangodb.springframework.annotation.Edge; -import com.arangodb.springframework.annotation.From; -import com.arangodb.springframework.annotation.Ref; -import com.arangodb.springframework.annotation.Relations; -import com.arangodb.springframework.annotation.To; -import com.arangodb.springframework.core.ArangoOperations; -import com.arangodb.springframework.core.convert.ArangoConverter; -import com.arangodb.springframework.core.convert.ArangoCustomConversions; -import com.arangodb.springframework.core.convert.ArangoTypeMapper; -import com.arangodb.springframework.core.convert.DefaultArangoConverter; -import com.arangodb.springframework.core.convert.DefaultArangoTypeMapper; -import com.arangodb.springframework.core.convert.resolver.DocumentFromResolver; -import com.arangodb.springframework.core.convert.resolver.DocumentToResolver; -import com.arangodb.springframework.core.convert.resolver.EdgeFromResolver; -import com.arangodb.springframework.core.convert.resolver.EdgeToResolver; -import com.arangodb.springframework.core.convert.resolver.RefResolver; -import com.arangodb.springframework.core.convert.resolver.ReferenceResolver; -import com.arangodb.springframework.core.convert.resolver.RelationResolver; -import com.arangodb.springframework.core.convert.resolver.RelationsResolver; -import com.arangodb.springframework.core.convert.resolver.ResolverFactory; -import com.arangodb.springframework.core.mapping.ArangoMappingContext; -import com.arangodb.springframework.core.template.ArangoTemplate; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; /** * Defines methods to customize the Java-based configuration for Spring Data @@ -104,9 +87,14 @@ public byte[] serialize(Object value) { @Override public T deserialize(byte[] content, Class clazz) { + return deserialize(content, clazz, RequestContext.EMPTY); + } + + @Override + public T deserialize(byte[] content, Class clazz, RequestContext ctx) { try { - return converter.read(clazz, om.readTree(content)); - } catch (IOException e) { + return converter.read(clazz, new ArangoJsonNode(om.readTree(content), new TransactionMappingContext(ctx))); + } catch (Exception e) { throw new MappingException("Exception while deserializing.", e); } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java b/src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java index b2de69b0..7858f927 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java +++ b/src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java @@ -20,14 +20,18 @@ package com.arangodb.springframework.core.convert; -import org.springframework.data.convert.EntityReader; - +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import com.fasterxml.jackson.databind.JsonNode; +import org.springframework.data.convert.EntityReader; /** * @author Mark Vollmary * @author Christian Lechner */ -public interface ArangoEntityReader extends EntityReader { +public interface ArangoEntityReader extends EntityReader { + + default R read(Class type, JsonNode source) { + return read(type, new ArangoJsonNode(source, TransactionMappingContext.EMPTY)); + } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/ArangoJsonNode.java b/src/main/java/com/arangodb/springframework/core/convert/ArangoJsonNode.java new file mode 100644 index 00000000..80f54b0b --- /dev/null +++ b/src/main/java/com/arangodb/springframework/core/convert/ArangoJsonNode.java @@ -0,0 +1,13 @@ +package com.arangodb.springframework.core.convert; + +import com.arangodb.springframework.core.mapping.TransactionMappingContext; +import com.fasterxml.jackson.databind.JsonNode; + +/** + * JsonNode with TransactionMappingContext + */ +public record ArangoJsonNode( + JsonNode value, + TransactionMappingContext transactionContext +) { +} diff --git a/src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java b/src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java index 7ff009a9..06f6e638 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java +++ b/src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java @@ -32,9 +32,7 @@ import com.arangodb.springframework.core.convert.resolver.ReferenceResolver; import com.arangodb.springframework.core.convert.resolver.RelationResolver; import com.arangodb.springframework.core.convert.resolver.ResolverFactory; -import com.arangodb.springframework.core.mapping.ArangoPersistentEntity; -import com.arangodb.springframework.core.mapping.ArangoPersistentProperty; -import com.arangodb.springframework.core.mapping.ArangoSimpleTypes; +import com.arangodb.springframework.core.mapping.*; import com.arangodb.springframework.core.util.MetadataUtils; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -116,32 +114,41 @@ public ArangoTypeMapper getTypeMapper() { @SuppressWarnings("unchecked") @Override - public R read(final Class type, final JsonNode source) { + public R read(Class type, ArangoJsonNode source) { return (R) readInternal(TypeInformation.of(type), source); } - private Object readInternal(final TypeInformation type, final JsonNode source) { - if (source == null) { + private Object readInternal(final TypeInformation type, final ArangoJsonNode source) { + JsonNode value = source.value(); + if (value == null) { return null; } - if (JsonNode.class.isAssignableFrom(type.getType())) { + if (ArangoJsonNode.class.isAssignableFrom(type.getType())) { return source; } - TypeInformation typeToUse = (source.isArray() || source.isObject()) ? typeMapper.readType(source, type) + if (JsonNode.class.isAssignableFrom(type.getType())) { + return value; + } + + TypeInformation typeToUse = (value.isArray() || value.isObject()) ? typeMapper.readType(value, type) : type; Class rawTypeToUse = typeToUse.getType(); - if (conversions.hasCustomReadTarget(JsonNode.class, typeToUse.getType())) { + if (conversions.hasCustomReadTarget(ArangoJsonNode.class, typeToUse.getType())) { return conversionService.convert(source, rawTypeToUse); } + if (conversions.hasCustomReadTarget(JsonNode.class, typeToUse.getType())) { + return conversionService.convert(value, rawTypeToUse); + } + if (conversions.hasCustomReadTarget(DBDocumentEntity.class, typeToUse.getType())) { return conversionService.convert(readSimple(DBDocumentEntity.class, source), rawTypeToUse); } - if (!source.isArray() && !source.isObject()) { + if (!value.isArray() && !value.isObject()) { return convertIfNecessary(readSimple(rawTypeToUse, source), rawTypeToUse); } @@ -161,8 +168,8 @@ private Object readInternal(final TypeInformation type, final JsonNode source return readMap(typeToUse, source); } - if (!source.isArray() && (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class))) { - return readMap(TypeInformation.MAP, source); + if (!value.isArray() && (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class))) { + return readMap(TypeInformation.MAP, source); } if (typeToUse.getType().isArray()) { @@ -173,8 +180,8 @@ private Object readInternal(final TypeInformation type, final JsonNode source return readCollection(typeToUse, source); } - if (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class)) { - return readCollection(TypeInformation.COLLECTION, source); + if (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class)) { + return readCollection(TypeInformation.COLLECTION, source); } ArangoPersistentEntity entity = context.getRequiredPersistentEntity(rawTypeToUse); @@ -183,12 +190,13 @@ private Object readInternal(final TypeInformation type, final JsonNode source private Object readEntity( final TypeInformation type, - final JsonNode source, - final ArangoPersistentEntity entity) { - - if (!source.isObject()) { + final ArangoJsonNode source, + final ArangoPersistentEntity entity + ) { + JsonNode value = source.value(); + if (!value.isObject()) { throw new MappingException( - String.format("Can't read entity type %s from type %s!", type, source.getNodeType())); + String.format("Can't read entity type %s from type %s!", type, value.getNodeType())); } EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); @@ -196,21 +204,21 @@ private Object readEntity( Object instance = instantiator.createInstance(entity, provider); PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance); - JsonNode idNode = getOrMissing(source, _ID); + JsonNode idNode = getOrMissing(value, _ID); String id = idNode.isTextual() ? idNode.textValue() : null; entity.doWithProperties((ArangoPersistentProperty property) -> { if (!entity.isCreatorArgument(property)) { - JsonNode value = getOrMissing(source, property.getFieldName()); - readProperty(entity, id, accessor, value, property); + JsonNode propValue = getOrMissing(value, property.getFieldName()); + readProperty(entity, id, accessor, new ArangoJsonNode(propValue, source.transactionContext()), property); } }); entity.doWithAssociations((Association association) -> { ArangoPersistentProperty property = association.getInverse(); if (!entity.isCreatorArgument(property)) { - JsonNode value = getOrMissing(source, property.getFieldName()); - readProperty(entity, id, accessor, value, property); + JsonNode propValue = getOrMissing(value, property.getFieldName()); + readProperty(entity, id, accessor, new ArangoJsonNode(propValue, source.transactionContext()), property); } }); @@ -221,7 +229,7 @@ private void readProperty( final ArangoPersistentEntity entity, final String parentId, final PersistentPropertyAccessor accessor, - final JsonNode source, + final ArangoJsonNode source, final ArangoPersistentProperty property ) { Object propertyValue = readPropertyValue(entity, parentId, source, property); @@ -233,7 +241,7 @@ private void readProperty( private Object readPropertyValue( final ArangoPersistentEntity entity, final String parentId, - final JsonNode source, + final ArangoJsonNode source, final ArangoPersistentProperty property ) { Optional ref = property.getRef(); @@ -259,13 +267,14 @@ private Object readPropertyValue( return readInternal(property.getTypeInformation(), source); } - private Map readMap(final TypeInformation type, final JsonNode source) { - if (!source.isObject()) { + private Map readMap(final TypeInformation type, final ArangoJsonNode source) { + JsonNode value = source.value(); + if (!value.isObject()) { throw new MappingException( - String.format("Can't read map type %s from type %s!", type, source.getNodeType())); + String.format("Can't read map type %s from type %s!", type, value.getNodeType())); } - ObjectNode node = (ObjectNode) source; + ObjectNode node = (ObjectNode) value; Class keyType = getNonNullComponentType(type).getType(); TypeInformation valueType = getNonNullMapValueType(type); Map map = CollectionFactory.createMap(type.getType(), keyType, node.size()); @@ -278,21 +287,21 @@ private Object readPropertyValue( } Object key = convertIfNecessary(entry.getKey(), keyType); - JsonNode value = entry.getValue(); - - map.put(key, readInternal(valueType, value)); + JsonNode entryValue = entry.getValue(); + map.put(key, readInternal(valueType, new ArangoJsonNode(entryValue, source.transactionContext()))); } return map; } - private Collection readCollection(final TypeInformation type, final JsonNode source) { - if (!source.isArray()) { + private Collection readCollection(final TypeInformation type, final ArangoJsonNode source) { + JsonNode value = source.value(); + if (!value.isArray()) { throw new MappingException( - String.format("Can't read collection type %s from type %s!", type, source.getNodeType())); + String.format("Can't read collection type %s from type %s!", type, value.getNodeType())); } - ArrayNode node = (ArrayNode) source; + ArrayNode node = (ArrayNode) value; TypeInformation componentType = getNonNullComponentType(type); Class collectionType = Iterable.class.equals(type.getType()) ? Collection.class : type.getType(); @@ -300,26 +309,27 @@ private Collection readCollection(final TypeInformation type, final JsonNo new ArrayList<>(node.size()) : CollectionFactory.createCollection(collectionType, componentType.getType(), node.size()); - for (JsonNode elem : source) { - collection.add(readInternal(componentType, elem)); + for (JsonNode elem : value) { + collection.add(readInternal(componentType, new ArangoJsonNode(elem, source.transactionContext()))); } return collection; } - private Object readArray(final TypeInformation type, final JsonNode source) { - if (!source.isArray()) { + private Object readArray(final TypeInformation type, final ArangoJsonNode source) { + JsonNode value = source.value(); + if (!value.isArray()) { throw new MappingException( - String.format("Can't read array type %s from type %s!", type, source.getNodeType())); + String.format("Can't read array type %s from type %s!", type, value.getNodeType())); } - ArrayNode node = (ArrayNode) source; + ArrayNode node = (ArrayNode) value; TypeInformation componentType = getNonNullComponentType(type); int length = node.size(); Object array = Array.newInstance(componentType.getType(), length); for (int i = 0; i < length; ++i) { - Array.set(array, i, readInternal(componentType, node.get(i))); + Array.set(array, i, readInternal(componentType, new ArangoJsonNode(node.get(i), source.transactionContext()))); } return array; @@ -327,15 +337,16 @@ private Object readArray(final TypeInformation type, final JsonNode source) { @SuppressWarnings("unchecked") private Optional readReference( - final JsonNode source, + final ArangoJsonNode source, final ArangoPersistentProperty property, final Annotation annotation ) { - if (source.isMissingNode() || source.isNull()) { - return Optional.empty(); - } + JsonNode value = source.value(); + if (value.isMissingNode() || value.isNull()) { + return Optional.empty(); + } - Optional> resolver = resolverFactory.getReferenceResolver(annotation); + Optional> resolver = resolverFactory.getReferenceResolver(annotation); if (resolver.isEmpty()) { return Optional.empty(); @@ -347,28 +358,29 @@ private Optional readReference( throw new MappingException("All references must be of type String!", e); } - return resolver.map(res -> res.resolveMultiple(ids, property.getTypeInformation(), annotation)); - } else if (source.isObject()) { - // source contains target - return Optional.of(readInternal(property.getTypeInformation(), source)); - } else { - if (!source.isTextual()) { + return resolver.map(res -> res.resolveMultiple(ids, property.getTypeInformation(), annotation, source.transactionContext())); + } else if (value.isObject()) { + // value contains target + return Optional.of(readInternal(property.getTypeInformation(), source)); + } else { + if (!value.isTextual()) { throw new MappingException( - String.format("A reference must be of type String, but got type %s!", source.getNodeType())); + String.format("A reference must be of type String, but got type %s!", value.getNodeType())); } - return resolver.map(res -> res.resolveOne(source.textValue(), property.getTypeInformation(), annotation)); + return resolver.map(res -> res.resolveOne(value.textValue(), property.getTypeInformation(), annotation, source.transactionContext())); } } private Optional readRelation( final ArangoPersistentEntity entity, final String parentId, - final JsonNode source, + final ArangoJsonNode source, final ArangoPersistentProperty property, final A annotation ) { - if (source.isNull()) { + JsonNode value = source.value(); + if (value.isNull()) { return Optional.empty(); } @@ -383,103 +395,104 @@ private Optional readRelation( if (resolver.isEmpty()) { return Optional.empty(); } else if (property.isCollectionLike()) { - if (source.isArray()) { - // source contains target array + if (value.isArray()) { + // value contains target array return Optional.of(readInternal(property.getTypeInformation(), source)); } if (parentId == null) { return Optional.empty(); } - return resolver.map(res -> res.resolveMultiple(parentId, property.getTypeInformation(), traversedTypes, annotation)); - } else if (source.isTextual()) { - return resolver.map(res -> res.resolveOne(source.textValue(), property.getTypeInformation(), traversedTypes, annotation)); - } else if (source.isObject()) { - // source contains target + return resolver.map(res -> res.resolveMultiple(parentId, property.getTypeInformation(), traversedTypes, annotation, source.transactionContext())); + } else if (value.isTextual()) { + return resolver.map(res -> res.resolveOne(value.textValue(), property.getTypeInformation(), traversedTypes, annotation, source.transactionContext())); + } else if (value.isObject()) { + // value contains target return Optional.of(readInternal(property.getTypeInformation(), source)); } else { - return resolver.map(res -> res.resolveOne(parentId, property.getTypeInformation(), traversedTypes, annotation)); + return resolver.map(res -> res.resolveOne(parentId, property.getTypeInformation(), traversedTypes, annotation, source.transactionContext())); } } - private Object readSimple(final Class type, final JsonNode source) { - if (source.isMissingNode() || source.isNull()) { + private Object readSimple(final Class type, final ArangoJsonNode source) { + JsonNode value = source.value(); + if (value.isMissingNode() || value.isNull()) { return null; } - if (source.isBoolean()) { - return source.booleanValue(); + if (value.isBoolean()) { + return value.booleanValue(); } - if (source.isNumber()) { + if (value.isNumber()) { if (byte.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type)) { - return source.numberValue().byteValue(); + return value.numberValue().byteValue(); } else if (short.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type)) { - return source.shortValue(); + return value.shortValue(); } else if (int.class.isAssignableFrom(type) || Integer.class.isAssignableFrom(type)) { - return source.intValue(); + return value.intValue(); } else if (long.class.isAssignableFrom(type) || Long.class.isAssignableFrom(type)) { - return source.longValue(); + return value.longValue(); } else if (float.class.isAssignableFrom(type) || Float.class.isAssignableFrom(type)) { - return source.floatValue(); + return value.floatValue(); } else if (double.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type)) { - return source.doubleValue(); - } else if (BigInteger.class.isAssignableFrom(type) && (source.isIntegralNumber())) { - return source.bigIntegerValue(); - } else if (BigDecimal.class.isAssignableFrom(type) && source.isFloatingPointNumber()) { - return source.decimalValue(); + return value.doubleValue(); + } else if (BigInteger.class.isAssignableFrom(type) && (value.isIntegralNumber())) { + return value.bigIntegerValue(); + } else if (BigDecimal.class.isAssignableFrom(type) && value.isFloatingPointNumber()) { + return value.decimalValue(); } else { - return source.numberValue(); + return value.numberValue(); } } - if (source.isTextual()) { - String value = source.textValue(); + if (value.isTextual()) { + String textValue = value.textValue(); if (Class.class.isAssignableFrom(type)) { try { - return Class.forName(value); + return Class.forName(textValue); } catch (ClassNotFoundException e) { - throw new MappingException(String.format("Could not load type %s!", value), e); + throw new MappingException(String.format("Could not load type %s!", textValue), e); } } else if (Enum.class.isAssignableFrom(type)) { @SuppressWarnings({"unchecked", "rawtypes"}) - Enum e = Enum.valueOf((Class) type, value); + Enum e = Enum.valueOf((Class) type, textValue); return e; } else if (byte[].class.isAssignableFrom(type)) { - return Base64.getDecoder().decode(value); + return Base64.getDecoder().decode(textValue); } else if (java.sql.Date.class.isAssignableFrom(type)) { - return new java.sql.Date(parseDate(value).getTime()); + return new java.sql.Date(parseDate(textValue).getTime()); } else if (Timestamp.class.isAssignableFrom(type)) { - return new Timestamp(parseDate(value).getTime()); + return new Timestamp(parseDate(textValue).getTime()); } else if (Date.class.isAssignableFrom(type)) { - return parseDate(value); + return parseDate(textValue); } else if (BigInteger.class.isAssignableFrom(type)) { - return new BigInteger(value); + return new BigInteger(textValue); } else if (BigDecimal.class.isAssignableFrom(type)) { - return new BigDecimal(value); + return new BigDecimal(textValue); } else if (Instant.class.isAssignableFrom(type)) { - return JavaTimeUtil.parseInstant(value); + return JavaTimeUtil.parseInstant(textValue); } else if (LocalDate.class.isAssignableFrom(type)) { - return JavaTimeUtil.parseLocalDate(value); + return JavaTimeUtil.parseLocalDate(textValue); } else if (LocalDateTime.class.isAssignableFrom(type)) { - return JavaTimeUtil.parseLocalDateTime(value); + return JavaTimeUtil.parseLocalDateTime(textValue); } else if (OffsetDateTime.class.isAssignableFrom(type)) { - return JavaTimeUtil.parseOffsetDateTime(value); + return JavaTimeUtil.parseOffsetDateTime(textValue); } else if (ZonedDateTime.class.isAssignableFrom(type)) { - return JavaTimeUtil.parseZonedDateTime(value); + return JavaTimeUtil.parseZonedDateTime(textValue); } else { - return value; + return textValue; } } - if (source.isObject() && DBDocumentEntity.class.isAssignableFrom(type)) { + if (value.isObject() && DBDocumentEntity.class.isAssignableFrom(type)) { return readDBDocumentEntity(source); } - throw new MappingException(String.format("Can't read type %s from type %s!", type, source.getNodeType())); + throw new MappingException(String.format("Can't read type %s from type %s!", type, value.getNodeType())); } - private BaseDocument readBaseDocument(final Class type, final JsonNode source) { + private BaseDocument readBaseDocument(final Class type, final ArangoJsonNode source) { if (BaseDocument.class.equals(type)) { @SuppressWarnings("unchecked") Map properties = (Map) readMap(TypeInformation.MAP, source); @@ -488,7 +501,7 @@ private BaseDocument readBaseDocument(final Class type, final JsonNode source throw new MappingException(String.format("Can't read type %s as %s!", type, BaseDocument.class)); } - private BaseEdgeDocument readBaseEdgeDocument(final Class type, final JsonNode source) { + private BaseEdgeDocument readBaseEdgeDocument(final Class type, final ArangoJsonNode source) { if (BaseEdgeDocument.class.equals(type)) { @SuppressWarnings("unchecked") Map properties = (Map) readMap(TypeInformation.MAP, source); @@ -497,7 +510,7 @@ private BaseEdgeDocument readBaseEdgeDocument(final Class type, final JsonNod throw new MappingException(String.format("Can't read type %s as %s!", type, BaseEdgeDocument.class)); } - private DBDocumentEntity readDBDocumentEntity(final JsonNode source) { + private DBDocumentEntity readDBDocumentEntity(final ArangoJsonNode source) { @SuppressWarnings("unchecked") Map properties = (Map) readMap(TypeInformation.MAP, source); return new DBDocumentEntity(properties); @@ -505,7 +518,7 @@ private DBDocumentEntity readDBDocumentEntity(final JsonNode source) { private ParameterValueProvider getParameterProvider( final ArangoPersistentEntity entity, - final JsonNode source + final ArangoJsonNode source ) { PropertyValueProvider provider = new ArangoPropertyValueProvider(entity, source); return new PersistentEntityParameterValueProvider<>(entity, provider, null); @@ -514,21 +527,23 @@ private ParameterValueProvider getParameterProvider( private class ArangoPropertyValueProvider implements PropertyValueProvider { private final ArangoPersistentEntity entity; - private final JsonNode source; + private final JsonNode value; private final String id; + private final TransactionMappingContext ctx; - public ArangoPropertyValueProvider(final ArangoPersistentEntity entity, final JsonNode source) { + public ArangoPropertyValueProvider(final ArangoPersistentEntity entity, final ArangoJsonNode source) { this.entity = entity; - this.source = source; - JsonNode idNode = getOrMissing(source, _ID); + this.value = source.value(); + this.ctx = source.transactionContext(); + JsonNode idNode = getOrMissing(value, _ID); this.id = idNode.isTextual() ? idNode.textValue() : null; } @SuppressWarnings("unchecked") @Override public T getPropertyValue(final ArangoPersistentProperty property) { - JsonNode value = getOrMissing(source, property.getFieldName()); - return (T) readPropertyValue(entity, id, value, property); + JsonNode propValue = getOrMissing(value, property.getFieldName()); + return (T) readPropertyValue(entity, id, new ArangoJsonNode(propValue, ctx), property); } } @@ -778,16 +793,16 @@ private Optional getRefId(final Object source, final ArangoPersistentEnt } return Optional.ofNullable(entity.getIdentifierAccessor(source).getIdentifier()) - .map(key -> { - if (annotation != null) { - return resolverFactory.getReferenceResolver(annotation) - .map(resolver -> resolver.write(source, entity, convertId(key))) - .orElseThrow(() -> new IllegalArgumentException("Missing reference resolver for " + annotation)); - } else { - return MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key)); - } - }) - .or(() -> Optional.ofNullable((String) entity.getArangoIdAccessor(source).getIdentifier())); + .map(key -> { + if (annotation != null) { + return resolverFactory.getReferenceResolver(annotation) + .map(resolver -> resolver.write(source, entity, convertId(key))) + .orElseThrow(() -> new IllegalArgumentException("Missing reference resolver for " + annotation)); + } else { + return MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key)); + } + }) + .or(() -> Optional.ofNullable((String) entity.getArangoIdAccessor(source).getIdentifier())); } private static Collection asCollection(final Object source) { diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java index a98372dc..40e28e24 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java @@ -42,177 +42,173 @@ */ public abstract class AbstractResolver { - private static final Method GET_ENTITY_METHOD; - private static final Method GET_REF_ID_METHOD; + private static final Method GET_ENTITY_METHOD; + private static final Method GET_REF_ID_METHOD; private static final Method IS_RESOLVED; - static { - try { - GET_ENTITY_METHOD = LazyLoadingProxy.class.getMethod("getEntity"); - GET_REF_ID_METHOD = LazyLoadingProxy.class.getMethod("getRefId"); + static { + try { + GET_ENTITY_METHOD = LazyLoadingProxy.class.getMethod("getEntity"); + GET_REF_ID_METHOD = LazyLoadingProxy.class.getMethod("getRefId"); IS_RESOLVED = LazyLoadingProxy.class.getMethod("isResolved"); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } - - private final ObjenesisStd objenesis; - private final ConversionService conversionService; - - protected AbstractResolver(final ConversionService conversionService) { - super(); - this.conversionService = conversionService; - this.objenesis = new ObjenesisStd(true); - } - - protected Object proxy( - final String id, - final TypeInformation type, - final Supplier callback) { + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + + private final ObjenesisStd objenesis; + private final ConversionService conversionService; + + protected AbstractResolver(final ConversionService conversionService) { + super(); + this.conversionService = conversionService; + this.objenesis = new ObjenesisStd(true); + } + + protected Object proxy( + final String id, + final TypeInformation type, + final Supplier callback) { final ProxyInterceptor interceptor = new ProxyInterceptor(id, type, callback, conversionService); - if (type.getType().isInterface()) { - final ProxyFactory proxyFactory = new ProxyFactory(new Class[] { type.getType() }); - for (final Class interf : type.getType().getInterfaces()) { - proxyFactory.addInterface(interf); - } - proxyFactory.addInterface(LazyLoadingProxy.class); - proxyFactory.addAdvice(interceptor); - return proxyFactory.getProxy(); - } else { - final Factory factory = (Factory) objenesis.newInstance(enhancedTypeFor(type.getType())); - factory.setCallbacks(new Callback[] { interceptor }); - return factory; - } - } - - private Class enhancedTypeFor(final Class type) { - final Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(type); - enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class); - enhancer.setInterfaces(new Class[] { LazyLoadingProxy.class }); - return enhancer.createClass(); - } + if (type.getType().isInterface()) { + final ProxyFactory proxyFactory = new ProxyFactory(new Class[]{type.getType()}); + for (final Class interf : type.getType().getInterfaces()) { + proxyFactory.addInterface(interf); + } + proxyFactory.addInterface(LazyLoadingProxy.class); + proxyFactory.addAdvice(interceptor); + return proxyFactory.getProxy(); + } else { + final Factory factory = (Factory) objenesis.newInstance(enhancedTypeFor(type.getType())); + factory.setCallbacks(new Callback[]{interceptor}); + return factory; + } + } + + private Class enhancedTypeFor(final Class type) { + final Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(type); + enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class); + enhancer.setInterfaces(new Class[]{LazyLoadingProxy.class}); + return enhancer.createClass(); + } static class ProxyInterceptor implements Serializable, - org.springframework.cglib.proxy.MethodInterceptor, org.aopalliance.intercept.MethodInterceptor { + org.springframework.cglib.proxy.MethodInterceptor, org.aopalliance.intercept.MethodInterceptor { - private static final long serialVersionUID = -6722757823918987065L; - private final String id; - final TypeInformation type; + private static final long serialVersionUID = -6722757823918987065L; + private final String id; + final TypeInformation type; private final Supplier callback; - private volatile boolean resolved; - private Object result; - private final ConversionService conversionService; + private volatile boolean resolved; + private Object result; + private final ConversionService conversionService; public ProxyInterceptor(final String id, final TypeInformation type, final Supplier callback, final ConversionService conversionService) { - super(); - this.id = id; - this.type = type; - this.callback = callback; - this.conversionService = conversionService; - result = null; - resolved = false; - } - - @Override - public Object invoke(final MethodInvocation invocation) throws Throwable { - return intercept(invocation.getThis(), invocation.getMethod(), invocation.getArguments(), null); - } - - @Override - public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy proxy) - throws Throwable { - - if (GET_ENTITY_METHOD.equals(method)) { - return ensureResolved(); - } - - if (GET_REF_ID_METHOD.equals(method)) { - return id; - } + super(); + this.id = id; + this.type = type; + this.callback = callback; + this.conversionService = conversionService; + result = null; + resolved = false; + } + + @Override + public Object invoke(final MethodInvocation invocation) throws Throwable { + return intercept(invocation.getThis(), invocation.getMethod(), invocation.getArguments(), null); + } + + @Override + public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy proxy) + throws Throwable { + + if (GET_ENTITY_METHOD.equals(method)) { + return ensureResolved(); + } + + if (GET_REF_ID_METHOD.equals(method)) { + return id; + } if (IS_RESOLVED.equals(method)) { return resolved; } - if (method.getName().equals("canEqual")) { - return proxyCanEqual(ensureResolved(), args[0]); - } - if (ReflectionUtils.isObjectMethod(method)) { - if (ReflectionUtils.isToStringMethod(method)) { - return proxyToString(); - } - - else if (ReflectionUtils.isEqualsMethod(method)) { - return proxyEquals(proxy, args[0]); - } - - else if (ReflectionUtils.isHashCodeMethod(method)) { - return proxyHashCode(); - } - } - - final Object result = ensureResolved(); - return result == null ? null : method.invoke(result, args); - } - - private Object ensureResolved() { - if (!resolved) { - result = resolve(); - resolved = true; - } - return result; - } - - private synchronized Object resolve() { - if (!resolved) { + if (method.getName().equals("canEqual")) { + return proxyCanEqual(ensureResolved(), args[0]); + } + if (ReflectionUtils.isObjectMethod(method)) { + if (ReflectionUtils.isToStringMethod(method)) { + return proxyToString(); + } else if (ReflectionUtils.isEqualsMethod(method)) { + return proxyEquals(proxy, args[0]); + } else if (ReflectionUtils.isHashCodeMethod(method)) { + return proxyHashCode(); + } + } + + final Object result = ensureResolved(); + return result == null ? null : method.invoke(result, args); + } + + private Object ensureResolved() { + if (!resolved) { + result = resolve(); + resolved = true; + } + return result; + } + + private synchronized Object resolve() { + if (!resolved) { return convertIfNecessary(callback.get(), type.getType()); - } - return result; - } - - private String proxyToString() { - return new StringBuilder() - .append(LazyLoadingProxy.class.getSimpleName()) - .append(" [") - .append(id) - .append("]") - .toString(); - } - - private int proxyHashCode() { - return proxyToString().hashCode(); - } - - private boolean proxyCanEqual(final Object proxy, final Object obj) { - return obj.getClass().isInstance(proxy); - } - - private boolean proxyEquals(final Object proxy, final Object obj) { - if (!(obj instanceof LazyLoadingProxy)) { - return false; - } - if (obj == proxy) { - return true; - } - return proxyToString().equals(obj.toString()); - } - - @SuppressWarnings("unchecked") - private T convertIfNecessary(@Nullable final Object source, final Class type) { - return (T) (source == null ? null - : type.isAssignableFrom(source.getClass()) ? source : conversionService.convert(source, type)); - } - } - - protected static TypeInformation getNonNullComponentType(final TypeInformation type) { - final TypeInformation compType = type.getComponentType(); - return compType != null ? compType : TypeInformation.OBJECT; - } - - protected static RuntimeException cannotResolveException(final String id, final TypeInformation type) { - return new IllegalArgumentException("Cannot resolve " + type.getType() + " for id " + id); - } + } + return result; + } + + private String proxyToString() { + return new StringBuilder() + .append(LazyLoadingProxy.class.getSimpleName()) + .append(" [") + .append(id) + .append("]") + .toString(); + } + + private int proxyHashCode() { + return proxyToString().hashCode(); + } + + private boolean proxyCanEqual(final Object proxy, final Object obj) { + return obj.getClass().isInstance(proxy); + } + + private boolean proxyEquals(final Object proxy, final Object obj) { + if (!(obj instanceof LazyLoadingProxy)) { + return false; + } + if (obj == proxy) { + return true; + } + return proxyToString().equals(obj.toString()); + } + + @SuppressWarnings("unchecked") + private T convertIfNecessary(@Nullable final Object source, final Class type) { + return (T) (source == null ? null + : type.isAssignableFrom(source.getClass()) ? source : conversionService.convert(source, type)); + } + } + + protected static TypeInformation getNonNullComponentType(final TypeInformation type) { + final TypeInformation compType = type.getComponentType(); + return compType != null ? compType : TypeInformation.OBJECT; + } + + protected static RuntimeException cannotResolveException(final String id, final TypeInformation type) { + return new IllegalArgumentException("Cannot resolve " + type.getType() + " for id " + id); + } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentFromResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentFromResolver.java index 4f32a8cc..0efb1b1e 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentFromResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentFromResolver.java @@ -20,6 +20,7 @@ package com.arangodb.springframework.core.convert.resolver; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; import com.arangodb.ArangoCursor; @@ -47,33 +48,36 @@ public DocumentFromResolver(final ArangoOperations template) { @Override public Object resolveOne(final String id, final TypeInformation type, Collection> traversedTypes, - final From annotation) { - Supplier supplier = () -> _resolveOne(id, type); + final From annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveOne(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } - private Object _resolveOne(final String id, final TypeInformation type) { - ArangoCursor it = _resolve(id, type.getType(), true); + private Object _resolveOne(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + ArangoCursor it = _resolve(id, type.getType(), true, ctx); return it.hasNext() ? it.next() : null; } @Override public Object resolveMultiple(final String id, final TypeInformation type, Collection> traversedTypes, - final From annotation) { - Supplier supplier = () -> _resolveMultiple(id, type); + final From annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveMultiple(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } - private Object _resolveMultiple(final String id, final TypeInformation type) { - return _resolve(id, getNonNullComponentType(type).getType(), false).asListRemaining(); + private Object _resolveMultiple(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + return _resolve(id, getNonNullComponentType(type).getType(), false, ctx).asListRemaining(); } - private ArangoCursor _resolve(final String id, final Class type, final boolean limit) { + private ArangoCursor _resolve(final String id, final Class type, final boolean limit, final TransactionMappingContext ctx) { final String query = String.format("FOR e IN @@edge FILTER e._from == @id %s RETURN e", limit ? "LIMIT 1" : ""); Map bindVars = new HashMap<>(); bindVars.put("@edge", type); bindVars.put("id", id); - return template.query(query, bindVars, new AqlQueryOptions(), type); + + AqlQueryOptions opts = new AqlQueryOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.query(query, bindVars, opts, type); } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentToResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentToResolver.java index e8440a4e..bbfd16f7 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentToResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/DocumentToResolver.java @@ -20,6 +20,7 @@ package com.arangodb.springframework.core.convert.resolver; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; import com.arangodb.ArangoCursor; @@ -47,33 +48,36 @@ public DocumentToResolver(final ArangoOperations template) { @Override public Object resolveOne(final String id, final TypeInformation type, Collection> traversedTypes, - final To annotation) { - Supplier supplier = () -> _resolveOne(id, type); + final To annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveOne(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } - private Object _resolveOne(final String id, final TypeInformation type) { - ArangoCursor it = _resolve(id, type.getType(), true); + private Object _resolveOne(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + ArangoCursor it = _resolve(id, type.getType(), true, ctx); return it.hasNext() ? it.next() : null; } @Override public Object resolveMultiple(final String id, final TypeInformation type, Collection> traversedTypes, - final To annotation) { - Supplier supplier = () -> _resolveMultiple(id, type); + final To annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveMultiple(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } - private Object _resolveMultiple(final String id, final TypeInformation type) { - return _resolve(id, getNonNullComponentType(type).getType(), false).asListRemaining(); + private Object _resolveMultiple(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + return _resolve(id, getNonNullComponentType(type).getType(), false, ctx).asListRemaining(); } - private ArangoCursor _resolve(final String id, final Class type, final boolean limit) { + private ArangoCursor _resolve(final String id, final Class type, final boolean limit, final TransactionMappingContext ctx) { final String query = String.format("FOR e IN @@edge FILTER e._to == @id %s RETURN e", limit ? "LIMIT 1" : ""); Map bindVars = new HashMap<>(); bindVars.put("@edge", type); bindVars.put("id", id); - return template.query(query, bindVars, new AqlQueryOptions(), type); + + AqlQueryOptions opts = new AqlQueryOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.query(query, bindVars, opts, type); } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeFromResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeFromResolver.java index a1dcc083..362078da 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeFromResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeFromResolver.java @@ -20,6 +20,8 @@ package com.arangodb.springframework.core.convert.resolver; +import com.arangodb.model.DocumentReadOptions; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; import com.arangodb.springframework.annotation.From; @@ -33,29 +35,31 @@ */ public class EdgeFromResolver extends AbstractResolver implements RelationResolver { - private final ArangoOperations template; + private final ArangoOperations template; - public EdgeFromResolver(final ArangoOperations template) { - super(template.getConverter().getConversionService()); - this.template = template; - } + public EdgeFromResolver(final ArangoOperations template) { + super(template.getConverter().getConversionService()); + this.template = template; + } - @Override + @Override public Object resolveOne(final String id, final TypeInformation type, Collection> traversedTypes, - final From annotation) { - Supplier supplier = () -> _resolveOne(id, type); + final From annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveOne(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); - } + } - private Object _resolveOne(final String id, final TypeInformation type) { - return template.find(id, type.getType()) - .orElseThrow(() -> cannotResolveException(id, type)); - } + private Object _resolveOne(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + DocumentReadOptions opts = new DocumentReadOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.find(id, type.getType(), opts) + .orElseThrow(() -> cannotResolveException(id, type)); + } - @Override + @Override public Object resolveMultiple(final String id, final TypeInformation type, Collection> traversedTypes, - final From annotation) { - throw new UnsupportedOperationException("Edges with multiple 'from' values are not supported."); - } + final From annotation, final TransactionMappingContext ctx) { + throw new UnsupportedOperationException("Edges with multiple 'from' values are not supported."); + } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeToResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeToResolver.java index 69e92369..aa90f09f 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeToResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/EdgeToResolver.java @@ -20,6 +20,8 @@ package com.arangodb.springframework.core.convert.resolver; +import com.arangodb.model.DocumentReadOptions; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; import com.arangodb.springframework.annotation.To; @@ -33,28 +35,31 @@ */ public class EdgeToResolver extends AbstractResolver implements RelationResolver { - private final ArangoOperations template; + private final ArangoOperations template; - public EdgeToResolver(final ArangoOperations template) { - super(template.getConverter().getConversionService()); - this.template = template; - } + public EdgeToResolver(final ArangoOperations template) { + super(template.getConverter().getConversionService()); + this.template = template; + } - @Override + @Override public Object resolveOne(final String id, final TypeInformation type, Collection> traversedTypes, - final To annotation) { - Supplier supplier = () -> _resolveOne(id, type); + final To annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveOne(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); - } + } - private Object _resolveOne(final String id, final TypeInformation type) { - return template.find(id, type.getType()) - .orElseThrow(() -> cannotResolveException(id, type)); - } + private Object _resolveOne(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + DocumentReadOptions opts = new DocumentReadOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.find(id, type.getType(), opts) + .orElseThrow(() -> cannotResolveException(id, type)); + } - @Override - public Object resolveMultiple(final String id, final TypeInformation type, Collection> traversedTypes, final To annotation) { - throw new UnsupportedOperationException("Edges with multiple 'to' values are not supported."); - } + @Override + public Object resolveMultiple(final String id, final TypeInformation type, Collection> traversedTypes, + final To annotation, final TransactionMappingContext ctx) { + throw new UnsupportedOperationException("Edges with multiple 'to' values are not supported."); + } } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/RefResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/RefResolver.java index 6dfc8058..61a80bc7 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/RefResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/RefResolver.java @@ -24,7 +24,9 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import com.arangodb.model.DocumentReadOptions; import com.arangodb.springframework.core.mapping.ArangoPersistentEntity; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import com.arangodb.springframework.core.util.MetadataUtils; import org.springframework.data.util.TypeInformation; @@ -45,20 +47,22 @@ public RefResolver(final ArangoOperations template) { } @Override - public Object resolveOne(final String id, final TypeInformation type, final Ref annotation) { - Supplier supplier = () -> _resolve(id, type); + public Object resolveOne(final String id, final TypeInformation type, final Ref annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolve(id, type, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } @Override - public Object resolveMultiple(final Collection ids, final TypeInformation type, final Ref annotation) { + public Object resolveMultiple(final Collection ids, final TypeInformation type, final Ref annotation, final TransactionMappingContext ctx) { return ids.stream() - .map(id -> resolveOne(id, getNonNullComponentType(type), annotation)) + .map(id -> resolveOne(id, getNonNullComponentType(type), annotation, ctx)) .collect(Collectors.toList()); } - private Object _resolve(final String id, final TypeInformation type) { - return template.find(id, type.getType()) + private Object _resolve(final String id, final TypeInformation type, final TransactionMappingContext ctx) { + DocumentReadOptions opts = new DocumentReadOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.find(id, type.getType(), opts) .orElseThrow(() -> cannotResolveException(id, type)); } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/ReferenceResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/ReferenceResolver.java index 17c62411..1ecf4eef 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/ReferenceResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/ReferenceResolver.java @@ -24,13 +24,14 @@ import java.util.Collection; import com.arangodb.springframework.core.mapping.ArangoPersistentEntity; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; /** * @author Mark Vollmary */ public interface ReferenceResolver { - Object resolveOne(String id, TypeInformation type, A annotation); - Object resolveMultiple(Collection ids, TypeInformation type, A annotation); + Object resolveOne(String id, TypeInformation type, A annotation, TransactionMappingContext ctx); + Object resolveMultiple(Collection ids, TypeInformation type, A annotation, TransactionMappingContext ctx); String write(Object source, ArangoPersistentEntity entity, Object id); } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationResolver.java index 6cd62c18..5508f908 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationResolver.java @@ -23,13 +23,13 @@ import java.lang.annotation.Annotation; import java.util.Collection; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; /** * @author Mark Vollmary */ public interface RelationResolver { - Object resolveOne(String id, TypeInformation type, Collection> traversedTypes, A annotation); - Object resolveMultiple(String id, TypeInformation type, Collection> traversedTypes, A annotation); - + Object resolveOne(String id, TypeInformation type, Collection> traversedTypes, A annotation, TransactionMappingContext ctx); + Object resolveMultiple(String id, TypeInformation type, Collection> traversedTypes, A annotation, TransactionMappingContext ctx); } diff --git a/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationsResolver.java b/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationsResolver.java index 7a69c892..1fdc723d 100644 --- a/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationsResolver.java +++ b/src/main/java/com/arangodb/springframework/core/convert/resolver/RelationsResolver.java @@ -24,6 +24,8 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import com.arangodb.model.AqlQueryOptions; +import com.arangodb.springframework.core.mapping.TransactionMappingContext; import org.springframework.data.util.TypeInformation; import com.arangodb.ArangoCursor; @@ -45,35 +47,35 @@ public RelationsResolver(final ArangoOperations template) { @Override public Object resolveOne(final String id, final TypeInformation type, final Collection> traversedTypes, - final Relations annotation) { - Supplier supplier = () -> _resolveOne(id, type, traversedTypes, annotation); + final Relations annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveOne(id, type, traversedTypes, annotation, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } @Override public Object resolveMultiple(final String id, final TypeInformation type, final Collection> traversedTypes, - final Relations annotation) { - Supplier supplier = () -> _resolveMultiple(id, type, traversedTypes, annotation); + final Relations annotation, final TransactionMappingContext ctx) { + Supplier supplier = () -> _resolveMultiple(id, type, traversedTypes, annotation, ctx); return annotation.lazy() ? proxy(id, type, supplier) : supplier.get(); } private Object _resolveOne(final String id, final TypeInformation type, final Collection> traversedTypes, - final Relations annotation) { + final Relations annotation, final TransactionMappingContext ctx) { Collection> rawTypes = new ArrayList<>(); for (TypeInformation it : traversedTypes) { rawTypes.add(it.getType()); } - ArangoCursor it = _resolve(id, type.getType(), rawTypes, annotation, true); + ArangoCursor it = _resolve(id, type.getType(), rawTypes, annotation, true, ctx); return it.hasNext() ? it.next() : null; } private Object _resolveMultiple(final String id, final TypeInformation type, final Collection> traversedTypes, - final Relations annotation) { + final Relations annotation, final TransactionMappingContext ctx) { Collection> rawTypes = new ArrayList<>(); for (TypeInformation it : traversedTypes) { rawTypes.add(it.getType()); } - return _resolve(id, getNonNullComponentType(type).getType(), rawTypes, annotation, false).asListRemaining(); + return _resolve(id, getNonNullComponentType(type).getType(), rawTypes, annotation, false, ctx).asListRemaining(); } private ArangoCursor _resolve( @@ -81,7 +83,8 @@ private ArangoCursor _resolve( final Class type, final Collection> traversedTypes, final Relations annotation, - final boolean limit) { + final boolean limit, + final TransactionMappingContext ctx) { final String edges = Arrays.stream(annotation.edges()).map(e -> template.collection(e).name()) .collect(Collectors.joining(",")); @@ -108,7 +111,9 @@ private ArangoCursor _resolve( edges, // limit ? "LIMIT 1" : ""); - return template.query(query, bindVars, type); + AqlQueryOptions opts = new AqlQueryOptions(); + ctx.getStreamTransactionId().ifPresent(opts::streamTransactionId); + return template.query(query, bindVars, opts, type); } } diff --git a/src/main/java/com/arangodb/springframework/core/mapping/ArangoSimpleTypes.java b/src/main/java/com/arangodb/springframework/core/mapping/ArangoSimpleTypes.java index 1f6dcd07..bf3bc8f0 100644 --- a/src/main/java/com/arangodb/springframework/core/mapping/ArangoSimpleTypes.java +++ b/src/main/java/com/arangodb/springframework/core/mapping/ArangoSimpleTypes.java @@ -33,6 +33,7 @@ import java.util.HashSet; import java.util.Set; +import com.arangodb.springframework.core.convert.ArangoJsonNode; import org.springframework.data.mapping.model.SimpleTypeHolder; import com.arangodb.springframework.core.convert.DBDocumentEntity; @@ -54,6 +55,7 @@ public final class ArangoSimpleTypes { simpleTypes.add(JsonNode.class); // com.arangodb.* + simpleTypes.add(ArangoJsonNode.class); simpleTypes.add(DBDocumentEntity.class); // java.math.* diff --git a/src/main/java/com/arangodb/springframework/core/mapping/TransactionMappingContext.java b/src/main/java/com/arangodb/springframework/core/mapping/TransactionMappingContext.java new file mode 100644 index 00000000..bfe5b6ba --- /dev/null +++ b/src/main/java/com/arangodb/springframework/core/mapping/TransactionMappingContext.java @@ -0,0 +1,44 @@ +package com.arangodb.springframework.core.mapping; + +import com.arangodb.RequestContext; + +import java.util.Objects; +import java.util.Optional; + +public class TransactionMappingContext { + public static TransactionMappingContext EMPTY = new TransactionMappingContext((String) null); + + private final String streamTransactionId; + + private TransactionMappingContext(final String streamTransactionId) { + this.streamTransactionId = streamTransactionId; + } + + public TransactionMappingContext(final RequestContext ctx) { + this(ctx.getStreamTransactionId().orElse(null)); + } + + public Optional getStreamTransactionId() { + return Optional.ofNullable(streamTransactionId); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TransactionMappingContext that = (TransactionMappingContext) o; + return Objects.equals(streamTransactionId, that.streamTransactionId); + } + + @Override + public int hashCode() { + return Objects.hashCode(streamTransactionId); + } + + @Override + public String toString() { + return "TransactionMappingContext{" + + "streamTransactionId='" + streamTransactionId + '\'' + + '}'; + } +} diff --git a/src/main/java/com/arangodb/springframework/repository/query/AbstractArangoQuery.java b/src/main/java/com/arangodb/springframework/repository/query/AbstractArangoQuery.java index 54c1f6af..c790a4b8 100644 --- a/src/main/java/com/arangodb/springframework/repository/query/AbstractArangoQuery.java +++ b/src/main/java/com/arangodb/springframework/repository/query/AbstractArangoQuery.java @@ -20,12 +20,11 @@ package com.arangodb.springframework.repository.query; -import java.util.Collection; import java.util.HashMap; import java.util.Map; +import com.arangodb.springframework.core.convert.ArangoJsonNode; import com.arangodb.springframework.core.mapping.ArangoMappingContext; -import com.fasterxml.jackson.databind.JsonNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.repository.query.RepositoryQuery; @@ -187,7 +186,7 @@ private Class getTypeToRead(final ResultProcessor processor) { } if (method.isGeoQuery()) { - return JsonNode.class; + return ArangoJsonNode.class; } final Class typeToRead = processor.getReturnedType().getTypeToRead(); diff --git a/src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java b/src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java index fda4664c..e3d0ad5c 100644 --- a/src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java +++ b/src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java @@ -29,6 +29,7 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import com.arangodb.springframework.core.convert.ArangoJsonNode; import com.fasterxml.jackson.databind.JsonNode; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -46,7 +47,7 @@ /** * Converts the result returned from the ArangoDB Java driver to the desired type. - * + * * @author Audrius Malele * @author Mark McCormick * @author Mark Vollmary @@ -54,37 +55,37 @@ */ public class ArangoResultConverter { - private final static String MISSING_FULL_COUNT = "Query result does not contain the full result count! " - + "The most likely cause is a forgotten LIMIT clause in the query."; + private final static String MISSING_FULL_COUNT = "Query result does not contain the full result count! " + + "The most likely cause is a forgotten LIMIT clause in the query."; - private final ArangoParameterAccessor accessor; - private final ArangoCursor result; - private final ArangoOperations operations; + private final ArangoParameterAccessor accessor; + private final ArangoCursor result; + private final ArangoOperations operations; private final Class domainClass; - /** - * @param accessor + /** + * @param accessor * @param result the query result returned by the driver * @param operations instance of arangoTemplate * @param domainClass class type of documents - */ - public ArangoResultConverter(final ArangoParameterAccessor accessor, final ArangoCursor result, + */ + public ArangoResultConverter(final ArangoParameterAccessor accessor, final ArangoCursor result, final ArangoOperations operations, final Class domainClass) { - this.accessor = accessor; - this.result = result; - this.operations = operations; - this.domainClass = domainClass; - } - - /** - * Called to convert result from ArangoCursor to given type, by invoking the appropriate converter method - * - * @param type - * @return result in desired type - */ - public Object convertResult(final Class type) { - try { + this.accessor = accessor; + this.result = result; + this.operations = operations; + this.domainClass = domainClass; + } + + /** + * Called to convert result from ArangoCursor to given type, by invoking the appropriate converter method + * + * @param type + * @return result in desired type + */ + public Object convertResult(final Class type) { + try { return convert(type); } catch (final Exception e) { throw new MappingException(String.format("Can't convert result to type %s!", type.getName()), e); @@ -92,7 +93,7 @@ public Object convertResult(final Class type) { } private Object convert(Class type) { - if (type.isArray()) { + if (type.isArray()) { return convertArray(); } else if (List.class.equals(type) || Iterable.class.equals(type) || Collection.class.equals(type)) { return convertList(); @@ -111,102 +112,103 @@ private Object convert(Class type) { } else if (Optional.class.equals(type)) { return convertOptional(); } else { - return getNext(result); - } - } + return getNext(result); + } + } - /** - * Creates a Set return type from the given cursor - * + /** + * Creates a Set return type from the given cursor + * * @param cursor query result from driver - * @return Set containing the results - */ - private Set buildSet(final ArangoCursor cursor) { - return StreamSupport.stream(Spliterators.spliteratorUnknownSize(cursor, 0), false).collect(Collectors.toSet()); - } - - /** - * Build a GeoResult from the given ArangoCursor - * + * @return Set containing the results + */ + private Set buildSet(final ArangoCursor cursor) { + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(cursor, 0), false).collect(Collectors.toSet()); + } + + /** + * Build a GeoResult from the given ArangoCursor + * * @param cursor query result from driver - * @return GeoResult object - */ - private GeoResult buildGeoResult(final ArangoCursor cursor) { + * @return GeoResult object + */ + private GeoResult buildGeoResult(final ArangoCursor cursor) { return buildGeoResult(cursor.next()); - } - - /** - * Construct a GeoResult from the given object - * - * @param slice object representing one document in the result - * @return GeoResult object - */ - private GeoResult buildGeoResult(final JsonNode slice) { + } + + /** + * Construct a GeoResult from the given object + * + * @param data object representing one document in the result + * @return GeoResult object + */ + private GeoResult buildGeoResult(final ArangoJsonNode data) { + JsonNode slice = data.value(); JsonNode distSlice = slice.get("_distance"); Double distanceInMeters = distSlice.isDouble() ? distSlice.doubleValue() : null; - T entity = operations.getConverter().read(domainClass, slice); - // FIXME: Unboxing of 'distanceInMeters' may produce 'NullPointerException' + T entity = operations.getConverter().read(domainClass, data); + // FIXME: Unboxing of 'distanceInMeters' may produce 'NullPointerException' Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS); - return new GeoResult<>(entity, distance); - } + return new GeoResult<>(entity, distance); + } - /** - * Build a GeoResults object with the ArangoCursor returned by query execution - * + /** + * Build a GeoResults object with the ArangoCursor returned by query execution + * * @param cursor ArangoCursor containing query results - * @return GeoResults object with all results - */ - private GeoResults buildGeoResults(final ArangoCursor cursor) { + * @return GeoResults object with all results + */ + private GeoResults buildGeoResults(final ArangoCursor cursor) { final List> list = new LinkedList<>(); cursor.forEachRemaining(o -> list.add(buildGeoResult(o))); // FIXME: DE-803 // convert geoResults to Metrics.NEUTRAL before // invoking GeoResults.GeoResults(java.util.List>) return new GeoResults<>(list); - } + } - public Optional convertOptional() { - return Optional.ofNullable(getNext(result)); - } + public Optional convertOptional() { + return Optional.ofNullable(getNext(result)); + } - public List convertList() { - return result.asListRemaining(); - } + public List convertList() { + return result.asListRemaining(); + } - public PageImpl convertPage() { - Assert.notNull(result.getStats().getFullCount(), MISSING_FULL_COUNT); - return new PageImpl<>(result.asListRemaining(), accessor.getPageable(), ((Number) result.getStats().getFullCount()).longValue()); - } + public PageImpl convertPage() { + Assert.notNull(result.getStats().getFullCount(), MISSING_FULL_COUNT); + return new PageImpl<>(result.asListRemaining(), accessor.getPageable(), ((Number) result.getStats().getFullCount()).longValue()); + } - public Set convertSet() { - return buildSet(result); - } + public Set convertSet() { + return buildSet(result); + } - public ArangoCursor convertArangoCursor() { - return result; - } + public ArangoCursor convertArangoCursor() { + return result; + } @SuppressWarnings("unchecked") public GeoResult convertGeoResult() { - return buildGeoResult((ArangoCursor) result); - } + return buildGeoResult((ArangoCursor) result); + } @SuppressWarnings("unchecked") public GeoResults convertGeoResults() { - return buildGeoResults((ArangoCursor) result); - } + return buildGeoResults((ArangoCursor) result); + } @SuppressWarnings("unchecked") public GeoPage convertGeoPage() { - Assert.notNull(result.getStats().getFullCount(), MISSING_FULL_COUNT); - return new GeoPage<>(buildGeoResults((ArangoCursor) result), accessor.getPageable(), ((Number) result.getStats().getFullCount()).longValue()); - } + Assert.notNull(result.getStats().getFullCount(), MISSING_FULL_COUNT); + return new GeoPage<>(buildGeoResults((ArangoCursor) result), accessor.getPageable(), ((Number) result.getStats().getFullCount()).longValue()); + } - public Object convertArray() { - return result.asListRemaining().toArray(); - } + public Object convertArray() { + return result.asListRemaining().toArray(); + } - private Object getNext(final ArangoCursor cursor) { - return cursor.hasNext() ? cursor.next() : null; - } + private Object getNext(final ArangoCursor cursor) { + return cursor.hasNext() ? cursor.next() : null; + } } diff --git a/src/test/java/com/arangodb/springframework/AbstractTxTest.java b/src/test/java/com/arangodb/springframework/AbstractTxTest.java new file mode 100644 index 00000000..12a181b6 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/AbstractTxTest.java @@ -0,0 +1,54 @@ +package com.arangodb.springframework; + +import com.arangodb.model.AqlQueryOptions; +import com.arangodb.model.DocumentCreateOptions; +import com.arangodb.model.DocumentReadOptions; +import com.arangodb.model.StreamTransactionOptions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import java.util.Arrays; + +public abstract class AbstractTxTest extends AbstractArangoTest { + protected String tx; + protected final DocumentCreateOptions insertOpts = new DocumentCreateOptions(); + protected final DocumentReadOptions findOpts = new DocumentReadOptions(); + protected final AqlQueryOptions queryOpts = new AqlQueryOptions().batchSize(1); + private final boolean withinTx; + + protected AbstractTxTest(boolean withinTx, Class... collections) { + super(collections); + this.withinTx = withinTx; + } + + @BeforeEach + void beginTx() { + if (!withinTx) { + return; + } + + String[] txCols = Arrays.stream(collections) + .map(it -> template.collection(it).name()) + .toArray(String[]::new); + + tx = db.beginStreamTransaction(new StreamTransactionOptions() + .readCollections(txCols) + .writeCollections(txCols) + ).getId(); + + insertOpts.streamTransactionId(tx); + findOpts.streamTransactionId(tx); + queryOpts + .streamTransactionId(tx) + .batchSize(1); + } + + @AfterEach + void abortTx() { + if (!withinTx) { + return; + } + db.abortStreamTransaction(tx); + } + +} diff --git a/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java index 516fccf8..ed0abc46 100644 --- a/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java +++ b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyRepositoryTestConfiguration.java @@ -64,6 +64,7 @@ public String database() { converters.add(new CustomMappingTest.CustomJsonNodeWriteTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityReadTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityWriteTestConverter()); + converters.add(new CustomMappingTest.CustomArangoJsonNodeReadTestConverter()); return converters; } diff --git a/src/test/java/com/arangodb/springframework/ArangoMultiTenancyTestConfiguration.java b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyTestConfiguration.java index 708cfbcb..6fd7425f 100644 --- a/src/test/java/com/arangodb/springframework/ArangoMultiTenancyTestConfiguration.java +++ b/src/test/java/com/arangodb/springframework/ArangoMultiTenancyTestConfiguration.java @@ -59,6 +59,7 @@ public String database() { converters.add(new CustomMappingTest.CustomJsonNodeWriteTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityReadTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityWriteTestConverter()); + converters.add(new CustomMappingTest.CustomArangoJsonNodeReadTestConverter()); return converters; } diff --git a/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java b/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java index 69972d23..898e6948 100644 --- a/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java +++ b/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java @@ -87,6 +87,7 @@ public String database() { converters.add(new CustomMappingTest.CustomJsonNodeWriteTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityReadTestConverter()); converters.add(new CustomMappingTest.CustomDBEntityWriteTestConverter()); + converters.add(new CustomMappingTest.CustomArangoJsonNodeReadTestConverter()); return converters; } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/AbstractMappingTxTestAbstract.java b/src/test/java/com/arangodb/springframework/core/mapping/AbstractMappingTxTestAbstract.java new file mode 100644 index 00000000..bf613884 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/AbstractMappingTxTestAbstract.java @@ -0,0 +1,26 @@ +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.AbstractTxTest; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; + +import java.util.ArrayList; +import java.util.Collections; + +public class AbstractMappingTxTestAbstract extends AbstractTxTest { + + private static Class[] enrichCollections(final Class... collections) { + ArrayList> classes = new ArrayList<>(); + Collections.addAll(classes, collections); + classes.add(BasicTestEntity.class); + classes.add(BasicEdgeTestEntity.class); + classes.add(BasicEdgeLazyTestEntity.class); + return classes.toArray(new Class[0]); + } + + public AbstractMappingTxTestAbstract(boolean withinTx, final Class... collections) { + super(withinTx, enrichCollections(collections)); + } + +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/CustomMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/CustomMappingTest.java index 23631e8b..553e4104 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/CustomMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/CustomMappingTest.java @@ -27,9 +27,11 @@ import java.util.Optional; import com.arangodb.ArangoCollection; -import com.arangodb.springframework.ArangoTestConfiguration; +import com.arangodb.springframework.core.convert.ArangoJsonNode; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import lombok.AllArgsConstructor; +import lombok.Data; import org.junit.jupiter.api.Test; import org.springframework.core.convert.converter.Converter; @@ -84,6 +86,13 @@ public void setValue(final String value) { } + @Data + @AllArgsConstructor + @Document + public static class CustomArangoJsonNodeTestEntity { + private String value; + } + public static class CustomJsonNodeWriteTestConverter implements Converter { @Override public JsonNode convert(final CustomJsonNodeTestEntity source) { @@ -98,6 +107,13 @@ public CustomJsonNodeTestEntity convert(final JsonNode source) { } } + public static class CustomArangoJsonNodeReadTestConverter implements Converter { + @Override + public CustomArangoJsonNodeTestEntity convert(final ArangoJsonNode source) { + return new CustomArangoJsonNodeTestEntity(source.value().get(FIELD).textValue()); + } + } + @Test public void customToJsonNode() { final DocumentEntity meta = template.insert(new CustomJsonNodeTestEntity("abc")); @@ -115,6 +131,14 @@ public void jsonNodeToCustom() { assertThat(doc.get().getValue(), is("abc")); } + @Test + public void arangoJsonNodeToCustom() { + final DocumentEntity meta = template.insert(new TestEntity("abc")); + final Optional doc = template.find(meta.getId(), CustomArangoJsonNodeTestEntity.class); + assertThat(doc.isPresent(), is(true)); + assertThat(doc.get().getValue(), is("abc")); + } + @Test public void customToJsonNodeFromDriver() { ArangoCollection col = db.collection("customJsonNodeTestEntity"); @@ -132,6 +156,14 @@ public void jsonNodeToCustomFromDriver() { assertThat(doc.getValue(), is("abc")); } + @Test + public void arangoJsonNodeToCustomFromDriver() { + ArangoCollection col = db.collection("testEntity"); + final DocumentEntity meta = col.insertDocument(new TestEntity("abc")); + final CustomArangoJsonNodeTestEntity doc = col.getDocument(meta.getKey(), CustomArangoJsonNodeTestEntity.class); + assertThat(doc.getValue(), is("abc")); + } + @Document() public static class CustomDBEntityTestEntity { private String value; diff --git a/src/test/java/com/arangodb/springframework/core/mapping/EdgeMapping.java b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMapping.java new file mode 100644 index 00000000..eb226382 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMapping.java @@ -0,0 +1,133 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.annotation.From; +import com.arangodb.springframework.annotation.To; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +/** + * @author Mark Vollmary + * + */ +abstract class EdgeMapping extends AbstractMappingTxTestAbstract { + + EdgeMapping(boolean withinTx) { + super(withinTx, EdgeMapping.class.getDeclaredClasses()); + } + + @Test + public void edgeFromTo() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final BasicEdgeTestEntity e0 = new BasicEdgeTestEntity(e1, e2); + template.insert(e0, insertOpts); + final BasicEdgeTestEntity document = template.find(e0.id, BasicEdgeTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getFrom(), is(notNullValue())); + assertThat(document.getFrom().getId(), is(e1.getId())); + assertThat(document.getTo(), is(notNullValue())); + assertThat(document.getTo().getId(), is(e2.getId())); + } + + @Test + public void edgeFromToLazy() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final BasicEdgeLazyTestEntity e0 = new BasicEdgeLazyTestEntity(e1, e2); + template.insert(e0, insertOpts); + final BasicEdgeLazyTestEntity document = template.find(e0.id, BasicEdgeLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getFrom(), is(notNullValue())); + assertThat(document.getFrom().getId(), is(e1.getId())); + assertThat(document.getTo(), is(notNullValue())); + assertThat(document.getTo().getId(), is(e2.getId())); + } + + public static class EdgeConstructorWithFromToParamsTestEntity extends BasicEdgeTestEntity { + @From + private final BasicTestEntity from; + @To + private final BasicTestEntity to; + + public EdgeConstructorWithFromToParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { + super(); + this.from = from; + this.to = to; + } + } + + @Test + public void edgeConstructorWithFromToParams() { + final BasicTestEntity from = new BasicTestEntity(); + final BasicTestEntity to = new BasicTestEntity(); + template.insert(from, insertOpts); + template.insert(to, insertOpts); + final EdgeConstructorWithFromToParamsTestEntity edge = new EdgeConstructorWithFromToParamsTestEntity(from, to); + template.insert(edge, insertOpts); + final EdgeConstructorWithFromToParamsTestEntity document = template + .find(edge.id, EdgeConstructorWithFromToParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.from.id, is(from.id)); + assertThat(document.to.id, is(to.id)); + } + + public static class EdgeConstructorWithFromToLazyParamsTestEntity extends BasicEdgeTestEntity { + @From + private final BasicTestEntity from; + @To + private final BasicTestEntity to; + + public EdgeConstructorWithFromToLazyParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { + super(); + this.from = from; + this.to = to; + } + } + + @Test + public void edgeConstructorWithFromToLazyParams() { + final BasicTestEntity from = new BasicTestEntity(); + final BasicTestEntity to = new BasicTestEntity(); + template.insert(from, insertOpts); + template.insert(to, insertOpts); + final EdgeConstructorWithFromToLazyParamsTestEntity edge = new EdgeConstructorWithFromToLazyParamsTestEntity( + from, to); + template.insert(edge, insertOpts); + final EdgeConstructorWithFromToLazyParamsTestEntity document = template + .find(edge.id, EdgeConstructorWithFromToLazyParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.from.getId(), is(from.id)); + assertThat(document.to.getId(), is(to.id)); + } + +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTest.java index f2944090..91b27b4f 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTest.java @@ -1,131 +1,7 @@ -/* - * DISCLAIMER - * - * Copyright 2018 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - package com.arangodb.springframework.core.mapping; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; - -import org.junit.jupiter.api.Test; - -import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.annotation.From; -import com.arangodb.springframework.annotation.To; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; - -/** - * @author Mark Vollmary - * - */ -public class EdgeMappingTest extends AbstractArangoTest { - - @Test - public void edgeFromTo() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final BasicEdgeTestEntity e0 = new BasicEdgeTestEntity(e1, e2); - template.insert(e0); - final BasicEdgeTestEntity document = template.find(e0.id, BasicEdgeTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getFrom(), is(notNullValue())); - assertThat(document.getFrom().getId(), is(e1.getId())); - assertThat(document.getTo(), is(notNullValue())); - assertThat(document.getTo().getId(), is(e2.getId())); - } - - @Test - public void edgeFromToLazy() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final BasicEdgeLazyTestEntity e0 = new BasicEdgeLazyTestEntity(e1, e2); - template.insert(e0); - final BasicEdgeLazyTestEntity document = template.find(e0.id, BasicEdgeLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getFrom(), is(notNullValue())); - assertThat(document.getFrom().getId(), is(e1.getId())); - assertThat(document.getTo(), is(notNullValue())); - assertThat(document.getTo().getId(), is(e2.getId())); - } - - public static class EdgeConstructorWithFromToParamsTestEntity extends BasicEdgeTestEntity { - @From - private final BasicTestEntity from; - @To - private final BasicTestEntity to; - - public EdgeConstructorWithFromToParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { - super(); - this.from = from; - this.to = to; - } - } - - @Test - public void edgeConstructorWithFromToParams() { - final BasicTestEntity from = new BasicTestEntity(); - final BasicTestEntity to = new BasicTestEntity(); - template.insert(from); - template.insert(to); - final EdgeConstructorWithFromToParamsTestEntity edge = new EdgeConstructorWithFromToParamsTestEntity(from, to); - template.insert(edge); - final EdgeConstructorWithFromToParamsTestEntity document = template - .find(edge.id, EdgeConstructorWithFromToParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.from.id, is(from.id)); - assertThat(document.to.id, is(to.id)); - } - - public static class EdgeConstructorWithFromToLazyParamsTestEntity extends BasicEdgeTestEntity { - @From - private final BasicTestEntity from; - @To - private final BasicTestEntity to; - - public EdgeConstructorWithFromToLazyParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { - super(); - this.from = from; - this.to = to; - } - } - - @Test - public void edgeConstructorWithFromToLazyParams() { - final BasicTestEntity from = new BasicTestEntity(); - final BasicTestEntity to = new BasicTestEntity(); - template.insert(from); - template.insert(to); - final EdgeConstructorWithFromToLazyParamsTestEntity edge = new EdgeConstructorWithFromToLazyParamsTestEntity( - from, to); - template.insert(edge); - final EdgeConstructorWithFromToLazyParamsTestEntity document = template - .find(edge.id, EdgeConstructorWithFromToLazyParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.from.getId(), is(from.id)); - assertThat(document.to.getId(), is(to.id)); - } - +public class EdgeMappingTest extends EdgeMapping { + EdgeMappingTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTxTest.java b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTxTest.java new file mode 100644 index 00000000..37834ea0 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/EdgeMappingTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.core.mapping; + +public class EdgeMappingTxTest extends EdgeMapping { + EdgeMappingTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/FromMapping.java b/src/test/java/com/arangodb/springframework/core/mapping/FromMapping.java new file mode 100644 index 00000000..15244724 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/FromMapping.java @@ -0,0 +1,229 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.annotation.From; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +/** + * @author Mark Vollmary + */ +abstract class FromMapping extends AbstractMappingTxTestAbstract { + + FromMapping(boolean withinTx) { + super(withinTx, FromMapping.class.getDeclaredClasses()); + } + + public static class DocumentFromTestEntity extends BasicTestEntity { + @From + private Collection entities; + } + + @Test + public void documentFrom() { + final DocumentFromTestEntity e0 = new DocumentFromTestEntity(); + template.insert(e0, insertOpts); + final DocumentFromTestEntity e1 = new DocumentFromTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge1, insertOpts); + final DocumentFromTestEntity document = template.find(e0.id, DocumentFromTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getFrom(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(e0.getId())); + } + } + + public static class DocumentFromLazyTestEntity extends BasicTestEntity { + @From(lazy = true) + private Collection entities; + } + + @Test + public void documentFromLazy() { + final DocumentFromLazyTestEntity e0 = new DocumentFromLazyTestEntity(); + template.insert(e0, insertOpts); + final DocumentFromLazyTestEntity e1 = new DocumentFromLazyTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge1, insertOpts); + final DocumentFromLazyTestEntity document = template.find(e0.id, DocumentFromLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getFrom(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(e0.getId())); + } + } + + public static class DocumentFromLazySetTestEntity extends BasicTestEntity { + @From(lazy = true) + private Set entities; + } + + @Test + public void documentFromLazySet() { + final DocumentFromLazySetTestEntity e0 = new DocumentFromLazySetTestEntity(); + template.insert(e0, insertOpts); + final DocumentFromLazySetTestEntity e1 = new DocumentFromLazySetTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge1, insertOpts); + final DocumentFromLazySetTestEntity document = template.find(e0.id, DocumentFromLazySetTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getFrom(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(notNullValue())); + assertThat(e.getFrom().getId(), is(e0.getId())); + } + } + + public static class ConstructorWithFromParamsTestEntity extends BasicTestEntity { + @From + private final Collection value; + + public ConstructorWithFromParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithFromParams() { + final ConstructorWithFromParamsTestEntity entity = new ConstructorWithFromParamsTestEntity(null); + template.insert(entity, insertOpts); + final BasicTestEntity to = new BasicTestEntity(); + template.insert(to, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(entity, to); + final BasicEdgeLazyTestEntity edge2 = new BasicEdgeLazyTestEntity(entity, to); + template.insert(edge1, insertOpts); + template.insert(edge2, insertOpts); + final ConstructorWithFromParamsTestEntity document = template + .find(entity.id, ConstructorWithFromParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), hasItems(edge1.id, edge2.id)); + } + + public static class ConstructorWithFromLazyParamsTestEntity extends BasicTestEntity { + @From(lazy = true) + private final Collection value; + + public ConstructorWithFromLazyParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithFromLazyParams() { + final ConstructorWithFromLazyParamsTestEntity entity = new ConstructorWithFromLazyParamsTestEntity(null); + template.insert(entity, insertOpts); + final BasicTestEntity to = new BasicTestEntity(); + template.insert(to, insertOpts); + final BasicEdgeTestEntity edge1 = new BasicEdgeTestEntity(entity, to); + final BasicEdgeTestEntity edge2 = new BasicEdgeTestEntity(entity, to); + template.insert(edge1, insertOpts); + template.insert(edge2, insertOpts); + final ConstructorWithFromLazyParamsTestEntity document = template + .find(entity.id, ConstructorWithFromLazyParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.getId()).collect(Collectors.toList()), + hasItems(edge1.id, edge2.id)); + } + + public static class SingleDocumentFromTestEntity extends BasicTestEntity { + @From + private BasicEdgeLazyTestEntity entity; + } + + @Test + public void singleDocumentFrom() { + final SingleDocumentFromTestEntity e0 = new SingleDocumentFromTestEntity(); + template.insert(e0, insertOpts); + final SingleDocumentFromTestEntity e1 = new SingleDocumentFromTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge0, insertOpts); + final SingleDocumentFromTestEntity document = template.find(e0.id, SingleDocumentFromTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.getId(), is(edge0.id)); + assertThat(document.entity.getFrom(), is(notNullValue())); + assertThat(document.entity.getFrom().getId(), is(notNullValue())); + assertThat(document.entity.getFrom().getId(), is(e0.getId())); + } + + public static class SingleDocumentFromLazyTestEntity extends BasicTestEntity { + @From(lazy = true) + private BasicEdgeLazyTestEntity entity; + } + + @Test + public void singleDocumentFromLazy() { + final SingleDocumentFromLazyTestEntity e0 = new SingleDocumentFromLazyTestEntity(); + template.insert(e0, insertOpts); + final SingleDocumentFromLazyTestEntity e1 = new SingleDocumentFromLazyTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); + template.insert(edge0, insertOpts); + final SingleDocumentFromLazyTestEntity document = template.find(e0.id, SingleDocumentFromLazyTestEntity.class, findOpts) + .get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.getId(), is(edge0.id)); + assertThat(document.entity.getFrom(), is(notNullValue())); + assertThat(document.entity.getFrom().getId(), is(notNullValue())); + assertThat(document.entity.getFrom().getId(), is(e0.getId())); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTest.java index 58e8a6d5..bc6338ed 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTest.java @@ -1,232 +1,7 @@ -/* - * DISCLAIMER - * - * Copyright 2018 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - package com.arangodb.springframework.core.mapping; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.oneOf; -import static org.hamcrest.Matchers.notNullValue; - -import java.util.Collection; -import java.util.Set; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; - -import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.annotation.From; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; - -/** - * @author Mark Vollmary - * - */ -public class FromMappingTest extends AbstractArangoTest { - - public static class DocumentFromTestEntity extends BasicTestEntity { - @From - private Collection entities; - } - - @Test - public void documentFrom() { - final DocumentFromTestEntity e0 = new DocumentFromTestEntity(); - template.insert(e0); - final DocumentFromTestEntity e1 = new DocumentFromTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge1); - final DocumentFromTestEntity document = template.find(e0.id, DocumentFromTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getFrom(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(e0.getId())); - } - } - - public static class DocumentFromLazyTestEntity extends BasicTestEntity { - @From(lazy = true) - private Collection entities; - } - - @Test - public void documentFromLazy() { - final DocumentFromLazyTestEntity e0 = new DocumentFromLazyTestEntity(); - template.insert(e0); - final DocumentFromLazyTestEntity e1 = new DocumentFromLazyTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge1); - final DocumentFromLazyTestEntity document = template.find(e0.id, DocumentFromLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getFrom(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(e0.getId())); - } - } - - public static class DocumentFromLazySetTestEntity extends BasicTestEntity { - @From(lazy = true) - private Set entities; - } - - @Test - public void documentFromLazySet() { - final DocumentFromLazySetTestEntity e0 = new DocumentFromLazySetTestEntity(); - template.insert(e0); - final DocumentFromLazySetTestEntity e1 = new DocumentFromLazySetTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge1); - final DocumentFromLazySetTestEntity document = template.find(e0.id, DocumentFromLazySetTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getFrom(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(notNullValue())); - assertThat(e.getFrom().getId(), is(e0.getId())); - } - } - - public static class ConstructorWithFromParamsTestEntity extends BasicTestEntity { - @From - private final Collection value; - - public ConstructorWithFromParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithFromParams() { - final ConstructorWithFromParamsTestEntity entity = new ConstructorWithFromParamsTestEntity(null); - template.insert(entity); - final BasicTestEntity to = new BasicTestEntity(); - template.insert(to); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(entity, to); - final BasicEdgeLazyTestEntity edge2 = new BasicEdgeLazyTestEntity(entity, to); - template.insert(edge1); - template.insert(edge2); - final ConstructorWithFromParamsTestEntity document = template - .find(entity.id, ConstructorWithFromParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), hasItems(edge1.id, edge2.id)); - } - - public static class ConstructorWithFromLazyParamsTestEntity extends BasicTestEntity { - @From(lazy = true) - private final Collection value; - - public ConstructorWithFromLazyParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithFromLazyParams() { - final ConstructorWithFromLazyParamsTestEntity entity = new ConstructorWithFromLazyParamsTestEntity(null); - template.insert(entity); - final BasicTestEntity to = new BasicTestEntity(); - template.insert(to); - final BasicEdgeTestEntity edge1 = new BasicEdgeTestEntity(entity, to); - final BasicEdgeTestEntity edge2 = new BasicEdgeTestEntity(entity, to); - template.insert(edge1); - template.insert(edge2); - final ConstructorWithFromLazyParamsTestEntity document = template - .find(entity.id, ConstructorWithFromLazyParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.getId()).collect(Collectors.toList()), - hasItems(edge1.id, edge2.id)); - } - - public static class SingleDocumentFromTestEntity extends BasicTestEntity { - @From - private BasicEdgeLazyTestEntity entity; - } - - @Test - public void singleDocumentFrom() { - final SingleDocumentFromTestEntity e0 = new SingleDocumentFromTestEntity(); - template.insert(e0); - final SingleDocumentFromTestEntity e1 = new SingleDocumentFromTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge0); - final SingleDocumentFromTestEntity document = template.find(e0.id, SingleDocumentFromTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity.getId(), is(edge0.id)); - assertThat(document.entity.getFrom(), is(notNullValue())); - assertThat(document.entity.getFrom().getId(), is(notNullValue())); - assertThat(document.entity.getFrom().getId(), is(e0.getId())); - } - - public static class SingleDocumentFromLazyTestEntity extends BasicTestEntity { - @From(lazy = true) - private BasicEdgeLazyTestEntity entity; - } - - @Test - public void singleDocumentFromLazy() { - final SingleDocumentFromLazyTestEntity e0 = new SingleDocumentFromLazyTestEntity(); - template.insert(e0); - final SingleDocumentFromLazyTestEntity e1 = new SingleDocumentFromLazyTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e0, e1); - template.insert(edge0); - final SingleDocumentFromLazyTestEntity document = template.find(e0.id, SingleDocumentFromLazyTestEntity.class) - .get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity.getId(), is(edge0.id)); - assertThat(document.entity.getFrom(), is(notNullValue())); - assertThat(document.entity.getFrom().getId(), is(notNullValue())); - assertThat(document.entity.getFrom().getId(), is(e0.getId())); - } +public class FromMappingTest extends FromMapping { + FromMappingTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTxTest.java b/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTxTest.java new file mode 100644 index 00000000..0d4bad88 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/FromMappingTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.core.mapping; + +public class FromMappingTxTest extends FromMapping { + FromMappingTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java index aeaf1819..69cbae98 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java @@ -23,7 +23,6 @@ import com.arangodb.entity.DocumentEntity; import com.arangodb.model.AqlQueryOptions; import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.ArangoTestConfiguration; import com.arangodb.springframework.AuditorProvider; import com.arangodb.springframework.annotation.*; import com.arangodb.springframework.core.convert.ArangoConverter; diff --git a/src/test/java/com/arangodb/springframework/core/mapping/InheritanceMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/InheritanceMappingTest.java index d88e7258..ed4a1510 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/InheritanceMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/InheritanceMappingTest.java @@ -33,7 +33,6 @@ import org.junit.jupiter.api.Test; import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.ArangoTestConfiguration; import com.arangodb.springframework.annotation.Document; import com.arangodb.springframework.annotation.Edge; import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; diff --git a/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyCollectionLevelMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyCollectionLevelMappingTest.java index 1ddf0b52..42c39608 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyCollectionLevelMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyCollectionLevelMappingTest.java @@ -27,7 +27,6 @@ import org.springframework.beans.factory.annotation.Autowired; import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.ArangoTestConfiguration; import com.arangodb.springframework.annotation.Document; import com.arangodb.springframework.component.TenantProvider; diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RefMapping.java b/src/test/java/com/arangodb/springframework/core/mapping/RefMapping.java new file mode 100644 index 00000000..90113a00 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/RefMapping.java @@ -0,0 +1,449 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.annotation.ArangoId; +import com.arangodb.springframework.annotation.Document; +import com.arangodb.springframework.annotation.Ref; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; +import org.junit.jupiter.api.Test; +import org.springframework.data.annotation.Id; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +abstract class RefMapping extends AbstractMappingTxTestAbstract { + + RefMapping(boolean withinTx) { + super(withinTx, RefMapping.class.getDeclaredClasses()); + } + + public static class SingleReferenceTestEntity extends BasicTestEntity { + @Ref + private BasicTestEntity entity; + } + + @Test + public void singleRef() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final SingleReferenceTestEntity e0 = new SingleReferenceTestEntity(); + e0.entity = e1; + template.insert(e0, insertOpts); + final SingleReferenceTestEntity document = template.find(e0.id, SingleReferenceTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.id, is(e1.id)); + } + + public static class SingleReferenceLazyTestEntity extends BasicTestEntity { + @Ref(lazy = true) + private BasicTestEntity entity; + + public BasicTestEntity getEntity() { + return entity; + } + } + + @Test + public void singleRefLazy() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final SingleReferenceLazyTestEntity e0 = new SingleReferenceLazyTestEntity(); + e0.entity = e1; + template.insert(e0, insertOpts); + final SingleReferenceLazyTestEntity document = template.find(e0.id, SingleReferenceLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity, instanceOf(BasicTestEntity.class)); + assertThat(document.entity.getId(), is(e1.getId())); + } + + public static class DeepReferenceTestEntity extends BasicTestEntity { + @Ref + private SingleReferenceTestEntity entity; + } + + @Test + public void deepRef() { + BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + + SingleReferenceTestEntity e2 = new SingleReferenceTestEntity(); + e2.entity = e1; + template.insert(e2, insertOpts); + + DeepReferenceTestEntity e3 = new DeepReferenceTestEntity(); + e3.entity = e2; + template.insert(e3, insertOpts); + + final DeepReferenceTestEntity document = template.find(e3.id, DeepReferenceTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.id, is(e2.id)); + assertThat(document.entity.entity.id, is(e1.id)); + } + + public static class DeepReferenceLazyTestEntity extends BasicTestEntity { + @Ref(lazy = true) + private SingleReferenceLazyTestEntity entity; + + public SingleReferenceLazyTestEntity getEntity() { + return entity; + } + } + + @Test + public void deepRefLazy() { + BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + + SingleReferenceLazyTestEntity e2 = new SingleReferenceLazyTestEntity(); + e2.entity = e1; + template.insert(e2, insertOpts); + + DeepReferenceLazyTestEntity e3 = new DeepReferenceLazyTestEntity(); + e3.entity = e2; + template.insert(e3, insertOpts); + + final DeepReferenceLazyTestEntity document = template.find(e3.id, DeepReferenceLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getEntity(), is(notNullValue())); + assertThat(document.getEntity().getId(), is(e2.id)); + assertThat(document.getEntity().getEntity().getId(), is(e1.id)); + } + + public static class MultiReferenceTestEntity extends BasicTestEntity { + @Ref + private Collection entities; + } + + @Test + public void multiRef() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final MultiReferenceTestEntity e0 = new MultiReferenceTestEntity(); + e0.entities = Arrays.asList(e1, e2); + template.insert(e0, insertOpts); + final MultiReferenceTestEntity document = template.find(e0.id, MultiReferenceTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } + + public static class MultiReferenceLazyTestEntity extends BasicTestEntity { + @Ref(lazy = true) + private Collection entities; + } + + @Test + public void multiRefLazy() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final MultiReferenceLazyTestEntity e0 = new MultiReferenceLazyTestEntity(); + e0.entities = Arrays.asList(e1, e2); + template.insert(e0, insertOpts); + final MultiReferenceLazyTestEntity document = template.find(e0.id, MultiReferenceLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } + + public static class NestedReferenceTestEntity extends BasicTestEntity { + private NestedReferenceSubTestEntity sub; + } + + public static class NestedReferenceSubTestEntity { + @Ref + private Collection entities; + } + + @Test + public void testNestedRef() { + final NestedReferenceTestEntity o = new NestedReferenceTestEntity(); + o.sub = new NestedReferenceSubTestEntity(); + o.sub.entities = new ArrayList<>(); + final BasicTestEntity e = new BasicTestEntity(); + o.sub.entities.add(e); + template.insert(e, insertOpts); + template.insert(o, insertOpts); + final NestedReferenceTestEntity document = template.find(o.id, NestedReferenceTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.sub, is(notNullValue())); + assertThat(document.sub.entities, is(notNullValue())); + assertThat(document.sub.entities.size(), is(1)); + assertThat(document.sub.entities.iterator().next().id, is(e.id)); + } + + public static class NestedReferenceLazyTestEntity extends BasicTestEntity { + private NestedReferenceLazySubTestEntity sub; + } + + public static class NestedReferenceLazySubTestEntity { + @Ref(lazy = true) + private Collection entities; + + public Collection getEntities() { + return entities; + } + } + + @Test + public void testNestedRefLazy() { + final NestedReferenceLazyTestEntity o = new NestedReferenceLazyTestEntity(); + o.sub = new NestedReferenceLazySubTestEntity(); + o.sub.entities = new ArrayList<>(); + final BasicTestEntity e = new BasicTestEntity(); + o.sub.entities.add(e); + template.insert(e, insertOpts); + template.insert(o, insertOpts); + final NestedReferenceLazyTestEntity document = template.find(o.id, NestedReferenceLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.sub, is(notNullValue())); + assertThat(document.sub.getEntities(), is(notNullValue())); + assertThat(document.sub.getEntities().size(), is(1)); + assertThat(document.sub.getEntities().iterator().next().getId(), is(e.id)); + } + + public static class ConstructorWithRefParamsTestEntity extends BasicTestEntity { + @Ref + private final BasicTestEntity value1; + @Ref + private final Collection value2; + + public ConstructorWithRefParamsTestEntity(final BasicTestEntity value1, + final Collection value2) { + super(); + this.value1 = value1; + this.value2 = value2; + } + } + + @Test + public void constructorWithRefParams() { + final BasicTestEntity value1 = new BasicTestEntity(); + final BasicTestEntity value2 = new BasicTestEntity(); + final BasicTestEntity value3 = new BasicTestEntity(); + template.insert(value1, insertOpts); + template.insert(value2, insertOpts); + template.insert(value3, insertOpts); + final ConstructorWithRefParamsTestEntity entity = new ConstructorWithRefParamsTestEntity(value1, + Arrays.asList(value2, value3)); + template.insert(entity, insertOpts); + final ConstructorWithRefParamsTestEntity document = template + .find(entity.id, ConstructorWithRefParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value1.id, is(value1.id)); + assertThat(document.value2.size(), is(2)); + assertThat(document.value2.stream().map((e) -> e.id).collect(Collectors.toList()), + hasItems(value2.id, value3.id)); + } + + public static class ConstructorWithRefLazyParamsTestEntity extends BasicTestEntity { + @Ref(lazy = true) + private final BasicTestEntity value1; + @Ref(lazy = true) + private final Collection value2; + + public ConstructorWithRefLazyParamsTestEntity(final BasicTestEntity value1, + final Collection value2) { + super(); + this.value1 = value1; + this.value2 = value2; + } + } + + @Test + public void constructorWithRefLazyParams() { + final BasicTestEntity value1 = new BasicTestEntity(); + final BasicTestEntity value2 = new BasicTestEntity(); + final BasicTestEntity value3 = new BasicTestEntity(); + template.insert(value1, insertOpts); + template.insert(value2, insertOpts); + template.insert(value3, insertOpts); + final ConstructorWithRefLazyParamsTestEntity entity = new ConstructorWithRefLazyParamsTestEntity(value1, + Arrays.asList(value2, value3)); + template.insert(entity, insertOpts); + final ConstructorWithRefLazyParamsTestEntity document = template + .find(entity.id, ConstructorWithRefLazyParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value1.getId(), is(value1.id)); + assertThat(document.value2.size(), is(2)); + assertThat(document.value2.stream().map((e) -> e.getId()).collect(Collectors.toList()), + hasItems(value2.id, value3.id)); + } + + public static class PropertyRefInheritanceTestEntity extends BasicTestEntity { + @Ref + private BasicTestEntity value; + } + + public static class SimpleBasicChildTestEntity extends BasicTestEntity { + private String field; + } + + public static class ComplexBasicChildTestEntity extends BasicTestEntity { + private BasicTestEntity nestedEntity; + } + + @Test + public void propertyRefInheritanceMapping() { + final SimpleBasicChildTestEntity innerChild = new SimpleBasicChildTestEntity(); + innerChild.field = "value"; + final ComplexBasicChildTestEntity child = new ComplexBasicChildTestEntity(); + child.nestedEntity = innerChild; + final PropertyRefInheritanceTestEntity entity = new PropertyRefInheritanceTestEntity(); + entity.value = child; + template.insert(child, insertOpts); + template.insert(entity, insertOpts); + final PropertyRefInheritanceTestEntity document = template + .find(entity.getId(), PropertyRefInheritanceTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value, is(instanceOf(ComplexBasicChildTestEntity.class))); + final ComplexBasicChildTestEntity complexDocument = (ComplexBasicChildTestEntity) document.value; + assertThat(complexDocument.nestedEntity, is(instanceOf(SimpleBasicChildTestEntity.class))); + final SimpleBasicChildTestEntity simpleDocument = (SimpleBasicChildTestEntity) complexDocument.nestedEntity; + assertThat(simpleDocument.field, is(innerChild.field)); + } + + @Document("sameCollection") + static class TwoTypesInSameCollectionA extends BasicTestEntity { + String value; + String a; + } + + @Document("sameCollection") + static class TwoTypesInSameCollectionB extends BasicTestEntity { + String value; + String b; + } + + static class SameCollectionTestEntity extends BasicTestEntity { + @Ref + Collection value; + } + + @Test + public void twoTypesInSameCollection() { + final TwoTypesInSameCollectionA a = new TwoTypesInSameCollectionA(); + a.value = "testA"; + a.a = "testA"; + final TwoTypesInSameCollectionB b = new TwoTypesInSameCollectionB(); + b.value = "testB"; + b.b = "testB"; + final SameCollectionTestEntity c = new SameCollectionTestEntity(); + c.value = new ArrayList<>(); + c.value.add(a); + c.value.add(b); + + template.insert(a, insertOpts); + template.insert(b, insertOpts); + template.insert(c, insertOpts); + final Optional findC = template.find(c.getId(), SameCollectionTestEntity.class, findOpts); + assertThat(findC.isPresent(), is(true)); + final Collection value = findC.get().value; + assertThat(value.size(), is(2)); + { + assertThat(value.stream().filter(v -> v instanceof TwoTypesInSameCollectionA).count(), is(1L)); + final Optional findA = value.stream().filter(v -> v instanceof TwoTypesInSameCollectionA) + .findFirst(); + assertThat(findA.isPresent(), is(true)); + final TwoTypesInSameCollectionA aa = (TwoTypesInSameCollectionA) findA.get(); + assertThat(aa.value, is("testA")); + assertThat(aa.a, is("testA")); + } + { + assertThat(value.stream().filter(v -> v instanceof TwoTypesInSameCollectionB).count(), is(1L)); + final Optional findB = value.stream().filter(v -> v instanceof TwoTypesInSameCollectionB) + .findFirst(); + assertThat(findB.isPresent(), is(true)); + final TwoTypesInSameCollectionB bb = (TwoTypesInSameCollectionB) findB.get(); + assertThat(bb.value, is("testB")); + assertThat(bb.b, is("testB")); + } + } + + static class ArangoIdOnlyTestEntity { + @ArangoId + String id; + @Ref + Collection refs; + } + + @Test + public void arangoIdOnly() { + final ArangoIdOnlyTestEntity a = new ArangoIdOnlyTestEntity(); + final ArangoIdOnlyTestEntity b = new ArangoIdOnlyTestEntity(); + b.refs = Arrays.asList(a); + + template.insert(a, insertOpts); + template.insert(b, insertOpts); + + final Optional find = template.find(b.id, ArangoIdOnlyTestEntity.class, findOpts); + assertThat(find.isPresent(), is(true)); + final Collection refs = find.get().refs; + assertThat(refs.size(), is(1)); + assertThat(refs.stream().findFirst().get().id, is(a.id)); + } + + static class ArangoIdAndIdTestEntity { + @ArangoId + String arangoId; + @Id + String id; + @Ref + Collection refs; + } + + @Test + public void arangoIdAndId() { + final ArangoIdAndIdTestEntity a = new ArangoIdAndIdTestEntity(); + final ArangoIdAndIdTestEntity b = new ArangoIdAndIdTestEntity(); + b.refs = Arrays.asList(a); + + template.insert(a, insertOpts); + a.id = null; + template.insert(b, insertOpts); + + final Optional find = template.find(b.arangoId, ArangoIdAndIdTestEntity.class, findOpts); + assertThat(find.isPresent(), is(true)); + final Collection refs = find.get().refs; + assertThat(refs.size(), is(1)); + assertThat(refs.stream().findFirst().get().arangoId, is(a.arangoId)); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTest.java index f7728b1a..b5858300 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTest.java @@ -1,370 +1,7 @@ -/* - * DISCLAIMER - * - * Copyright 2018 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - package com.arangodb.springframework.core.mapping; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.oneOf; -import static org.hamcrest.Matchers.notNullValue; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Optional; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; -import org.springframework.data.annotation.Id; - -import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.annotation.ArangoId; -import com.arangodb.springframework.annotation.Document; -import com.arangodb.springframework.annotation.Ref; -import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; - -/** - * @author Mark Vollmary - * - */ -public class RefMappingTest extends AbstractArangoTest { - - public static class SingleReferenceTestEntity extends BasicTestEntity { - @Ref - private BasicTestEntity entity; - } - - @Test - public void singleRef() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final SingleReferenceTestEntity e0 = new SingleReferenceTestEntity(); - e0.entity = e1; - template.insert(e0); - final SingleReferenceTestEntity document = template.find(e0.id, SingleReferenceTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity.id, is(e1.id)); - } - - public static class SingleReferenceLazyTestEntity extends BasicTestEntity { - @Ref(lazy = true) - private BasicTestEntity entity; - } - - @Test - public void singleRefLazy() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final SingleReferenceLazyTestEntity e0 = new SingleReferenceLazyTestEntity(); - e0.entity = e1; - template.insert(e0); - final SingleReferenceLazyTestEntity document = template.find(e0.id, SingleReferenceLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity, instanceOf(BasicTestEntity.class)); - assertThat(document.entity.getId(), is(e1.getId())); - } - - public static class MultiReferenceTestEntity extends BasicTestEntity { - @Ref - private Collection entities; - } - - @Test - public void multiRef() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final MultiReferenceTestEntity e0 = new MultiReferenceTestEntity(); - e0.entities = Arrays.asList(e1, e2); - template.insert(e0); - final MultiReferenceTestEntity document = template.find(e0.id, MultiReferenceTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } - - public static class MultiReferenceLazyTestEntity extends BasicTestEntity { - @Ref(lazy = true) - private Collection entities; - } - - @Test - public void multiRefLazy() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final MultiReferenceLazyTestEntity e0 = new MultiReferenceLazyTestEntity(); - e0.entities = Arrays.asList(e1, e2); - template.insert(e0); - final MultiReferenceLazyTestEntity document = template.find(e0.id, MultiReferenceLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } - - public static class NestedReferenceTestEntity extends BasicTestEntity { - private NestedReferenceSubTestEntity sub; - } - - public static class NestedReferenceSubTestEntity { - @Ref - private Collection entities; - } - - @Test - public void testNestedRef() { - final NestedReferenceTestEntity o = new NestedReferenceTestEntity(); - o.sub = new NestedReferenceSubTestEntity(); - o.sub.entities = new ArrayList<>(); - final BasicTestEntity e = new BasicTestEntity(); - o.sub.entities.add(e); - template.insert(e); - template.insert(o); - final NestedReferenceTestEntity document = template.find(o.id, NestedReferenceTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.sub, is(notNullValue())); - assertThat(document.sub.entities, is(notNullValue())); - assertThat(document.sub.entities.size(), is(1)); - assertThat(document.sub.entities.iterator().next().id, is(e.id)); - } - - public static class ConstructorWithRefParamsTestEntity extends BasicTestEntity { - @Ref - private final BasicTestEntity value1; - @Ref - private final Collection value2; - - public ConstructorWithRefParamsTestEntity(final BasicTestEntity value1, - final Collection value2) { - super(); - this.value1 = value1; - this.value2 = value2; - } - } - - @Test - public void constructorWithRefParams() { - final BasicTestEntity value1 = new BasicTestEntity(); - final BasicTestEntity value2 = new BasicTestEntity(); - final BasicTestEntity value3 = new BasicTestEntity(); - template.insert(value1); - template.insert(value2); - template.insert(value3); - final ConstructorWithRefParamsTestEntity entity = new ConstructorWithRefParamsTestEntity(value1, - Arrays.asList(value2, value3)); - template.insert(entity); - final ConstructorWithRefParamsTestEntity document = template - .find(entity.id, ConstructorWithRefParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value1.id, is(value1.id)); - assertThat(document.value2.size(), is(2)); - assertThat(document.value2.stream().map((e) -> e.id).collect(Collectors.toList()), - hasItems(value2.id, value3.id)); - } - - public static class ConstructorWithRefLazyParamsTestEntity extends BasicTestEntity { - @Ref(lazy = true) - private final BasicTestEntity value1; - @Ref(lazy = true) - private final Collection value2; - - public ConstructorWithRefLazyParamsTestEntity(final BasicTestEntity value1, - final Collection value2) { - super(); - this.value1 = value1; - this.value2 = value2; - } - } - - @Test - public void constructorWithRefLazyParams() { - final BasicTestEntity value1 = new BasicTestEntity(); - final BasicTestEntity value2 = new BasicTestEntity(); - final BasicTestEntity value3 = new BasicTestEntity(); - template.insert(value1); - template.insert(value2); - template.insert(value3); - final ConstructorWithRefLazyParamsTestEntity entity = new ConstructorWithRefLazyParamsTestEntity(value1, - Arrays.asList(value2, value3)); - template.insert(entity); - final ConstructorWithRefLazyParamsTestEntity document = template - .find(entity.id, ConstructorWithRefLazyParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value1.getId(), is(value1.id)); - assertThat(document.value2.size(), is(2)); - assertThat(document.value2.stream().map((e) -> e.getId()).collect(Collectors.toList()), - hasItems(value2.id, value3.id)); - } - - public static class PropertyRefInheritanceTestEntity extends BasicTestEntity { - @Ref - private BasicTestEntity value; - } - - public static class SimpleBasicChildTestEntity extends BasicTestEntity { - private String field; - } - - public static class ComplexBasicChildTestEntity extends BasicTestEntity { - private BasicTestEntity nestedEntity; - } - - @Test - public void propertyRefInheritanceMapping() { - final SimpleBasicChildTestEntity innerChild = new SimpleBasicChildTestEntity(); - innerChild.field = "value"; - final ComplexBasicChildTestEntity child = new ComplexBasicChildTestEntity(); - child.nestedEntity = innerChild; - final PropertyRefInheritanceTestEntity entity = new PropertyRefInheritanceTestEntity(); - entity.value = child; - template.insert(child); - template.insert(entity); - final PropertyRefInheritanceTestEntity document = template - .find(entity.getId(), PropertyRefInheritanceTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value, is(instanceOf(ComplexBasicChildTestEntity.class))); - final ComplexBasicChildTestEntity complexDocument = (ComplexBasicChildTestEntity) document.value; - assertThat(complexDocument.nestedEntity, is(instanceOf(SimpleBasicChildTestEntity.class))); - final SimpleBasicChildTestEntity simpleDocument = (SimpleBasicChildTestEntity) complexDocument.nestedEntity; - assertThat(simpleDocument.field, is(innerChild.field)); - } - - @Document("sameCollection") - static class TwoTypesInSameCollectionA extends BasicTestEntity { - String value; - String a; - } - - @Document("sameCollection") - static class TwoTypesInSameCollectionB extends BasicTestEntity { - String value; - String b; - } - - static class SameCollectionTestEntity extends BasicTestEntity { - @Ref - Collection value; - } - - @Test - public void twoTypesInSameCollection() { - final TwoTypesInSameCollectionA a = new TwoTypesInSameCollectionA(); - a.value = "testA"; - a.a = "testA"; - final TwoTypesInSameCollectionB b = new TwoTypesInSameCollectionB(); - b.value = "testB"; - b.b = "testB"; - final SameCollectionTestEntity c = new SameCollectionTestEntity(); - c.value = new ArrayList<>(); - c.value.add(a); - c.value.add(b); - - template.insert(a); - template.insert(b); - template.insert(c); - final Optional findC = template.find(c.getId(), SameCollectionTestEntity.class); - assertThat(findC.isPresent(), is(true)); - final Collection value = findC.get().value; - assertThat(value.size(), is(2)); - { - assertThat(value.stream().filter(v -> v instanceof TwoTypesInSameCollectionA).count(), is(1L)); - final Optional findA = value.stream().filter(v -> v instanceof TwoTypesInSameCollectionA) - .findFirst(); - assertThat(findA.isPresent(), is(true)); - final TwoTypesInSameCollectionA aa = (TwoTypesInSameCollectionA) findA.get(); - assertThat(aa.value, is("testA")); - assertThat(aa.a, is("testA")); - } - { - assertThat(value.stream().filter(v -> v instanceof TwoTypesInSameCollectionB).count(), is(1L)); - final Optional findB = value.stream().filter(v -> v instanceof TwoTypesInSameCollectionB) - .findFirst(); - assertThat(findB.isPresent(), is(true)); - final TwoTypesInSameCollectionB bb = (TwoTypesInSameCollectionB) findB.get(); - assertThat(bb.value, is("testB")); - assertThat(bb.b, is("testB")); - } - } - - static class ArangoIdOnlyTestEntity { - @ArangoId - String id; - @Ref - Collection refs; - } - - @Test - public void arangoIdOnly() { - final ArangoIdOnlyTestEntity a = new ArangoIdOnlyTestEntity(); - final ArangoIdOnlyTestEntity b = new ArangoIdOnlyTestEntity(); - b.refs = Arrays.asList(a); - - template.insert(a); - template.insert(b); - - final Optional find = template.find(b.id, ArangoIdOnlyTestEntity.class); - assertThat(find.isPresent(), is(true)); - final Collection refs = find.get().refs; - assertThat(refs.size(), is(1)); - assertThat(refs.stream().findFirst().get().id, is(a.id)); - } - - static class ArangoIdAndIdTestEntity { - @ArangoId - String arangoId; - @Id - String id; - @Ref - Collection refs; - } - - @Test - public void arangoIdAndId() { - final ArangoIdAndIdTestEntity a = new ArangoIdAndIdTestEntity(); - final ArangoIdAndIdTestEntity b = new ArangoIdAndIdTestEntity(); - b.refs = Arrays.asList(a); - - template.insert(a); - a.id = null; - template.insert(b); - - final Optional find = template.find(b.arangoId, ArangoIdAndIdTestEntity.class); - assertThat(find.isPresent(), is(true)); - final Collection refs = find.get().refs; - assertThat(refs.size(), is(1)); - assertThat(refs.stream().findFirst().get().arangoId, is(a.arangoId)); - } +public class RefMappingTest extends RefMapping { + RefMappingTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTxTest.java b/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTxTest.java new file mode 100644 index 00000000..48dc9fe5 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/RefMappingTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.core.mapping; + +public class RefMappingTxTest extends RefMapping { + RefMappingTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RelationsMapping.java b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMapping.java new file mode 100644 index 00000000..25aec3f4 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMapping.java @@ -0,0 +1,254 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.annotation.Relations; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; +import org.junit.jupiter.api.Test; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +/** + * @author Mark Vollmary + */ +abstract class RelationsMapping extends AbstractMappingTxTestAbstract { + + RelationsMapping(boolean withinTx) { + super(withinTx, RelationsMapping.class.getDeclaredClasses()); + } + + public static class RelationsTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class) + private Collection entities; + } + + @Test + public void relations() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final RelationsTestEntity e0 = new RelationsTestEntity(); + template.insert(e0, insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e1), insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e2), insertOpts); + + final RelationsTestEntity document = template.find(e0.id, RelationsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } + + public static class RelationsLazyTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class, lazy = true) + private Collection entities; + } + + @Test + public void relationsLazy() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final RelationsLazyTestEntity e0 = new RelationsLazyTestEntity(); + template.insert(e0, insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e1), insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e2), insertOpts); + + final RelationsLazyTestEntity document = template.find(e0.id, RelationsLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } + + public static class RelationsLazySetTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class, lazy = true) + private Set entities; + } + + @Test + public void relationsLazySet() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final RelationsLazySetTestEntity e0 = new RelationsLazySetTestEntity(); + template.insert(e0, insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e1), insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e2), insertOpts); + + final RelationsLazySetTestEntity document = template.find(e0.id, RelationsLazySetTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } + + public static class ConstructorWithRelationsParamsTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class) + private final Collection value; + + public ConstructorWithRelationsParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithRelationsParams() { + final BasicTestEntity vertex1 = new BasicTestEntity(); + final BasicTestEntity vertex2 = new BasicTestEntity(); + template.insert(vertex1, insertOpts); + template.insert(vertex2, insertOpts); + final ConstructorWithRelationsParamsTestEntity entity = new ConstructorWithRelationsParamsTestEntity( + Arrays.asList(vertex1, vertex2)); + template.insert(entity, insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex1), insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex2), insertOpts); + final ConstructorWithRelationsParamsTestEntity document = template + .find(entity.id, ConstructorWithRelationsParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), + hasItems(vertex1.id, vertex2.id)); + } + + public static class ConstructorWithRelationsLazyParamsTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class, lazy = true) + private final Collection value; + + public ConstructorWithRelationsLazyParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithRelationsLazyParams() { + final BasicTestEntity vertex1 = new BasicTestEntity(); + final BasicTestEntity vertex2 = new BasicTestEntity(); + template.insert(vertex1, insertOpts); + template.insert(vertex2, insertOpts); + final ConstructorWithRelationsLazyParamsTestEntity entity = new ConstructorWithRelationsLazyParamsTestEntity( + Arrays.asList(vertex1, vertex2)); + template.insert(entity, insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex1), insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex2), insertOpts); + final ConstructorWithRelationsLazyParamsTestEntity document = template + .find(entity.id, ConstructorWithRelationsLazyParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), + hasItems(vertex1.id, vertex2.id)); + } + + static class SingleRelationsTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class) + private BasicTestEntity value; + } + + @Test + public void singleRelations() { + final BasicTestEntity vertex = new BasicTestEntity(); + template.insert(vertex, insertOpts); + final SingleRelationsTestEntity entity = new SingleRelationsTestEntity(); + template.insert(entity, insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex), insertOpts); + final SingleRelationsTestEntity document = template.find(entity.id, SingleRelationsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.id, is(vertex.id)); + + } + + static class SingleRelationsLazyTestEntity extends BasicTestEntity { + @Relations(edges = BasicEdgeTestEntity.class, lazy = true) + private BasicTestEntity value; + } + + @Test + public void singleRelationsLazy() { + final BasicTestEntity vertex = new BasicTestEntity(); + template.insert(vertex, insertOpts); + final SingleRelationsLazyTestEntity entity = new SingleRelationsLazyTestEntity(); + template.insert(entity, insertOpts); + template.insert(new BasicEdgeTestEntity(entity, vertex), insertOpts); + final SingleRelationsLazyTestEntity document = template.find(entity.id, SingleRelationsLazyTestEntity.class, findOpts) + .get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.getId(), is(vertex.id)); + + } + + static class AnotherBasicEdgeTestEntity extends BasicEdgeTestEntity { + public AnotherBasicEdgeTestEntity() { + super(); + } + + public AnotherBasicEdgeTestEntity(final BasicTestEntity from, final BasicTestEntity to) { + super(from, to); + } + } + + static class MultipleEdgeTypeRelationsTestEntity extends BasicTestEntity { + @Relations(edges = {BasicEdgeTestEntity.class, AnotherBasicEdgeTestEntity.class}, maxDepth = 2) + private Collection entities; + } + + @Test + public void multipleEdgeTypeRelations() { + final BasicTestEntity e1 = new BasicTestEntity(); + template.insert(e1, insertOpts); + final BasicTestEntity e2 = new BasicTestEntity(); + template.insert(e2, insertOpts); + final MultipleEdgeTypeRelationsTestEntity e0 = new MultipleEdgeTypeRelationsTestEntity(); + template.insert(e0, insertOpts); + template.insert(new BasicEdgeTestEntity(e0, e1), insertOpts); + template.insert(new AnotherBasicEdgeTestEntity(e1, e2), insertOpts); + + final MultipleEdgeTypeRelationsTestEntity document = template + .find(e0.id, MultipleEdgeTypeRelationsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); + } + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTest.java index dc41f245..13aa4f5d 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTest.java @@ -1,260 +1,7 @@ -/* - * DISCLAIMER - * - * Copyright 2018 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - package com.arangodb.springframework.core.mapping; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.oneOf; -import static org.hamcrest.Matchers.notNullValue; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Set; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; - -import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.annotation.Relations; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; - -/** - * @author Mark Vollmary - * - */ -public class RelationsMappingTest extends AbstractArangoTest { - - public static class RelationsTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class) - private Collection entities; - } - - @Test - public void relations() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final RelationsTestEntity e0 = new RelationsTestEntity(); - template.insert(e0); - template.insert(new BasicEdgeTestEntity(e0, e1)); - template.insert(new BasicEdgeTestEntity(e0, e2)); - - final RelationsTestEntity document = template.find(e0.id, RelationsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } - - public static class RelationsLazyTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class, lazy = true) - private Collection entities; - } - - @Test - public void relationsLazy() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final RelationsLazyTestEntity e0 = new RelationsLazyTestEntity(); - template.insert(e0); - template.insert(new BasicEdgeTestEntity(e0, e1)); - template.insert(new BasicEdgeTestEntity(e0, e2)); - - final RelationsLazyTestEntity document = template.find(e0.id, RelationsLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } - - public static class RelationsLazySetTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class, lazy = true) - private Set entities; - } - - @Test - public void relationsLazySet() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final RelationsLazySetTestEntity e0 = new RelationsLazySetTestEntity(); - template.insert(e0); - template.insert(new BasicEdgeTestEntity(e0, e1)); - template.insert(new BasicEdgeTestEntity(e0, e2)); - - final RelationsLazySetTestEntity document = template.find(e0.id, RelationsLazySetTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } - - public static class ConstructorWithRelationsParamsTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class) - private final Collection value; - - public ConstructorWithRelationsParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithRelationsParams() { - final BasicTestEntity vertex1 = new BasicTestEntity(); - final BasicTestEntity vertex2 = new BasicTestEntity(); - template.insert(vertex1); - template.insert(vertex2); - final ConstructorWithRelationsParamsTestEntity entity = new ConstructorWithRelationsParamsTestEntity( - Arrays.asList(vertex1, vertex2)); - template.insert(entity); - template.insert(new BasicEdgeTestEntity(entity, vertex1)); - template.insert(new BasicEdgeTestEntity(entity, vertex2)); - final ConstructorWithRelationsParamsTestEntity document = template - .find(entity.id, ConstructorWithRelationsParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), - hasItems(vertex1.id, vertex2.id)); - } - - public static class ConstructorWithRelationsLazyParamsTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class, lazy = true) - private final Collection value; - - public ConstructorWithRelationsLazyParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithRelationsLazyParams() { - final BasicTestEntity vertex1 = new BasicTestEntity(); - final BasicTestEntity vertex2 = new BasicTestEntity(); - template.insert(vertex1); - template.insert(vertex2); - final ConstructorWithRelationsLazyParamsTestEntity entity = new ConstructorWithRelationsLazyParamsTestEntity( - Arrays.asList(vertex1, vertex2)); - template.insert(entity); - template.insert(new BasicEdgeTestEntity(entity, vertex1)); - template.insert(new BasicEdgeTestEntity(entity, vertex2)); - final ConstructorWithRelationsLazyParamsTestEntity document = template - .find(entity.id, ConstructorWithRelationsLazyParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), - hasItems(vertex1.id, vertex2.id)); - } - - static class SingleRelationsTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class) - private BasicTestEntity value; - } - - @Test - public void singleRelations() { - final BasicTestEntity vertex = new BasicTestEntity(); - template.insert(vertex); - final SingleRelationsTestEntity entity = new SingleRelationsTestEntity(); - template.insert(entity); - template.insert(new BasicEdgeTestEntity(entity, vertex)); - final SingleRelationsTestEntity document = template.find(entity.id, SingleRelationsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.id, is(vertex.id)); - - } - - static class SingleRelationsLazyTestEntity extends BasicTestEntity { - @Relations(edges = BasicEdgeTestEntity.class, lazy = true) - private BasicTestEntity value; - } - - @Test - public void singleRelationsLazy() { - final BasicTestEntity vertex = new BasicTestEntity(); - template.insert(vertex); - final SingleRelationsLazyTestEntity entity = new SingleRelationsLazyTestEntity(); - template.insert(entity); - template.insert(new BasicEdgeTestEntity(entity, vertex)); - final SingleRelationsLazyTestEntity document = template.find(entity.id, SingleRelationsLazyTestEntity.class) - .get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.getId(), is(vertex.id)); - - } - - static class AnotherBasicEdgeTestEntity extends BasicEdgeTestEntity { - public AnotherBasicEdgeTestEntity() { - super(); - } - - public AnotherBasicEdgeTestEntity(final BasicTestEntity from, final BasicTestEntity to) - { - super(from, to); - } - } - - static class MultipleEdgeTypeRelationsTestEntity extends BasicTestEntity { - @Relations(edges = {BasicEdgeTestEntity.class, AnotherBasicEdgeTestEntity.class}, maxDepth = 2) - private Collection entities; - } - - @Test - public void multipleEdgeTypeRelations() { - final BasicTestEntity e1 = new BasicTestEntity(); - template.insert(e1); - final BasicTestEntity e2 = new BasicTestEntity(); - template.insert(e2); - final MultipleEdgeTypeRelationsTestEntity e0 = new MultipleEdgeTypeRelationsTestEntity(); - template.insert(e0); - template.insert(new BasicEdgeTestEntity(e0, e1)); - template.insert(new AnotherBasicEdgeTestEntity(e1, e2)); - - final MultipleEdgeTypeRelationsTestEntity document = template - .find(e0.id, MultipleEdgeTypeRelationsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(e1.getId(), e2.getId()))); - } - } +public class RelationsMappingTest extends RelationsMapping { + RelationsMappingTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTxTest.java b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTxTest.java new file mode 100644 index 00000000..db14dec7 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/RelationsMappingTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.core.mapping; + +public class RelationsMappingTxTest extends RelationsMapping { + RelationsMappingTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/ToMapping.java b/src/test/java/com/arangodb/springframework/core/mapping/ToMapping.java new file mode 100644 index 00000000..793b5e20 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/ToMapping.java @@ -0,0 +1,230 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * 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. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import com.arangodb.springframework.annotation.To; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +/** + * @author Mark Vollmary + */ +abstract class ToMapping extends AbstractMappingTxTestAbstract { + + ToMapping(boolean withinTx) { + super(withinTx, ToMapping.class.getDeclaredClasses()); + } + + public static class DocumentToTestEntity extends BasicTestEntity { + @To + private Collection entities; + } + + @Test + public void documentTo() { + final DocumentToTestEntity e0 = new DocumentToTestEntity(); + template.insert(e0, insertOpts); + final DocumentToTestEntity e1 = new DocumentToTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge1, insertOpts); + final DocumentToTestEntity document = template.find(e0.id, DocumentToTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getTo(), is(notNullValue())); + assertThat(e.getTo().getId(), is(notNullValue())); + assertThat(e.getTo().getId(), is(e0.getId())); + } + } + + public static class DocumentToLazyTestEntity extends BasicTestEntity { + @To(lazy = true) + private Collection entities; + } + + @Test + public void documentToLazy() { + final DocumentToLazyTestEntity e0 = new DocumentToLazyTestEntity(); + template.insert(e0, insertOpts); + final DocumentToLazyTestEntity e1 = new DocumentToLazyTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge1, insertOpts); + final DocumentToLazyTestEntity document = template.find(e0.id, DocumentToLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getTo(), is(notNullValue())); + assertThat(e.getTo().getId(), is(notNullValue())); + assertThat(e.getTo().getId(), is(e0.getId())); + } + } + + public static class DocumentToLazyTestSetEntity extends BasicTestEntity { + @To(lazy = true) + private Collection entities; + } + + @Test + public void documentToLazySet() { + final DocumentToLazyTestEntity e0 = new DocumentToLazyTestEntity(); + template.insert(e0, insertOpts); + final DocumentToLazyTestEntity e1 = new DocumentToLazyTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge0, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge1, insertOpts); + final DocumentToLazyTestEntity document = template.find(e0.id, DocumentToLazyTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entities, is(notNullValue())); + assertThat(document.entities.size(), is(2)); + for (final BasicEdgeLazyTestEntity e : document.entities) { + assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); + assertThat(e.getId(), is(notNullValue())); + assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); + assertThat(e.getTo(), is(notNullValue())); + assertThat(e.getTo().getId(), is(notNullValue())); + assertThat(e.getTo().getId(), is(e0.getId())); + } + } + + public static class ConstructorWithToParamsTestEntity extends BasicTestEntity { + @To + private final Collection value; + + public ConstructorWithToParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithToParams() { + final ConstructorWithToParamsTestEntity entity = new ConstructorWithToParamsTestEntity(null); + template.insert(entity, insertOpts); + final BasicTestEntity from = new BasicTestEntity(); + template.insert(from, insertOpts); + final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(from, entity); + final BasicEdgeLazyTestEntity edge2 = new BasicEdgeLazyTestEntity(from, entity); + template.insert(edge1, insertOpts); + template.insert(edge2, insertOpts); + final ConstructorWithToParamsTestEntity document = template + .find(entity.id, ConstructorWithToParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), hasItems(edge1.id, edge2.id)); + } + + public static class ConstructorWithToLazyParamsTestEntity extends BasicTestEntity { + @To(lazy = true) + private final Collection value; + + public ConstructorWithToLazyParamsTestEntity(final Collection value) { + super(); + this.value = value; + } + } + + @Test + public void constructorWithToLazyParams() { + final ConstructorWithToLazyParamsTestEntity entity = new ConstructorWithToLazyParamsTestEntity(null); + template.insert(entity, insertOpts); + final BasicTestEntity from = new BasicTestEntity(); + template.insert(from, insertOpts); + final BasicEdgeTestEntity edge1 = new BasicEdgeTestEntity(from, entity); + final BasicEdgeTestEntity edge2 = new BasicEdgeTestEntity(from, entity); + template.insert(edge1, insertOpts); + template.insert(edge2, insertOpts); + final ConstructorWithToLazyParamsTestEntity document = template + .find(entity.id, ConstructorWithToLazyParamsTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.value.stream().map((e) -> e.getId()).collect(Collectors.toList()), + hasItems(edge1.id, edge2.id)); + } + + public static class SingleDocumentToTestEntity extends BasicTestEntity { + @To + private BasicEdgeLazyTestEntity entity; + } + + @Test + public void singleDocumentTo() { + final SingleDocumentToTestEntity e0 = new SingleDocumentToTestEntity(); + template.insert(e0, insertOpts); + final SingleDocumentToTestEntity e1 = new SingleDocumentToTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge0, insertOpts); + final SingleDocumentToTestEntity document = template.find(e0.id, SingleDocumentToTestEntity.class, findOpts).get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.getId(), is(notNullValue())); + assertThat(document.entity.getId(), is(edge0.getId())); + assertThat(document.entity.getTo(), is(notNullValue())); + assertThat(document.entity.getTo().getId(), is(notNullValue())); + assertThat(document.entity.getTo().getId(), is(e0.getId())); + } + + public static class SingleDocumentToLazyTestEntity extends BasicTestEntity { + @To(lazy = true) + private BasicEdgeLazyTestEntity entity; + } + + @Test + public void singleDocumentToLazy() { + final SingleDocumentToLazyTestEntity e0 = new SingleDocumentToLazyTestEntity(); + template.insert(e0, insertOpts); + final SingleDocumentToLazyTestEntity e1 = new SingleDocumentToLazyTestEntity(); + template.insert(e1, insertOpts); + final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); + template.insert(edge0, insertOpts); + final SingleDocumentToLazyTestEntity document = template.find(e0.id, SingleDocumentToLazyTestEntity.class, findOpts) + .get(); + assertThat(document, is(notNullValue())); + assertThat(document.entity, is(notNullValue())); + assertThat(document.entity.getId(), is(notNullValue())); + assertThat(document.entity.getId(), is(edge0.getId())); + assertThat(document.entity.getTo(), is(notNullValue())); + assertThat(document.entity.getTo().getId(), is(notNullValue())); + assertThat(document.entity.getTo().getId(), is(e0.getId())); + } +} diff --git a/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTest.java index 92090731..bd75f505 100644 --- a/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTest.java +++ b/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTest.java @@ -1,233 +1,7 @@ -/* - * DISCLAIMER - * - * Copyright 2018 ArangoDB GmbH, Cologne, Germany - * - * 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. - * - * Copyright holder is ArangoDB GmbH, Cologne, Germany - */ - package com.arangodb.springframework.core.mapping; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.oneOf; -import static org.hamcrest.Matchers.notNullValue; - -import java.util.Collection; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; - -import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.annotation.To; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; -import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; - -/** - * @author Mark Vollmary - * - */ -public class ToMappingTest extends AbstractArangoTest { - - public static class DocumentToTestEntity extends BasicTestEntity { - @To - private Collection entities; - } - - @Test - public void documentTo() { - final DocumentToTestEntity e0 = new DocumentToTestEntity(); - template.insert(e0); - final DocumentToTestEntity e1 = new DocumentToTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge1); - final DocumentToTestEntity document = template.find(e0.id, DocumentToTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getTo(), is(notNullValue())); - assertThat(e.getTo().getId(), is(notNullValue())); - assertThat(e.getTo().getId(), is(e0.getId())); - } - } - - public static class DocumentToLazyTestEntity extends BasicTestEntity { - @To(lazy = true) - private Collection entities; - } - - @Test - public void documentToLazy() { - final DocumentToLazyTestEntity e0 = new DocumentToLazyTestEntity(); - template.insert(e0); - final DocumentToLazyTestEntity e1 = new DocumentToLazyTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge1); - final DocumentToLazyTestEntity document = template.find(e0.id, DocumentToLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getTo(), is(notNullValue())); - assertThat(e.getTo().getId(), is(notNullValue())); - assertThat(e.getTo().getId(), is(e0.getId())); - } - } - - public static class DocumentToLazyTestSetEntity extends BasicTestEntity { - @To(lazy = true) - private Collection entities; - } - - @Test - public void documentToLazySet() { - final DocumentToLazyTestEntity e0 = new DocumentToLazyTestEntity(); - template.insert(e0); - final DocumentToLazyTestEntity e1 = new DocumentToLazyTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge0); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge1); - final DocumentToLazyTestEntity document = template.find(e0.id, DocumentToLazyTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entities, is(notNullValue())); - assertThat(document.entities.size(), is(2)); - for (final BasicEdgeLazyTestEntity e : document.entities) { - assertThat(e, instanceOf(BasicEdgeLazyTestEntity.class)); - assertThat(e.getId(), is(notNullValue())); - assertThat(e.getId(), is(oneOf(edge0.getId(), edge1.getId()))); - assertThat(e.getTo(), is(notNullValue())); - assertThat(e.getTo().getId(), is(notNullValue())); - assertThat(e.getTo().getId(), is(e0.getId())); - } - } - - public static class ConstructorWithToParamsTestEntity extends BasicTestEntity { - @To - private final Collection value; - - public ConstructorWithToParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithToParams() { - final ConstructorWithToParamsTestEntity entity = new ConstructorWithToParamsTestEntity(null); - template.insert(entity); - final BasicTestEntity from = new BasicTestEntity(); - template.insert(from); - final BasicEdgeLazyTestEntity edge1 = new BasicEdgeLazyTestEntity(from, entity); - final BasicEdgeLazyTestEntity edge2 = new BasicEdgeLazyTestEntity(from, entity); - template.insert(edge1); - template.insert(edge2); - final ConstructorWithToParamsTestEntity document = template - .find(entity.id, ConstructorWithToParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.id).collect(Collectors.toList()), hasItems(edge1.id, edge2.id)); - } - - public static class ConstructorWithToLazyParamsTestEntity extends BasicTestEntity { - @To(lazy = true) - private final Collection value; - - public ConstructorWithToLazyParamsTestEntity(final Collection value) { - super(); - this.value = value; - } - } - - @Test - public void constructorWithToLazyParams() { - final ConstructorWithToLazyParamsTestEntity entity = new ConstructorWithToLazyParamsTestEntity(null); - template.insert(entity); - final BasicTestEntity from = new BasicTestEntity(); - template.insert(from); - final BasicEdgeTestEntity edge1 = new BasicEdgeTestEntity(from, entity); - final BasicEdgeTestEntity edge2 = new BasicEdgeTestEntity(from, entity); - template.insert(edge1); - template.insert(edge2); - final ConstructorWithToLazyParamsTestEntity document = template - .find(entity.id, ConstructorWithToLazyParamsTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.value.stream().map((e) -> e.getId()).collect(Collectors.toList()), - hasItems(edge1.id, edge2.id)); - } - - public static class SingleDocumentToTestEntity extends BasicTestEntity { - @To - private BasicEdgeLazyTestEntity entity; - } - - @Test - public void singleDocumentTo() { - final SingleDocumentToTestEntity e0 = new SingleDocumentToTestEntity(); - template.insert(e0); - final SingleDocumentToTestEntity e1 = new SingleDocumentToTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge0); - final SingleDocumentToTestEntity document = template.find(e0.id, SingleDocumentToTestEntity.class).get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity.getId(), is(notNullValue())); - assertThat(document.entity.getId(), is(edge0.getId())); - assertThat(document.entity.getTo(), is(notNullValue())); - assertThat(document.entity.getTo().getId(), is(notNullValue())); - assertThat(document.entity.getTo().getId(), is(e0.getId())); - } - - public static class SingleDocumentToLazyTestEntity extends BasicTestEntity { - @To(lazy = true) - private BasicEdgeLazyTestEntity entity; - } - - @Test - public void singleDocumentToLazy() { - final SingleDocumentToLazyTestEntity e0 = new SingleDocumentToLazyTestEntity(); - template.insert(e0); - final SingleDocumentToLazyTestEntity e1 = new SingleDocumentToLazyTestEntity(); - template.insert(e1); - final BasicEdgeLazyTestEntity edge0 = new BasicEdgeLazyTestEntity(e1, e0); - template.insert(edge0); - final SingleDocumentToLazyTestEntity document = template.find(e0.id, SingleDocumentToLazyTestEntity.class) - .get(); - assertThat(document, is(notNullValue())); - assertThat(document.entity, is(notNullValue())); - assertThat(document.entity.getId(), is(notNullValue())); - assertThat(document.entity.getId(), is(edge0.getId())); - assertThat(document.entity.getTo(), is(notNullValue())); - assertThat(document.entity.getTo().getId(), is(notNullValue())); - assertThat(document.entity.getTo().getId(), is(e0.getId())); - } +public class ToMappingTest extends ToMapping { + ToMappingTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTxTest.java b/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTxTest.java new file mode 100644 index 00000000..1b525159 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/ToMappingTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.core.mapping; + +public class ToMappingTxTest extends ToMapping { + ToMappingTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java b/src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java index 54d82683..3f6f6995 100644 --- a/src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java +++ b/src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java @@ -31,7 +31,6 @@ import com.arangodb.model.DocumentReplaceOptions; import com.arangodb.model.DocumentUpdateOptions; import com.arangodb.springframework.AbstractArangoTest; -import com.arangodb.springframework.ArangoTestConfiguration; import com.arangodb.springframework.example.polymorphic.entity.Animal; import com.arangodb.springframework.example.polymorphic.entity.Dog; import com.arangodb.springframework.example.polymorphic.entity.Eagle; diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/AbstractRepositoryTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/AbstractRepositoryTest.java index d82ce7ae..960445ee 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/AbstractRepositoryTest.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/AbstractRepositoryTest.java @@ -1,6 +1,6 @@ package com.arangodb.springframework.testdata.chess; -import com.arangodb.springframework.AbstractArangoTest; +import com.arangodb.springframework.AbstractTxTest; import com.arangodb.springframework.core.ArangoOperations; import com.arangodb.springframework.testdata.chess.entity.Player; import com.arangodb.springframework.testdata.chess.entity.Score; @@ -12,7 +12,7 @@ import java.time.LocalDate; import java.util.List; -abstract class AbstractRepositoryTest extends AbstractArangoTest { +abstract class AbstractRepositoryTest extends AbstractTxTest { @Autowired private ArangoOperations ops; @@ -67,15 +67,15 @@ abstract class AbstractRepositoryTest extends AbstractArangoTest { new Score(players.get(0), tournaments.get(2), 6) ); - protected AbstractRepositoryTest() { - super(Player.class, Score.class, Tournament.class); + protected AbstractRepositoryTest(boolean withinTx) { + super(withinTx, Player.class, Score.class, Tournament.class); } @BeforeEach void importData() { - ops.insertAll(players, Player.class); - ops.insertAll(tournaments, Tournament.class); - ops.insertAll(scores, Score.class); + ops.insertAll(players, insertOpts, Player.class); + ops.insertAll(tournaments, insertOpts, Tournament.class); + ops.insertAll(scores, insertOpts, Score.class); } } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryAbstract.java b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryAbstract.java index 190a03c0..8772ee79 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryAbstract.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryAbstract.java @@ -18,12 +18,16 @@ abstract class PlayerRepositoryAbstract extends AbstractRepositoryTest { @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") PlayerRepository repo; + PlayerRepositoryAbstract(boolean withinTx) { + super(withinTx); + } + @Test void findAllByCountry() { List expected = players.stream() .filter(it -> "US".equals(it.getCountry())) .toList(); - Iterable found = repo.findAllByCountry("US"); + Iterable found = repo.findAllByCountry("US", queryOpts); assertThat(found).containsExactlyInAnyOrderElementsOf(expected); found.forEach(this::checkRefs); } @@ -37,7 +41,7 @@ void findAllByRatingGreaterThan() { .toList(); for (int i = 0; i < expected.size(); i++) { - Page page = repo.findAllByRatingGreaterThanOrderByRatingDesc(PageRequest.of(i, 1), rating); + Page page = repo.findAllByRatingGreaterThanOrderByRatingDesc(PageRequest.of(i, 1), rating, queryOpts); assertThat(page.getTotalElements()).isEqualTo(expected.size()); assertThat(page.getTotalPages()).isEqualTo(expected.size()); Player current = page.iterator().next(); diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTest.java index f3a48fd1..41cceff8 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTest.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTest.java @@ -1,4 +1,7 @@ package com.arangodb.springframework.testdata.chess; public class PlayerRepositoryTest extends PlayerRepositoryAbstract { + PlayerRepositoryTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTxTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTxTest.java new file mode 100644 index 00000000..0ba82fdd --- /dev/null +++ b/src/test/java/com/arangodb/springframework/testdata/chess/PlayerRepositoryTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.testdata.chess; + +public class PlayerRepositoryTxTest extends PlayerRepositoryAbstract { + PlayerRepositoryTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryAbstract.java b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryAbstract.java index 32387c45..0e649b62 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryAbstract.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryAbstract.java @@ -13,10 +13,14 @@ abstract class ScoreRepositoryAbstract extends AbstractRepositoryTest { @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") ScoreRepository repo; + ScoreRepositoryAbstract(boolean withinTx) { + super(withinTx); + } + @Test @Disabled("BTS-1859") void findAll() { - assertThat(repo.findAll()) + assertThat(repo.findAll(queryOpts)) .hasSize(scores.size()) .containsExactlyInAnyOrderElementsOf(scores); } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTest.java index 0bc653f9..ff3cd19c 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTest.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTest.java @@ -1,4 +1,7 @@ package com.arangodb.springframework.testdata.chess; public class ScoreRepositoryTest extends ScoreRepositoryAbstract { + ScoreRepositoryTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTxTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTxTest.java new file mode 100644 index 00000000..9fad9b21 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/testdata/chess/ScoreRepositoryTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.testdata.chess; + +public class ScoreRepositoryTxTest extends ScoreRepositoryAbstract { + ScoreRepositoryTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryAbstract.java b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryAbstract.java index d7e41cc9..a76bef00 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryAbstract.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryAbstract.java @@ -24,6 +24,10 @@ abstract class TournamentRepositoryAbstract extends AbstractRepositoryTest { @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") TournamentRepository repo; + TournamentRepositoryAbstract(boolean withinTx) { + super(withinTx); + } + // equirectangular approximation static Distance calculateDistance(Point p1, Point p2) { double lat1Rad = Math.toRadians(p1.getY()); @@ -46,7 +50,7 @@ void findAllByDateBetween() { .filter(it -> it.getDate().isAfter(start)) .filter(it -> it.getDate().isBefore(end)) .toList(); - List found = repo.findAllByDateBetween(start, end); + List found = repo.findAllByDateBetween(start, end, queryOpts); assertThat(found) .hasSize(expected.size()) .containsExactlyInAnyOrderElementsOf(expected); @@ -59,7 +63,7 @@ void findByNameContainingIgnoreCase() { List expected = tournaments.stream() .filter(it -> it.getName().contains(match)) .toList(); - Iterable found = repo.findByNameContainingIgnoreCase(match); + Iterable found = repo.findByNameContainingIgnoreCase(match, queryOpts); assertThat(found) .hasSize(expected.size()) .containsExactlyInAnyOrderElementsOf(expected); @@ -75,7 +79,7 @@ public void findAllByLocationWithin() { .filter(it -> calculateDistance(p, it.getLocation()).compareTo(d) < 0) .toList(); - List found = repo.findAllByLocationWithin(p, d); + List found = repo.findAllByLocationWithin(p, d, queryOpts); assertThat(found) .hasSize(expected.size()) .containsExactlyInAnyOrderElementsOf(expected); @@ -94,7 +98,7 @@ public void findAllByLocationWithinPageable() { .toList(); for (int i = 0; i < expected.size(); i++) { - GeoPage page = repo.findAllByLocationWithin(PageRequest.of(i, 1), p, d); + GeoPage page = repo.findAllByLocationWithin(PageRequest.of(i, 1), p, d, queryOpts); assertThat(page.getTotalElements()).isEqualTo(expected.size()); assertThat(page.getTotalPages()).isEqualTo(expected.size()); GeoResult current = page.iterator().next(); diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTest.java index 6842182c..a4cd012a 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTest.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTest.java @@ -1,4 +1,7 @@ package com.arangodb.springframework.testdata.chess; public class TournamentRepositoryTest extends TournamentRepositoryAbstract { + TournamentRepositoryTest() { + super(false); + } } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTxTest.java b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTxTest.java new file mode 100644 index 00000000..23713887 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/testdata/chess/TournamentRepositoryTxTest.java @@ -0,0 +1,7 @@ +package com.arangodb.springframework.testdata.chess; + +public class TournamentRepositoryTxTest extends TournamentRepositoryAbstract { + TournamentRepositoryTxTest() { + super(true); + } +} diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/repo/PlayerRepository.java b/src/test/java/com/arangodb/springframework/testdata/chess/repo/PlayerRepository.java index 3efec4e6..233fe06b 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/repo/PlayerRepository.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/repo/PlayerRepository.java @@ -1,14 +1,15 @@ package com.arangodb.springframework.testdata.chess.repo; +import com.arangodb.model.AqlQueryOptions; import com.arangodb.springframework.repository.ArangoRepository; import com.arangodb.springframework.testdata.chess.entity.Player; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface PlayerRepository extends ArangoRepository { - Iterable findAllByCountry(String country); + Iterable findAllByCountry(String country, AqlQueryOptions opts); - Page findAllByRatingGreaterThanOrderByRatingDesc(Pageable pageable, int rating); + Page findAllByRatingGreaterThanOrderByRatingDesc(Pageable pageable, int rating, AqlQueryOptions opts); } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/repo/ScoreRepository.java b/src/test/java/com/arangodb/springframework/testdata/chess/repo/ScoreRepository.java index 2f0c8ffc..578489aa 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/repo/ScoreRepository.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/repo/ScoreRepository.java @@ -1,7 +1,11 @@ package com.arangodb.springframework.testdata.chess.repo; +import com.arangodb.model.AqlQueryOptions; +import com.arangodb.springframework.annotation.Query; import com.arangodb.springframework.repository.ArangoRepository; import com.arangodb.springframework.testdata.chess.entity.Score; public interface ScoreRepository extends ArangoRepository { + @Query("FOR d IN #collection RETURN d") + Iterable findAll(AqlQueryOptions opts); } diff --git a/src/test/java/com/arangodb/springframework/testdata/chess/repo/TournamentRepository.java b/src/test/java/com/arangodb/springframework/testdata/chess/repo/TournamentRepository.java index 125c1187..c4603205 100644 --- a/src/test/java/com/arangodb/springframework/testdata/chess/repo/TournamentRepository.java +++ b/src/test/java/com/arangodb/springframework/testdata/chess/repo/TournamentRepository.java @@ -1,5 +1,6 @@ package com.arangodb.springframework.testdata.chess.repo; +import com.arangodb.model.AqlQueryOptions; import com.arangodb.springframework.repository.ArangoRepository; import com.arangodb.springframework.testdata.chess.entity.Tournament; import org.springframework.data.domain.Pageable; @@ -11,13 +12,13 @@ import java.util.List; public interface TournamentRepository extends ArangoRepository { - List findAllByDateBetween(LocalDate start, LocalDate end); + List findAllByDateBetween(LocalDate start, LocalDate end, AqlQueryOptions opts); - Iterable findByNameContainingIgnoreCase(String match); + Iterable findByNameContainingIgnoreCase(String match, AqlQueryOptions opts); - List findAllByLocationWithin(Point location, Distance distance); + List findAllByLocationWithin(Point location, Distance distance, AqlQueryOptions opts); - GeoPage findAllByLocationWithin(Pageable pageable, Point location, Distance distance); + GeoPage findAllByLocationWithin(Pageable pageable, Point location, Distance distance, AqlQueryOptions opts); } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index c4237c3f..4bdb7579 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -12,6 +12,6 @@ - - + +