### Question:
TaskResourceAssembler implements ResourceAssembler<Task, Resource<CloudTask>> { @Override public Resource<CloudTask> toResource(Task task) { CloudTask cloudTask = converter.from(task); List<Link> links = new ArrayList<>(); links.add(linkTo(methodOn(TaskControllerImpl.class).getTaskById(cloudTask.getId())).withSelfRel()); if (ASSIGNED != cloudTask.getStatus()) { links.add(linkTo(methodOn(TaskControllerImpl.class).claimTask(cloudTask.getId())).withRel("claim")); } else { links.add(linkTo(methodOn(TaskControllerImpl.class).releaseTask(cloudTask.getId())).withRel("release")); links.add(linkTo(methodOn(TaskControllerImpl.class).completeTask(cloudTask.getId(), null)).withRel("complete")); } if (cloudTask.getProcessInstanceId() != null && !cloudTask.getProcessInstanceId().isEmpty()) { links.add(linkTo(methodOn(ProcessInstanceControllerImpl.class).getProcessInstanceById(cloudTask.getProcessInstanceId())).withRel("processInstance")); } if (cloudTask.getParentTaskId() != null && !cloudTask.getParentTaskId().isEmpty()) { links.add(linkTo(methodOn(TaskControllerImpl.class).getTaskById(cloudTask.getParentTaskId())).withRel("parent")); } links.add(linkTo(HomeControllerImpl.class).withRel("home")); return new Resource<>(cloudTask, links); } TaskResourceAssembler(ToCloudTaskConverter converter); @Override Resource<CloudTask> toResource(Task task);  }### Answer:
@Test public void toResourceShouldReturnResourceWithSelfLinkContainingResourceId() { Task model = new TaskImpl("my-identifier", "myTask", CREATED); given(converter.from(model)).willReturn(new CloudTaskImpl(model)); Resource<CloudTask> resource = resourceAssembler.toResource(model); Link selfResourceLink = resource.getLink("self"); assertThat(selfResourceLink).isNotNull(); assertThat(selfResourceLink.getHref()).contains("my-identifier"); }
@Test public void toResourceShouldReturnResourceWithReleaseAndCompleteLinksWhenStatusIsAssigned() { Task model = new TaskImpl("my-identifier", "myTask", CREATED); given(converter.from(model)).willReturn(new CloudTaskImpl(model)); Resource<CloudTask> resource = resourceAssembler.toResource(model); assertThat(resource.getLink("claim")).isNotNull(); assertThat(resource.getLink("release")).isNull(); assertThat(resource.getLink("complete")).isNull(); }
@Test public void toResourceShouldReturnResourceWithClaimLinkWhenStatusIsNotAssigned() { Task model = new TaskImpl("my-identifier", "myTask", ASSIGNED); given(converter.from(model)).willReturn(new CloudTaskImpl(model)); Resource<CloudTask> resource = resourceAssembler.toResource(model); assertThat(resource.getLink("claim")).isNull(); assertThat(resource.getLink("release")).isNotNull(); assertThat(resource.getLink("complete")).isNotNull(); }
@Test public void toResourceShouldNotReturnResourceWithProcessInstanceLinkWhenNewTaskIsCreated() { Task model = new TaskImpl("my-identifier", "myTask", CREATED); given(converter.from(model)).willReturn(new CloudTaskImpl(model)); Resource<CloudTask> resource = resourceAssembler.toResource(model); assertThat(resource.getLink("processInstance")).isNull(); }
@Test public void toResourceShouldReturnResourceWithProcessInstanceLinkForProcessInstanceTask() { Task model = new TaskImpl("my-identifier", "myTask", CREATED); ((TaskImpl) model).setProcessInstanceId("processInstanceId"); given(converter.from(model)).willReturn(new CloudTaskImpl(model)); Resource<CloudTask> resource = resourceAssembler.toResource(model); assertThat(resource.getLink("processInstance")).isNotNull(); }