use of org.mockito.Mock in project che by eclipse.
the class TestServiceClientTest method initMocks.
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
testServiceClient = spy(new TestServiceClient(appContext, asyncRequestFactory, dtoUnmarshallerFactory, dtoFactory, commandManager, execAgentCommandManager, promiseProvider, macroProcessor, commandConsoleFactory, processesPanelPresenter));
doReturn(new PromiseMocker<TestResult>().getPromise()).when(testServiceClient).sendTests(anyString(), anyString(), anyMapOf(String.class, String.class));
doAnswer(new FunctionAnswer<Executor.ExecutorBody<TestResult>, Promise<TestResult>>(executorBody -> {
ExecutorPromiseMocker<TestResult> mocker = new ExecutorPromiseMocker<TestResult>(executorBody, (testResult, thisMocker) -> {
thisMocker.applyOnThenOperation(testResult);
return null;
}, (promiseError, thisMocker) -> {
thisMocker.applyOnCatchErrorOperation(promiseError);
return null;
});
executorBody.apply(mocker.getResolveFunction(), mocker.getRejectFunction());
return mocker.getPromise();
})).when(testServiceClient).promiseFromExecutorBody(Matchers.<Executor.ExecutorBody<TestResult>>any());
doAnswer(new FunctionAnswer<Throwable, PromiseError>(throwable -> {
PromiseError promiseError = mock(PromiseError.class);
when(promiseError.getCause()).thenReturn(throwable);
return promiseError;
})).when(testServiceClient).promiseFromThrowable(any(Throwable.class));
when(appContext.getDevMachine()).thenReturn(devMachine);
when(machine.getId()).thenReturn("DevMachineId");
doAnswer(new FunctionAnswer<String, Promise<String>>(commandLine -> {
String processedCommandLine = commandLine.replace("${current.project.path}", rootOfProjects + "/" + projectPath);
return new PromiseMocker<String>().applyOnThenOperation(processedCommandLine).getPromise();
})).when(macroProcessor).expandMacros(anyString());
when(commandConsoleFactory.create(any(CommandImpl.class), any(Machine.class))).then(createCall -> {
CommandOutputConsole commandOutputConsole = mock(CommandOutputConsole.class);
when(commandOutputConsole.getProcessStartedOperation()).thenReturn(processStartedEvent -> {
consoleEvents.add(processStartedEvent);
});
when(commandOutputConsole.getProcessDiedOperation()).thenReturn(processDiedEvent -> {
consoleEvents.add(processDiedEvent);
});
when(commandOutputConsole.getStdErrOperation()).thenReturn(processStdErrEvent -> {
consoleEvents.add(processStdErrEvent);
});
when(commandOutputConsole.getStdOutOperation()).thenReturn(processStdOutEvent -> {
consoleEvents.add(processStdOutEvent);
});
return commandOutputConsole;
});
consoleEvents.clear();
when(execAgentCommandManager.startProcess(anyString(), any(Command.class))).then(startProcessCall -> {
@SuppressWarnings("unchecked") ExecAgentPromise<ProcessStartResponseDto> execAgentPromise = (ExecAgentPromise<ProcessStartResponseDto>) mock(ExecAgentPromise.class);
class ProcessEventForward<DtoType> extends FunctionAnswer<Operation<DtoType>, ExecAgentPromise<ProcessStartResponseDto>> {
public ProcessEventForward(Class<DtoType> dtoClass) {
super(new java.util.function.Function<Operation<DtoType>, ExecAgentPromise<ProcessStartResponseDto>>() {
@Override
public ExecAgentPromise<ProcessStartResponseDto> apply(Operation<DtoType> op) {
operationsOnProcessEvents.put(dtoClass, op);
return execAgentPromise;
}
});
}
}
when(execAgentPromise.then(any())).then(new ProcessEventForward<>(ProcessStartResponseDto.class));
when(execAgentPromise.thenIfProcessStartedEvent(any())).then(new ProcessEventForward<>(ProcessStartedEventDto.class));
when(execAgentPromise.thenIfProcessDiedEvent(any())).then(new ProcessEventForward<>(ProcessDiedEventDto.class));
when(execAgentPromise.thenIfProcessStdErrEvent(any())).then(new ProcessEventForward<>(ProcessStdErrEventDto.class));
when(execAgentPromise.thenIfProcessStdOutEvent(any())).then(new ProcessEventForward<>(ProcessStdOutEventDto.class));
return execAgentPromise;
});
operationsOnProcessEvents.clear();
}
use of org.mockito.Mock in project sonarlint-core by SonarSource.
the class NotificationTimerTaskTest method testLimit24h.
@Test
public void testLimit24h() {
when(notificationTime.get()).thenReturn(ZonedDateTime.now().minusDays(30));
// return one notification for our project
SonarQubeNotification notif = mock(SonarQubeNotification.class);
when(notif.projectKey()).thenReturn("myproject");
when(notificationChecker.request(anyMap())).thenReturn(Collections.singletonList(notif));
// execute with one project
NotificationConfiguration project = createProject("myproject");
timerTask.setProjects(Collections.singleton(project));
timerTask.run();
// verify checker used once and notification was returned through the listener
verify(notificationCheckerFactory, times(1)).create(any(ServerConfiguration.class));
verify(notificationChecker).request(ArgumentMatchers.argThat(map -> {
ZonedDateTime time = map.values().iterator().next();
return time.isAfter(ZonedDateTime.now().minusHours(25)) && time.isBefore(ZonedDateTime.now().minusHours(23));
}));
verify(listener).handle(notif);
}
use of org.mockito.Mock in project ORCID-Source by ORCID.
the class NotificationManagerTest method testFindPermissionsByOrcidAndClient.
/**
* Test independent of spring context, sets up NotificationManager with
* mocked notifiation dao and notification adapter
*/
@Test
public void testFindPermissionsByOrcidAndClient() {
List<Notification> notificationPermissions = IntStream.range(0, 10).mapToObj(i -> new NotificationPermission()).collect(Collectors.toList());
NotificationDao notificationDao = mock(NotificationDaoImpl.class);
JpaJaxbNotificationAdapter adapter = mock(JpaJaxbNotificationAdapterImpl.class);
when(notificationDao.findPermissionsByOrcidAndClient(anyString(), anyString(), anyInt(), anyInt())).thenReturn(new ArrayList<NotificationEntity>());
when(adapter.toNotification(Matchers.<ArrayList<NotificationEntity>>any())).thenReturn(notificationPermissions);
NotificationManager notificationManager = new NotificationManagerImpl();
ReflectionTestUtils.setField(notificationManager, "notificationAdapter", adapter);
ReflectionTestUtils.setField(notificationManager, "notificationDao", notificationDao);
NotificationPermissions notifications = notificationManager.findPermissionsByOrcidAndClient("some-orcid", "some-client", 0, OrcidApiConstants.MAX_NOTIFICATIONS_AVAILABLE);
assertEquals(notificationPermissions.size(), notifications.getNotifications().size());
}
use of org.mockito.Mock in project ORCID-Source by ORCID.
the class NotificationManagerTest method testFindPermissionsByOrcidAndClient.
/**
* Test independent of spring context, sets up NotificationManager with
* mocked notifiation dao and notification adapter
*/
@Test
public void testFindPermissionsByOrcidAndClient() {
List<Notification> notificationPermissions = IntStream.range(0, 10).mapToObj(i -> new NotificationPermission()).collect(Collectors.toList());
NotificationDao notificationDao = mock(NotificationDaoImpl.class);
JpaJaxbNotificationAdapter adapter = mock(JpaJaxbNotificationAdapterImpl.class);
when(notificationDao.findPermissionsByOrcidAndClient(anyString(), anyString(), anyInt(), anyInt())).thenReturn(new ArrayList<NotificationEntity>());
when(adapter.toNotification(Matchers.<ArrayList<NotificationEntity>>any())).thenReturn(notificationPermissions);
NotificationManager notificationManager = new NotificationManagerImpl();
ReflectionTestUtils.setField(notificationManager, "notificationAdapter", adapter);
ReflectionTestUtils.setField(notificationManager, "notificationDao", notificationDao);
NotificationPermissions notifications = notificationManager.findPermissionsByOrcidAndClient("some-orcid", "some-client", 0, OrcidApiConstants.MAX_NOTIFICATIONS_AVAILABLE);
assertEquals(notificationPermissions.size(), notifications.getNotifications().size());
}
use of org.mockito.Mock in project google-cloud-intellij by GoogleCloudPlatform.
the class GoogleApiClientAppEngineAdminServiceTest method testCreateApplication.
@Test
public void testCreateApplication() throws IOException, GoogleApiException {
String operationId = "my-operation-id";
String operationName = "apps/-/operations/" + operationId;
Operation inProgressOperation = buildInProgressOperation(operationName);
when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
final String locationId = "us-east1";
final String projectId = "my-project";
Map<String, Object> response = new HashMap<>();
response.put("name", projectId);
response.put("locationId", locationId);
Operation doneOperation = new Operation();
doneOperation.setName(operationName);
doneOperation.setDone(true);
doneOperation.setResponse(response);
// require polling several times
when(appengineClientMock.getAppsOperationsGetQuery().execute()).thenReturn(inProgressOperation).thenReturn(inProgressOperation).thenReturn(doneOperation);
Application result = service.createApplication(locationId, projectId, mock(Credential.class));
// ensure the 'getOperation' API call(s) were made correctly
verify(appengineClientMock.apps().operations(), atLeastOnce()).get(eq(projectId), eq(operationId));
// ensure the 'createApplication' API call was made with the correct args
verify(appengineClientMock.apps(), times(1)).create(argThat(application -> application.getId().equals(projectId) && application.getLocationId().equals(locationId)));
// ensure the 'createApplication' API call was only made once
verify(appengineClientMock.getAppsCreateQuery(), times(1)).execute();
assertEquals(projectId, result.getName());
assertEquals(locationId, result.getLocationId());
}
Aggregations