import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.SoneImpl;
import net.pterodactylus.sone.data.TemporaryImage;
+import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.PostBuilder;
return posts;
}
- /**
- * Returns the album with the given ID, creating a new album if no album
- * with the given ID can be found.
- *
- * @param albumId
- * The ID of the album
- * @return The album with the given ID
- */
- public Album getOrCreateAlbum(String albumId) {
- return getAlbum(albumId, true);
+ public AlbumBuilder albumBuilder() {
+ return database.newAlbumBuilder();
}
/**
*
* @param albumId
* The ID of the album
- * @param create
- * {@code true} to create a new album if none exists for the
- * given ID
* @return The album with the given ID, or {@code null} if no album with the
- * given ID exists and {@code create} is {@code false}
+ * given ID exists
*/
- public Album getAlbum(String albumId, boolean create) {
- Optional<Album> album = database.getAlbum(albumId);
- if (album.isPresent()) {
- return album.get();
- }
- if (!create) {
- return null;
- }
- Album newAlbum = database.newAlbumBuilder().withId(albumId).build();
- database.storeAlbum(newAlbum);
- return newAlbum;
+ public Album getAlbum(String albumId) {
+ return database.getAlbum(albumId).orNull();
}
/**
logger.log(Level.WARNING, "Invalid album found, aborting load!");
return;
}
- Album album = getOrCreateAlbum(albumId).setSone(sone).modify().setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId).update();
+ Album album = database.newAlbumBuilder()
+ .withId(albumId)
+ .by(sone)
+ .build()
+ .modify()
+ .setTitle(albumTitle)
+ .setDescription(albumDescription)
+ .setAlbumImage(albumImageId)
+ .update();
if (albumParentId != null) {
- Album parentAlbum = getAlbum(albumParentId, false);
+ Album parentAlbum = getAlbum(albumParentId);
if (parentAlbum == null) {
logger.log(Level.WARNING, String.format("Invalid parent album ID: %s", albumParentId));
return;
logger.log(Level.WARNING, "Invalid image found, aborting load!");
return;
}
- Album album = getAlbum(albumId, false);
+ Album album = getAlbum(albumId);
if (album == null) {
logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
return;
* @return The new album
*/
public Album createAlbum(Sone sone, Album parent) {
- Album album = database.newAlbumBuilder().randomId().build();
+ Album album = database.newAlbumBuilder().randomId().by(sone).build();
database.storeAlbum(album);
- album.setSone(sone);
parent.addAlbum(album);
return album;
}
}
Album parent = null;
if (parentId != null) {
- parent = core.getAlbum(parentId, false);
+ parent = core.getAlbum(parentId);
if (parent == null) {
logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
return null;
}
}
- Album album = core.getOrCreateAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update();
+ Album album = core.albumBuilder()
+ .withId(id)
+ .by(sone)
+ .build()
+ .modify()
+ .setTitle(title)
+ .setDescription(description)
+ .update();
if (parent != null) {
parent.addAlbum(album);
} else {
Sone getSone();
/**
- * Sets the owner of the album. The owner can only be set as long as the
- * current owner is {@code null}.
- *
- * @param sone
- * The album owner
- * @return This album
- */
- Album setSone(Sone sone);
-
- /**
* Returns the nested albums.
*
* @return The nested albums
private final String id;
/** The Sone this album belongs to. */
- private Sone sone;
+ private final Sone sone;
/** Nested albums. */
private final List<Album> albums = new ArrayList<Album>();
private String albumImage;
/** Creates a new album with a random ID. */
- public AlbumImpl() {
- this(UUID.randomUUID().toString());
+ public AlbumImpl(Sone sone) {
+ this(sone, UUID.randomUUID().toString());
}
/**
* @param id
* The ID of the album
*/
- public AlbumImpl(String id) {
+ public AlbumImpl(Sone sone, String id) {
+ this.sone = checkNotNull(sone, "Sone must not be null");
this.id = checkNotNull(id, "id must not be null");
}
}
@Override
- public Album setSone(Sone sone) {
- checkNotNull(sone, "sone must not be null");
- checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
- this.sone = sone;
- return this;
- }
-
- @Override
public List<Album> getAlbums() {
return new ArrayList<Album>(albums);
}
private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
/** The root album containing all albums. */
- private final Album rootAlbum = new AlbumImpl().setSone(this);
+ private final Album rootAlbum = new AlbumImpl(this);
/** Sone-specific options. */
private SoneOptions options = new DefaultSoneOptions();
import static com.google.common.base.Preconditions.checkState;
+import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.AlbumBuilder;
/**
/** The ID of the album to create. */
protected String id;
+ protected Sone sone;
@Override
public AlbumBuilder randomId() {
return this;
}
+ public AlbumBuilder by(Sone sone) {
+ this.sone = sone;
+ return this;
+ }
+
//
// PROTECTED METHODS
//
*/
protected void validate() throws IllegalStateException {
checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
+ checkState(sone != null, "Sone must not be null");
}
}
@Override
public Album build() throws IllegalStateException {
validate();
- return randomId ? new AlbumImpl() : new AlbumImpl(id);
+ return randomId ? new AlbumImpl(sone) : new AlbumImpl(sone, id);
}
}
package net.pterodactylus.sone.database;
import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Sone;
/**
* Builder for {@link Album} objects.
*/
AlbumBuilder withId(String id);
+ AlbumBuilder by(Sone sone);
+
/**
* Creates the album.
*
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim();
Sone currentSone = getCurrentSone(request.getToadletContext());
String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
- Album parent = webInterface.getCore().getAlbum(parentId, false);
+ Album parent = webInterface.getCore().getAlbum(parentId);
if (parentId.equals("")) {
parent = currentSone.getRootAlbum();
}
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
- Album album = webInterface.getCore().getAlbum(albumId, false);
+ Album album = webInterface.getCore().getAlbum(albumId);
if (album == null) {
throw new RedirectException("invalid.html");
}
throw new RedirectException("imageBrowser.html?album=" + parentAlbum.getId());
}
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId, false);
+ Album album = webInterface.getCore().getAlbum(albumId);
if (album == null) {
throw new RedirectException("invalid.html");
}
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
- Album album = webInterface.getCore().getAlbum(albumId, false);
+ Album album = webInterface.getCore().getAlbum(albumId);
if (album == null) {
throw new RedirectException("invalid.html");
}
super.processTemplate(request, templateContext);
String albumId = request.getHttpRequest().getParam("album", null);
if (albumId != null) {
- Album album = webInterface.getCore().getAlbum(albumId, false);
+ Album album = webInterface.getCore().getAlbum(albumId);
templateContext.set("albumRequested", true);
templateContext.set("album", album);
templateContext.set("page", request.getHttpRequest().getParam("page"));
*/
private String getAlbumId(String phrase) {
String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
- return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null;
+ return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
}
/**
if (request.getMethod() == Method.POST) {
Sone currentSone = getCurrentSone(request.getToadletContext());
String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
- Album parent = webInterface.getCore().getAlbum(parentId, false);
+ Album parent = webInterface.getCore().getAlbum(parentId);
if (parent == null) {
/* TODO - signal error */
return;
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId, false);
+ Album album = webInterface.getCore().getAlbum(albumId);
if (album == null) {
return createErrorJsonObject("invalid-album-id");
}
package net.pterodactylus.sone.core;
import static com.google.common.base.Optional.of;
+import static java.util.UUID.randomUUID;
import static net.pterodactylus.sone.data.Sone.SoneStatus.downloading;
import static net.pterodactylus.sone.data.Sone.SoneStatus.idle;
import static net.pterodactylus.sone.data.Sone.SoneStatus.unknown;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import net.pterodactylus.sone.core.FreenetInterface.Fetched;
import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.AlbumImpl;
+import net.pterodactylus.sone.data.Album.Modifier;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.ImageImpl;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.freenet.wot.Identity;
import freenet.support.api.Bucket;
import com.google.common.base.Optional;
+import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ListMultimap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
private final PostReplyBuilder postReplyBuilder = mock(PostReplyBuilder.class);
private final Set<PostReply> createdPostReplies = new HashSet<PostReply>();
private PostReply postReply = mock(PostReply.class);
+ private final AlbumBuilder albumBuilder = mock(AlbumBuilder.class);
+ private final ListMultimap<Album, Album> nestedAlbums = ArrayListMultimap.create();
+ private final ListMultimap<Album, Image> albumImages = ArrayListMultimap.create();
+ private Album album = mock(Album.class);
private final Map<String, Album> albums = new HashMap<String, Album>();
@Before
}
@Before
- public void setupAlbums() {
- albums.put("album-id-1", new AlbumImpl("album-id-1"));
- albums.put("album-id-2", new AlbumImpl("album-id-2"));
- when(core.getOrCreateAlbum(anyString())).thenAnswer(new Answer<Album>() {
+ public void setupAlbum() {
+ setupAlbum(album);
+ }
+
+ private void setupAlbum(final Album album) {
+ when(album.getAlbumImage()).thenReturn(mock(Image.class));
+ doAnswer(new Answer<Void>() {
@Override
- public Album answer(InvocationOnMock invocation) throws Throwable {
- return albums.get(invocation.getArguments()[0]);
+ public Void answer(InvocationOnMock invocation) {
+ nestedAlbums.put(album, (Album) invocation.getArguments()[0]);
+ return null;
+ }
+ }).when(album).addAlbum(any(Album.class));
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) {
+ albumImages.put(album, (Image) invocation.getArguments()[0]);
+ return null;
+ }
+ }).when(album).addImage(any(Image.class));
+ when(album.getAlbums()).thenAnswer(new Answer<List<Album>>() {
+ @Override
+ public List<Album> answer(InvocationOnMock invocation) {
+ return nestedAlbums.get(album);
+ }
+ });
+ when(album.getImages()).thenAnswer(new Answer<List<Image>>() {
+ @Override
+ public List<Image> answer(InvocationOnMock invocation) {
+ return albumImages.get(album);
}
});
- when(core.getAlbum(anyString(), anyBoolean())).thenAnswer(new Answer<Album>() {
+ final Modifier albumModifier = new Modifier() {
+ private String title = album.getTitle();
+ private String description = album.getDescription();
+ private String imageId = album.getAlbumImage().getId();
+
+ @Override
+ public Modifier setTitle(String title) {
+ this.title = title;
+ return this;
+ }
+
+ @Override
+ public Modifier setDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ @Override
+ public Modifier setAlbumImage(String imageId) {
+ this.imageId = imageId;
+ return this;
+ }
+
+ @Override
+ public Album update() throws IllegalStateException {
+ when(album.getTitle()).thenReturn(title);
+ when(album.getDescription()).thenReturn(description);
+ Image image = mock(Image.class);
+ when(image.getId()).thenReturn(imageId);
+ when(album.getAlbumImage()).thenReturn(image);
+ return album;
+ }
+ };
+ when(album.modify()).thenReturn(albumModifier);
+ }
+
+ @Before
+ public void setupAlbumBuilder() {
+ when(albumBuilder.withId(anyString())).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getId()).thenReturn((String) invocation.getArguments()[0]);
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.randomId()).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getId()).thenReturn(randomUUID().toString());
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.by(any(Sone.class))).thenAnswer(new Answer<AlbumBuilder>() {
+ @Override
+ public AlbumBuilder answer(InvocationOnMock invocation) {
+ when(album.getSone()).thenReturn((Sone) invocation.getArguments()[0]);
+ return albumBuilder;
+ }
+ });
+ when(albumBuilder.build()).thenAnswer(new Answer<Album>() {
+ @Override
+ public Album answer(InvocationOnMock invocation) {
+ Album album = SoneDownloaderTest.this.album;
+ albums.put(album.getId(), album);
+ SoneDownloaderTest.this.album = mock(Album.class);
+ setupAlbum(SoneDownloaderTest.this.album);
+ return album;
+ }
+ });
+ when(core.albumBuilder()).thenReturn(albumBuilder);
+ }
+
+ @Before
+ public void setupAlbums() {
+ when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
@Override
public Album answer(InvocationOnMock invocation) throws Throwable {
return albums.get(invocation.getArguments()[0]);
import static com.google.common.base.Optional.of;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.AlbumImpl;
+import net.pterodactylus.sone.data.Sone;
import com.google.common.base.Optional;
import org.junit.Test;
@Test
public void testBasicAlbumFunctionality() {
- Album newAlbum = new AlbumImpl();
+ Album newAlbum = new AlbumImpl(mock(Sone.class));
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
memoryDatabase.storeAlbum(newAlbum);
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));