NewCaseInstancePresenter extends AbstractPresenter<NewCaseInstancePresenter.NewCaseInstanceView> { protected void createCaseInstance(final String caseDefinitionId, final String owner, final List<CaseRoleAssignmentSummary> assignments) { final Optional<CaseDefinitionSummary> cds = getCaseDefinitionSummary(caseDefinitionId); if (cds.isPresent()) { final CaseDefinitionSummary caseDefinition = cds.get(); final List<String> assignmentErrors = caseRolesValidations.validateRolesAssignments(caseDefinition, assignments); if (assignmentErrors.isEmpty() == false) { view.showError(assignmentErrors); return; } caseService.call( (String caseId) -> { view.hide(); notification.fire(new NotificationEvent(translationService.format(Constants.CASE_CREATED_WITH_ID, caseId), NotificationEvent.NotificationType.SUCCESS)); newCaseEvent.fire(new CaseCreatedEvent(caseId)); }, (Message message, Throwable t) -> { view.showError(singletonList(t.getMessage())); return false; } ).startCaseInstance(caseDefinition.getContainerId(), caseDefinition.getId(), owner, assignments); } else { notification.fire(new NotificationEvent(translationService.format(Constants.INVALID_CASE_DEFINITION), NotificationEvent.NotificationType.ERROR)); } }  void show(); @Inject void setNotification(final Event<NotificationEvent> notification); @Inject void setNewCaseEvent(final Event<CaseCreatedEvent> newCaseEvent); @Inject void setCaseService(final Caller<CaseManagementService> caseService);  }
@Test public void testCreateInvalidCaseInstance() { presenter.createCaseInstance(null, anyString(), null); final ArgumentCaptor<NotificationEvent> captor = ArgumentCaptor.forClass(NotificationEvent.class); verify(notificationEvent).fire(captor.capture()); assertEquals(1, captor.getAllValues().size()); assertEquals(NotificationEvent.NotificationType.ERROR, captor.getValue().getType()); }
@Test public void testCreateCaseInstance() { final CaseDefinitionSummary cds = CaseDefinitionSummary.builder().id("id").name("name").containerId("containerId").build(); when(caseManagementService.getCaseDefinitions()).thenReturn(Arrays.asList(cds)); final String owner = "userx"; when(identity.getIdentifier()).thenReturn(owner); when(caseRolesValidations.validateRolesAssignments(any(CaseDefinitionSummary.class), anyList())).thenReturn(EMPTY_LIST); presenter.show(); verify(view).clearCaseDefinitions(); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(view).setCaseDefinitions(captor.capture()); final List list = captor.getValue(); assertEquals(1, list.size()); assertEquals(cds, list.get(0)); verify(view).setOwner(owner); verify(view).show(); presenter.createCaseInstance(cds.getUniqueId(), owner, emptyList()); verify(caseManagementService).startCaseInstance(cds.getContainerId(), cds.getId(), owner, emptyList()); verify(view).hide(); verify(notificationEvent).fire(any(NotificationEvent.class)); verify(caseCreatedEvent).fire(any(CaseCreatedEvent.class)); }