Last active
March 30, 2017 13:37
-
-
Save westonal/a5164f8479e7ceb06d5a7b3b0c6d863f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package entities; | |
| /** Represents a content item. */ | |
| abstract class Content { | |
| UUID id; | |
| } | |
| ——————————————————————————————————————————————————————————————————————————— | |
| package domain.usecases; | |
| public interface GetLobbyContent { | |
| List<Content> get(); | |
| } | |
| ——————————————————————————————————————————————————————————————————————————— | |
| package domain; | |
| /** | |
| Business use case: show the user a list of content in the lobby | |
| */ | |
| public class GetLobbyContentUseCase implements domain.usercases.GetLobbyContent { | |
| private ContentRepository repository; | |
| public GetLobbyContentUseCase(ContentRepository repository) { | |
| this.repository = repository; | |
| } | |
| public List<Content> get() throws Exception { | |
| return repository.getLobbyContent(); | |
| } | |
| } | |
| /** Collects together provider methods for content-related data */ | |
| public interface ContentRepository { | |
| /** Returns a page of lobby content */ | |
| public List<Content> getLobbyContent(); | |
| /** Returns a page of video content */ | |
| public List<Content> getVideoContent(); | |
| } | |
| public class GetLobbyContentUseCaseTest { | |
| @InjectMocks | |
| GetLobbyContentUseCase testSubject; | |
| @Mock | |
| ContentRepository repository; | |
| @Before | |
| public void setup() { | |
| initMocks(this); | |
| } | |
| @Test | |
| public void execute_returnsContent() { | |
| //GIVEN | |
| when(repository.getLobbyContent()).thenReturn(mockData); | |
| //WHEN | |
| List<Content> result = testSubject.execute(); | |
| //THEN | |
| assertThat(result, is(mockData)); | |
| } | |
| @Test (expected = Exception.class) | |
| public void execute_whenRepositoryThrowsException_throwsException() { | |
| //GIVEN | |
| when(repository.getLobbyContent()).thenThrow(mockException); | |
| //WHEN | |
| testSubject.execute(); | |
| } | |
| } | |
| ——————————————————————————————————————————————————————————————————————————— | |
| package ui; | |
| class LoaderPresenter { | |
| @Inject LoaderView view; | |
| @Inject domain.usecases.GetLobbyContent useCase; | |
| @Inject Dispatcher dispatcher; | |
| public void loadData(){ | |
| view.showLoader(); | |
| dispatcher.invoke( | |
| () -> useCase.get(); | |
| () -> view.hideLoader()); | |
| } | |
| } | |
| class LobbyPresenter { | |
| @Inject LobbyView view; | |
| @Inject domain.usecases.GetLobbyContent useCase; | |
| @Inject Dispatcher dispatcher; | |
| public void displayLobbyContent() { | |
| dispatcher.invoke( | |
| () -> useCase.get(), | |
| items -> view.showItems(items)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment