Skip to content

Commit

Permalink
Added Media endpoints and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed Aug 16, 2024
1 parent 4a100e3 commit 0ee93a2
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
* limitations under the License.
*/

package org.mbari.oni.etc.jdk
package org.mbari.oni.endpoints

import scala.util.Random
import org.mbari.oni.PostgresMixin

object Strings:
class PostgresMediaEndpointsSuite extends MediaEndpointsSuite with PostgresMixin {

private val chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
private val random = new Random

def random(length: Int): String =
val xs = for (_ <- 0 until length) yield chars.charAt(random.nextInt(chars.length))
new String(xs.toArray)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 Monterey Bay Aquarium Research Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.mbari.oni.services

import org.mbari.oni.PostgresMixin

class PostgresMediaServiceSuite extends MediaServiceSuite with PostgresMixin {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 Monterey Bay Aquarium Research Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.mbari.oni.endpoints

import org.mbari.oni.SqlServerMixin

class SqlServerMediaEndpointsSuite extends MediaEndpointsSuite with SqlServerMixin {

}
158 changes: 158 additions & 0 deletions it/src/main/scala/org/mbari/oni/endpoints/MediaEndpointsSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2024 Monterey Bay Aquarium Research Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.mbari.oni.endpoints

import org.mbari.oni.domain.{Media, MediaCreate, MediaTypes, MediaUpdate}
import org.mbari.oni.etc.jdk.Strings
import org.mbari.oni.etc.jwt.JwtService
import org.mbari.oni.jdbc.FastPhylogenyService
import org.mbari.oni.jpa.DataInitializer
import org.mbari.oni.services.{ConceptService, UserAuthMixin}
import sttp.model.StatusCode
import org.mbari.oni.etc.circe.CirceCodecs.{*, given}

import java.net.URI
import scala.jdk.CollectionConverters.*

trait MediaEndpointsSuite extends EndpointsSuite with DataInitializer with UserAuthMixin {

given jwtService: JwtService = JwtService("mbari", "foo", "bar")
lazy val fastPhylogenyService = FastPhylogenyService(entityManagerFactory)
lazy val endpoints: MediaEndpoints = MediaEndpoints(entityManagerFactory, fastPhylogenyService, conceptService)
private val password = Strings.random(10)

def createMedia(): Seq[Media] = {
val root = init(1, 6)
root.getDescendants
.asScala
.flatMap(_.getConceptMetadata.getMedias.asScala)
.toSeq
.map(Media.from)
.sortBy(_.url.toExternalForm)
}

test("mediaForConcept") {
val expected = createMedia()
val opt = expected.head.conceptName
assert(opt.isDefined)
val name = opt.get
runGet(
endpoints.mediaForConceptEndpointImpl,
s"http://test.com/v1/media/${name}",
response =>
assertEquals(response.code, StatusCode.Ok)
val obtained = checkResponse[Seq[Media]](response.body)
.sortBy(_.url.toExternalForm)
assertEquals(obtained, expected)
)
}

test("createMedia") {
val root = init(2, 0)
assert(root != null)
val mediaCreate = MediaCreate(
conceptName = root.getName,
url = URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL,
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt = testWithUserAuth(
user =>
runPost(
endpoints.createMediaEndpointImpl,
"http://test.com/v1/media",
mediaCreate.stringify,
response =>
assertEquals(response.code, StatusCode.Ok)
val obtained = checkResponse[Media](response.body)
assertEquals(mediaCreate.conceptName, obtained.conceptName.getOrElse(""))
assertEquals(mediaCreate.url, obtained.url)
assertEquals(mediaCreate.caption, obtained.caption)
assertEquals(mediaCreate.credit, obtained.credit)
val t = Media.resolveMimeType(mediaCreate.mediaType.getOrElse(""), obtained.url.toExternalForm)
assertEquals(t, obtained.mimeType)
assertEquals(mediaCreate.isPrimary.getOrElse(false), obtained.isPrimary)
,
jwt = jwtService.login(user.username, password, user.toEntity)
),
password
)
attempt match
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}

test("updateMedia") {
val media = createMedia().head
val mediaUpdate = MediaUpdate(
url = Some(URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL),
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt = testWithUserAuth(
user =>
runPut(
endpoints.updateMediaEndpointImpl,
s"http://test.com/v1/media/${media.id.get}",
mediaUpdate.stringify,
response =>
assertEquals(response.code, StatusCode.Ok)
val obtained = checkResponse[Media](response.body)
assertEquals(mediaUpdate.url.orNull, obtained.url)
assertEquals(mediaUpdate.caption, obtained.caption)
assertEquals(mediaUpdate.credit, obtained.credit)
val t = Media.resolveMimeType(mediaUpdate.mediaType.getOrElse(""), obtained.url.toExternalForm)
assertEquals(t, obtained.mimeType)
assertEquals(mediaUpdate.isPrimary.getOrElse(false), obtained.isPrimary)
,
jwt = jwtService.login(user.username, password, user.toEntity)
),
password
)
attempt match
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}

test("deleteMedia") {
val media = createMedia().head
val attempt = testWithUserAuth(
user =>
runDelete(
endpoints.deleteMediaEndpointImpl,
s"http://test.com/v1/media/${media.id.get}",
response => assertEquals(response.code, StatusCode.Ok),
jwt = jwtService.login(user.username, password, user.toEntity)
),
password
)
attempt match
case Left(value) => fail(value.toString)
case Right(value) => assert(true)
}







}
124 changes: 114 additions & 10 deletions it/src/main/scala/org/mbari/oni/services/MediaServiceSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package org.mbari.oni.services

import org.mbari.oni.domain.MediaCreate
import org.mbari.oni.domain.{Media, MediaCreate, MediaTypes, MediaUpdate}
import org.mbari.oni.jdbc.FastPhylogenyService
import org.mbari.oni.jpa.DataInitializer
import org.mbari.oni.etc.circe.CirceCodecs.{*, given}
import org.mbari.oni.etc.jdk.Strings

import java.net.URI

Expand All @@ -30,16 +32,118 @@ trait MediaServiceSuite extends DataInitializer with UserAuthMixin:
test("create") {
val root = init(2, 0)
assert(root != null)
val a = conceptService.findByName(root.getName)
val mediaCreate = MediaCreate(
conceptName = root.getName,
url = URI.create("http://www.mbari.org").toURL
url = URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL,
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
// mediaService.create(mediaCreate) match
// case Left(e) => fail(e.getMessage)
// case Right(media) =>
// assertEquals(mediaCreate.conceptName, media.conceptName)
// assertEquals(mediaCreate.url, media.url)
// assertEquals(mediaCreate.user, media.user)
// assertEquals(mediaCreate.timestamp, media.timestamp)
val attempt = runWithUserAuth(user => mediaService.create(mediaCreate, user.username))

attempt match
case Left(e) => fail(e.getMessage)
case Right(media) =>
assert(media.id.isDefined)
assertEquals(mediaCreate.conceptName, media.conceptName.getOrElse(""))
assertEquals(mediaCreate.url, media.url)
assertEquals(mediaCreate.caption, media.caption)
assertEquals(mediaCreate.credit, media.credit)
val t = Media.resolveMimeType(mediaCreate.mediaType.getOrElse(""), media.url.toExternalForm)
assertEquals(t, media.mimeType)
assertEquals(mediaCreate.isPrimary.getOrElse(false), media.isPrimary)

}

test("update") {
val root = init(2, 0)
assert(root != null)
val mediaCreate = MediaCreate(
conceptName = root.getName,
url = URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL,
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val mediaUpdate = MediaUpdate(
url = Some(URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL),
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)

val attempt = runWithUserAuth(user =>
for
m0 <- mediaService.create(mediaCreate, user.username)
m1 <- mediaService.update(m0.id.getOrElse(0L), mediaUpdate, user.username)
yield m1
)

attempt match
case Left(e) => fail(e.getMessage)
case Right(media) =>
assertEquals(mediaUpdate.url.orNull, media.url)
assertEquals(mediaUpdate.caption, media.caption)
assertEquals(mediaUpdate.credit, media.credit)
val t = Media.resolveMimeType(mediaUpdate.mediaType.getOrElse(""), media.url.toExternalForm)
assertEquals(t, media.mimeType)
assertEquals(mediaUpdate.isPrimary.getOrElse(false), media.isPrimary)
}

test("delete") {
val root = init(2, 0)
assert(root != null)
val mediaCreate = MediaCreate(
conceptName = root.getName,
url = URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL,
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt = runWithUserAuth(user => mediaService.create(mediaCreate, user.username))

attempt match
case Left(e) => fail(e.getMessage)
case Right(media) =>
val attempt = runWithUserAuth(user => mediaService.deleteById(media.id.getOrElse(0L), user.username))
attempt match
case Left(e) => fail(e.getMessage)
case Right(_) =>
mediaService.findById(media.id.getOrElse(0L)) match
case Left(e) => fail(e.getMessage)
case Right(opt) => assert(opt.isEmpty)
}

test("findById") {
val root = init(2, 0)
assert(root != null)
val mediaCreate = MediaCreate(
conceptName = root.getName,
url = URI.create(s"http://www.mbari.org/${Strings.random(10)}.png").toURL,
caption = Some(Strings.random(1000)),
credit = Some(Strings.random(255)),
mediaType = Some(MediaTypes.IMAGE.name),
isPrimary = Some(true)
)
val attempt0 = runWithUserAuth(user => mediaService.create(mediaCreate, user.username))

attempt0 match
case Left(e) => fail(e.getMessage)
case Right(media) =>
val attempt1 = runWithUserAuth(user => mediaService.findById(media.id.getOrElse(0L)))
attempt1 match
case Left(e) => fail(e.getMessage)
case Right(opt) =>
assert(opt.isDefined)
val m = opt.get
assertEquals(media.id, m.id)
assertEquals(media.url, m.url)
assertEquals(media.caption, m.caption)
assertEquals(media.credit, m.credit)
assertEquals(media.mimeType, m.mimeType)
assertEquals(media.isPrimary, m.isPrimary)
}
7 changes: 5 additions & 2 deletions oni/src/main/scala/org/mbari/oni/domain/Media.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.mbari.oni.domain

import org.mbari.oni.etc.jdk.Strings

import java.net.{URI, URL}
import java.util.regex.Pattern
import org.mbari.oni.jpa.entities.MediaEntity
Expand Down Expand Up @@ -59,8 +61,9 @@ object Media:
)

def resolveMimeType(t: String, url: String): String =
val ext = url.split(Pattern.quote(".")).last
Try(MediaType.valueOf(t)) match
val ext = url.split(Pattern.quote(".")).last.toLowerCase
val mediaType = Strings.initCap(t)
Try(MediaType.valueOf(mediaType)) match
case Success(MediaType.Image) => s"image/$ext"
case Success(MediaType.Video) =>
ext match
Expand Down
Loading

0 comments on commit 0ee93a2

Please sign in to comment.