UseCaseHandler { void execute(RosieUseCase useCase, UseCaseParams useCaseParams) { UseCaseFilter.filter(useCase, useCaseParams); useCase.setOnSuccessCallback(useCaseParams.getOnSuccessCallback()); OnErrorCallback onErrorCallback = new OnErrorCallbackToErrorHandlerAdapter(errorHandler, useCaseParams.getOnErrorCallback()); errorCallbacks.add(onErrorCallback); useCase.setOnErrorCallback(onErrorCallback); UseCaseWrapper useCaseWrapper = new UseCaseWrapper(useCase, useCaseParams, errorHandler); taskScheduler.execute(useCaseWrapper); } UseCaseHandler(TaskScheduler taskScheduler, ErrorHandler errorHandler); void registerGlobalErrorCallback(OnErrorCallback globalError); void unregisterGlobalErrorCallback(OnErrorCallback globalError);  }
@Test public void testExecuteAnyObject() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); EmptyUseCase anyUseCase = new EmptyUseCase(); UseCaseParams emptyParams = givenEmptyUseCaseParms(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); useCaseHandler.execute(anyUseCase, emptyParams); verify(taskScheduler, only()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteFailNotAnyUseCase() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); NoUseCase noUseCase = new NoUseCase(); UseCaseParams emptyUseCaseParms = givenEmptyUseCaseParms(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); boolean exception = false; try { useCaseHandler.execute(noUseCase, emptyUseCaseParms); } catch (IllegalArgumentException e) { exception = true; } assertTrue(exception); verify(taskScheduler, never()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteWithMethodName() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); AnyUseCase anyUseCase = new AnyUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.useCaseName(ANY_EXECUTION_NAME).execute(); verify(taskScheduler, only()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteFailsWithWrongMethodName() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); AnyUseCase anyUseCase = new AnyUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); boolean error = false; try { useCaseCall.useCaseName(NO_EXIST_METHOD_NAME).execute(); } catch (IllegalArgumentException e) { error = true; } assertTrue(error); verify(taskScheduler, never()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteWithArgs() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); AnyUseCase anyUseCase = new AnyUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); String anyArg1 = ANY_PARAM1; int anyArg2 = ANY_PARAM2; UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.args(anyArg1, anyArg2).execute(); verify(taskScheduler, only()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteAmbigous() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); AmbiguousUseCase ambigousUseCase = new AmbiguousUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(ambigousUseCase, useCaseHandler); boolean exception = false; try { useCaseCall.args(ANY_PARAM1, ANY_PARAM2).execute(); } catch (IllegalArgumentException e) { exception = true; } assertTrue(exception); verify(taskScheduler, never()).execute(any(UseCaseWrapper.class)); }
@Test public void testExecuteNoAmbigous() throws Exception { TaskScheduler taskScheduler = mock(TaskScheduler.class); AmbiguousUseCase ambigousUseCase = new AmbiguousUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(ambigousUseCase, useCaseHandler); useCaseCall.args(ANY_PARAM1, ANY_PARAM2).useCaseName("method1").execute(); verify(taskScheduler, only()).execute(any(UseCaseWrapper.class)); }
@Test public void onSuccessCallbackShouldBeCalledWithSuccessArgs() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); EmptyResponseUseCase anyUseCase = new EmptyResponseUseCase(); EmptyOnSuccess onSuccessCallback = new EmptyOnSuccess(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.onSuccess(onSuccessCallback).execute(); assertTrue(onSuccessCallback.isSuccess()); }
@Test public void completeCallbackShouldBeCalledWithSuccessArgs() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); AnyUseCase anyUseCase = new AnyUseCase(); AnyOnSuccess onSuccessCallback = new AnyOnSuccess(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.useCaseName("anyExecution").onSuccess(onSuccessCallback).execute(); assertEquals(ANY_RETURN_VALUE, onSuccessCallback.getValue()); }
@Test public void onSuccessCallbackShouldBeCalledWithSuccessArgsAndDowncastingResponse() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); AnyUseCase anyUseCase = new AnyUseCase(); AnyOnSuccessWithDowncast onSuccessCallback = new AnyOnSuccessWithDowncast(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.useCaseName("downcastResponse").onSuccess(onSuccessCallback).execute(); assertNotNull(onSuccessCallback.getValue()); }
@Test public void completeCallbackShouldNotBeExecutedWhenNotMatchArgs() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); AnyUseCase anyUseCase = new AnyUseCase(); EmptyOnSuccess onSuccessCallback = new EmptyOnSuccess(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(anyUseCase, useCaseHandler); useCaseCall.useCaseName("anyExecution").onSuccess(onSuccessCallback).execute(); assertFalse(onSuccessCallback.isSuccess()); }
@Test public void shouldCallErrorOnErrorWhenUseCaseInvokeAnError() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); ErrorUseCase errorUseCase = new ErrorUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); OnErrorCallback errorCallback = spy(onErrorCallback); UseCaseCall useCaseCall = new UseCaseCall(errorUseCase, useCaseHandler); useCaseCall.useCaseName("customError").onError(errorCallback).execute(); verify(errorCallback).onError(any(Error.class)); }
@Test public void shouldCallOnErrorCallbackWhenUseCaseThrowAnUnhandledException() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); ErrorUseCase errorUseCase = new ErrorUseCase(); ErrorHandler errorHandler = new ErrorHandler(new FakeCallbackScheduler()); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); OnErrorCallback errorCallback = spy(onErrorCallback); UseCaseCall useCaseCall = new UseCaseCall(errorUseCase, useCaseHandler); useCaseCall.useCaseName("launchException").onError(errorCallback).execute(); verify(errorCallback).onError(any(Error.class)); }
@Test public void shouldCallErrorHandlerWhenUseCaseInvokeAnError() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); ErrorUseCase errorUseCase = new ErrorUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(errorUseCase, useCaseHandler); useCaseCall.useCaseName("customError").execute(); verify(errorHandler).notifyError(any(Error.class), eq((OnErrorCallback) null)); }
@Test public void shouldNotifyErrorHandlerWhenUseCaseOnErrorCallbackDoesNotExist() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); ErrorUseCase errorUseCase = new ErrorUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(errorUseCase, useCaseHandler); useCaseCall.useCaseName("customError").onError(specificErrorCallback).execute(); verify(errorHandler).notifyError(any(Error.class), eq(specificErrorCallback)); }
@Test public void shouldCallErrorHandlerErrorWhenUseCaseThrowsAnException() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); ErrorUseCase errorUseCase = new ErrorUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(errorUseCase, useCaseHandler); useCaseCall.useCaseName("launchException").execute(); verify(errorHandler).notifyException(any(Exception.class), eq((OnErrorCallback) null)); }
@Test public void shouldThrowExceptionIfUseCaseNotifiesSuccessButThereIsNoOnSuccessCallback() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); SuccessUseCase successUseCase = new SuccessUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(successUseCase, useCaseHandler); useCaseCall.execute(); verify(errorHandler).notifyException(any(IllegalStateException.class), eq((OnErrorCallback) null)); }
@Test public void shouldThrowExceptionIfTheOnSuccessCallbackHasNoMethodsWithSuccessAnnotations() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); SuccessUseCase successUseCase = new SuccessUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); UseCaseCall useCaseCall = new UseCaseCall(successUseCase, useCaseHandler); useCaseCall.onSuccess(new OnSuccessCallback() { }).execute(); verify(errorHandler).notifyException(any(IllegalStateException.class), eq((OnErrorCallback) null)); }
@Test public void shouldSupportNullUseCaseParams() { FakeTaskScheduler taskScheduler = new FakeTaskScheduler(); AnyUseCase useCase = new AnyUseCase(); ErrorHandler errorHandler = mock(ErrorHandler.class); UseCaseHandler useCaseHandler = new UseCaseHandler(taskScheduler, errorHandler); AnyOnSuccess onSuccessCallback = new AnyOnSuccess(); UseCaseCall useCaseCall = new UseCaseCall(useCase, useCaseHandler); useCaseCall.args(null, ANY_INT_PARAM).onSuccess(onSuccessCallback).execute(); assertEquals(ANY_INT_PARAM, onSuccessCallback.getValue()); }