🚧 Add interactions helper for page maker
[Sone.git] / src / test / java / net / pterodactylus / sone / test / TestAlbumBuilder.java
1 package net.pterodactylus.sone.test;
2
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;
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 public class TestAlbumBuilder implements AlbumBuilder {
25
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;
30         private String title;
31         private String description;
32         private String imageId;
33
34         public TestAlbumBuilder() {
35                 when(album.getTitle()).thenAnswer(new Answer<String>() {
36                         @Override
37                         public String answer(InvocationOnMock invocation) {
38                                 return title;
39                         }
40                 });
41                 when(album.getDescription()).thenAnswer(new Answer<String>() {
42                         @Override
43                         public String answer(InvocationOnMock invocation) {
44                                 return description;
45                         }
46                 });
47                 when(album.getAlbums()).thenReturn(albums);
48                 when(album.getImages()).thenReturn(images);
49                 doAnswer(new Answer<Void>() {
50                         @Override
51                         public Void answer(InvocationOnMock invocation) {
52                                 albums.add((Album) invocation.getArguments()[0]);
53                                 ((Album) invocation.getArguments()[0]).setParent(album);
54                                 return null;
55                         }
56                 }).when(album).addAlbum(any(Album.class));
57                 doAnswer(new Answer<Void>() {
58                         @Override
59                         public Void answer(InvocationOnMock invocation) {
60                                 images.add((Image) invocation.getArguments()[0]);
61                                 return null;
62                         }
63                 }).when(album).addImage(any(Image.class));
64                 doAnswer(new Answer<Void>() {
65                         @Override
66                         public Void answer(InvocationOnMock invocation) {
67                                 parentAlbum = (Album) invocation.getArguments()[0];
68                                 return null;
69                         }
70                 }).when(album).setParent(any(Album.class));
71                 when(album.getParent()).thenAnswer(new Answer<Album>() {
72                         @Override
73                         public Album answer(InvocationOnMock invocation) {
74                                 return parentAlbum;
75                         }
76                 });
77                 when(album.modify()).thenReturn(new Modifier() {
78                         @Override
79                         public Modifier setTitle(String title) {
80                                 TestAlbumBuilder.this.title = title;
81                                 return this;
82                         }
83
84                         @Override
85                         public Modifier setDescription(String description) {
86                                 TestAlbumBuilder.this.description = description;
87                                 return this;
88                         }
89
90                         @Override
91                         public Album update() throws IllegalStateException {
92                                 return album;
93                         }
94                 });
95         }
96
97         @Override
98         public AlbumBuilder randomId() {
99                 when(album.getId()).thenReturn(randomUUID().toString());
100                 return this;
101         }
102
103         @Override
104         public AlbumBuilder withId(String id) {
105                 when(album.getId()).thenReturn(id);
106                 return this;
107         }
108
109         @Override
110         public AlbumBuilder by(Sone sone) {
111                 when(album.getSone()).thenReturn(sone);
112                 return this;
113         }
114
115         @Override
116         public Album build() throws IllegalStateException {
117                 return album;
118         }
119
120 }