-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create registry class for ThumbnailMetadata
- Loading branch information
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/jp/skypencil/brownie/registry/ThumbnailMetadataRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jp.skypencil.brownie.registry; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import jp.skypencil.brownie.FileId; | ||
import jp.skypencil.brownie.ThumbnailMetadata; | ||
import rx.Observable; | ||
import rx.Single; | ||
|
||
@ParametersAreNonnullByDefault | ||
interface ThumbnailMetadataRegistry { | ||
/** | ||
* Store a {@link ThumbnailMetadata} for specified video key. | ||
* | ||
* @param videoId | ||
* an UUID to specify target video | ||
* @param metadata | ||
* a {@link ThumbnailMetadata} to store into database | ||
* @return | ||
*/ | ||
@Nonnull | ||
Single<Void> store(@FileId UUID videoId, ThumbnailMetadata metadata); | ||
|
||
@Nonnull | ||
Observable<ThumbnailMetadata> search(@FileId UUID videoId); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/jp/skypencil/brownie/registry/ThumbnailMetadataRegistryOnPostgres.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package jp.skypencil.brownie.registry; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.ext.sql.ResultSet; | ||
import io.vertx.rxjava.ext.asyncsql.AsyncSQLClient; | ||
import jp.skypencil.brownie.ThumbnailMetadata; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
import rx.Observable; | ||
import rx.Single; | ||
|
||
@RequiredArgsConstructor | ||
@AllArgsConstructor(access = AccessLevel.PACKAGE) // for unit test | ||
public class ThumbnailMetadataRegistryOnPostgres | ||
implements ThumbnailMetadataRegistry { | ||
private static final String SQL_TO_INSERT = "INSERT INTO thumbnail_metadata (id, video_id, mime_type, content_length, width, height, second) VALUES (?, ?, ?, ?, ?, ?, ?)"; | ||
private static final String SQL_TO_SELECT = "SELECT id, video_id, mime_type, content_length, width, height, second FROM thumbnail_metadata WHERE video_id = ?"; | ||
|
||
@Resource | ||
private AsyncSQLClient asyncSQLClient; | ||
|
||
@Override | ||
public Single<Void> store(UUID videoId, ThumbnailMetadata metadata) { | ||
return asyncSQLClient.getConnectionObservable() | ||
.flatMap(con -> { | ||
return con.updateWithParamsObservable(SQL_TO_INSERT, metadata.toJsonArray()) | ||
.doAfterTerminate(con::close); | ||
}) | ||
.toSingle() | ||
.map(ur -> { | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public Observable<ThumbnailMetadata> search(UUID videoId) { | ||
return asyncSQLClient.getConnectionObservable() | ||
.flatMap(con -> { | ||
return con.queryWithParamsObservable(SQL_TO_SELECT, new JsonArray().add(videoId.toString())) | ||
.doAfterTerminate(con::close); | ||
}) | ||
.flatMapIterable(ResultSet::getRows) | ||
.map(ThumbnailMetadata::valueOf); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/jp/skypencil/brownie/registry/ThumbnailMetadataRegistryOnPostgresTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package jp.skypencil.brownie.registry; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.util.MimeType; | ||
|
||
import com.google.common.base.Objects; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.unit.Async; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import io.vertx.rxjava.core.Vertx; | ||
import io.vertx.rxjava.ext.asyncsql.AsyncSQLClient; | ||
import io.vertx.rxjava.ext.asyncsql.PostgreSQLClient; | ||
import jp.skypencil.brownie.ThumbnailMetadata; | ||
|
||
@RunWith(VertxUnitRunner.class) | ||
public class ThumbnailMetadataRegistryOnPostgresTest { | ||
private Vertx vertx; | ||
private AsyncSQLClient client; | ||
|
||
@Before | ||
public final void setUp() { | ||
vertx = Vertx.vertx(); | ||
client = PostgreSQLClient.createShared(vertx, createConfig()); | ||
} | ||
|
||
@After | ||
public final void cleanUp(TestContext context) { | ||
client.close(ar -> { | ||
vertx.close(context.asyncAssertSuccess()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testStore(TestContext context) { | ||
ThumbnailMetadataRegistry registry = new ThumbnailMetadataRegistryOnPostgres(client); | ||
UUID id = UUID.randomUUID(); | ||
UUID videoId = UUID.randomUUID(); | ||
ThumbnailMetadata metadata = new ThumbnailMetadata(id, videoId, MimeType.valueOf("image/jpg"), 100, 320, 240, 0.0); | ||
|
||
Async async = context.async(); | ||
registry.store(videoId, metadata).subscribe(v -> async.complete(), context::fail); | ||
} | ||
|
||
@Test | ||
public void testSearchNotExistId(TestContext context) { | ||
ThumbnailMetadataRegistry registry = new ThumbnailMetadataRegistryOnPostgres(client); | ||
UUID videoId = UUID.randomUUID(); | ||
|
||
Async async = context.async(); | ||
registry.search(videoId).count().subscribe(count -> { | ||
context.assertEquals(0, count); | ||
}, context::fail, async::complete); | ||
} | ||
|
||
@Test | ||
public void testStoreAndSearch(TestContext context) { | ||
ThumbnailMetadataRegistry registry = new ThumbnailMetadataRegistryOnPostgres(client); | ||
UUID id = UUID.randomUUID(); | ||
UUID videoId = UUID.randomUUID(); | ||
ThumbnailMetadata metadata = new ThumbnailMetadata(id, videoId, MimeType.valueOf("image/jpg"), 100, 320, 240, 0.0); | ||
|
||
Async async = context.async(); | ||
registry.store(videoId, metadata).toObservable().flatMap(v -> { | ||
return registry.search(videoId); | ||
}).filter(searched -> { | ||
return Objects.equal(metadata, searched); | ||
}).toSingle().subscribe(searched -> { | ||
async.complete(); | ||
}, context::fail); | ||
} | ||
|
||
private JsonObject createConfig() { | ||
return new JsonObject() | ||
.put("host", System.getProperty("db.host", "localhost")) | ||
.put("port", Integer.parseInt(System.getProperty("db.port", "5432"))) | ||
.put("username", "brownie") | ||
.put("password", "brownie") | ||
.put("database", "brownie"); | ||
} | ||
} |