|
| 1 | +package org.broadinstitute.dsde.workbench.sam.dataAccess |
| 2 | + |
| 3 | +import akka.http.scaladsl.model.StatusCodes |
| 4 | +import cats.effect.unsafe.implicits.global |
| 5 | +import org.broadinstitute.dsde.workbench.model.WorkbenchExceptionWithErrorReport |
| 6 | +import org.broadinstitute.dsde.workbench.sam.TestSupport.{databaseEnabled, databaseEnabledClue, samRequestContext} |
| 7 | +import org.broadinstitute.dsde.workbench.sam.matchers.TimeMatchers |
| 8 | +import org.broadinstitute.dsde.workbench.sam.model.api.SamLock |
| 9 | +import org.broadinstitute.dsde.workbench.sam.{RetryableAnyFreeSpec, TestSupport} |
| 10 | +import org.scalatest.matchers.should.Matchers |
| 11 | +import org.scalatest.{BeforeAndAfterEach, OptionValues} |
| 12 | +import spray.json.{JsObject, JsString} |
| 13 | + |
| 14 | +import java.util.UUID |
| 15 | + |
| 16 | +class PostgresLockDAOSpec extends RetryableAnyFreeSpec with Matchers with BeforeAndAfterEach with TimeMatchers with OptionValues { |
| 17 | + val dao = new PostgresLockDAO(TestSupport.dbRef, TestSupport.dbRef) |
| 18 | + val description = "Test lock" |
| 19 | + val lockType = "DUOS" |
| 20 | + val lockDetails: JsObject = JsObject("datasetId" -> JsString("DUOS_123")) |
| 21 | + val lockNoDescription: SamLock = SamLock(UUID.randomUUID(), null, lockType, lockDetails) |
| 22 | + |
| 23 | + override protected def beforeEach(): Unit = |
| 24 | + TestSupport.truncateAll |
| 25 | + |
| 26 | + "PostgresLockDAO" - { |
| 27 | + "create" - { |
| 28 | + "create a lock" in { |
| 29 | + assume(databaseEnabled, databaseEnabledClue) |
| 30 | + val lock: SamLock = SamLock(UUID.randomUUID(), description, lockType, lockDetails) |
| 31 | + dao.create(lock, samRequestContext = samRequestContext).unsafeRunSync() shouldEqual lock |
| 32 | + } |
| 33 | + "creating a lock with a duplicate id fails" in { |
| 34 | + assume(databaseEnabled, databaseEnabledClue) |
| 35 | + val lock: SamLock = SamLock(UUID.randomUUID(), description, lockType, lockDetails) |
| 36 | + dao.create(lock, samRequestContext = samRequestContext).unsafeRunSync() |
| 37 | + val exception = intercept[WorkbenchExceptionWithErrorReport] { |
| 38 | + dao.create(lock, samRequestContext = samRequestContext).unsafeRunSync() |
| 39 | + } |
| 40 | + exception.errorReport.statusCode shouldEqual Some(StatusCodes.Conflict) |
| 41 | + } |
| 42 | + } |
| 43 | + "delete" - { |
| 44 | + "delete a lock" in { |
| 45 | + assume(databaseEnabled, databaseEnabledClue) |
| 46 | + val lock: SamLock = SamLock(UUID.randomUUID(), description, lockType, lockDetails) |
| 47 | + dao.create(lock, samRequestContext = samRequestContext).unsafeRunSync() shouldEqual lock |
| 48 | + dao.delete(lock.id, samRequestContext).unsafeRunSync() shouldBe true |
| 49 | + } |
| 50 | + "delete a non-existent lock" in { |
| 51 | + assume(databaseEnabled, databaseEnabledClue) |
| 52 | + dao.delete(UUID.randomUUID(), samRequestContext).unsafeRunSync() shouldBe false |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments