Search in sources :

Example 1 with UserToProjectRepository

use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    localUserToProjectRepository = new UserToProjectRepository();
    localUserToProjectRepository.removeRelations("project");
    localUserToProjectRepository.removeRelations("assignedProjects");
}
Also used : UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) Before(org.junit.Before)

Example 2 with UserToProjectRepository

use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.

the class RepositoryFilterTest method prepare.

@Before
public void prepare() {
    boot = new CrnkBoot();
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
    SimpleModule filterModule = new SimpleModule("filter");
    filterModule.addRepositoryFilter(filter);
    boot.addModule(filterModule);
    boot.boot();
    resourceRegistry = boot.getResourceRegistry();
    querySpec = new QuerySpec(User.class);
    querySpec.setPagingSpec(new OffsetLimitPagingSpec());
    queryAdapter = new QuerySpecAdapter(querySpec, resourceRegistry);
    scheduleInfo = resourceRegistry.getEntry(Schedule.class).getResourceInformation();
    RegistryEntry userEntry = resourceRegistry.getEntry(User.class);
    resourceAdapter = userEntry.getResourceRepository(null);
    projectRelationAdapter = userEntry.getRelationshipRepository("assignedProjects", null);
    taskRelationAdapter = userEntry.getRelationshipRepository("assignedTasks", null);
    userInfo = userEntry.getResourceInformation();
    UserRepository resourceRepository = (UserRepository) resourceAdapter.getResourceRepository();
    user1 = new User();
    user1.setId(1L);
    resourceRepository.save(user1);
    user2 = new User();
    user2.setId(2L);
    resourceRepository.save(user2);
    UserToProjectRepository userProjectRepository = (UserToProjectRepository) ((AnnotatedRelationshipRepositoryAdapter<?, ?, ?, ?>) projectRelationAdapter.getRelationshipRepository()).getImplementationObject();
    userProjectRepository.setRelation(user1, 11L, "assignedProjects");
    UserToTaskRepository userTaskRepository = new UserToTaskRepository();
    userTaskRepository.addRelations(user1, Arrays.asList(21L), "assignedTasks");
    userTaskRepository.addRelations(user2, Arrays.asList(22L), "assignedTasks");
    assignedTasksField = resourceRegistry.getEntry(User.class).getResourceInformation().findRelationshipFieldByName("assignedTasks");
    assignedProjectsField = resourceRegistry.getEntry(User.class).getResourceInformation().findRelationshipFieldByName("assignedProjects");
}
Also used : UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) CrnkBoot(io.crnk.core.boot.CrnkBoot) User(io.crnk.core.mock.models.User) UserRepository(io.crnk.core.mock.repository.UserRepository) OffsetLimitPagingSpec(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec) UserToTaskRepository(io.crnk.core.mock.repository.UserToTaskRepository) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) SimpleModule(io.crnk.core.module.SimpleModule) Before(org.junit.Before)

Example 3 with UserToProjectRepository

use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    localUserToProjectRepository = new UserToProjectRepository();
    localUserToProjectRepository.removeRelations("project");
    localUserToProjectRepository.removeRelations("assignedProjects");
}
Also used : UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) Before(org.junit.Before)

Example 4 with UserToProjectRepository

use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method onExistingResourcesShouldAddToManyRelationship.

@Test
public void onExistingResourcesShouldAddToManyRelationship() throws Exception {
    // GIVEN
    Document newUserBody = new Document();
    Resource data = createUser();
    newUserBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/users");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
    // WHEN -- adding a user
    Response taskResponse = resourcePost.handle(taskPath, emptyUserQuery, null, newUserBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
    Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    // WHEN -- adding a project
    Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    /* ------- */
    // GIVEN
    Document newTaskToProjectBody = new Document();
    data = createProject();
    data.setId(projectId.toString());
    newTaskToProjectBody.setData(Nullable.of((Object) Collections.singletonList(data)));
    JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
    RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
    // WHEN -- adding a relation between user and project
    Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
    Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
    assertThat(project.getId()).isEqualTo(projectId);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 5 with UserToProjectRepository

use of io.crnk.core.mock.repository.UserToProjectRepository in project crnk-framework by crnk-project.

the class RelationshipsResourcePostTest method onExistingResourcesShouldAddToManyRelationship.

@Test
public void onExistingResourcesShouldAddToManyRelationship() throws Exception {
    // GIVEN
    Document newUserBody = new Document();
    Resource data = createUser();
    newUserBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/users");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
    // WHEN -- adding a user
    Response taskResponse = resourcePost.handle(taskPath, emptyUserQuery, null, newUserBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
    Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    data.setType("projects");
    JsonPath projectPath = pathBuilder.build("/projects");
    // WHEN -- adding a project
    Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    /* ------- */
    // GIVEN
    Document newTaskToProjectBody = new Document();
    data = new Resource();
    newTaskToProjectBody.setData(Nullable.of((Object) Collections.singletonList(data)));
    data.setType("projects");
    data.setId(projectId.toString());
    JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN -- adding a relation between user and project
    Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
    Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
    assertThat(project.getId()).isEqualTo(projectId);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

UserToProjectRepository (io.crnk.core.mock.repository.UserToProjectRepository)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)4 Response (io.crnk.core.engine.dispatcher.Response)3 Document (io.crnk.core.engine.document.Document)3 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)3 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)3 Project (io.crnk.core.mock.models.Project)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Resource (io.crnk.core.engine.document.Resource)2 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)2 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)2 JsonApiResource (io.crnk.core.resource.annotations.JsonApiResource)2 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 RelationshipsResourceDelete (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceDelete)1 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1 User (io.crnk.core.mock.models.User)1