Search in sources :

Example 6 with Task

use of io.crnk.test.mock.models.Task in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testAddSetRemoveRelations.

@Test
public void testAddSetRemoveRelations() {
    Project project0 = new Project();
    project0.setId(1L);
    project0.setName("project0");
    projectRepo.create(project0);
    Project project1 = new Project();
    project1.setId(2L);
    project1.setName("project1");
    projectRepo.create(project1);
    Task task = new Task();
    task.setId(3L);
    task.setName("test");
    taskRepo.create(task);
    relRepo.addRelations(task, Arrays.asList(project0.getId(), project1.getId()), "projects");
    List<Project> relProjects = relRepo.findManyTargets(task.getId(), "projects", new QuerySpec(Task.class));
    Assert.assertEquals(2, relProjects.size());
    relRepo.setRelations(task, Arrays.asList(project1.getId()), "projects");
    relProjects = relRepo.findManyTargets(task.getId(), "projects", new QuerySpec(Task.class));
    Assert.assertEquals(1, relProjects.size());
    Assert.assertEquals(project1.getId(), relProjects.get(0).getId());
// TODO HTTP DELETE method with payload not supported? at least in
// Jersey
/*
		relRepo.removeRelations(task, Arrays.asList(project1.getId()),
				"projects");
		relProjects = relRepo.findManyTargets(task.getId(), "projects", new QuerySpec(Task.class));
		Assert.assertEquals(0, relProjects.size());
		*/
}
Also used : Project(io.crnk.test.mock.models.Project) Task(io.crnk.test.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 7 with Task

use of io.crnk.test.mock.models.Task in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testSaveRelationWithCreate.

@Test
public void testSaveRelationWithCreate() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("schedule");
    scheduleRepo.create(schedule);
    Task task = new Task();
    task.setId(2L);
    task.setName("test");
    task.setSchedule(schedule);
    taskRepo.create(task);
    // check relationship available
    Task savedTask = taskRepo.findOne(task.getId(), new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask.getSchedule());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 8 with Task

use of io.crnk.test.mock.models.Task in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testSetRelation.

@Test
public void testSetRelation() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("schedule");
    scheduleRepo.create(schedule);
    Task task = new Task();
    task.setId(2L);
    task.setName("test");
    taskRepo.create(task);
    relRepo.setRelation(task, schedule.getId(), "schedule");
    Schedule relSchedule = taskScheduleRepo.findOneTarget(task.getId(), "schedule", new QuerySpec(Schedule.class));
    Assert.assertNotNull(relSchedule);
    Assert.assertEquals(schedule.getId(), relSchedule.getId());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 9 with Task

use of io.crnk.test.mock.models.Task in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testNullNonLazyRelationWithSave.

@Test
public void testNullNonLazyRelationWithSave() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("schedule");
    scheduleRepo.create(schedule);
    Task task = new Task();
    task.setId(2L);
    task.setName("test");
    task.setSchedule(schedule);
    taskRepo.create(task);
    Task savedTask = taskRepo.findOne(task.getId(), new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask.getSchedule());
    // null
    savedTask.setSchedule(null);
    taskRepo.save(savedTask);
    // relation must be null
    Task updatedTask = taskRepo.findOne(task.getId(), new QuerySpec(Task.class));
    Assert.assertNull(updatedTask.getSchedule());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 10 with Task

use of io.crnk.test.mock.models.Task in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testUpdate.

public void testUpdate(boolean pushAlways) {
    final List<String> methods = new ArrayList<>();
    final List<String> paths = new ArrayList<>();
    final Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            methods.add(request.method());
            paths.add(request.url().encodedPath());
            return chain.proceed(request);
        }
    };
    HttpAdapter httpAdapter = client.getHttpAdapter();
    if (httpAdapter instanceof OkHttpAdapter) {
        ((OkHttpAdapter) httpAdapter).addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(Builder builder) {
                builder.addInterceptor(interceptor);
            }
        });
    }
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    Task savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    // perform update
    task.setName("updatedName");
    taskRepo.save(task);
    // check updated
    savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    Assert.assertEquals("updatedName", task.getName());
    if (httpAdapter instanceof OkHttpAdapter) {
        // check HTTP handling
        Assert.assertEquals(4, methods.size());
        Assert.assertEquals(4, paths.size());
        Assert.assertEquals("POST", methods.get(0));
        Assert.assertEquals("GET", methods.get(1));
        if (pushAlways) {
            Assert.assertEquals("POST", methods.get(2));
            Assert.assertEquals("/tasks", paths.get(2));
        } else {
            Assert.assertEquals("PATCH", methods.get(2));
            Assert.assertEquals("/tasks/1", paths.get(2));
        }
        Assert.assertEquals("GET", methods.get(3));
        Assert.assertEquals("/tasks", paths.get(0));
        Assert.assertEquals("/tasks/1", paths.get(1));
        Assert.assertEquals("/tasks/1", paths.get(3));
    }
}
Also used : Task(io.crnk.test.mock.models.Task) HttpAdapter(io.crnk.client.http.HttpAdapter) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) Builder(okhttp3.OkHttpClient.Builder) ArrayList(java.util.ArrayList) Request(okhttp3.Request) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor)

Aggregations

Task (io.crnk.test.mock.models.Task)53 Test (org.junit.Test)42 QuerySpec (io.crnk.core.queryspec.QuerySpec)27 Project (io.crnk.test.mock.models.Project)11 Schedule (io.crnk.test.mock.models.Schedule)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)6 ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Resource (io.crnk.core.engine.document.Resource)4 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)4 Span (org.springframework.cloud.sleuth.Span)4 SortSpec (io.crnk.core.queryspec.SortSpec)3 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)3 TaskRepository (io.crnk.test.mock.repository.TaskRepository)3 Interceptor (okhttp3.Interceptor)3 Before (org.junit.Before)3 OkHttpAdapter (io.crnk.client.http.okhttp.OkHttpAdapter)2 OkHttpAdapterListener (io.crnk.client.http.okhttp.OkHttpAdapterListener)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)2