1 package net.pterodactylus.sone;
3 import static java.util.UUID.randomUUID;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.doAnswer;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import java.util.ArrayList;
10 import java.util.List;
12 import net.pterodactylus.sone.data.Album;
13 import net.pterodactylus.sone.data.Album.Modifier;
14 import net.pterodactylus.sone.data.Image;
15 import net.pterodactylus.sone.data.Sone;
16 import net.pterodactylus.sone.database.AlbumBuilder;
18 import org.mockito.invocation.InvocationOnMock;
19 import org.mockito.stubbing.Answer;
22 * {@link AlbumBuilder} that returns a mocked {@link Album}.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 public class TestAlbumBuilder implements AlbumBuilder {
28 private final Album album = mock(Album.class);
29 private final List<Album> albums = new ArrayList<Album>();
30 private final List<Image> images = new ArrayList<Image>();
31 private Album parentAlbum;
33 private String description;
34 private String imageId;
36 public TestAlbumBuilder() {
37 when(album.getTitle()).thenAnswer(new Answer<String>() {
39 public String answer(InvocationOnMock invocation) {
43 when(album.getDescription()).thenAnswer(new Answer<String>() {
45 public String answer(InvocationOnMock invocation) {
49 when(album.getAlbumImage()).thenAnswer(new Answer<Image>() {
51 public Image answer(InvocationOnMock invocation) {
52 if (imageId == null) {
55 Image image = mock(Image.class);
56 when(image.getId()).thenReturn(imageId);
60 when(album.getAlbums()).thenReturn(albums);
61 when(album.getImages()).thenReturn(images);
62 doAnswer(new Answer<Void>() {
64 public Void answer(InvocationOnMock invocation) {
65 albums.add((Album) invocation.getArguments()[0]);
66 ((Album) invocation.getArguments()[0]).setParent(album);
69 }).when(album).addAlbum(any(Album.class));
70 doAnswer(new Answer<Void>() {
72 public Void answer(InvocationOnMock invocation) {
73 images.add((Image) invocation.getArguments()[0]);
76 }).when(album).addImage(any(Image.class));
77 doAnswer(new Answer<Void>() {
79 public Void answer(InvocationOnMock invocation) {
80 parentAlbum = (Album) invocation.getArguments()[0];
83 }).when(album).setParent(any(Album.class));
84 when(album.getParent()).thenAnswer(new Answer<Album>() {
86 public Album answer(InvocationOnMock invocation) {
90 when(album.modify()).thenReturn(new Modifier() {
92 public Modifier setTitle(String title) {
93 TestAlbumBuilder.this.title = title;
98 public Modifier setDescription(String description) {
99 TestAlbumBuilder.this.description = description;
104 public Modifier setAlbumImage(String imageId) {
105 TestAlbumBuilder.this.imageId = imageId;
110 public Album update() throws IllegalStateException {
117 public AlbumBuilder randomId() {
118 when(album.getId()).thenReturn(randomUUID().toString());
123 public AlbumBuilder withId(String id) {
124 when(album.getId()).thenReturn(id);
129 public AlbumBuilder by(Sone sone) {
130 when(album.getSone()).thenReturn(sone);
135 public Album build() throws IllegalStateException {