Add “randomImage” accessor for albums
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 23 Jun 2016 18:12:56 +0000 (20:12 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 23 Jun 2016 18:12:56 +0000 (20:12 +0200)
src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java
src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java

index e19a89a..5093442 100644 (file)
@@ -19,8 +19,10 @@ package net.pterodactylus.sone.template;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Random;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
@@ -34,6 +36,8 @@ import net.pterodactylus.util.template.TemplateContext;
  */
 public class AlbumAccessor extends ReflectionAccessor {
 
+       private final Random random = new Random();
+
        /**
         * {@inheritDoc}
         */
@@ -49,6 +53,9 @@ public class AlbumAccessor extends ReflectionAccessor {
                        }
                        backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
                        return backlinks;
+               } else if ("randomImage".equals(member)) {
+                       List<Image> images = album.getImages();
+                       return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
                }
                return super.get(templateContext, object, member);
        }
index afc2a54..b31eabf 100644 (file)
@@ -1,15 +1,22 @@
 package net.pterodactylus.sone.template;
 
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
+import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 import net.pterodactylus.sone.TestUtil;
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 
@@ -90,4 +97,25 @@ public class AlbumAccessorTest {
                };
        }
 
+       @Test
+       public void albumImageIsGeneratedRandomly() {
+               Image image = mock(Image.class);
+               List<Image> albumImages = Arrays.asList(mock(Image.class), image);
+               when(album.getImages()).thenReturn(albumImages);
+               int matchedImage = 0;
+               for (int i = 0; i < 1000; i++) {
+                       Image randomImage = (Image) albumAccessor.get(null, album, "randomImage");
+                       if (randomImage == image) {
+                               matchedImage++;
+                       }
+               }
+               assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750)));
+       }
+
+       @Test
+       public void albumImageIsNullIfThereAreNoImagesInAnAlbum() {
+               when(album.getImages()).thenReturn(Collections.<Image>emptyList());
+               assertThat(albumAccessor.get(null, album, "randomImage"), nullValue());
+       }
+
 }