1 package net.pterodactylus.sone.test;
3 import static java.util.UUID.randomUUID;
4 import static org.mockito.ArgumentMatchers.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 public class TestAlbumBuilder implements AlbumBuilder {
26 private final Album album = mock(Album.class);
27 private final List<Album> albums = new ArrayList<>();
28 private final List<Image> images = new ArrayList<>();
29 private Album parentAlbum;
31 private String description;
32 private String imageId;
34 public TestAlbumBuilder() {
35 when(album.getTitle()).thenAnswer(new Answer<String>() {
37 public String answer(InvocationOnMock invocation) {
41 when(album.getDescription()).thenAnswer(new Answer<String>() {
43 public String answer(InvocationOnMock invocation) {
47 when(album.getAlbums()).thenReturn(albums);
48 when(album.getImages()).thenReturn(images);
49 doAnswer(new Answer<Void>() {
51 public Void answer(InvocationOnMock invocation) {
52 albums.add((Album) invocation.getArguments()[0]);
53 ((Album) invocation.getArguments()[0]).setParent(album);
56 }).when(album).addAlbum(any(Album.class));
57 doAnswer(new Answer<Void>() {
59 public Void answer(InvocationOnMock invocation) {
60 images.add((Image) invocation.getArguments()[0]);
63 }).when(album).addImage(any(Image.class));
64 doAnswer(new Answer<Void>() {
66 public Void answer(InvocationOnMock invocation) {
67 parentAlbum = (Album) invocation.getArguments()[0];
70 }).when(album).setParent(any(Album.class));
71 when(album.getParent()).thenAnswer(new Answer<Album>() {
73 public Album answer(InvocationOnMock invocation) {
77 when(album.modify()).thenReturn(new Modifier() {
79 public Modifier setTitle(String title) {
80 TestAlbumBuilder.this.title = title;
85 public Modifier setDescription(String description) {
86 TestAlbumBuilder.this.description = description;
91 public Album update() throws IllegalStateException {
98 public AlbumBuilder randomId() {
99 when(album.getId()).thenReturn(randomUUID().toString());
104 public AlbumBuilder withId(String id) {
105 when(album.getId()).thenReturn(id);
110 public AlbumBuilder by(Sone sone) {
111 when(album.getSone()).thenReturn(sone);
116 public Album build() throws IllegalStateException {