X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParserTest.java;h=4edc1b77089b7266db9b2cb7a352f470919b9d98;hb=6940f7d3df4f6164bfc69a9393a0f5796627b880;hp=b5e57f7942b1ad655ec356051da266bd4e2e8cae;hpb=ffb2ea1773cf7e3d1b7fc41ab0e9c3c1eed514e0;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java
index b5e57f7..4edc1b7 100644
--- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java
@@ -1,12 +1,10 @@
package net.pterodactylus.sone.core;
-import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Optional.of;
-import static java.lang.System.currentTimeMillis;
-import static java.util.UUID.randomUUID;
-import static net.pterodactylus.sone.Matchers.isAlbum;
-import static net.pterodactylus.sone.Matchers.isPost;
-import static net.pterodactylus.sone.Matchers.isPostReply;
+import static net.pterodactylus.sone.test.Matchers.isAlbum;
+import static net.pterodactylus.sone.test.Matchers.isImage;
+import static net.pterodactylus.sone.test.Matchers.isPost;
+import static net.pterodactylus.sone.test.Matchers.isPostReply;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -15,26 +13,22 @@ import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.Set;
-import java.util.concurrent.atomic.AtomicReference;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Album.Modifier;
-import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
@@ -42,13 +36,18 @@ import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.AlbumBuilderFactory;
+import net.pterodactylus.sone.database.ImageBuilder;
+import net.pterodactylus.sone.database.ImageBuilderFactory;
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostBuilderFactory;
import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.database.PostReplyBuilderFactory;
+import net.pterodactylus.sone.test.TestAlbumBuilder;
+import net.pterodactylus.sone.test.TestImageBuilder;
+import net.pterodactylus.sone.test.TestPostBuilder;
+import net.pterodactylus.sone.test.TestPostReplyBuilder;
+import net.pterodactylus.sone.test.TestValue;
import net.pterodactylus.util.config.Configuration;
-import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.config.Value;
import com.google.common.base.Optional;
import org.hamcrest.Matchers;
@@ -58,8 +57,6 @@ import org.mockito.stubbing.Answer;
/**
* Unit test for {@link ConfigurationSoneParser}.
- *
- * @author David âBombeâ Roden
*/
public class ConfigurationSoneParserTest {
@@ -89,9 +86,9 @@ public class ConfigurationSoneParserTest {
private void setupEmptyProfile() {
when(configuration.getStringValue(anyString())).thenReturn(
- new TestValue(null));
+ TestValue.from(null));
when(configuration.getIntValue(anyString())).thenReturn(
- new TestValue(null));
+ TestValue.from(null));
}
@Test
@@ -129,12 +126,12 @@ public class ConfigurationSoneParserTest {
private void setupString(String nodeName, String value) {
when(configuration.getStringValue(eq(nodeName))).thenReturn(
- new TestValue(value));
+ TestValue.from(value));
}
private void setupInteger(String nodeName, Integer value) {
when(configuration.getIntValue(eq(nodeName))).thenReturn(
- new TestValue(value));
+ TestValue.from(value));
}
@Test
@@ -181,7 +178,7 @@ public class ConfigurationSoneParserTest {
private void setupLong(String nodeName, Long value) {
when(configuration.getLongValue(eq(nodeName))).thenReturn(
- new TestValue(value));
+ TestValue.from(value));
}
@Test(expected = InvalidPostFound.class)
@@ -334,15 +331,15 @@ public class ConfigurationSoneParserTest {
albumBuilderFactory);
assertThat(topLevelAlbums, hasSize(2));
Album firstAlbum = topLevelAlbums.get(0);
- assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1", "I1"));
+ assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1"));
assertThat(firstAlbum.getAlbums(), emptyIterable());
assertThat(firstAlbum.getImages(), emptyIterable());
Album secondAlbum = topLevelAlbums.get(1);
- assertThat(secondAlbum, isAlbum("A2", null, "T2", "D2", null));
+ assertThat(secondAlbum, isAlbum("A2", null, "T2", "D2"));
assertThat(secondAlbum.getAlbums(), hasSize(1));
assertThat(secondAlbum.getImages(), emptyIterable());
Album thirdAlbum = secondAlbum.getAlbums().get(0);
- assertThat(thirdAlbum, isAlbum("A3", "A2", "T3", "D3", "I3"));
+ assertThat(thirdAlbum, isAlbum("A3", "A2", "T3", "D3"));
assertThat(thirdAlbum.getAlbums(), emptyIterable());
assertThat(thirdAlbum.getImages(), emptyIterable());
}
@@ -399,262 +396,127 @@ public class ConfigurationSoneParserTest {
createAlbumBuilderFactory());
}
- private static class TestValue implements Value {
+ @Test
+ public void imagesAreParsedCorrectly() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImages();
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ Map albums = configurationSoneParser.getAlbums();
+ assertThat(albums.get("A1").getImages(),
+ contains(isImage("I1", 1000L, "K1", "T1", "D1", 16, 9)));
+ assertThat(albums.get("A2").getImages(), contains(
+ isImage("I2", 2000L, "K2", "T2", "D2", 16 * 2, 9 * 2)));
+ assertThat(albums.get("A3").getImages(), contains(
+ isImage("I3", 3000L, "K3", "T3", "D3", 16 * 3, 9 * 3)));
+ }
+
+ private void setupImages() {
+ setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", 16, 9);
+ setupImage(1, "I2", "A2", 2000L, "K2", "T2", "D2", 16 * 2, 9 * 2);
+ setupImage(2, "I3", "A3", 3000L, "K3", "T3", "D3", 16 * 3, 9 * 3);
+ setupImage(3, null, null, 0L, null, null, null, 0, 0);
+ }
+
+ private void setupImage(int imageNumber, String id,
+ String parentAlbumId, Long creationTime, String key, String title,
+ String description, Integer width, Integer height) {
+ final String imagePrefix = "Sone/1/Images/" + imageNumber;
+ setupString(imagePrefix + "/ID", id);
+ setupString(imagePrefix + "/Album", parentAlbumId);
+ setupLong(imagePrefix + "/CreationTime", creationTime);
+ setupString(imagePrefix + "/Key", key);
+ setupString(imagePrefix + "/Title", title);
+ setupString(imagePrefix + "/Description", description);
+ setupInteger(imagePrefix + "/Width", width);
+ setupInteger(imagePrefix + "/Height", height);
+ }
+
+ private ImageBuilderFactory createImageBuilderFactory() {
+ ImageBuilderFactory imageBuilderFactory =
+ mock(ImageBuilderFactory.class);
+ when(imageBuilderFactory.newImageBuilder()).thenAnswer(
+ new Answer() {
+ @Override
+ public ImageBuilder answer(InvocationOnMock invocation)
+ throws Throwable {
+ return new TestImageBuilder();
+ }
+ });
+ return imageBuilderFactory;
+ }
- private final AtomicReference value = new AtomicReference();
+ @Test(expected = InvalidImageFound.class)
+ public void missingAlbumIdIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", null, 1000L, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
- public TestValue(T originalValue) {
- value.set(originalValue);
- }
+ @Test(expected = InvalidParentAlbumFound.class)
+ public void invalidAlbumIdIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A4", 1000L, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
- @Override
- public T getValue() throws ConfigurationException {
- return value.get();
- }
+ @Test(expected = InvalidImageFound.class)
+ public void missingCreationTimeIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", null, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
- @Override
- public T getValue(T defaultValue) {
- final T realValue = value.get();
- return (realValue != null) ? realValue : defaultValue;
- }
-
- @Override
- public void setValue(T newValue) throws ConfigurationException {
- value.set(newValue);
- }
-
- }
-
- private static class TestPostBuilder implements PostBuilder {
-
- private final Post post = mock(Post.class);
- private String recipientId = null;
-
- @Override
- public PostBuilder copyPost(Post post) throws NullPointerException {
- return this;
- }
-
- @Override
- public PostBuilder from(String senderId) {
- final Sone sone = mock(Sone.class);
- when(sone.getId()).thenReturn(senderId);
- when(post.getSone()).thenReturn(sone);
- return this;
- }
-
- @Override
- public PostBuilder randomId() {
- when(post.getId()).thenReturn(randomUUID().toString());
- return this;
- }
-
- @Override
- public PostBuilder withId(String id) {
- when(post.getId()).thenReturn(id);
- return this;
- }
-
- @Override
- public PostBuilder currentTime() {
- when(post.getTime()).thenReturn(currentTimeMillis());
- return this;
- }
-
- @Override
- public PostBuilder withTime(long time) {
- when(post.getTime()).thenReturn(time);
- return this;
- }
-
- @Override
- public PostBuilder withText(String text) {
- when(post.getText()).thenReturn(text);
- return this;
- }
-
- @Override
- public PostBuilder to(String recipientId) {
- this.recipientId = recipientId;
- return this;
- }
-
- @Override
- public Post build() throws IllegalStateException {
- when(post.getRecipientId()).thenReturn(fromNullable(recipientId));
- return post;
- }
-
- }
-
- private static class TestPostReplyBuilder implements PostReplyBuilder {
-
- private final PostReply postReply = mock(PostReply.class);
-
- @Override
- public PostReplyBuilder to(String postId) {
- when(postReply.getPostId()).thenReturn(postId);
- return this;
- }
-
- @Override
- public PostReply build() throws IllegalStateException {
- return postReply;
- }
-
- @Override
- public PostReplyBuilder randomId() {
- when(postReply.getId()).thenReturn(randomUUID().toString());
- return this;
- }
-
- @Override
- public PostReplyBuilder withId(String id) {
- when(postReply.getId()).thenReturn(id);
- return this;
- }
-
- @Override
- public PostReplyBuilder from(String senderId) {
- Sone sone = mock(Sone.class);
- when(sone.getId()).thenReturn(senderId);
- when(postReply.getSone()).thenReturn(sone);
- return this;
- }
-
- @Override
- public PostReplyBuilder currentTime() {
- when(postReply.getTime()).thenReturn(currentTimeMillis());
- return this;
- }
-
- @Override
- public PostReplyBuilder withTime(long time) {
- when(postReply.getTime()).thenReturn(time);
- return this;
- }
-
- @Override
- public PostReplyBuilder withText(String text) {
- when(postReply.getText()).thenReturn(text);
- return this;
- }
-
- }
-
- private static class TestAlbumBuilder implements AlbumBuilder {
-
- private final Album album = mock(Album.class);
- private final List albums = new ArrayList();
- private final List images = new ArrayList();
- private Album parentAlbum;
- private String title;
- private String description;
- private String imageId;
-
- public TestAlbumBuilder() {
- when(album.getTitle()).thenAnswer(new Answer() {
- @Override
- public String answer(InvocationOnMock invocation) {
- return title;
- }
- });
- when(album.getDescription()).thenAnswer(new Answer() {
- @Override
- public String answer(InvocationOnMock invocation) {
- return description;
- }
- });
- when(album.getAlbumImage()).thenAnswer(new Answer() {
- @Override
- public Image answer(InvocationOnMock invocation) {
- if (imageId == null) {
- return null;
- }
- Image image = mock(Image.class);
- when(image.getId()).thenReturn(imageId);
- return image;
- }
- });
- when(album.getAlbums()).thenReturn(albums);
- when(album.getImages()).thenReturn(images);
- doAnswer(new Answer() {
- @Override
- public Void answer(InvocationOnMock invocation) {
- albums.add((Album) invocation.getArguments()[0]);
- ((Album) invocation.getArguments()[0]).setParent(album);
- return null;
- }
- }).when(album).addAlbum(any(Album.class));
- doAnswer(new Answer() {
- @Override
- public Void answer(InvocationOnMock invocation) {
- images.add((Image) invocation.getArguments()[0]);
- return null;
- }
- }).when(album).addImage(any(Image.class));
- doAnswer(new Answer() {
- @Override
- public Void answer(InvocationOnMock invocation) {
- parentAlbum = (Album) invocation.getArguments()[0];
- return null;
- }
- }).when(album).setParent(any(Album.class));
- when(album.getParent()).thenAnswer(new Answer() {
- @Override
- public Album answer(InvocationOnMock invocation) {
- return parentAlbum;
- }
- });
- when(album.modify()).thenReturn(new Modifier() {
- @Override
- public Modifier setTitle(String title) {
- TestAlbumBuilder.this.title = title;
- return this;
- }
-
- @Override
- public Modifier setDescription(String description) {
- TestAlbumBuilder.this.description = description;
- return this;
- }
-
- @Override
- public Modifier setAlbumImage(String imageId) {
- TestAlbumBuilder.this.imageId = imageId;
- return this;
- }
-
- @Override
- public Album update() throws IllegalStateException {
- return album;
- }
- });
- }
-
- @Override
- public AlbumBuilder randomId() {
- when(album.getId()).thenReturn(randomUUID().toString());
- return this;
- }
-
- @Override
- public AlbumBuilder withId(String id) {
- when(album.getId()).thenReturn(id);
- return this;
- }
-
- @Override
- public AlbumBuilder by(Sone sone) {
- when(album.getSone()).thenReturn(sone);
- return this;
- }
-
- @Override
- public Album build() throws IllegalStateException {
- return album;
- }
+ @Test(expected = InvalidImageFound.class)
+ public void missingKeyIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", 1000L, null, "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @Test(expected = InvalidImageFound.class)
+ public void missingTitleIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", 1000L, "K1", null, "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @Test(expected = InvalidImageFound.class)
+ public void missingDescriptionIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", 1000L, "K1", "T1", null, 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+ @Test(expected = InvalidImageFound.class)
+ public void missingWidthIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", null, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @Test(expected = InvalidImageFound.class)
+ public void missingHeightIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", 16, null);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
}
}