DATAREST-1008 - Adapt to API changes in Spring Data Commons, Java 8 upgrades and Mockito 2.7.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.rest.core;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
@@ -25,6 +26,7 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.GenericConverter;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.repository.support.Repositories;
|
||||
import org.springframework.data.repository.support.RepositoryInvokerFactory;
|
||||
@@ -66,9 +68,9 @@ public class UriToEntityConverter implements ConditionalGenericConverter {
|
||||
for (TypeInformation<?> domainType : entities.getManagedTypes()) {
|
||||
|
||||
Class<?> rawType = domainType.getType();
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(rawType);
|
||||
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(rawType);
|
||||
|
||||
if (entity != null && entity.hasIdProperty()) {
|
||||
if (entity.map(it -> it.hasIdProperty()).orElse(false)) {
|
||||
convertiblePairs.add(new ConvertiblePair(URI.class, domainType.getType()));
|
||||
}
|
||||
}
|
||||
@@ -86,7 +88,7 @@ public class UriToEntityConverter implements ConditionalGenericConverter {
|
||||
@Override
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return !sourceType.equals(URI_TYPE) ? false
|
||||
: repositories.getRepositoryInformationFor(targetType.getType()) != null;
|
||||
: repositories.getRepositoryInformationFor(targetType.getType()).isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -105,9 +107,10 @@ public class UriToEntityConverter implements ConditionalGenericConverter {
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(targetType.getType());
|
||||
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities
|
||||
.getPersistentEntity(targetType.getType());
|
||||
|
||||
if (entity == null) {
|
||||
if (!entity.isPresent()) {
|
||||
throw new ConversionFailedException(sourceType, targetType, source,
|
||||
new IllegalArgumentException("No PersistentEntity information available for " + targetType.getType()));
|
||||
}
|
||||
@@ -120,6 +123,6 @@ public class UriToEntityConverter implements ConditionalGenericConverter {
|
||||
"Cannot resolve URI " + uri + ". Is it local or remote? Only local URIs are resolvable."));
|
||||
}
|
||||
|
||||
return invokerFactory.getInvokerFor(targetType.getType()).invokeFindOne(parts[parts.length - 1]);
|
||||
return invokerFactory.getInvokerFor(targetType.getType()).invokeFindOne(parts[parts.length - 1]).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.rest.core;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.ConfigurablePropertyAccessor;
|
||||
@@ -83,17 +84,11 @@ public class ValidationErrors extends AbstractPropertyBindingResult {
|
||||
do {
|
||||
|
||||
String segment = iterator.next();
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(value.getClass());
|
||||
PersistentProperty<?> property = entity.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment));
|
||||
|
||||
if (property == null) {
|
||||
throw new NotReadablePropertyException(source.getClass(), propertyName);
|
||||
}
|
||||
Optional<? extends PersistentProperty<?>> property = entities.getPersistentEntity(value.getClass())//
|
||||
.flatMap(it -> it.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment)));
|
||||
|
||||
ConfigurablePropertyAccessor accessor = property.usePropertyAccess()
|
||||
? PropertyAccessorFactory.forBeanPropertyAccess(value)
|
||||
: PropertyAccessorFactory.forDirectFieldAccess(value);
|
||||
value = accessor.getPropertyValue(segment);
|
||||
value = getValue(value, property, segment, propertyName);
|
||||
|
||||
} while (iterator.hasNext());
|
||||
|
||||
@@ -110,4 +105,18 @@ public class ValidationErrors extends AbstractPropertyBindingResult {
|
||||
public Object getTarget() {
|
||||
return source;
|
||||
}
|
||||
|
||||
private static Object getValue(Object source, Optional<? extends PersistentProperty<?>> property, String segment,
|
||||
String name) {
|
||||
|
||||
return property.map(it -> {
|
||||
|
||||
ConfigurablePropertyAccessor accessor = it.usePropertyAccess()
|
||||
? PropertyAccessorFactory.forBeanPropertyAccess(source)
|
||||
: PropertyAccessorFactory.forDirectFieldAccess(source);
|
||||
|
||||
return accessor.getPropertyValue(segment);
|
||||
|
||||
}).orElseThrow(() -> new NotReadablePropertyException(source.getClass(), name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import lombok.Value;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.repository.Repository;
|
||||
@@ -188,11 +189,15 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
|
||||
Assert.notNull(repositories, "Repositories must not be null!");
|
||||
Assert.notNull(lookupInformation, "LookupInformation must not be null!");
|
||||
|
||||
RepositoryInformation information = repositories.getRepositoryInformation(lookupInformation.repositoryType);
|
||||
RepositoryInformation information = repositories.getRepositoryInformation(lookupInformation.repositoryType)//
|
||||
.orElseThrow(() -> new IllegalStateException(
|
||||
"No repository found for type " + lookupInformation.repositoryType.getName() + "!"));
|
||||
|
||||
this.repository = (Repository<? extends T, ?>) repositories.getRepositoryFor(information.getDomainType());
|
||||
this.domainType = information.getDomainType();
|
||||
this.lookupInfo = lookupInformation;
|
||||
this.repository = (Repository<? extends T, ?>) repositories.getRepositoryFor(information.getDomainType())//
|
||||
.orElseThrow(() -> new IllegalStateException(
|
||||
"No repository found for type " + information.getDomainType().getName() + "!"));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -209,8 +214,8 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
|
||||
* @see org.springframework.data.rest.core.support.EntityLookup#lookupEntity(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public Object lookupEntity(Serializable id) {
|
||||
return lookupInfo.getLookup().lookup(repository, id);
|
||||
public Optional<Object> lookupEntity(Serializable id) {
|
||||
return Optional.ofNullable(lookupInfo.getLookup().lookup(repository, id));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -64,7 +64,6 @@ public class RepositoryRestConfiguration {
|
||||
private final ProjectionDefinitionConfiguration projectionConfiguration;
|
||||
private final MetadataConfiguration metadataConfiguration;
|
||||
private final EntityLookupConfiguration entityLookupConfiguration;
|
||||
private final List<Class<?>> valueTypes = new ArrayList<Class<?>>();
|
||||
|
||||
private final EnumTranslationConfiguration enumTranslationConfiguration;
|
||||
private boolean enableEnumTranslation = false;
|
||||
|
||||
@@ -24,6 +24,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
@@ -179,14 +180,14 @@ public class CrudMethodsSupportedHttpMethods implements SupportedHttpMethods {
|
||||
return exposes(crudMethods.getFindAllMethod());
|
||||
}
|
||||
|
||||
private static boolean exposes(Method method) {
|
||||
private static boolean exposes(Optional<Method> method) {
|
||||
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
return method.map(it -> {
|
||||
|
||||
RestResource annotation = AnnotationUtils.findAnnotation(method, RestResource.class);
|
||||
return annotation == null ? true : annotation.exported();
|
||||
RestResource annotation = AnnotationUtils.findAnnotation(it, RestResource.class);
|
||||
return annotation == null ? true : annotation.exported();
|
||||
|
||||
}).orElse(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.rest.core.mapping;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
@@ -55,8 +56,8 @@ class MappingResourceMetadata extends TypeBasedCollectionResourceMapping impleme
|
||||
this.entity.doWithAssociations(propertyMappings);
|
||||
this.entity.doWithProperties(propertyMappings);
|
||||
|
||||
RestResource annotation = entity.findAnnotation(RestResource.class);
|
||||
this.explicitlyExported = annotation != null && annotation.exported();
|
||||
Optional<RestResource> annotation = entity.findAnnotation(RestResource.class);
|
||||
this.explicitlyExported = annotation.map(it -> it.exported()).orElse(false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
@@ -84,23 +85,23 @@ public class PersistentEntitiesResourceMappings implements ResourceMappings {
|
||||
MappingResourceMetadata getMappingMetadataFor(Class<?> type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
type = ClassUtils.getUserClass(type);
|
||||
Class<?> userType = ClassUtils.getUserClass(type);
|
||||
|
||||
MappingResourceMetadata mappingMetadata = mappingCache.get(type);
|
||||
MappingResourceMetadata mappingMetadata = mappingCache.get(userType);
|
||||
|
||||
if (mappingMetadata != null) {
|
||||
return mappingMetadata;
|
||||
}
|
||||
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
|
||||
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(userType);
|
||||
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
return entity.map(it -> {
|
||||
|
||||
mappingMetadata = new MappingResourceMetadata(entity, this);
|
||||
mappingCache.put(type, mappingMetadata);
|
||||
return mappingMetadata;
|
||||
MappingResourceMetadata metadata = new MappingResourceMetadata(it, this);
|
||||
mappingCache.put(userType, metadata);
|
||||
return metadata;
|
||||
|
||||
}).orElse(null);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,10 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.rest.core.Path;
|
||||
import org.springframework.data.rest.core.annotation.Description;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
import org.springframework.data.util.Optionals;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -31,8 +34,8 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
|
||||
private final PersistentProperty<?> property;
|
||||
private final ResourceMappings mappings;
|
||||
private final RestResource annotation;
|
||||
private final Description description;
|
||||
private final Optional<RestResource> annotation;
|
||||
private final Optional<Description> description;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RootPropertyResourceMapping}.
|
||||
@@ -46,7 +49,7 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
|
||||
this.property = property;
|
||||
this.mappings = mappings;
|
||||
this.annotation = property.isAssociation() ? property.findAnnotation(RestResource.class) : null;
|
||||
this.annotation = property.isAssociation() ? property.findAnnotation(RestResource.class) : Optional.empty();
|
||||
this.description = property.findAnnotation(Description.class);
|
||||
}
|
||||
|
||||
@@ -56,8 +59,10 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
*/
|
||||
@Override
|
||||
public Path getPath() {
|
||||
return annotation != null && StringUtils.hasText(annotation.path()) ? new Path(annotation.path()) : new Path(
|
||||
property.getName());
|
||||
|
||||
return annotation.filter(it -> StringUtils.hasText(it.path()))//
|
||||
.map(it -> new Path(it.path()))//
|
||||
.orElseGet(() -> new Path(property.getName()));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,7 +71,10 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
*/
|
||||
@Override
|
||||
public String getRel() {
|
||||
return annotation != null && StringUtils.hasText(annotation.rel()) ? annotation.rel() : property.getName();
|
||||
|
||||
return annotation.filter(it -> StringUtils.hasText(it.rel()))//
|
||||
.map(it -> it.rel())//
|
||||
.orElseGet(() -> property.getName());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -81,7 +89,7 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
}
|
||||
|
||||
ResourceMapping typeMapping = mappings.getMetadataFor(property.getActualType());
|
||||
return !typeMapping.isExported() ? false : annotation == null ? true : annotation.exported();
|
||||
return !typeMapping.isExported() ? false : annotation.map(it -> it.exported()).orElse(true);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -103,15 +111,11 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
|
||||
CollectionResourceMapping ownerTypeMapping = mappings.getMetadataFor(property.getOwner().getType());
|
||||
ResourceDescription fallback = TypedResourceDescription.defaultFor(ownerTypeMapping.getItemResourceRel(), property);
|
||||
|
||||
if (description != null) {
|
||||
return new AnnotationBasedResourceDescription(description, fallback);
|
||||
}
|
||||
|
||||
if (annotation != null) {
|
||||
return new AnnotationBasedResourceDescription(annotation.description(), fallback);
|
||||
}
|
||||
|
||||
return fallback;
|
||||
return Optionals
|
||||
.<ResourceDescription> firstNonEmpty(//
|
||||
() -> description.map(it -> new AnnotationBasedResourceDescription(it, fallback)), //
|
||||
() -> annotation.map(it -> new AnnotationBasedResourceDescription(it.description(), fallback)))
|
||||
.orElse(fallback);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -81,7 +81,7 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin
|
||||
|
||||
for (Class<?> type : repositories) {
|
||||
|
||||
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(type);
|
||||
RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(type);
|
||||
Class<?> repositoryInterface = repositoryInformation.getRepositoryInterface();
|
||||
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(type);
|
||||
|
||||
@@ -111,7 +111,7 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin
|
||||
return searchCache.get(domainType);
|
||||
}
|
||||
|
||||
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainType);
|
||||
RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(domainType);
|
||||
List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>();
|
||||
ResourceMetadata resourceMapping = getMetadataFor(domainType);
|
||||
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import org.springframework.data.util.Streamable;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface ResourceMappings extends Iterable<ResourceMetadata> {
|
||||
public interface ResourceMappings extends Streamable<ResourceMetadata> {
|
||||
|
||||
/**
|
||||
* Returns a {@link ResourceMetadata} for the given type if available.
|
||||
|
||||
@@ -17,12 +17,10 @@ package org.springframework.data.rest.core.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.rest.core.util.Java8PluginRegistry;
|
||||
import org.springframework.hateoas.EntityLinks;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.plugin.core.OrderAwarePluginRegistry;
|
||||
import org.springframework.plugin.core.PluginRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -37,7 +35,7 @@ public class DefaultSelfLinkProvider implements SelfLinkProvider {
|
||||
|
||||
private final PersistentEntities entities;
|
||||
private final EntityLinks entityLinks;
|
||||
private final PluginRegistry<EntityLookup<?>, Class<?>> lookups;
|
||||
private final Java8PluginRegistry<EntityLookup<?>, Class<?>> lookups;
|
||||
|
||||
/**
|
||||
* Creates a new {@link DefaultSelfLinkProvider} from the {@link PersistentEntities}, {@link EntityLinks} and
|
||||
@@ -56,7 +54,7 @@ public class DefaultSelfLinkProvider implements SelfLinkProvider {
|
||||
|
||||
this.entities = entities;
|
||||
this.entityLinks = entityLinks;
|
||||
this.lookups = OrderAwarePluginRegistry.create(lookups);
|
||||
this.lookups = Java8PluginRegistry.of(lookups);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -81,19 +79,16 @@ public class DefaultSelfLinkProvider implements SelfLinkProvider {
|
||||
|
||||
Class<? extends Object> instanceType = instance.getClass();
|
||||
|
||||
EntityLookup<Object> lookup = (EntityLookup<Object>) lookups.getPluginFor(instanceType);
|
||||
return lookups.getPluginFor(instanceType)//
|
||||
.map(it -> it.getClass().cast(it))//
|
||||
.map(it -> (Object) it.getResourceIdentifier(instance))//
|
||||
.orElseGet(() -> identifierOrNull(instance));
|
||||
}
|
||||
|
||||
if (lookup != null) {
|
||||
return lookup.getResourceIdentifier(instance);
|
||||
}
|
||||
private Object identifierOrNull(Object instance) {
|
||||
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(instanceType);
|
||||
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Cannot create self link for %s! No persistent entity found!", instanceType));
|
||||
}
|
||||
|
||||
return entity.getIdentifierAccessor(instance).getIdentifier();
|
||||
return entities.getRequiredPersistentEntity(instance.getClass())//
|
||||
.getIdentifierAccessor(instance).getIdentifier()//
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.springframework.data.rest.core.support.DomainObjectMerger.NullHandlingPolicy.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.mapping.Association;
|
||||
@@ -87,18 +89,18 @@ public class DomainObjectMerger {
|
||||
@Override
|
||||
public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
|
||||
|
||||
Object sourceValue = sourceWrapper.getProperty(persistentProperty);
|
||||
Object targetValue = targetWrapper.getProperty(persistentProperty);
|
||||
Optional<Object> sourceValue = sourceWrapper.getProperty(persistentProperty);
|
||||
Optional<Object> targetValue = targetWrapper.getProperty(persistentProperty);
|
||||
|
||||
if (targetEntity.isIdProperty(persistentProperty)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(sourceValue, targetValue)) {
|
||||
if (sourceValue.equals(targetValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nullPolicy == APPLY_NULLS || sourceValue != null) {
|
||||
if (nullPolicy == APPLY_NULLS || sourceValue.isPresent()) {
|
||||
targetWrapper.setProperty(persistentProperty, sourceValue);
|
||||
}
|
||||
}
|
||||
@@ -114,7 +116,7 @@ public class DomainObjectMerger {
|
||||
public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
|
||||
|
||||
PersistentProperty<?> persistentProperty = association.getInverse();
|
||||
Object fromVal = sourceWrapper.getProperty(persistentProperty);
|
||||
Optional<Object> fromVal = sourceWrapper.getProperty(persistentProperty);
|
||||
|
||||
if (!isNullOrEmpty(fromVal) && !fromVal.equals(targetWrapper.getProperty(persistentProperty))) {
|
||||
targetWrapper.setProperty(persistentProperty, fromVal);
|
||||
@@ -130,21 +132,21 @@ public class DomainObjectMerger {
|
||||
* @param source can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
static boolean isNullOrEmpty(Object source) {
|
||||
static boolean isNullOrEmpty(Optional<Object> source) {
|
||||
|
||||
if (source == null) {
|
||||
return true;
|
||||
}
|
||||
return source.map(it -> {
|
||||
|
||||
if (source instanceof Iterable) {
|
||||
return !((Iterable<?>) source).iterator().hasNext();
|
||||
}
|
||||
if (it instanceof Iterable) {
|
||||
return !((Iterable<?>) it).iterator().hasNext();
|
||||
}
|
||||
|
||||
if (ObjectUtils.isArray(source)) {
|
||||
return ObjectUtils.isEmpty((Object[]) source);
|
||||
}
|
||||
if (ObjectUtils.isArray(it)) {
|
||||
return ObjectUtils.isEmpty((Object[]) it);
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
|
||||
}).orElse(true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.plugin.core.Plugin;
|
||||
|
||||
@@ -54,5 +55,5 @@ public interface EntityLookup<T> extends Plugin<Class<?>> {
|
||||
* @param id will never be {@literal null}.
|
||||
* @return can be {@literal null}.
|
||||
*/
|
||||
Object lookupEntity(Serializable id);
|
||||
Optional<Object> lookupEntity(Serializable id);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,7 @@ import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
@@ -32,10 +28,8 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.support.RepositoryInvoker;
|
||||
import org.springframework.data.repository.support.RepositoryInvokerFactory;
|
||||
import org.springframework.plugin.core.OrderAwarePluginRegistry;
|
||||
import org.springframework.plugin.core.PluginRegistry;
|
||||
import org.springframework.data.rest.core.util.Java8PluginRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
@@ -46,42 +40,8 @@ import org.springframework.util.MultiValueMap;
|
||||
*/
|
||||
public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFactory {
|
||||
|
||||
private static final List<Converter<Object, Object>> CONVERTERS;
|
||||
|
||||
static {
|
||||
|
||||
List<Converter<Object, Object>> converters = new ArrayList<Converter<Object, Object>>();
|
||||
ClassLoader classLoader = UnwrappingRepositoryInvokerFactory.class.getClassLoader();
|
||||
|
||||
// Add unwrapper for Java 8 Optional
|
||||
|
||||
if (ClassUtils.isPresent("java.util.Optional", classLoader)) {
|
||||
converters.add(new Converter<Object, Object>() {
|
||||
@Override
|
||||
public Object convert(Object source) {
|
||||
return source instanceof Optional ? ((Optional<?>) source).orElse(null) : source;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add unwrapper for Guava Optional
|
||||
|
||||
if (ClassUtils.isPresent("com.google.common.base.Optional", classLoader)) {
|
||||
|
||||
converters.add(new Converter<Object, Object>() {
|
||||
@Override
|
||||
public Object convert(Object source) {
|
||||
return source instanceof com.google.common.base.Optional
|
||||
? ((com.google.common.base.Optional<?>) source).orNull() : source;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
CONVERTERS = Collections.unmodifiableList(converters);
|
||||
}
|
||||
|
||||
private final RepositoryInvokerFactory delegate;
|
||||
private final PluginRegistry<EntityLookup<?>, Class<?>> lookups;
|
||||
private final Java8PluginRegistry<EntityLookup<?>, Class<?>> lookups;
|
||||
|
||||
/**
|
||||
* @param delegate must not be {@literal null}.
|
||||
@@ -94,7 +54,7 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
Assert.notNull(lookups, "EntityLookups must not be null!");
|
||||
|
||||
this.delegate = delegate;
|
||||
this.lookups = OrderAwarePluginRegistry.create(lookups);
|
||||
this.lookups = Java8PluginRegistry.of(lookups);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -104,9 +64,9 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
@Override
|
||||
public RepositoryInvoker getInvokerFor(Class<?> domainType) {
|
||||
|
||||
EntityLookup<?> lookup = lookups.getPluginFor(domainType);
|
||||
Optional<EntityLookup<?>> lookup = lookups.getPluginFor(domainType);
|
||||
|
||||
return new UnwrappingRepositoryInvoker(delegate.getInvokerFor(domainType), CONVERTERS, lookup);
|
||||
return new UnwrappingRepositoryInvoker(delegate.getInvokerFor(domainType), lookup);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,25 +79,18 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
private static class UnwrappingRepositoryInvoker implements RepositoryInvoker {
|
||||
|
||||
private final @NonNull RepositoryInvoker delegate;
|
||||
private final @NonNull Collection<Converter<Object, Object>> converters;
|
||||
private final EntityLookup<?> lookup;
|
||||
private final @NonNull Optional<EntityLookup<?>> lookup;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeFindOne(java.io.Serializable)
|
||||
*/
|
||||
public <T> T invokeFindOne(Serializable id) {
|
||||
return postProcess(lookup != null ? lookup.lookupEntity(id) : delegate.invokeFindOne(id));
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> invokeFindOne(Serializable id) {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public Object invokeQueryMethod(Method method, Map<String, String[]> parameters, Pageable pageable, Sort sort) {
|
||||
return postProcess(delegate.invokeQueryMethod(method, parameters, pageable, sort));
|
||||
return (Optional<T>) lookup//
|
||||
.map(it -> it.lookupEntity(id).orElse(Optional.empty()))//
|
||||
.orElseGet(() -> delegate.invokeFindOne(id));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -145,9 +98,9 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, org.springframework.util.MultiValueMap, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
|
||||
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
|
||||
Pageable pageable, Sort sort) {
|
||||
return postProcess(delegate.invokeQueryMethod(method, parameters, pageable, sort));
|
||||
return delegate.invokeQueryMethod(method, parameters, pageable, sort);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -221,21 +174,5 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
public <T> T invokeSave(T object) {
|
||||
return delegate.invokeSave(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the configured converters for the given result.
|
||||
*
|
||||
* @param result can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T postProcess(Object result) {
|
||||
|
||||
for (Converter<Object, Object> converter : converters) {
|
||||
result = converter.convert(result);
|
||||
}
|
||||
|
||||
return (T) result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.util;
|
||||
|
||||
/**
|
||||
* Simple function interface.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface Function<S, T> {
|
||||
|
||||
T apply(S input) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.util;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.plugin.core.OrderAwarePluginRegistry;
|
||||
import org.springframework.plugin.core.Plugin;
|
||||
import org.springframework.plugin.core.PluginRegistry;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class Java8PluginRegistry<T extends Plugin<S>, S> {
|
||||
|
||||
private final PluginRegistry<T, S> registry;
|
||||
|
||||
public static <T extends Plugin<S>, S> Java8PluginRegistry<T, S> of(List<? extends T> plugins) {
|
||||
return Java8PluginRegistry.of(OrderAwarePluginRegistry.create(plugins));
|
||||
}
|
||||
|
||||
public static <T extends Plugin<S>, S> Java8PluginRegistry<T, S> of(PluginRegistry<T, S> plugins) {
|
||||
return new Java8PluginRegistry<>(plugins);
|
||||
}
|
||||
|
||||
public static <T extends Plugin<S>, S> Java8PluginRegistry<T, S> empty() {
|
||||
return Java8PluginRegistry.of(Collections.emptyList());
|
||||
}
|
||||
|
||||
public Optional<T> getPluginFor(S delimiter) {
|
||||
return Optional.ofNullable(registry.getPluginFor(delimiter));
|
||||
}
|
||||
|
||||
public T getPluginOrDefaultFor(S delimiter, T fallback) {
|
||||
return getPluginFor(delimiter).orElse(fallback);
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ import org.springframework.util.MultiValueMap;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class MapUtils {
|
||||
public interface MapUtils {
|
||||
|
||||
/**
|
||||
* Turns a {@link MultiValueMap} into its {@link Map} equivalent.
|
||||
|
||||
@@ -26,11 +26,9 @@ import org.springframework.util.ReflectionUtils;
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class Methods {
|
||||
public interface Methods {
|
||||
|
||||
private Methods() {}
|
||||
|
||||
public static final ReflectionUtils.MethodFilter USER_METHODS = new ReflectionUtils.MethodFilter() {
|
||||
static final ReflectionUtils.MethodFilter USER_METHODS = new ReflectionUtils.MethodFilter() {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -38,12 +36,12 @@ public abstract class Methods {
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(Method method) {
|
||||
|
||||
|
||||
return !method.isSynthetic() && //
|
||||
!method.isBridge() && //
|
||||
!ReflectionUtils.isObjectMethod(method) && //
|
||||
!ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && //
|
||||
!ReflectionUtils.isCglibRenamedMethod(method);
|
||||
!method.isBridge() && //
|
||||
!ReflectionUtils.isObjectMethod(method) && //
|
||||
!ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && //
|
||||
!ReflectionUtils.isCglibRenamedMethod(method);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.util;
|
||||
|
||||
/**
|
||||
* Mimics Java 8's Supplier interface to allow deferring a computation.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 2.6
|
||||
* @soundtrack KRS-One - Sound Of Da Police (Return Of The Boom Bap)
|
||||
*/
|
||||
public interface Supplier<T> {
|
||||
|
||||
T get();
|
||||
}
|
||||
0
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/AbstractIntegrationTests.java
Normal file → Executable file
0
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/AbstractIntegrationTests.java
Normal file → Executable file
21
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java
Normal file → Executable file
21
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/PathUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -31,50 +30,50 @@ public class PathUnitTests {
|
||||
public void combinesSimplePaths() {
|
||||
|
||||
Path builder = new Path("foo").slash("bar");
|
||||
assertThat(builder.toString(), is("/foo/bar"));
|
||||
assertThat(builder.toString()).isEqualTo("/foo/bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removesLeadingAndTrailingSlashes() {
|
||||
|
||||
Path builder = new Path("foo/").slash("/bar").slash("//foobar///");
|
||||
assertThat(builder.toString(), is("/foo/bar/foobar"));
|
||||
assertThat(builder.toString()).isEqualTo("/foo/bar/foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removesWhitespace() {
|
||||
|
||||
Path builder = new Path("foo/ ").slash("/ b a r").slash(" //foobar/// ");
|
||||
assertThat(builder.toString(), is("/foo/bar/foobar"));
|
||||
assertThat(builder.toString()).isEqualTo("/foo/bar/foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWithLeadingSlash() {
|
||||
assertThat(new Path("/foobar").matches("/foobar"), is(true));
|
||||
assertThat(new Path("/foobar").matches("/foobar")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesWithoutLeadingSlash() {
|
||||
assertThat(new Path("/foobar").matches("foobar"), is(true));
|
||||
assertThat(new Path("/foobar").matches("foobar")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotMatchIfDifferent() {
|
||||
assertThat(new Path("/foobar").matches("barfoo"), is(false));
|
||||
assertThat(new Path("/foobar").matches("barfoo")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotPrefixAbsoluteUris() {
|
||||
assertThat(new Path("http://localhost").toString(), is("http://localhost"));
|
||||
assertThat(new Path("http://localhost").toString()).isEqualTo("http://localhost");
|
||||
}
|
||||
|
||||
@Test // DATAREST-222
|
||||
public void doesNotMatchIfReferenceContainsReservedCharacters() {
|
||||
assertThat(new Path("/foobar").matches("barfoo{?foo}"), is(false));
|
||||
assertThat(new Path("/foobar").matches("barfoo{?foo}")).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-222
|
||||
public void doesNotMatchNullReference() {
|
||||
assertThat(new Path("/foobar").matches(null), is(false));
|
||||
assertThat(new Path("/foobar").matches(null)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
29
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/RepositoryRestConfigurationIntegrationTests.java
Normal file → Executable file
29
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/RepositoryRestConfigurationIntegrationTests.java
Normal file → Executable file
@@ -1,7 +1,21 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -13,6 +27,7 @@ import org.springframework.data.rest.core.domain.ConfiguredPersonRepository;
|
||||
* Tests to check that {@link ResourceMapping}s are handled correctly.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class RepositoryRestConfigurationIntegrationTests extends AbstractIntegrationTests {
|
||||
@@ -21,12 +36,12 @@ public class RepositoryRestConfigurationIntegrationTests extends AbstractIntegra
|
||||
|
||||
@Test
|
||||
public void shouldProvideResourceMappingForConfiguredRepository() throws Exception {
|
||||
|
||||
ResourceMapping mapping = config.getResourceMappingForRepository(ConfiguredPersonRepository.class);
|
||||
|
||||
assertThat(mapping, notNullValue());
|
||||
assertThat(mapping.getRel(), is("people"));
|
||||
assertThat(mapping.getPath(), is("people"));
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping).isNotNull();
|
||||
assertThat(mapping.getRel()).isEqualTo("people");
|
||||
assertThat(mapping.getPath()).isEqualTo("people");
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
45
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/RepositoryRestConfigurationUnitTests.java
Normal file → Executable file
45
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/RepositoryRestConfigurationUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -54,22 +53,22 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
@Test // DATAREST-34
|
||||
public void returnsBodiesIfAcceptHeaderPresentByDefault() {
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE)).isTrue();
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
public void doesNotReturnBodiesIfNoAcceptHeaderPresentByDefault() {
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(null)).isFalse();
|
||||
assertThat(configuration.returnBodyOnUpdate(null)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
public void doesNotReturnBodiesIfEmptyAcceptHeaderPresentByDefault() {
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate("")).isFalse();
|
||||
assertThat(configuration.returnBodyOnUpdate("")).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
@@ -77,9 +76,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
configuration.setReturnBodyOnUpdate(false);
|
||||
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(null)).isFalse();
|
||||
assertThat(configuration.returnBodyOnUpdate("")).isFalse();
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
@@ -87,9 +86,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
configuration.setReturnBodyOnCreate(false);
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(null)).isFalse();
|
||||
assertThat(configuration.returnBodyOnCreate("")).isFalse();
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
@@ -97,9 +96,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
configuration.setReturnBodyOnUpdate(true);
|
||||
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(null)).isTrue();
|
||||
assertThat(configuration.returnBodyOnUpdate("")).isTrue();
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-34
|
||||
@@ -107,9 +106,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
configuration.setReturnBodyOnCreate(true);
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(null)).isTrue();
|
||||
assertThat(configuration.returnBodyOnCreate("")).isTrue();
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-776
|
||||
@@ -117,7 +116,7 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
configuration.withEntityLookup().forLookupRepository(ProfileRepository.class);
|
||||
|
||||
assertThat(configuration.isLookupType(Profile.class), is(true));
|
||||
assertThat(configuration.isLookupType(Profile.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-573
|
||||
@@ -127,9 +126,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
registry.addMapping("/hello").maxAge(1234);
|
||||
|
||||
Map<String, CorsConfiguration> corsConfigurations = registry.getCorsConfigurations();
|
||||
assertThat(corsConfigurations, hasKey("/hello"));
|
||||
assertThat(corsConfigurations).containsKey("/hello");
|
||||
|
||||
CorsConfiguration corsConfiguration = corsConfigurations.get("/hello");
|
||||
assertThat(corsConfiguration.getMaxAge(), is(1234L));
|
||||
assertThat(corsConfiguration.getMaxAge()).isEqualTo(1234L);
|
||||
}
|
||||
}
|
||||
|
||||
32
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java
Normal file → Executable file
32
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java
Normal file → Executable file
@@ -15,20 +15,20 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
|
||||
@@ -40,6 +40,7 @@ import org.springframework.data.repository.support.Repositories;
|
||||
import org.springframework.data.repository.support.RepositoryInvoker;
|
||||
import org.springframework.data.repository.support.RepositoryInvokerFactory;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.Streamable;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UriToEntityConverter}.
|
||||
@@ -56,14 +57,13 @@ public class UriToEntityConverterUnitTests {
|
||||
@Mock Repositories repositories;
|
||||
@Mock RepositoryInvokerFactory invokerFactory;
|
||||
|
||||
KeyValueMappingContext context;
|
||||
KeyValueMappingContext<?, ?> context;
|
||||
UriToEntityConverter converter;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
|
||||
this.context = new KeyValueMappingContext();
|
||||
this.context = new KeyValueMappingContext<>();
|
||||
this.context.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(Entity.class, NonEntity.class)));
|
||||
this.context.afterPropertiesSet();
|
||||
|
||||
@@ -76,26 +76,27 @@ public class UriToEntityConverterUnitTests {
|
||||
|
||||
Set<ConvertiblePair> result = converter.getConvertibleTypes();
|
||||
|
||||
assertThat(result, hasItem(new ConvertiblePair(URI.class, Entity.class)));
|
||||
assertThat(result, not(hasItem(new ConvertiblePair(URI.class, NonEntity.class))));
|
||||
assertThat(result).contains(new ConvertiblePair(URI.class, Entity.class));
|
||||
assertThat(result).doesNotContain(new ConvertiblePair(URI.class, NonEntity.class));
|
||||
}
|
||||
|
||||
@Test // DATAREST-427
|
||||
public void cannotConvertEntityWithIdPropertyIfStringConversionMissing() {
|
||||
assertThat(converter.matches(URI_TYPE, ENTITY_TYPE), is(false));
|
||||
assertThat(converter.matches(URI_TYPE, ENTITY_TYPE)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-427
|
||||
public void canConvertEntityWithIdPropertyAndFromStringConversionPossible() {
|
||||
|
||||
doReturn(mock(RepositoryInformation.class)).when(repositories).getRepositoryInformationFor(ENTITY_TYPE.getType());
|
||||
doReturn(Optional.of(mock(RepositoryInformation.class))).when(repositories)
|
||||
.getRepositoryInformationFor(ENTITY_TYPE.getType());
|
||||
|
||||
assertThat(converter.matches(URI_TYPE, ENTITY_TYPE), is(true));
|
||||
assertThat(converter.matches(URI_TYPE, ENTITY_TYPE)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-427
|
||||
public void cannotConvertEntityWithoutIdentifier() {
|
||||
assertThat(converter.matches(URI_TYPE, TypeDescriptor.valueOf(NonEntity.class)), is(false));
|
||||
assertThat(converter.matches(URI_TYPE, TypeDescriptor.valueOf(NonEntity.class))).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-427
|
||||
@@ -104,10 +105,10 @@ public class UriToEntityConverterUnitTests {
|
||||
Entity reference = new Entity();
|
||||
|
||||
RepositoryInvoker invoker = mock(RepositoryInvoker.class);
|
||||
doReturn(reference).when(invoker).invokeFindOne("1");
|
||||
doReturn(Optional.of(reference)).when(invoker).invokeFindOne("1");
|
||||
doReturn(invoker).when(invokerFactory).getInvokerFor(ENTITY_TYPE.getType());
|
||||
|
||||
assertThat(converter.convert(URI.create("/foo/bar/1"), URI_TYPE, ENTITY_TYPE), is((Object) reference));
|
||||
assertThat(converter.convert(URI.create("/foo/bar/1"), URI_TYPE, ENTITY_TYPE)).isEqualTo((Object) reference);
|
||||
}
|
||||
|
||||
@Test(expected = ConversionFailedException.class) // DATAREST-427
|
||||
@@ -139,11 +140,10 @@ public class UriToEntityConverterUnitTests {
|
||||
* @see DATAREST-1018
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void doesNotRegisterTypeWithUnmanagedRawType() {
|
||||
|
||||
PersistentEntities entities = mock(PersistentEntities.class);
|
||||
doReturn(Arrays.asList(ClassTypeInformation.OBJECT)).when(entities).getManagedTypes();
|
||||
doReturn(Streamable.of(ClassTypeInformation.OBJECT)).when(entities).getManagedTypes();
|
||||
|
||||
new UriToEntityConverter(entities, invokerFactory, repositories);
|
||||
}
|
||||
|
||||
11
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/ValidationErrorsUnitTests.java
Normal file → Executable file
11
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/ValidationErrorsUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -41,7 +40,7 @@ public class ValidationErrorsUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
KeyValueMappingContext context = new KeyValueMappingContext();
|
||||
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
|
||||
context.getPersistentEntity(Foo.class);
|
||||
|
||||
this.entities = new PersistentEntities(Arrays.asList(context));
|
||||
@@ -56,7 +55,7 @@ public class ValidationErrorsUnitTests {
|
||||
errors.rejectValue("field", "asdf");
|
||||
errors.popNestedPath();
|
||||
|
||||
assertThat(errors.getFieldError().getField(), is("bars[0].field"));
|
||||
assertThat(errors.getFieldError().getField()).isEqualTo("bars[0].field");
|
||||
}
|
||||
|
||||
@Test // DATAREST-801
|
||||
@@ -66,7 +65,7 @@ public class ValidationErrorsUnitTests {
|
||||
|
||||
private static void expectedErrorBehavior(Errors errors) {
|
||||
|
||||
assertThat(errors.getFieldValue("bars"), is(notNullValue()));
|
||||
assertThat(errors.getFieldValue("bars")).isNotNull();
|
||||
|
||||
errors.pushNestedPath("bars[0]");
|
||||
|
||||
@@ -75,7 +74,7 @@ public class ValidationErrorsUnitTests {
|
||||
fail("Expected NotReadablePropertyException!");
|
||||
} catch (NotReadablePropertyException e) {}
|
||||
|
||||
assertThat(errors.getFieldValue("field"), is((Object) "Hello"));
|
||||
assertThat(errors.getFieldValue("field")).isEqualTo((Object) "Hello");
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
38
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java
Normal file → Executable file
38
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.config;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,7 +27,6 @@ import org.springframework.data.rest.core.config.ProjectionDefinitionConfigurati
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class ProjectionDefinitionConfigurationUnitTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATAREST-221
|
||||
@@ -67,7 +65,7 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
|
||||
configuration.addProjection(Integer.class, "name", String.class);
|
||||
|
||||
assertThat(configuration.getProjectionType(String.class, "name"), is(equalTo((Class) Integer.class)));
|
||||
assertThat(configuration.getProjectionType(String.class, "name")).isEqualTo(Integer.class);
|
||||
}
|
||||
|
||||
@Test // DATAREST-221
|
||||
@@ -76,7 +74,7 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
|
||||
configuration.addProjection(SampleProjection.class);
|
||||
|
||||
assertThat(configuration.getProjectionType(Integer.class, "name"), is(equalTo((Class) SampleProjection.class)));
|
||||
assertThat(configuration.getProjectionType(Integer.class, "name")).isEqualTo(SampleProjection.class);
|
||||
}
|
||||
|
||||
@Test // DATAREST-221
|
||||
@@ -85,7 +83,7 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
|
||||
configuration.addProjection(Default.class);
|
||||
|
||||
assertThat(configuration.getProjectionType(Integer.class, "default"), is(equalTo((Class) Default.class)));
|
||||
assertThat(configuration.getProjectionType(Integer.class, "default")).isEqualTo(Default.class);
|
||||
}
|
||||
|
||||
@Test // DATAREST-221
|
||||
@@ -96,17 +94,17 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
ProjectionDefinition stringName = ProjectionDefinition.of(String.class, Object.class, "name");
|
||||
ProjectionDefinition objectOtherNameKey = ProjectionDefinition.of(Object.class, Object.class, "otherName");
|
||||
|
||||
assertThat(objectName, is(objectName));
|
||||
assertThat(objectName, is(sameObjectName));
|
||||
assertThat(sameObjectName, is(objectName));
|
||||
assertThat(objectName).isEqualTo(objectName);
|
||||
assertThat(objectName).isEqualTo(sameObjectName);
|
||||
assertThat(sameObjectName).isEqualTo(objectName);
|
||||
|
||||
assertThat(objectName, is(not(stringName)));
|
||||
assertThat(stringName, is(not(objectName)));
|
||||
assertThat(objectName).isNotEqualTo(stringName);
|
||||
assertThat(stringName).isNotEqualTo(objectName);
|
||||
|
||||
assertThat(objectName, is(not(objectOtherNameKey)));
|
||||
assertThat(objectOtherNameKey, is(not(objectName)));
|
||||
assertThat(objectName).isNotEqualTo(objectOtherNameKey);
|
||||
assertThat(objectOtherNameKey).isNotEqualTo(objectName);
|
||||
|
||||
assertThat(objectName, is(not(new Object())));
|
||||
assertThat(objectName).isNotEqualTo(new Object());
|
||||
}
|
||||
|
||||
@Test // DATAREST-385
|
||||
@@ -115,14 +113,14 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
|
||||
configuration.addProjection(ParentProjection.class);
|
||||
|
||||
assertThat(configuration.hasProjectionFor(Child.class), is(true));
|
||||
assertThat(configuration.getProjectionsFor(Child.class).values(), hasItem(ParentProjection.class));
|
||||
assertThat(configuration.getProjectionType(Child.class, "summary"), is(typeCompatibleWith(ParentProjection.class)));
|
||||
assertThat(configuration.hasProjectionFor(Child.class)).isTrue();
|
||||
assertThat(configuration.getProjectionsFor(Child.class).values()).contains(ParentProjection.class);
|
||||
assertThat(configuration.getProjectionType(Child.class, "summary")).isAssignableFrom(ParentProjection.class);
|
||||
}
|
||||
|
||||
@Test // DATAREST-221
|
||||
public void defaultsParamternameToProjection() {
|
||||
assertThat(new ProjectionDefinitionConfiguration().getParameterName(), is("projection"));
|
||||
assertThat(new ProjectionDefinitionConfiguration().getParameterName()).isEqualTo("projection");
|
||||
}
|
||||
|
||||
@Test // DATAREST-747
|
||||
@@ -134,8 +132,8 @@ public class ProjectionDefinitionConfigurationUnitTests {
|
||||
|
||||
Map<String, Class<?>> projections = configuration.getProjectionsFor(Child.class);
|
||||
|
||||
assertThat(projections.values(), hasSize(1));
|
||||
assertThat(projections.values(), hasItem(ChildProjection.class));
|
||||
assertThat(projections.values()).hasSize(1);
|
||||
assertThat(projections.values()).contains(ChildProjection.class);
|
||||
}
|
||||
|
||||
@Projection(name = "name", types = Integer.class)
|
||||
|
||||
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ResourceMappingUnitTests.java
Normal file → Executable file
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ResourceMappingUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.config;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.rest.core.support.ResourceMappingUtils.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -43,16 +42,17 @@ public class ResourceMappingUnitTests {
|
||||
|
||||
@Test
|
||||
public void shouldDetectPathAndRemoveLeadingSlashIfAny() {
|
||||
|
||||
org.springframework.data.rest.core.config.ResourceMapping mapping = new org.springframework.data.rest.core.config.ResourceMapping(
|
||||
findRel(AnnotatedWithLeadingSlashPersonRepository.class),
|
||||
findPath(AnnotatedWithLeadingSlashPersonRepository.class),
|
||||
findExported(AnnotatedWithLeadingSlashPersonRepository.class));
|
||||
|
||||
// The rel attribute defaults to class name
|
||||
assertThat(mapping.getRel(), is("annotatedWithLeadingSlashPerson"));
|
||||
assertThat(mapping.getPath(), is("people"));
|
||||
assertThat(mapping.getRel()).isEqualTo("annotatedWithLeadingSlashPerson");
|
||||
assertThat(mapping.getPath()).isEqualTo("people");
|
||||
// The exported defaults to true
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,10 +63,10 @@ public class ResourceMappingUnitTests {
|
||||
findRel(method), findPath(method), findExported(method));
|
||||
|
||||
// The rel attribute defaults to class name
|
||||
assertThat(mapping.getRel(), is("findByFirstName"));
|
||||
assertThat(mapping.getPath(), is("firstname"));
|
||||
assertThat(mapping.getRel()).isEqualTo("findByFirstName");
|
||||
assertThat(mapping.getPath()).isEqualTo("firstname");
|
||||
// The exported defaults to true
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -77,11 +77,11 @@ public class ResourceMappingUnitTests {
|
||||
findRel(method), findPath(method), findExported(method));
|
||||
|
||||
// The rel defaults to method name
|
||||
assertThat(mapping.getRel(), is("findByLastName"));
|
||||
assertThat(mapping.getRel()).isEqualTo("findByLastName");
|
||||
// The path contains only a leading slash therefore defaults to method name
|
||||
assertThat(mapping.getPath(), is("findByLastName"));
|
||||
assertThat(mapping.getPath()).isEqualTo("findByLastName");
|
||||
// The exported defaults to true
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@RestResource(path = "/people")
|
||||
|
||||
0
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java
Normal file → Executable file
0
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java
Normal file → Executable file
2
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/ValidatorIntegrationTests.java
Normal file → Executable file
2
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/ValidatorIntegrationTests.java
Normal file → Executable file
@@ -60,7 +60,7 @@ public class ValidatorIntegrationTests {
|
||||
}
|
||||
|
||||
@Autowired ConfigurableApplicationContext context;
|
||||
@Autowired KeyValueMappingContext mappingContext;
|
||||
@Autowired KeyValueMappingContext<?, ?> mappingContext;
|
||||
|
||||
@Test(expected = RepositoryConstraintViolationException.class)
|
||||
public void shouldValidateLastName() throws Exception {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
@@ -29,12 +30,12 @@ public interface OrderRepository extends CrudRepository<Order, UUID> {
|
||||
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <S extends Order> S save(S entity);
|
||||
<S extends Order> S save(S entity);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public Order findOne(UUID id);
|
||||
Optional<Order> findOne(UUID id);
|
||||
}
|
||||
|
||||
17
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java
Normal file → Executable file
17
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.event;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
@@ -51,7 +50,7 @@ public class AnnotatedEventHandlerInvokerUnitTests {
|
||||
MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod> methods = (MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod>) ReflectionTestUtils
|
||||
.getField(invoker, "handlerMethods");
|
||||
|
||||
assertThat(methods.get(BeforeCreateEvent.class), hasSize(1));
|
||||
assertThat(methods.get(BeforeCreateEvent.class)).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // DATAREST-606
|
||||
@@ -64,7 +63,7 @@ public class AnnotatedEventHandlerInvokerUnitTests {
|
||||
|
||||
invoker.onApplicationEvent(new BeforeCreateEvent(new Person("Dave", "Matthews")));
|
||||
|
||||
assertThat(sampleHandler.wasCalled, is(true));
|
||||
assertThat(sampleHandler.wasCalled).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-970
|
||||
@@ -79,10 +78,10 @@ public class AnnotatedEventHandlerInvokerUnitTests {
|
||||
|
||||
invoker.onApplicationEvent(new BeforeCreateEvent(new Person("Dave", "Matthews")));
|
||||
|
||||
assertThat(orderHandler1.wasCalled, is(true));
|
||||
assertThat(orderHandler2.wasCalled, is(true));
|
||||
assertThat(orderHandler1.wasCalled).isTrue();
|
||||
assertThat(orderHandler2.wasCalled).isTrue();
|
||||
|
||||
assertThat(orderHandler1.timestamp, is(greaterThan(orderHandler2.timestamp)));
|
||||
assertThat(orderHandler1.timestamp).isGreaterThan(orderHandler2.timestamp);
|
||||
}
|
||||
|
||||
@Test // DATAREST-983
|
||||
@@ -98,8 +97,8 @@ public class AnnotatedEventHandlerInvokerUnitTests {
|
||||
invoker.onApplicationEvent(new BeforeCreateEvent(new FirstEntity()));
|
||||
invoker.onApplicationEvent(new BeforeCreateEvent(new SecondEntity()));
|
||||
|
||||
assertThat(firstHandler.callCount, is(1));
|
||||
assertThat(secondHandler.callCount, is(1));
|
||||
assertThat(firstHandler.callCount).isEqualTo(1);
|
||||
assertThat(secondHandler.callCount).isEqualTo(1);
|
||||
}
|
||||
|
||||
@RepositoryEventHandler
|
||||
|
||||
36
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/CrudMethodsSupportedHttpMethodsUnitTests.java
Normal file → Executable file
36
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/CrudMethodsSupportedHttpMethodsUnitTests.java
Normal file → Executable file
@@ -15,17 +15,17 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.rest.core.mapping.ResourceType.*;
|
||||
import static org.springframework.http.HttpMethod.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.annotation.ReadOnlyProperty;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
|
||||
@@ -88,22 +88,24 @@ public class CrudMethodsSupportedHttpMethodsUnitTests {
|
||||
@Test // DATAREST-523
|
||||
public void exposesMethodsForProperties() {
|
||||
|
||||
KeyValueMappingContext context = new KeyValueMappingContext();
|
||||
KeyValuePersistentEntity<?> entity = context.getPersistentEntity(Entity.class);
|
||||
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
|
||||
KeyValuePersistentEntity<?, ?> entity = context.getRequiredPersistentEntity(Entity.class);
|
||||
|
||||
SupportedHttpMethods methods = getSupportedHttpMethodsFor(EntityRepository.class);
|
||||
|
||||
assertThat(methods.getMethodsFor(entity.getPersistentProperty("embedded")), is(empty()));
|
||||
assertThat(methods.getMethodsFor(entity.getPersistentProperty("embeddedCollection")), is(empty()));
|
||||
assertThat(methods.getMethodsFor(entity.getRequiredPersistentProperty("embedded"))).isEmpty();
|
||||
assertThat(methods.getMethodsFor(entity.getRequiredPersistentProperty("embeddedCollection"))).isEmpty();
|
||||
|
||||
assertThat(methods.getMethodsFor(entity.getPersistentProperty("related")),
|
||||
allOf(hasItems(GET, DELETE, PATCH, PUT), not(hasItem(POST))));
|
||||
assertThat(methods.getMethodsFor(entity.getRequiredPersistentProperty("related")))//
|
||||
.contains(GET, DELETE, PATCH, PUT)//
|
||||
.doesNotContain(POST);
|
||||
|
||||
assertThat(methods.getMethodsFor(entity.getPersistentProperty("relatedCollection")),
|
||||
hasItems(GET, DELETE, PATCH, PUT, POST));
|
||||
assertThat(methods.getMethodsFor(entity.getRequiredPersistentProperty("relatedCollection")))//
|
||||
.contains(GET, DELETE, PATCH, PUT, POST);
|
||||
|
||||
assertThat(methods.getMethodsFor(entity.getPersistentProperty("readOnlyReference")),
|
||||
allOf(hasItem(GET), not(hasItems(DELETE, PATCH, PUT, POST))));
|
||||
assertThat(methods.getMethodsFor(entity.getRequiredPersistentProperty("readOnlyReference")))//
|
||||
.contains(GET)//
|
||||
.doesNotContain(DELETE, PATCH, PUT, POST);
|
||||
}
|
||||
|
||||
@Test // DATAREST-825
|
||||
@@ -129,10 +131,10 @@ public class CrudMethodsSupportedHttpMethodsUnitTests {
|
||||
|
||||
Set<HttpMethod> result = methods.getMethodsFor(type);
|
||||
|
||||
assertThat(result, supported ? hasItems(httpMethods) : not(hasItems(httpMethods)));
|
||||
|
||||
if (supported) {
|
||||
assertThat(result, hasSize(httpMethods.length));
|
||||
assertThat(result).containsExactlyInAnyOrder(httpMethods);
|
||||
} else {
|
||||
assertThat(result).doesNotContain(httpMethods);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +152,7 @@ public class CrudMethodsSupportedHttpMethodsUnitTests {
|
||||
|
||||
@Override
|
||||
@RestResource(exported = false)
|
||||
Object findOne(Long id);
|
||||
Optional<Object> findOne(Long id);
|
||||
}
|
||||
|
||||
interface NoFindOne extends Repository<Object, Long> {
|
||||
|
||||
23
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java
Normal file → Executable file
23
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java
Normal file → Executable file
@@ -15,14 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
|
||||
@@ -38,9 +37,9 @@ import org.springframework.data.rest.core.annotation.RestResource;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingResourceMetadataUnitTests {
|
||||
|
||||
KeyValueMappingContext context = new KeyValueMappingContext();
|
||||
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
|
||||
|
||||
KeyValuePersistentEntity<?> entity = context.getPersistentEntity(Entity.class);
|
||||
KeyValuePersistentEntity<?, ?> entity = context.getRequiredPersistentEntity(Entity.class);
|
||||
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(
|
||||
new PersistentEntities(Arrays.asList(context)));
|
||||
MappingResourceMetadata metadata = new MappingResourceMetadata(entity, resourceMappings);
|
||||
@@ -48,27 +47,27 @@ public class MappingResourceMetadataUnitTests {
|
||||
@Test // DATAREST-514
|
||||
public void allowsLookupOfPropertyByMappedName() {
|
||||
|
||||
KeyValuePersistentProperty property = entity.getPersistentProperty("related");
|
||||
KeyValuePersistentProperty<?> property = entity.getRequiredPersistentProperty("related");
|
||||
|
||||
PropertyAwareResourceMapping propertyMapping = metadata.getProperty("foo");
|
||||
|
||||
assertThat(propertyMapping, is(notNullValue()));
|
||||
assertThat(propertyMapping.getProperty(), is((Object) property));
|
||||
assertThat(metadata.getMappingFor(property).getPath().matches("foo"), is(true));
|
||||
assertThat(propertyMapping).isNotNull();
|
||||
assertThat(propertyMapping.getProperty()).isEqualTo((Object) property);
|
||||
assertThat(metadata.getMappingFor(property).getPath().matches("foo")).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-518
|
||||
public void isNotExportedByDefault() {
|
||||
|
||||
assertThat(metadata.isExported(), is(false));
|
||||
assertThat(metadata.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-518
|
||||
public void isExportedIfExplicitlyAnnotated() {
|
||||
|
||||
MappingResourceMetadata metadata = new MappingResourceMetadata(context.getPersistentEntity(Related.class),
|
||||
MappingResourceMetadata metadata = new MappingResourceMetadata(context.getRequiredPersistentEntity(Related.class),
|
||||
resourceMappings);
|
||||
assertThat(metadata.isExported(), is(true));
|
||||
assertThat(metadata.isExported()).isTrue();
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
|
||||
43
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java
Normal file → Executable file
43
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java
Normal file → Executable file
@@ -15,15 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
|
||||
@@ -41,17 +40,17 @@ import org.springframework.data.rest.core.annotation.RestResource;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PersistentPropertyResourceMappingUnitTests {
|
||||
|
||||
KeyValueMappingContext mappingContext = new KeyValueMappingContext();
|
||||
KeyValueMappingContext<?, ?> mappingContext = new KeyValueMappingContext<>();
|
||||
|
||||
@Test // DATAREST-175
|
||||
public void usesPropertyNameAsDefaultResourceMappingRelAndPath() {
|
||||
|
||||
ResourceMapping mapping = getPropertyMappingFor(Entity.class, "first");
|
||||
|
||||
assertThat(mapping, is(notNullValue()));
|
||||
assertThat(mapping.getPath(), is(new Path("first")));
|
||||
assertThat(mapping.getRel(), is("first"));
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping).isNotNull();
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("first"));
|
||||
assertThat(mapping.getRel()).isEqualTo("first");
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-175
|
||||
@@ -59,10 +58,10 @@ public class PersistentPropertyResourceMappingUnitTests {
|
||||
|
||||
ResourceMapping mapping = getPropertyMappingFor(Entity.class, "second");
|
||||
|
||||
assertThat(mapping, is(notNullValue()));
|
||||
assertThat(mapping.getPath(), is(new Path("secPath")));
|
||||
assertThat(mapping.getRel(), is("secRel"));
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping).isNotNull();
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("secPath"));
|
||||
assertThat(mapping.getRel()).isEqualTo("secRel");
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-175
|
||||
@@ -70,10 +69,10 @@ public class PersistentPropertyResourceMappingUnitTests {
|
||||
|
||||
ResourceMapping mapping = getPropertyMappingFor(Entity.class, "third");
|
||||
|
||||
assertThat(mapping, is(notNullValue()));
|
||||
assertThat(mapping.getPath(), is(new Path("thirdPath")));
|
||||
assertThat(mapping.getRel(), is("thirdRel"));
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping).isNotNull();
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("thirdPath"));
|
||||
assertThat(mapping.getRel()).isEqualTo("thirdRel");
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-233
|
||||
@@ -83,8 +82,8 @@ public class PersistentPropertyResourceMappingUnitTests {
|
||||
|
||||
ResourceDescription description = mapping.getDescription();
|
||||
|
||||
assertThat(description.isDefault(), is(true));
|
||||
assertThat(description.getMessage(), is("rest.description.entity.second"));
|
||||
assertThat(description.isDefault()).isTrue();
|
||||
assertThat(description.getMessage()).isEqualTo("rest.description.entity.second");
|
||||
}
|
||||
|
||||
@Test // DATAREST-233
|
||||
@@ -93,14 +92,14 @@ public class PersistentPropertyResourceMappingUnitTests {
|
||||
ResourceMapping mapping = getPropertyMappingFor(Entity.class, "fourth");
|
||||
|
||||
ResourceDescription description = mapping.getDescription();
|
||||
assertThat(description.isDefault(), is(false));
|
||||
assertThat(description.getMessage(), is("Some description"));
|
||||
assertThat(description.isDefault()).isFalse();
|
||||
assertThat(description.getMessage()).isEqualTo("Some description");
|
||||
}
|
||||
|
||||
private ResourceMapping getPropertyMappingFor(Class<?> entity, String propertyName) {
|
||||
|
||||
KeyValuePersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity);
|
||||
KeyValuePersistentProperty property = persistentEntity.getPersistentProperty(propertyName);
|
||||
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity);
|
||||
KeyValuePersistentProperty<?> property = persistentEntity.getRequiredPersistentProperty(propertyName);
|
||||
|
||||
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(
|
||||
new PersistentEntities(Arrays.asList(mappingContext)));
|
||||
|
||||
37
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java
Normal file → Executable file
37
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -41,10 +40,10 @@ public class RepositoryCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = getResourceMappingFor(PersonRepository.class);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("persons")));
|
||||
assertThat(mapping.getRel(), is("persons"));
|
||||
assertThat(mapping.getItemResourceRel(), is("person"));
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("persons"));
|
||||
assertThat(mapping.getRel()).isEqualTo("persons");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("person");
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,10 +51,10 @@ public class RepositoryCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = getResourceMappingFor(AnnotatedPersonRepository.class);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("bar")));
|
||||
assertThat(mapping.getRel(), is("foo"));
|
||||
assertThat(mapping.getItemResourceRel(), is("annotatedPerson"));
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("bar"));
|
||||
assertThat(mapping.getRel()).isEqualTo("foo");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("annotatedPerson");
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,30 +62,30 @@ public class RepositoryCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = getResourceMappingFor(AnnotatedAnnotatedPersonRepository.class);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("/trumpsAll")));
|
||||
assertThat(mapping.getRel(), is("foo"));
|
||||
assertThat(mapping.getItemResourceRel(), is("annotatedPerson"));
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("/trumpsAll"));
|
||||
assertThat(mapping.getRel()).isEqualTo("foo");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("annotatedPerson");
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotExposeRepositoryForPublicDomainTypeIfRepoIsPackageProtected() {
|
||||
|
||||
ResourceMapping mapping = getResourceMappingFor(PackageProtectedRepository.class);
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-229
|
||||
public void detectsPagingRepository() {
|
||||
assertThat(getResourceMappingFor(PersonRepository.class).isPagingResource(), is(true));
|
||||
assertThat(getResourceMappingFor(PersonRepository.class).isPagingResource()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversCustomizationsUsingRestRepositoryResource() {
|
||||
|
||||
CollectionResourceMapping mapping = getResourceMappingFor(RepositoryAnnotatedRepository.class);
|
||||
assertThat(mapping.getRel(), is("foo"));
|
||||
assertThat(mapping.getItemResourceRel(), is("bar"));
|
||||
assertThat(mapping.getRel()).isEqualTo("foo");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test // DATAREST-445
|
||||
@@ -103,7 +102,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
|
||||
RepositoryCollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(metadata,
|
||||
RepositoryDetectionStrategies.DEFAULT);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("/objects")));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("/objects"));
|
||||
}
|
||||
|
||||
private static CollectionResourceMapping getResourceMappingFor(Class<?> repositoryInterface) {
|
||||
|
||||
5
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryDetectionStrategiesUnitTests.java
Normal file → Executable file
5
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryDetectionStrategiesUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -93,7 +92,7 @@ public class RepositoryDetectionStrategiesUnitTests {
|
||||
private static void assertExposures(RepositoryDetectionStrategy strategy, Map<Class<?>, Boolean> expected) {
|
||||
|
||||
for (Entry<Class<?>, Boolean> entry : expected.entrySet()) {
|
||||
assertThat(strategy.isExported(new DefaultRepositoryMetadata(entry.getKey())), is(entry.getValue()));
|
||||
assertThat(strategy.isExported(new DefaultRepositoryMetadata(entry.getKey()))).isEqualTo(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
27
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java
Normal file → Executable file
27
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryMethodResourceMappingUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -49,7 +48,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
|
||||
ResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("findByLastname")));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("findByLastname"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,16 +57,16 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByFirstname", String.class);
|
||||
ResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("bar")));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("bar"));
|
||||
}
|
||||
|
||||
@Test // DATAREST-31
|
||||
public void doesNotDiscoverAnyParametersIfNotAnnotated() throws Exception {
|
||||
public void discoversParametersIfCompiledWithCorrespondingFlag() throws Exception {
|
||||
|
||||
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
|
||||
MethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames(), is(emptyIterable()));
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames()).contains("lastname");
|
||||
}
|
||||
|
||||
@Test // DATAREST-31
|
||||
@@ -76,8 +75,8 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByFirstname", String.class);
|
||||
MethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames(), hasSize(1));
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames(), hasItem("firstname"));
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames()).hasSize(1);
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames()).contains("firstname");
|
||||
}
|
||||
|
||||
@Test // DATAREST-229
|
||||
@@ -86,7 +85,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class);
|
||||
MethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.isPagingResource(), is(true));
|
||||
assertThat(mapping.isPagingResource()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -95,7 +94,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class);
|
||||
MethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getRel(), is("findByEmailAddress"));
|
||||
assertThat(mapping.getRel()).isEqualTo("findByEmailAddress");
|
||||
}
|
||||
|
||||
@Test // DATAREST-384
|
||||
@@ -104,12 +103,12 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Sort.class);
|
||||
RepositoryMethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.isSortableResource(), is(true));
|
||||
assertThat(mapping.isSortableResource()).isTrue();
|
||||
|
||||
method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class);
|
||||
mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.isSortableResource(), is(false));
|
||||
assertThat(mapping.isSortableResource()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-467
|
||||
@@ -119,7 +118,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
|
||||
MethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getReturnedDomainType(), is(equalTo((Class) Person.class)));
|
||||
assertThat(mapping.getReturnedDomainType()).isEqualTo((Class) Person.class);
|
||||
}
|
||||
|
||||
@Test // DATAREST-699
|
||||
@@ -128,7 +127,7 @@ public class RepositoryMethodResourceMappingUnitTests {
|
||||
Method method = PersonRepository.class.getMethod("findByLastname", String.class, Pageable.class);
|
||||
RepositoryMethodResourceMapping mapping = getMappingFor(method);
|
||||
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames(), not(hasItem("pageable")));
|
||||
assertThat(mapping.getParametersMetadata().getParameterNames()).doesNotContain("pageable");
|
||||
}
|
||||
|
||||
private RepositoryMethodResourceMapping getMappingFor(Method method) {
|
||||
|
||||
57
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappingsIntegrationTests.java
Normal file → Executable file
57
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappingsIntegrationTests.java
Normal file → Executable file
@@ -15,14 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -38,6 +36,7 @@ import org.springframework.data.rest.core.domain.Author;
|
||||
import org.springframework.data.rest.core.domain.CreditCard;
|
||||
import org.springframework.data.rest.core.domain.JpaRepositoryConfig;
|
||||
import org.springframework.data.rest.core.domain.Person;
|
||||
import org.springframework.data.rest.core.domain.Profile;
|
||||
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
|
||||
import org.springframework.hateoas.core.EvoInflectorRelProvider;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -54,13 +53,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
@Autowired ListableBeanFactory factory;
|
||||
@Autowired KeyValueMappingContext mappingContext;
|
||||
@Autowired KeyValueMappingContext<?, ?> mappingContext;
|
||||
|
||||
ResourceMappings mappings;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
mappingContext.getPersistentEntity(Profile.class);
|
||||
|
||||
Repositories repositories = new Repositories(factory);
|
||||
this.mappings = new RepositoryResourceMappings(repositories, new PersistentEntities(Arrays.asList(mappingContext)),
|
||||
new EvoInflectorRelProvider(), RepositoryDetectionStrategies.DEFAULT);
|
||||
@@ -68,7 +69,7 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void detectsAllMappings() {
|
||||
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(5)));
|
||||
assertThat(mappings).hasSize(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,8 +77,8 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
ResourceMetadata personMappings = mappings.getMetadataFor(Person.class);
|
||||
|
||||
assertThat(personMappings.isExported(), is(true));
|
||||
assertThat(personMappings.getSearchResourceMappings().isExported(), is(true));
|
||||
assertThat(personMappings.isExported()).isTrue();
|
||||
assertThat(personMappings.getSearchResourceMappings().isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,8 +86,8 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
ResourceMetadata creditCardMapping = mappings.getMetadataFor(CreditCard.class);
|
||||
|
||||
assertThat(creditCardMapping.isExported(), is(false));
|
||||
assertThat(creditCardMapping.getSearchResourceMappings().isExported(), is(false));
|
||||
assertThat(creditCardMapping.isExported()).isFalse();
|
||||
assertThat(creditCardMapping.getSearchResourceMappings().isExported()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-112
|
||||
@@ -94,27 +95,27 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
Repositories repositories = new Repositories(factory);
|
||||
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(Person.class);
|
||||
PersistentProperty<?> property = entity.getPersistentProperty("siblings");
|
||||
PersistentProperty<?> property = entity.getRequiredPersistentProperty("siblings");
|
||||
|
||||
ResourceMetadata metadata = mappings.getMetadataFor(Person.class);
|
||||
ResourceMapping mapping = metadata.getMappingFor(property);
|
||||
|
||||
assertThat(mapping.getRel(), is("siblings"));
|
||||
assertThat(mapping.getPath(), is(new Path("siblings")));
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.getRel()).isEqualTo("siblings");
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("siblings"));
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-111
|
||||
public void exposesResourceByPath() {
|
||||
|
||||
assertThat(mappings.exportsTopLevelResourceFor("people"), is(true));
|
||||
assertThat(mappings.exportsTopLevelResourceFor("orders"), is(true));
|
||||
assertThat(mappings.exportsTopLevelResourceFor("people")).isTrue();
|
||||
assertThat(mappings.exportsTopLevelResourceFor("orders")).isTrue();
|
||||
|
||||
ResourceMetadata creditCardMapping = mappings.getMetadataFor(CreditCard.class);
|
||||
assertThat(creditCardMapping, is(notNullValue()));
|
||||
assertThat(creditCardMapping.getPath(), is(new Path("creditCards")));
|
||||
assertThat(creditCardMapping.isExported(), is(false));
|
||||
assertThat(mappings.exportsTopLevelResourceFor("creditCards"), is(false));
|
||||
assertThat(creditCardMapping).isNotNull();
|
||||
assertThat(creditCardMapping.getPath()).isEqualTo(new Path("creditCards"));
|
||||
assertThat(creditCardMapping.isExported()).isFalse();
|
||||
assertThat(mappings.exportsTopLevelResourceFor("creditCards")).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAREST-107
|
||||
@@ -123,7 +124,7 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
ResourceMetadata creditCardMetadata = mappings.getMetadataFor(CreditCard.class);
|
||||
SearchResourceMappings searchResourceMappings = creditCardMetadata.getSearchResourceMappings();
|
||||
|
||||
assertThat(searchResourceMappings, is(Matchers.<MethodResourceMapping> iterableWithSize(0)));
|
||||
assertThat(searchResourceMappings).isEmpty();
|
||||
|
||||
ResourceMetadata personMetadata = mappings.getMetadataFor(Person.class);
|
||||
List<String> methodNames = new ArrayList<String>();
|
||||
@@ -132,25 +133,25 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
methodNames.add(method.getMethod().getName());
|
||||
}
|
||||
|
||||
assertThat(methodNames, hasSize(2));
|
||||
assertThat(methodNames, hasItems("findByFirstName", "findByCreatedGreaterThan"));
|
||||
assertThat(methodNames).hasSize(2);
|
||||
assertThat(methodNames).contains("findByFirstName", "findByCreatedGreaterThan");
|
||||
}
|
||||
|
||||
@Test // DATAREST-325
|
||||
public void exposesMethodResourceMappingInPackageProtectedButExportedRepo() {
|
||||
|
||||
ResourceMetadata metadata = mappings.getMetadataFor(Author.class);
|
||||
assertThat(metadata.isExported(), is(true));
|
||||
assertThat(metadata.isExported()).isTrue();
|
||||
|
||||
SearchResourceMappings searchMappings = metadata.getSearchResourceMappings();
|
||||
|
||||
assertThat(searchMappings.isExported(), is(true));
|
||||
assertThat(searchMappings.getMappedMethod("findByFirstnameContaining"), is(notNullValue()));
|
||||
assertThat(searchMappings.isExported()).isTrue();
|
||||
assertThat(searchMappings.getMappedMethod("findByFirstnameContaining")).isNotNull();
|
||||
|
||||
for (MethodResourceMapping methodMapping : searchMappings) {
|
||||
|
||||
System.out.println(methodMapping.getMethod().getName());
|
||||
assertThat(methodMapping.isExported(), is(true));
|
||||
assertThat(methodMapping.isExported()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +162,7 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
PropertyAwareResourceMapping propertyMapping = metadata.getProperty("father-mapped");
|
||||
|
||||
assertThat(propertyMapping.getRel(), is("father"));
|
||||
assertThat(propertyMapping.getPath(), is(new Path("father-mapped")));
|
||||
assertThat(propertyMapping.getRel()).isEqualTo("father");
|
||||
assertThat(propertyMapping.getPath()).isEqualTo(new Path("father-mapped"));
|
||||
}
|
||||
}
|
||||
|
||||
29
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMappingUnitTests.java
Normal file → Executable file
29
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMappingUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.rest.core.Path;
|
||||
@@ -34,10 +33,10 @@ public class TypeBasedCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = new TypeBasedCollectionResourceMapping(Sample.class);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("sample")));
|
||||
assertThat(mapping.getRel(), is("samples"));
|
||||
assertThat(mapping.getItemResourceRel(), is("sample"));
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("sample"));
|
||||
assertThat(mapping.getRel()).isEqualTo("samples");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("sample");
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -45,10 +44,10 @@ public class TypeBasedCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = new TypeBasedCollectionResourceMapping(CustomizedSample.class);
|
||||
|
||||
assertThat(mapping.getPath(), is(new Path("customizedSample")));
|
||||
assertThat(mapping.getRel(), is("myRel"));
|
||||
assertThat(mapping.getItemResourceRel(), is("customizedSample"));
|
||||
assertThat(mapping.isExported(), is(true));
|
||||
assertThat(mapping.getPath()).isEqualTo(new Path("customizedSample"));
|
||||
assertThat(mapping.getRel()).isEqualTo("myRel");
|
||||
assertThat(mapping.getItemResourceRel()).isEqualTo("customizedSample");
|
||||
assertThat(mapping.isExported()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAREST-99
|
||||
@@ -56,7 +55,7 @@ public class TypeBasedCollectionResourceMappingUnitTests {
|
||||
|
||||
CollectionResourceMapping mapping = new TypeBasedCollectionResourceMapping(HiddenSample.class);
|
||||
|
||||
assertThat(mapping.isExported(), is(false));
|
||||
assertThat(mapping.isExported()).isFalse();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,13 +67,13 @@ public class TypeBasedCollectionResourceMappingUnitTests {
|
||||
CollectionResourceMapping mapping = new TypeBasedCollectionResourceMapping(Sample.class);
|
||||
ResourceDescription description = mapping.getDescription();
|
||||
|
||||
assertThat(description.isDefault(), is(true));
|
||||
assertThat(description.getMessage(), is("rest.description.samples"));
|
||||
assertThat(description.isDefault()).isTrue();
|
||||
assertThat(description.getMessage()).isEqualTo("rest.description.samples");
|
||||
|
||||
ResourceDescription itemDescription = mapping.getItemResourceDescription();
|
||||
|
||||
assertThat(itemDescription.isDefault(), is(true));
|
||||
assertThat(itemDescription.getMessage(), is("rest.description.sample"));
|
||||
assertThat(itemDescription.isDefault()).isTrue();
|
||||
assertThat(itemDescription.getMessage()).isEqualTo("rest.description.sample");
|
||||
}
|
||||
|
||||
public interface Sample {}
|
||||
|
||||
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DefaultSelfLinkProviderUnitTests.java
Normal file → Executable file
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DefaultSelfLinkProviderUnitTests.java
Normal file → Executable file
@@ -16,7 +16,7 @@
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -31,9 +31,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.rest.core.domain.Profile;
|
||||
@@ -60,19 +58,15 @@ public class DefaultSelfLinkProviderUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
when(entityLinks.linkToSingleResource((Class<?>) any(), any())).then(new Answer<Link>() {
|
||||
when(entityLinks.linkToSingleResource((Class<?>) any(), any())).then(invocation -> {
|
||||
|
||||
@Override
|
||||
public Link answer(InvocationOnMock invocation) throws Throwable {
|
||||
Class<?> type = invocation.getArgument(0);
|
||||
Serializable id = invocation.getArgument(1);
|
||||
|
||||
Class<?> type = invocation.getArgumentAt(0, Class.class);
|
||||
Serializable id = invocation.getArgumentAt(1, Serializable.class);
|
||||
|
||||
return new Link("/".concat(type.getName()).concat("/").concat(id.toString()));
|
||||
}
|
||||
return new Link("/".concat(type.getName()).concat("/").concat(id.toString()));
|
||||
});
|
||||
|
||||
KeyValueMappingContext context = new KeyValueMappingContext();
|
||||
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
|
||||
context.getPersistentEntity(Profile.class);
|
||||
context.afterPropertiesSet();
|
||||
|
||||
@@ -125,7 +119,7 @@ public class DefaultSelfLinkProviderUnitTests {
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage(Object.class.getName());
|
||||
exception.expectMessage("No persistent entity found!");
|
||||
exception.expectMessage("Couldn't find PersistentEntity for");
|
||||
|
||||
provider.createSelfLinkFor(new Object());
|
||||
}
|
||||
|
||||
13
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DomainObjectMergerTests.java
Normal file → Executable file
13
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DomainObjectMergerTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.rest.core.support.DomainObjectMerger.NullHandlingPolicy.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -60,8 +59,8 @@ public class DomainObjectMergerTests {
|
||||
|
||||
merger.merge(incoming, existingDomainObject, APPLY_NULLS);
|
||||
|
||||
assertThat(existingDomainObject.getFirstName(), is(incoming.getFirstName()));
|
||||
assertThat(existingDomainObject.getLastName(), is(incoming.getLastName()));
|
||||
assertThat(existingDomainObject.getFirstName()).isEqualTo(incoming.getFirstName());
|
||||
assertThat(existingDomainObject.getLastName()).isEqualTo(incoming.getLastName());
|
||||
}
|
||||
|
||||
@Test // DATAREST-130
|
||||
@@ -72,8 +71,8 @@ public class DomainObjectMergerTests {
|
||||
|
||||
merger.merge(incoming, existingDomainObject, APPLY_NULLS);
|
||||
|
||||
assertThat(existingDomainObject.getFirstName(), is(incoming.getFirstName()));
|
||||
assertThat(existingDomainObject.getLastName(), is(incoming.getLastName()));
|
||||
assertThat(existingDomainObject.getFirstName()).isEqualTo(incoming.getFirstName());
|
||||
assertThat(existingDomainObject.getLastName()).isEqualTo(incoming.getLastName());
|
||||
}
|
||||
|
||||
@Test // DATAREST-327
|
||||
@@ -86,6 +85,6 @@ public class DomainObjectMergerTests {
|
||||
|
||||
merger.merge(new Person("Sam", null), frodo, IGNORE_NULLS);
|
||||
|
||||
assertThat(frodo.getSiblings(), is(not(emptyIterable())));
|
||||
assertThat(frodo.getSiblings()).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DomainObjectMergerUnitTests.java
Normal file → Executable file
22
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/DomainObjectMergerUnitTests.java
Normal file → Executable file
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.rest.core.support.DomainObjectMerger.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -34,16 +34,16 @@ public class DomainObjectMergerUnitTests {
|
||||
@Test // DATAREST-327
|
||||
public void considersEmptyObjectsEmpty() {
|
||||
|
||||
assertThat(isNullOrEmpty(null), is(true));
|
||||
assertThat(isNullOrEmpty(Collections.emptyList()), is(true));
|
||||
assertThat(isNullOrEmpty(new Object[0]), is(true));
|
||||
assertThat(isNullOrEmpty(new String[0]), is(true));
|
||||
assertThat(isNullOrEmpty(new MyIterable()), is(true));
|
||||
assertThat(isNullOrEmpty(Optional.empty())).isTrue();
|
||||
assertThat(isNullOrEmpty(Optional.of(Collections.emptyList()))).isTrue();
|
||||
assertThat(isNullOrEmpty(Optional.of(new Object[0]))).isTrue();
|
||||
assertThat(isNullOrEmpty(Optional.of(new String[0]))).isTrue();
|
||||
assertThat(isNullOrEmpty(Optional.of(new MyIterable()))).isTrue();
|
||||
|
||||
assertThat(isNullOrEmpty(new Object()), is(false));
|
||||
assertThat(isNullOrEmpty(Collections.singleton(new Object())), is(false));
|
||||
assertThat(isNullOrEmpty(new Object[] { "1" }), is(false));
|
||||
assertThat(isNullOrEmpty(new String[] { "1" }), is(false));
|
||||
assertThat(isNullOrEmpty(Optional.of(new Object()))).isFalse();
|
||||
assertThat(isNullOrEmpty(Optional.of(Collections.singleton(new Object())))).isFalse();
|
||||
assertThat(isNullOrEmpty(Optional.of(new Object[] { "1" }))).isFalse();
|
||||
assertThat(isNullOrEmpty(Optional.of(new String[] { "1" }))).isFalse();
|
||||
}
|
||||
|
||||
class MyIterable implements Iterable<Object> {
|
||||
|
||||
30
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/ResourceStringUtilsTests.java
Normal file → Executable file
30
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/ResourceStringUtilsTests.java
Normal file → Executable file
@@ -15,17 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.springframework.data.rest.core.support.ResourceStringUtils;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.runners.Parameterized.Parameters;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
/**
|
||||
* Ensures proper detection and removal of leading slash in strings.
|
||||
@@ -48,23 +46,25 @@ public class ResourceStringUtilsTests {
|
||||
|
||||
@Parameters(name = "{0}")
|
||||
public static Collection<?> parameters() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ "empty string has no text and should remain empty", "", "", false },
|
||||
{ "blank string has no text and should remain as is", " ", " ", false },
|
||||
{ "string made of only a leading slash has no text and should be returned empty", "/", "", false },
|
||||
{ "blank string with only slashes has no text and should be returned as is", " / ", " / ", false },
|
||||
{ "normal string has text and should be returned as such", "hello", "hello", true },
|
||||
{ "normal string with leading slash has text and should be returned without leading slash", "/hello", "hello",
|
||||
true }, });
|
||||
return Arrays
|
||||
.asList(
|
||||
new Object[][] { { "empty string has no text and should remain empty", "", "", false },
|
||||
{ "blank string has no text and should remain as is", " ", " ", false },
|
||||
{ "string made of only a leading slash has no text and should be returned empty", "/", "", false },
|
||||
{ "blank string with only slashes has no text and should be returned as is", " / ", " / ",
|
||||
false },
|
||||
{ "normal string has text and should be returned as such", "hello", "hello", true },
|
||||
{ "normal string with leading slash has text and should be returned without leading slash", "/hello",
|
||||
"hello", true }, });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDetectTextPresence() {
|
||||
assertThat(ResourceStringUtils.hasTextExceptSlash(actual), is(hasText));
|
||||
assertThat(ResourceStringUtils.hasTextExceptSlash(actual)).isEqualTo(hasText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveLeadingSlashIfAny() {
|
||||
assertThat(ResourceStringUtils.removeLeadingSlash(actual), is(expected));
|
||||
assertThat(ResourceStringUtils.removeLeadingSlash(actual)).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
47
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/UnwrappingRepositoryInvokerFactoryUnitTests.java
Normal file → Executable file
47
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/support/UnwrappingRepositoryInvokerFactoryUnitTests.java
Normal file → Executable file
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -25,8 +23,9 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.assertj.core.api.AbstractOptionalAssert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -36,7 +35,6 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.data.repository.support.RepositoryInvoker;
|
||||
import org.springframework.data.repository.support.RepositoryInvokerFactory;
|
||||
import org.springframework.data.rest.core.domain.Profile;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UnwrappingRepositoryInvokerFactory}.
|
||||
@@ -54,8 +52,8 @@ public class UnwrappingRepositoryInvokerFactoryUnitTests {
|
||||
RepositoryInvokerFactory factory;
|
||||
Method method;
|
||||
|
||||
public @Parameter(value = 0) Object source;
|
||||
public @Parameter(value = 1) Matcher<Object> value;
|
||||
public @Parameter(0) Object source;
|
||||
public @Parameter(1) Consumer<AbstractOptionalAssert<?, Object>> value;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@@ -68,31 +66,23 @@ public class UnwrappingRepositoryInvokerFactoryUnitTests {
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
|
||||
return Arrays.asList(new Object[][] { //
|
||||
{ Optional.empty(), is(nullValue()) }, //
|
||||
{ Optional.of(REFERENCE), is(REFERENCE) }, //
|
||||
{ com.google.common.base.Optional.absent(), is(nullValue()) }, //
|
||||
{ com.google.common.base.Optional.of(REFERENCE), is(REFERENCE) } //
|
||||
{ null, $(it -> it.isEmpty()) }, //
|
||||
{ Optional.empty(), $(it -> it.isEmpty()) }, //
|
||||
{ Optional.of(REFERENCE), $(it -> it.hasValue(REFERENCE)) }, //
|
||||
{ com.google.common.base.Optional.absent(), $(it -> it.isEmpty()) }, //
|
||||
{ com.google.common.base.Optional.of(REFERENCE), $(it -> it.hasValue(REFERENCE)) } //
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATAREST-511
|
||||
public void unwrapsValuesForFindOne() {
|
||||
assertFindOneValueForSource(source, value);
|
||||
}
|
||||
|
||||
@Test // DATAREST-511
|
||||
public void unwrapsValuesForQuery() {
|
||||
assertQueryValueForSource(source, value);
|
||||
}
|
||||
|
||||
@Test // DATAREST-724
|
||||
@SuppressWarnings("unchecked")
|
||||
public void usesRegisteredEntityLookup() {
|
||||
|
||||
EntityLookup<Object> lookup = mock(EntityLookup.class);
|
||||
when(lookup.supports(Profile.class)).thenReturn(true);
|
||||
|
||||
when(lookup.supports(Profile.class)).thenReturn(true);
|
||||
when(delegate.getInvokerFor(Profile.class)).thenReturn(invoker);
|
||||
|
||||
factory = new UnwrappingRepositoryInvokerFactory(delegate, Arrays.asList(lookup));
|
||||
@@ -101,16 +91,7 @@ public class UnwrappingRepositoryInvokerFactoryUnitTests {
|
||||
verify(lookup, times(1)).lookupEntity(eq(1L));
|
||||
}
|
||||
|
||||
private void assertFindOneValueForSource(Object source, Matcher<Object> value) {
|
||||
|
||||
when(invoker.invokeFindOne(1L)).thenReturn(source);
|
||||
assertThat(factory.getInvokerFor(Object.class).invokeFindOne(1L), value);
|
||||
}
|
||||
|
||||
private void assertQueryValueForSource(Object source, Matcher<Object> value) {
|
||||
|
||||
when(invoker.invokeQueryMethod(method, new LinkedMultiValueMap<String, Object>(), null, null)).thenReturn(source);
|
||||
assertThat(factory.getInvokerFor(Object.class).invokeQueryMethod(method, new LinkedMultiValueMap<String, Object>(),
|
||||
null, null), value);
|
||||
private static Consumer<AbstractOptionalAssert<?, Object>> $(Consumer<AbstractOptionalAssert<?, Object>> consumer) {
|
||||
return consumer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class FunctionTests {
|
||||
|
||||
@Test
|
||||
public void foo() {
|
||||
|
||||
Foo foo = new Foo();
|
||||
|
||||
foo.apply(new Function<String, Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer apply(String input) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
public Integer apply(Function<String, Integer> function) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java
Normal file → Executable file
18
spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.util;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
@@ -25,7 +24,6 @@ import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ReflectionUtils.MethodCallback;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Methods}.
|
||||
@@ -41,18 +39,12 @@ public class MethodsUnitTests {
|
||||
factory.setTarget(new Sample());
|
||||
factory.setProxyTargetClass(true);
|
||||
|
||||
final Set<Method> methods = new HashSet<Method>();
|
||||
Set<Method> methods = new HashSet<Method>();
|
||||
|
||||
ReflectionUtils.doWithMethods(factory.getProxy().getClass(), new MethodCallback() {
|
||||
ReflectionUtils.doWithMethods(factory.getProxy().getClass(), method -> methods.add(method), Methods.USER_METHODS);
|
||||
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
methods.add(method);
|
||||
}
|
||||
}, Methods.USER_METHODS);
|
||||
|
||||
assertThat(methods, hasSize(1));
|
||||
assertThat(methods, contains(Sample.class.getMethod("method")));
|
||||
assertThat(methods).hasSize(1);
|
||||
assertThat(methods).contains(Sample.class.getMethod("method"));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
Reference in New Issue
Block a user