Add unit test for returning not-yet loaded bookmarked posts.
[Sone.git] / src / test / java / net / pterodactylus / sone / TestAlbumBuilder.java
1 package net.pterodactylus.sone;
2
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;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
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;
17
18 import org.mockito.invocation.InvocationOnMock;
19 import org.mockito.stubbing.Answer;
20
21 /**
22  * {@link AlbumBuilder} that returns a mocked {@link Album}.
23  *
24  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25  */
26 public class TestAlbumBuilder implements AlbumBuilder {
27
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;
32         private String title;
33         private String description;
34         private String imageId;
35
36         public TestAlbumBuilder() {
37                 when(album.getTitle()).thenAnswer(new Answer<String>() {
38                         @Override
39                         public String answer(InvocationOnMock invocation) {
40                                 return title;
41                         }
42                 });
43                 when(album.getDescription()).thenAnswer(new Answer<String>() {
44                         @Override
45                         public String answer(InvocationOnMock invocation) {
46                                 return description;
47                         }
48                 });
49                 when(album.getAlbumImage()).thenAnswer(new Answer<Image>() {
50                         @Override
51                         public Image answer(InvocationOnMock invocation) {
52                                 if (imageId == null) {
53                                         return null;
54                                 }
55                                 Image image = mock(Image.class);
56                                 when(image.getId()).thenReturn(imageId);
57                                 return image;
58                         }
59                 });
60                 when(album.getAlbums()).thenReturn(albums);
61                 when(album.getImages()).thenReturn(images);
62                 doAnswer(new Answer<Void>() {
63                         @Override
64                         public Void answer(InvocationOnMock invocation) {
65                                 albums.add((Album) invocation.getArguments()[0]);
66                                 ((Album) invocation.getArguments()[0]).setParent(album);
67                                 return null;
68                         }
69                 }).when(album).addAlbum(any(Album.class));
70                 doAnswer(new Answer<Void>() {
71                         @Override
72                         public Void answer(InvocationOnMock invocation) {
73                                 images.add((Image) invocation.getArguments()[0]);
74                                 return null;
75                         }
76                 }).when(album).addImage(any(Image.class));
77                 doAnswer(new Answer<Void>() {
78                         @Override
79                         public Void answer(InvocationOnMock invocation) {
80                                 parentAlbum = (Album) invocation.getArguments()[0];
81                                 return null;
82                         }
83                 }).when(album).setParent(any(Album.class));
84                 when(album.getParent()).thenAnswer(new Answer<Album>() {
85                         @Override
86                         public Album answer(InvocationOnMock invocation) {
87                                 return parentAlbum;
88                         }
89                 });
90                 when(album.modify()).thenReturn(new Modifier() {
91                         @Override
92                         public Modifier setTitle(String title) {
93                                 TestAlbumBuilder.this.title = title;
94                                 return this;
95                         }
96
97                         @Override
98                         public Modifier setDescription(String description) {
99                                 TestAlbumBuilder.this.description = description;
100                                 return this;
101                         }
102
103                         @Override
104                         public Modifier setAlbumImage(String imageId) {
105                                 TestAlbumBuilder.this.imageId = imageId;
106                                 return this;
107                         }
108
109                         @Override
110                         public Album update() throws IllegalStateException {
111                                 return album;
112                         }
113                 });
114         }
115
116         @Override
117         public AlbumBuilder randomId() {
118                 when(album.getId()).thenReturn(randomUUID().toString());
119                 return this;
120         }
121
122         @Override
123         public AlbumBuilder withId(String id) {
124                 when(album.getId()).thenReturn(id);
125                 return this;
126         }
127
128         @Override
129         public AlbumBuilder by(Sone sone) {
130                 when(album.getSone()).thenReturn(sone);
131                 return this;
132         }
133
134         @Override
135         public Album build() throws IllegalStateException {
136                 return album;
137         }
138
139 }