Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 7, 2024
1 parent 70a0b12 commit 57cf27a
Show file tree
Hide file tree
Showing 64 changed files with 467 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ package org.mbari.oni
import jakarta.persistence.EntityManagerFactory
import org.mbari.oni.jpa.AzureEntityManagerFactoryProvider

trait DbMixin {
trait SqlServerMixin {
def entityManagerFactory: EntityManagerFactory = AzureEntityManagerFactoryProvider.entityManagerFactory
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class SqlServerPhylogenyEndpointsSuite extends PhylogenyEndpointsSuite {
override given entityManagerFactory: EntityManagerFactory = AzureEntityManagerFactoryProvider.entityManagerFactory
// AzureEntityManagerFactoryProvider.init

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ object AzureEntityManagerFactoryProvider extends EntityManagerFactoryProvider {
testProps
)

// lazy val init: ConceptEntity = TestRepository.init(entityManagerFactory)


}
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.SqlServerMixin

class SqlServerConceptNameServiceSuite extends ConceptNameServiceSuite with SqlServerMixin {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package org.mbari.oni.services
import jakarta.persistence.EntityManagerFactory
import org.mbari.oni.DbMixin
import org.mbari.oni.SqlServerMixin
import org.mbari.oni.jpa.AzureEntityManagerFactoryProvider

class SqlServerConceptServiceSuite extends ConceptServiceSuite with DbMixin{
class SqlServerConceptServiceSuite extends ConceptServiceSuite with SqlServerMixin {


}
1 change: 0 additions & 1 deletion it/src/main/resources/concat/01.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
1
d
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import sttp.client3.testing.SttpBackendStub
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.stub.TapirStubInterpreter

import scala.concurrent.Future
import sttp.tapir.server.nima.{Id, NimaServerOptions}
import sttp.tapir.server.interceptor.exception.ExceptionHandler
import sttp.tapir.server.interceptor.CustomiseInterceptors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,62 @@
package org.mbari.oni.endpoints

import jakarta.persistence.EntityManagerFactory
import org.mbari.oni.jpa.EntityManagerFactories.*
import org.mbari.oni.jpa.DatabaseFunSuite
import org.mbari.oni.etc.jdk.Loggers.given
import org.mbari.oni.jpa.entities.{ConceptEntity, TestEntityFactory}
import org.mbari.oni.services.ConceptService

import java.util.concurrent.atomic.AtomicReference

trait PhylogenyEndpointsSuite extends EndpointsSuite with DatabaseFunSuite:

private val log = System.getLogger(getClass.getName)

private lazy val endpoints = new PhylogenyEndpoints(entityManagerFactory)
private lazy val service = new ConceptService(entityManagerFactory)
private val atomicRoot = new AtomicReference[ConceptEntity]()

override def beforeEach(context: BeforeEach): Unit =
val root = TestEntityFactory.buildRoot(5, 1)
service.init(root) match
case Right(entity) =>
atomicRoot.set(entity)
case Left(error) =>
log.atError.withCause(error).log("Failed to initialize test data")

override def afterEach(context: AfterEach): Unit =
val root = atomicRoot.get()
service.deleteByName(root.getPrimaryConceptName.getName) match
case Right(_) =>
log.atInfo.log("Deleted test data")
case Left(error) =>
log.atError.withCause(error).log("Failed to delete test data")

test("up") {
val root = atomicRoot.get()
val last = root
.getChildConcepts
.iterator()
.next()
.getChildConcepts
.iterator()
.next()
.getChildConcepts
.iterator()
.next()
.getChildConcepts
.iterator()
.next()
val name = last.getPrimaryConceptName.getName

runGet(
endpoints.upEndpointImpl,
s"http://test.com/v1/pylogeny/up/Nanomia",
response =>
log.atInfo.log(response.body.toString)
assert(response.body.contains("Nanomia"))
)
// runGet(
// endpoints.upEndpointImpl,
// s"http://test.com/v1/phylogeny/up/$name",
// response =>
// log.atInfo.log(response.body.toString)
// assert(response.body.contains(name))
// )
}

test("down") {}
Expand All @@ -44,5 +82,3 @@ trait PhylogenyEndpointsSuite extends EndpointsSuite with DatabaseFunSuite:
test("basic") {}

test("taxa") {}

override def entityManagerFactory: EntityManagerFactory = ???
46 changes: 46 additions & 0 deletions it/src/main/scala/org/mbari/oni/jpa/DataInitializer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.jpa

import org.mbari.oni.jpa.entities.{ConceptEntity, TestEntityFactory}
import org.mbari.oni.services.ConceptService
import org.mbari.oni.etc.jdk.Loggers.given

import java.util.concurrent.atomic.AtomicReference

trait DataInitializer extends DatabaseFunSuite:

private val log = System.getLogger(getClass.getName)

lazy val conceptService: ConceptService = new ConceptService(entityManagerFactory)
val atomicRoot: AtomicReference[ConceptEntity] = new AtomicReference[ConceptEntity]()

override def beforeEach(context: BeforeEach): Unit =
val root = TestEntityFactory.buildRoot(5, 1)
conceptService.init(root) match
case Right(entity) =>
atomicRoot.set(entity)
case Left(error) =>
log.atError.withCause(error).log("Failed to initialize test data")

override def afterEach(context: AfterEach): Unit =
val root = atomicRoot.get()
conceptService.deleteByName(root.getPrimaryConceptName.getName) match
case Right(_) =>
log.atInfo.log("Deleted test data")
case Left(error) =>
log.atError.withCause(error).log("Failed to delete test data")
6 changes: 6 additions & 0 deletions it/src/main/scala/org/mbari/oni/jpa/DatabaseFunSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@

package org.mbari.oni.jpa

import org.mbari.oni.jpa.entities.{ConceptEntity, TestEntityFactory}
import org.mbari.oni.services.ConceptService
import org.mbari.oni.etc.jdk.Loggers.given

import java.util.concurrent.atomic.AtomicReference

trait DatabaseFunSuite extends munit.FunSuite with EntityManagerFactoryProvider {}
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,53 @@ object TestEntityFactory:
private val random = new scala.util.Random
private val nextConceptId = new AtomicLong(0)

def buildRoot(depth: Int = 0, maxBreadth: Int = 0): ConceptEntity =
val root = buildTree(depth, maxBreadth)
def buildRoot(depth: Int = 1, maxBreadth: Int = 0): ConceptEntity =
val root = buildNode(maxBreadth)
val synonym = new ConceptNameEntity("root", ConceptNameTypes.SYNONYM.getType)
val common = new ConceptNameEntity("object", ConceptNameTypes.COMMON.getType)
root.addConceptName(synonym)
root.addConceptName(common)
if depth > 1 then buildTree(root, depth - 1, maxBreadth)
root

def buildTree(depth: Int = 0, maxBreadth: Int = 0): ConceptEntity =
val entity = createConcept()
if depth > 0 && maxBreadth > 0 then
val breadth = random.nextInt(maxBreadth)
for _ <- 0 until breadth do
val child = buildTree(depth - 1, maxBreadth)
val metadata = child.getConceptMetadata

val mediaBreadth = random.nextInt(maxBreadth)
for _ <- 0 until mediaBreadth do
val media = createMedia()
metadata.addMedia(media)

val linkTemplateBreadth = random.nextInt(maxBreadth)
for _ <- 0 until linkTemplateBreadth do
val linkTemplate = createLinkTemplate()
metadata.addLinkTemplate(linkTemplate)

val linkRealizationBreadth = random.nextInt(maxBreadth)
for _ <- 0 until linkRealizationBreadth do
val linkRealization = createLinkRealization()
metadata.addLinkRealization(linkRealization)

val historyBreadth = random.nextInt(maxBreadth)
for _ <- 0 until historyBreadth do
val history = createHistory()
metadata.addHistory(history)

entity.addChildConcept(child)
def buildTree(parent: ConceptEntity, depth: Int = 0, maxBreadth: Int = 0): ConceptEntity =
if depth == 0 then parent
else
val entity = buildNode(maxBreadth)
parent.addChildConcept(entity)
buildTree(entity, depth - 1, maxBreadth)

def buildNode(maxBreadth: Int): ConceptEntity =
val entity = createConcept()
val metadata = entity.getConceptMetadata

if maxBreadth > 0 then

val conceptNameBreadth = random.between(1, maxBreadth + 1)
for _ <- 0 until conceptNameBreadth do
val conceptName = createConceptName(false)
entity.addConceptName(conceptName)

val mediaBreadth = random.between(1, maxBreadth + 1)
for _ <- 0 until mediaBreadth do
val media = createMedia()
metadata.addMedia(media)

val linkTemplateBreadth = random.between(1, maxBreadth + 1)
for _ <- 0 until linkTemplateBreadth do
val linkTemplate = createLinkTemplate()
metadata.addLinkTemplate(linkTemplate)

val linkRealizationBreadth = random.between(1, maxBreadth + 1)
for _ <- 0 until linkRealizationBreadth do
val linkRealization = createLinkRealization()
metadata.addLinkRealization(linkRealization)

val historyBreadth = random.between(1, maxBreadth + 1)
for _ <- 0 until historyBreadth do
val history = createHistory()
metadata.addHistory(history)

entity

def createConceptName(isPrimary: Boolean = true): ConceptNameEntity =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object TestRepository:
case None => throw new RuntimeException("Failed to read test data");
case Some(rawConcept) =>
log.atInfo.log(s"Inserting a kb tree from $path")
val conceptEntity = rawConcept.toEntity
val conceptEntity = rawConcept.toEntity
val conceptService = new ConceptService(entityManagerFactory)
conceptService.nonAcidInit(conceptEntity)
conceptEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@

package org.mbari.oni.services

import org.mbari.oni.jpa.DatabaseFunSuite
import org.mbari.oni.domain.RawConcept
import org.mbari.oni.jpa.entities.ConceptEntity
import org.mbari.oni.jpa.{DataInitializer, DatabaseFunSuite}
import org.mbari.oni.jpa.EntityManagerFactories.*

trait ConceptNameServiceSuite extends DatabaseFunSuite {
import scala.jdk.CollectionConverters.*

trait ConceptNameServiceSuite extends DataInitializer:

lazy val conceptNameService: ConceptNameService = new ConceptNameService(entityManagerFactory)

test("findAllNames") {
fail("Not implemented yet")
}
val root = atomicRoot.get()
assert(root != null)
val rawRoot = RawConcept.fromEntity(root)
val expected = rawRoot.descendantNames.toSeq.sorted
conceptNameService.findAllNames() match
case Right(names) =>
val obtained = names.sorted
assertEquals(obtained, expected)
case Left(error) =>
fail(error.toString)

}
}
Loading

0 comments on commit 57cf27a

Please sign in to comment.