Skip to content

Commit

Permalink
Added a few tests and supporting models
Browse files Browse the repository at this point in the history
Working prototype

Added inheritance projection mockup

Added subclass + superclass fields in graphQL test

Beautified tests.  Removed hack/mock in model builder to build to specific classes

Cleaned up name resolution

Code cleanup complete

Build passes with core changes

Refactored tests to use common graphQL utils.  Added error test for interface creation

Fixed build issues

Added tests for invalid filter predicates

Fixed GraphQL errors that return 500 error.  Added more tests to inheritanceIT

Added invalid sort test for JSON-API

Updated comments

Fixing build and codacy issues

Fixed NPE in WebApplicationException handling

Added more unit tests

Fixed broken pom
  • Loading branch information
Aaron Klish committed May 19, 2020
1 parent ae30c5b commit e00e950
Show file tree
Hide file tree
Showing 34 changed files with 1,010 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package com.yahoo.elide.core;

import com.yahoo.elide.core.exceptions.InvalidEntityBodyException;
import com.yahoo.elide.core.filter.InPredicate;
import com.yahoo.elide.core.filter.expression.AndFilterExpression;
import com.yahoo.elide.core.filter.expression.FilterExpression;
Expand Down Expand Up @@ -96,7 +97,8 @@ default <T> T createNewObject(Class<T> entityClass) {
try {
obj = entityClass.newInstance();
} catch (java.lang.InstantiationException | IllegalAccessException e) {
obj = null;
throw new InvalidEntityBodyException("Cannot create an entity model of type: "
+ entityClass.getSimpleName());
}
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public boolean updateAttribute(String fieldName, Object newVal) {
transaction.setAttribute(obj, Attribute.builder()
.name(fieldName)
.type(fieldClass)
.parentType(obj.getClass())
.argument(Argument.builder()
.name("_UNUSED_")
.value(newVal).build())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ private Set<Attribute> getSparseAttributes(Class<?> entityClass) {

return Sets.intersection(allAttributes, sparseFieldsForEntity).stream()
.map(attributeName -> Attribute.builder()
.parentType(entityClass)
.name(attributeName)
.type(dictionary.getType(entityClass, attributeName))
.build())
Expand Down
15 changes: 15 additions & 0 deletions elide-core/src/main/java/com/yahoo/elide/request/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@ public class Attribute {
@ToString.Exclude
private String alias;

@ToString.Exclude
//If null, the parentType is the same as the entity projection to which this attribute belongs.
//If not null, this represents the model type where this attribute can be found.
private Class<?> parentType;

@Singular
@ToString.Exclude
private Set<Argument> arguments;

private Attribute(@NonNull Class<?> type, @NonNull String name, String alias, Class<?> parentType,
Set<Argument> arguments) {
this.type = type;
this.parentType = parentType;
this.name = name;
this.alias = alias == null ? name : alias;
this.arguments = arguments;
}

private Attribute(@NonNull Class<?> type, @NonNull String name, String alias, Set<Argument> arguments) {
this.type = type;
this.parentType = null;
this.name = name;
this.alias = alias == null ? name : alias;
this.arguments = arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.yahoo.elide.request;

import com.yahoo.elide.core.exceptions.BadRequestException;
import com.yahoo.elide.core.filter.expression.FilterExpression;
import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
Expand All @@ -18,8 +19,6 @@
import java.util.Optional;
import java.util.Set;

import javax.ws.rs.BadRequestException;

/**
* Represents a client data request against a subgraph of the entity relationship graph.
*/
Expand Down Expand Up @@ -153,6 +152,7 @@ public EntityProjectionBuilder attributes(Set<Attribute> attributes) {
public EntityProjectionBuilder relationship(String name, EntityProjection projection) {
return relationship(Relationship.builder()
.alias(name)
.parentType(projection.type)
.name(name)
.projection(projection)
.build());
Expand All @@ -177,6 +177,7 @@ public EntityProjectionBuilder relationship(Relationship relationship) {
if (existing != null) {
relationships.remove(existing);
relationships.add(Relationship.builder()
.parentType(relationship.getParentType())
.name(relationshipName)
.alias(relationshipAlias)
.projection(existing.getProjection().merge(relationship.getProjection()))
Expand Down Expand Up @@ -213,6 +214,7 @@ public EntityProjectionBuilder attribute(Attribute attribute) {
attributes.remove(existing);
attributes.add(Attribute.builder()
.type(attribute.getType())
.parentType(attribute.getParentType())
.name(attributeName)
.alias(attributeAlias)
.arguments(Sets.union(attribute.getArguments(), existing.getArguments()))
Expand Down Expand Up @@ -242,6 +244,19 @@ public Attribute getAttributeByAlias(String attributeAlias) {
.orElse(null);
}

/**
* Get an relationship by alias.
*
* @param relationshipAlias alias to refer to a relationship field
* @return found attribute or null
*/
public Relationship getRelationshipByAlias(String relationshipAlias) {
return relationships.stream()
.filter(relationship -> relationship.getAlias().equals(relationshipAlias))
.findAny()
.orElse(null);
}

/**
* Check whether a field alias is ambiguous.
*
Expand Down
27 changes: 20 additions & 7 deletions elide-core/src/main/java/com/yahoo/elide/request/Relationship.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,40 @@
@Builder
public class Relationship {

public RelationshipBuilder copyOf() {
return Relationship.builder()
.alias(alias)
.name(name)
.projection(projection);
}

@NonNull
private String name;

private String alias;

//If null, the parentType is the same as the entity projection to which this relationship belongs.
//If not null, this represents the model type where this relationship can be found.
private Class<?> parentType;

@NonNull
private EntityProjection projection;

private Relationship(@NonNull String name, String alias, @NonNull EntityProjection projection) {
this.name = name;
this.parentType = null;
this.alias = alias == null ? name : alias;
this.projection = projection;
}

private Relationship(@NonNull String name, String alias, Class<?> parentType,
@NonNull EntityProjection projection) {
this.name = name;
this.parentType = parentType;
this.alias = alias == null ? name : alias;
this.projection = projection;
}

public RelationshipBuilder copyOf() {
return Relationship.builder()
.alias(alias)
.name(name)
.projection(projection);
}

public Relationship merge(Relationship toMerge) {
return Relationship.builder()
.name(name)
Expand Down
Loading

0 comments on commit e00e950

Please sign in to comment.