Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#225 - Add system property for annotation JsonApiLookupIncludeAuto #226

Merged
merged 7 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#225 - Correct type for FieldResourceGet includes
  • Loading branch information
mitch2na committed Nov 19, 2016
commit 711adfc8eebad0ea7a17435d8f335470cf41cf8d
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.katharsis.dispatcher.controller.resource;

import io.katharsis.dispatcher.controller.HttpMethod;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.queryspec.internal.QueryAdapter;
import io.katharsis.repository.RepositoryMethodParameterProvider;
import io.katharsis.request.dto.RequestBody;
import io.katharsis.request.path.FieldPath;
import io.katharsis.request.path.JsonPath;
import io.katharsis.request.path.PathIds;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.exception.ResourceFieldNotFoundException;
import io.katharsis.resource.field.ResourceField;
import io.katharsis.resource.include.IncludeLookupSetter;
Expand All @@ -26,7 +26,7 @@
public class FieldResourceGet extends ResourceIncludeField {

public FieldResourceGet(ResourceRegistry resourceRegistry, TypeParser typeParser, IncludeLookupSetter fieldSetter) {
super(resourceRegistry,typeParser,fieldSetter);
super(resourceRegistry, typeParser, fieldSetter);
}

@Override
Expand All @@ -38,7 +38,7 @@ public boolean isAcceptable(JsonPath jsonPath, String requestType) {

@Override
public BaseResponseContext handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider
parameterProvider, RequestBody requestBody) {
parameterProvider, RequestBody requestBody) {
String resourceName = jsonPath.getResourceName();
PathIds resourceIds = jsonPath.getIds();

Expand All @@ -54,19 +54,21 @@ public BaseResponseContext handle(JsonPath jsonPath, QueryAdapter queryAdapter,
Class<?> relationshipFieldClass = Generics.getResourceClass(relationshipField.getGenericType(), baseRelationshipFieldClass);

RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry
.getRelationshipRepositoryForClass(relationshipFieldClass, parameterProvider);
.getRelationshipRepositoryForClass(relationshipFieldClass, parameterProvider);
BaseResponseContext target;
if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
@SuppressWarnings("unchecked")
JsonApiResponse response = relationshipRepositoryForClass
.findManyTargets(castedResourceId, elementName, queryAdapter);
includeFieldSetter.setIncludedElements(resourceName, response, queryAdapter, parameterProvider);
.findManyTargets(castedResourceId, elementName, queryAdapter);
includeFieldSetter.setIncludedElements(registryEntry.getResourceInformation().findRelationshipFieldByName(elementName).getElementType().getAnnotation(JsonApiResource.class).type(), response, queryAdapter, parameterProvider);
target = new CollectionResponseContext(response, jsonPath, queryAdapter);
} else {
} else

{
@SuppressWarnings("unchecked")
JsonApiResponse response = relationshipRepositoryForClass
.findOneTarget(castedResourceId, elementName, queryAdapter);
includeFieldSetter.setIncludedElements(resourceName, response, queryAdapter, parameterProvider);
.findOneTarget(castedResourceId, elementName, queryAdapter);
includeFieldSetter.setIncludedElements(registryEntry.getResourceInformation().findRelationshipFieldByName(elementName).getElementType().getAnnotation(JsonApiResource.class).type(), response, queryAdapter, parameterProvider);
target = new ResourceResponseContext(response, jsonPath, queryAdapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import io.katharsis.jackson.JsonApiModuleBuilder;
import io.katharsis.locator.SampleJsonServiceLocator;
import io.katharsis.module.ModuleRegistry;
import io.katharsis.queryParams.DefaultQueryParamsParser;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.queryParams.QueryParamsBuilder;
import io.katharsis.request.path.PathBuilder;
import io.katharsis.resource.field.ResourceFieldNameTransformer;
import io.katharsis.resource.include.IncludeLookupSetter;
Expand All @@ -24,6 +26,11 @@
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public abstract class BaseControllerTest {
protected static final QueryParams REQUEST_PARAMS = new QueryParams();

Expand All @@ -32,9 +39,10 @@ public abstract class BaseControllerTest {
protected ResourceRegistry resourceRegistry;
protected TypeParser typeParser;
protected IncludeLookupSetter includeFieldSetter;
protected QueryParamsBuilder queryParamsBuilder = new QueryParamsBuilder(new DefaultQueryParamsParser());

@Rule
public ExpectedException expectedException = ExpectedException.none();
protected ExpectedException expectedException = ExpectedException.none();

@Before
public void prepare() {
Expand All @@ -53,4 +61,8 @@ public void prepare() {
TaskToProjectRepository.clear();
ProjectToTaskRepository.clear();
}

protected void addParams(Map<String, Set<String>> params, String key, String value) {
params.put(key, new HashSet<>(Arrays.asList(value)));
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package io.katharsis.dispatcher.controller.resource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import org.junit.Assert;
import org.junit.Test;

import io.katharsis.dispatcher.controller.BaseControllerTest;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.queryspec.internal.QueryAdapter;
import io.katharsis.queryspec.internal.QueryParamsAdapter;
import io.katharsis.queryspec.internal.QuerySpecAdapter;
import io.katharsis.request.path.JsonPath;
import io.katharsis.request.path.ResourcePath;
import io.katharsis.resource.include.IncludeLookupSetter;
import io.katharsis.resource.mock.models.Project;
import io.katharsis.resource.mock.models.Task;
import io.katharsis.resource.mock.models.User;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.resource.registry.repository.adapter.RelationshipRepositoryAdapter;
import io.katharsis.resource.registry.repository.adapter.ResourceRepositoryAdapter;
import io.katharsis.response.BaseResponseContext;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class FieldResourceGetTest extends BaseControllerTest {
private static final String REQUEST_TYPE = "GET";
Expand Down Expand Up @@ -93,4 +103,54 @@ public void onGivenRequestFieldResourcesGetShouldHandleIt() throws Exception {
// THEN
Assert.assertNotNull(response);
}

@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {

// get repositories
ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);

RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class)
.getRelationshipRepositoryForClass(Project.class, null);
RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class)
.getRelationshipRepositoryForClass(Task.class, null);
// setup test data
User user = new User();
user.setId(1L);
userRepo.create(user, null);
Project project = new Project();
project.setId(2L);
projectRepo.create(project, null);
Task task = new Task();
task.setId(3L);
taskRepo.create(task, null);
relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), "assignedProjects", null);
relRepositoryProjectToTask.setRelation(project, task.getId(), "includedTask", null);

Map<String, Set<String>> params = new HashMap<String, Set<String>>();
addParams(params, "include[projects]", "includedTask");
QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
QueryAdapter queryAdapter = new QueryParamsAdapter(Project.class, queryParams, resourceRegistry);
JsonPath jsonPath = pathBuilder.buildPath("/users/1/assignedProjects");
FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, includeFieldSetter);

BaseResponseContext response = sut.handle(jsonPath, queryAdapter, null, null);

// THEN
Assert.assertNotNull(response);
Assert.assertNotNull(response.getResponse().getEntity());
Assert.assertEquals(LinkedList.class, response.getResponse().getEntity().getClass());
LinkedList entityList = ((LinkedList) response.getResponse().getEntity());
Assert.assertTrue(entityList.size() > 0);
Assert.assertEquals(Project.class, entityList.get(0).getClass());
Project returnedProject = (Project) entityList.get(0);
Assert.assertEquals(project.getId(), returnedProject.getId());
Assert.assertNotNull(returnedProject.getIncludedTask());
Assert.assertEquals(task.getId(), returnedProject.getIncludedTask().getId());
}


}