NewJobPresenter { public void createJob(String jobName, Date dueDate, String jobType, String numberOfRetries, List<RequestParameterSummary> parameters) { if (validateForm(jobName, jobType, numberOfRetries)) { Map<String, String> jobCtxMap = new HashMap<String, String>(); if (parameters != null) { for (RequestParameterSummary param : parameters) { if (!(constants.ClickToEdit().equals(param.getKey()) || constants.ClickToEdit().equals(param.getValue()))) { jobCtxMap.put(param.getKey(), param.getValue()); } } } jobCtxMap.put(RequestDataSetConstants.COLUMN_RETRIES, numberOfRetries); jobCtxMap.put(RequestDataSetConstants.COLUMN_BUSINESSKEY, jobName); executorServices.call( (Long requestId) -> { displayNotification(constants.RequestScheduled(requestId)); requestCreatedEvent.fire(new RequestChangedEvent(requestId)); view.hide(); }, (Message message, Throwable throwable) -> { view.showBasicPane(); if (isInvalidCommandTypeError(throwable)) { view.showInvalidTypeErrorMessage(); } else { view.showInlineNotification(throwable.getCause().getMessage()); } return false; }).scheduleRequest(serverTemplateId, jobType, dueDate, jobCtxMap); } else { view.showBasicPane(); } }  @PostConstruct void init(); void openNewJobDialog(String serverTemplateId); void createJob(String jobName,
                          Date dueDate,
                          String jobType,
                          String numberOfRetries,
                          List<RequestParameterSummary> parameters); @Inject void setNotification(final Event<NotificationEvent> notification); @Inject void setRequestChangedEvent(final Event<RequestChangedEvent> requestChangedEvent); @Inject void setExecutorService(final Caller<ExecutorService> executorService);  }
@Test public void testScheduleJobSuccess_basicParametersOnly() { final Date dueDate = new Date(); final Long firstJobId = 1L; final Long secondJobId = 2L; when(executorServicesMock.scheduleRequest(eq(serverTemplateId), eq(JOB_TYPE), eq(dueDate), any())).thenReturn(firstJobId, secondJobId); presenter.createJob("firstJob", dueDate, JOB_TYPE, JOB_RETRIES, emptyList()); presenter.createJob("secondJob", dueDate, JOB_TYPE, JOB_RETRIES, null); verify(view).show(); verify(view, times(2)).cleanErrorMessages(); final ArgumentCaptor<NotificationEvent> captor = ArgumentCaptor.forClass(NotificationEvent.class); verify(notificationEvent, times(2)).fire(captor.capture()); assertEquals(2, captor.getAllValues().size()); assertEquals(NotificationEvent.NotificationType.DEFAULT, captor.getAllValues().get(0).getType()); assertEquals(NotificationEvent.NotificationType.DEFAULT, captor.getAllValues().get(1).getType()); assertEquals(Constants.INSTANCE.RequestScheduled(firstJobId), captor.getAllValues().get(0).getNotification()); assertEquals(Constants.INSTANCE.RequestScheduled(secondJobId), captor.getAllValues().get(1).getNotification()); verify(requestChangedEvent, times(2)).fire(any(RequestChangedEvent.class)); verify(view, times(2)).hide(); verifyNoMoreInteractions(view); }
@Test public void testScheduleJobSuccess_withAdvancedParameters() { final String editMessage = "ClickToEdit"; final String paramKey = "paramKey"; final String paramValue = "paramValue"; final Date dueOnDate = new Date(); final SoftAssertions softly = new SoftAssertions(); doAnswer(invocation -> { softly.assertThat(invocation.getArguments()[0]).isEqualTo(serverTemplateId); softly.assertThat(invocation.getArguments()[1]).isEqualTo(JOB_TYPE); softly.assertThat(invocation.getArguments()[2]).isEqualTo(dueOnDate); final HashMap<String, String> ctxValues = (HashMap) invocation.getArguments()[3]; softly.assertThat(ctxValues).hasSize(3); softly.assertThat(ctxValues.get(RequestDataSetConstants.COLUMN_BUSINESSKEY).equals(JOB_NAME)); softly.assertThat(ctxValues.get(RequestDataSetConstants.COLUMN_RETRIES).equals(JOB_RETRIES)); softly.assertThat(ctxValues.get(paramKey).equals(paramValue)); return null; }).when(executorServicesMock).scheduleRequest(anyString(), anyString(), any(Date.class), any(Map.class)); presenter.createJob(JOB_NAME, dueOnDate, JOB_TYPE, JOB_RETRIES, Arrays.asList(new RequestParameterSummary(paramKey, paramValue), new RequestParameterSummary("paramKeyOnly", editMessage), new RequestParameterSummary(editMessage, "paramValueOnly"), new RequestParameterSummary(editMessage, editMessage))); softly.assertAll(); }
@Test public void testScheduleJobFailure_unknownError() { final String genericErrorMessage = "Unknown error"; doThrow(new RuntimeException(new RuntimeException(genericErrorMessage))).when(executorServicesMock).scheduleRequest(anyString(), anyString(), any(Date.class), any(Map.class)); presenter.createJob(JOB_NAME, new Date(), JOB_TYPE, JOB_RETRIES, emptyList()); verify(view).show(); verify(view).cleanErrorMessages(); verify(view).showBasicPane(); verify(view).showInlineNotification(eq(genericErrorMessage)); verifyNoMoreInteractions(view); }
@Test public void testScheduleJobFailure_invalidClassError() { doAnswer(invocation -> { throw new Exception("Invalid command type"); }).when(executorServicesMock).scheduleRequest(anyString(), anyString(), any(Date.class), any(Map.class)); presenter.createJob(JOB_NAME, new Date(), JOB_TYPE, JOB_RETRIES, emptyList()); verify(view).show(); verify(view).cleanErrorMessages(); verify(view).showBasicPane(); verify(view).showInvalidTypeErrorMessage(); verifyNoMoreInteractions(view); }
@Test public void testScheduleJobFailure_emptyNameError() { presenter.createJob("", new Date(), JOB_TYPE, JOB_RETRIES, emptyList()); verify(view).show(); verify(view).cleanErrorMessages(); verify(view).showEmptyNameErrorMessage(); verifyZeroInteractions(executorServicesMock); verify(view).showBasicPane(); verifyNoMoreInteractions(view); }
@Test public void testScheduleJobFailure_emptyTypeError() { presenter.createJob(JOB_NAME, new Date(), "", JOB_RETRIES, emptyList()); verify(view).show(); verify(view).cleanErrorMessages(); verify(view).showEmptyTypeErrorMessage(); verifyZeroInteractions(executorServicesMock); verify(view).showBasicPane(); verifyNoMoreInteractions(view); }
@Test public void testScheduleJobFailure_emptyRetriesError() { presenter.createJob(JOB_NAME, new Date(), JOB_TYPE, "", emptyList()); verify(view).show(); verify(view).cleanErrorMessages(); verify(view).showEmptyRetriesErrorMessage(); verifyZeroInteractions(executorServicesMock); verify(view).showBasicPane(); verifyNoMoreInteractions(view); }