- @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;
- }
+ @Test(expected = InvalidParentAlbumFound.class)
+ public void albumWithInvalidParentIsRecognized() {
+ setupAlbum(0, "A1", "A0", "T1", "D1", "I1");
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ }
+
+ @Test
+ public void imagesAreParsedCorrectly() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImages();
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ Map<String, Album> 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<ImageBuilder>() {
+ @Override
+ public ImageBuilder answer(InvocationOnMock invocation)
+ throws Throwable {
+ return new TestImageBuilder();
+ }
+ });
+ return imageBuilderFactory;
+ }
+
+ @Test(expected = InvalidImageFound.class)
+ public void missingAlbumIdIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", null, 1000L, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @Test(expected = InvalidParentAlbumFound.class)
+ public void invalidAlbumIdIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A4", 1000L, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @Test(expected = InvalidImageFound.class)
+ public void missingCreationTimeIsRecognized() {
+ setupTopLevelAlbums();
+ configurationSoneParser.parseTopLevelAlbums(
+ createAlbumBuilderFactory());
+ setupImage(0, "I1", "A1", null, "K1", "T1", "D1", 16, 9);
+ configurationSoneParser.parseImages(createImageBuilderFactory());
+ }
+
+ @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());
+ }