Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Bobrowski committed Nov 14, 2024
1 parent 9706066 commit cba1dda
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,17 @@ import com.campudus.tableaux.{ForbiddenException, InvalidJsonException, Tableaux
import com.campudus.tableaux.ArgumentChecker._
import com.campudus.tableaux.cache.CacheClient
import com.campudus.tableaux.database._
import com.campudus.tableaux.database.domain.{
AttachmentColumn,
CreateAttachmentColumn,
EmptyObject,
LinkColumn,
NameOnly,
_
}
import com.campudus.tableaux.database.domain.{CreateColumn, _}
import com.campudus.tableaux.database.model.StructureModel
import com.campudus.tableaux.database.model.TableauxModel._
import com.campudus.tableaux.database.model.structure.{CachedColumnModel, ColumnModel, TableGroupModel, TableModel}
import com.campudus.tableaux.helper.JsonUtils
import com.campudus.tableaux.database.model.structure.{CachedColumnModel, TableGroupModel, TableModel}
import com.campudus.tableaux.router.auth.permission._
import com.campudus.tableaux.verticles.JsonSchemaValidator.{JsonSchemaValidatorClient, ValidatorKeys}
import com.campudus.tableaux.verticles.MessagingVerticle.MessagingVerticleClient

import io.vertx.scala.core.Vertx
import io.vertx.scala.core.eventbus.EventBus
import io.vertx.scala.core.eventbus.Message
import io.vertx.scala.ext.web.RoutingContext
import org.vertx.scala.core.json._

import scala.collection.JavaConverters._
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}

import org.everit.json.schema.ValidationException
import org.json.JSONObject

object StructureController {

Expand Down Expand Up @@ -174,22 +157,18 @@ class StructureController(
}

val validator = JsonSchemaValidatorClient(vertx)
val tableFuture: Future[Table] = attributes match {

(attributes match {
case Some(s) => {
validator.validateJson(ValidatorKeys.ATTRIBUTES, s).flatMap(createTable).recover {
case ex => throw new InvalidJsonException(ex.getMessage(), "attributes")
case ex => throw InvalidJsonException(ex.getMessage(), "attributes")
}
}
case None => createTable(Unit)
}) map { table =>
messagingClient.tableCreated(table.id)
table
}

for {
createdTable <- tableFuture
} yield {
messagingClient.tableCreated(createdTable.id)
createdTable
}

}

private def buildTable(tableType: TableType, columns: Option[Seq[CreateColumn]])(implicit user: TableauxUser) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ class LinkCardinalityTest extends LinkTestBase with Helper {
okTest {
for {
tableId1 <- createEmptyDefaultTable(name = "table1")
tableId2 <- createEmptyDefaultTable(name = "table2", tableNum = 2)
tableId2 <- createEmptyDefaultTable(name = "table2")

linkColumnId1 <- createCardinalityLinkColumn(tableId1, tableId2, "cardinality", 2, 3)

Expand Down
34 changes: 17 additions & 17 deletions src/test/scala/com/campudus/tableaux/api/content/LinkTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ class LinkColumnTest extends LinkTestBase {
okTest {
for {
table1 <-
createEmptyDefaultTable(name = "Test Table 1", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 1")), None)
createEmptyDefaultTable(name = "Test Table 1", Some(Json.obj("de" -> "Test Deutsch 1")), None)
table2 <-
createEmptyDefaultTable(name = "Test Table 2", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 2")), None)
createEmptyDefaultTable(name = "Test Table 2", Some(Json.obj("de" -> "Test Deutsch 2")), None)

linkColumn = Json.obj("kind" -> "link", "name" -> "link", "toTable" -> table2)
columns = Json.obj("columns" -> Json.arr(linkColumn))
Expand All @@ -427,9 +427,9 @@ class LinkColumnTest extends LinkTestBase {
okTest {
for {
table1 <-
createEmptyDefaultTable(name = "Test Table 1", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 1")), None)
createEmptyDefaultTable(name = "Test Table 1", Some(Json.obj("de" -> "Test Deutsch 1")), None)
table2 <-
createEmptyDefaultTable(name = "Test Table 2", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 2")), None)
createEmptyDefaultTable(name = "Test Table 2", Some(Json.obj("de" -> "Test Deutsch 2")), None)

linkColumn = Json.obj(
"kind" -> "link",
Expand Down Expand Up @@ -461,9 +461,9 @@ class LinkColumnTest extends LinkTestBase {
okTest {
for {
table1 <-
createEmptyDefaultTable(name = "Test Table 1", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 1")), None)
createEmptyDefaultTable(name = "Test Table 1", Some(Json.obj("de" -> "Test Deutsch 1")), None)
table2 <-
createEmptyDefaultTable(name = "Test Table 2", tableNum = 1, Some(Json.obj("de" -> "Test Deutsch 2")), None)
createEmptyDefaultTable(name = "Test Table 2", Some(Json.obj("de" -> "Test Deutsch 2")), None)

linkColumn = Json.obj(
"kind" -> "link",
Expand Down Expand Up @@ -1207,17 +1207,17 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsSingleDirection(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1, postDefaultTableRow("Row 1 in Table 1", 1))
row12 <- addRow(table1, postDefaultTableRow("Row 2 in Table 1", 2))
row13 <- addRow(table1, postDefaultTableRow("Row 3 in Table 1", 3))

table2 <- createEmptyDefaultTable("Table 2", 2)
table2 <- createEmptyDefaultTable("Table 2")
row21 <- addRow(table2, postDefaultTableRow("Row 1 in Table 2", 1))
row22 <- addRow(table2, postDefaultTableRow("Row 2 in Table 2", 2))
row23 <- addRow(table2, postDefaultTableRow("Row 3 in Table 2", 3))

table3 <- createEmptyDefaultTable("Table 3", 3)
table3 <- createEmptyDefaultTable("Table 3")
row31 <- addRow(table3, postDefaultTableRow("Row 1 in Table 3", 1))
row32 <- addRow(table3, postDefaultTableRow("Row 2 in Table 3", 2))
row33 <- addRow(table3, postDefaultTableRow("Row 3 in Table 3", 3))
Expand Down Expand Up @@ -1297,17 +1297,17 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsBothDirection(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1, postDefaultTableRow("Row 1 in Table 1", 1))
row12 <- addRow(table1, postDefaultTableRow("Row 2 in Table 1", 2))
row13 <- addRow(table1, postDefaultTableRow("Row 3 in Table 1", 3))

table2 <- createEmptyDefaultTable("Table 2", 2)
table2 <- createEmptyDefaultTable("Table 2")
row21 <- addRow(table2, postDefaultTableRow("Row 1 in Table 2", 1))
row22 <- addRow(table2, postDefaultTableRow("Row 2 in Table 2", 2))
row23 <- addRow(table2, postDefaultTableRow("Row 3 in Table 2", 3))

table3 <- createEmptyDefaultTable("Table 3", 3)
table3 <- createEmptyDefaultTable("Table 3")
row31 <- addRow(table3, postDefaultTableRow("Row 1 in Table 3", 1))
row32 <- addRow(table3, postDefaultTableRow("Row 2 in Table 3", 2))
row33 <- addRow(table3, postDefaultTableRow("Row 3 in Table 3", 3))
Expand Down Expand Up @@ -1419,12 +1419,12 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsWithTwoLinkColumns(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1, postDefaultTableRow("Row 1 in Table 1", 1))
row12 <- addRow(table1, postDefaultTableRow("Row 2 in Table 1", 2))
row13 <- addRow(table1, postDefaultTableRow("Row 3 in Table 1", 3))

table2 <- createEmptyDefaultTable("Table 2", 2)
table2 <- createEmptyDefaultTable("Table 2")
row21 <- addRow(table2, postDefaultTableRow("Row 1 in Table 2", 1))
row22 <- addRow(table2, postDefaultTableRow("Row 2 in Table 2", 2))
row23 <- addRow(table2, postDefaultTableRow("Row 3 in Table 2", 3))
Expand Down Expand Up @@ -1474,7 +1474,7 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsSelfLink(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1, postDefaultTableRow("Row 1 in Table 1", 1))
row12 <- addRow(table1, postDefaultTableRow("Row 2 in Table 1", 2))
row13 <- addRow(table1, postDefaultTableRow("Row 3 in Table 1", 3))
Expand Down Expand Up @@ -1541,7 +1541,7 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsOfRowWithoutDependencies(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1)
row12 <- addRow(table1)
row13 <- addRow(table1)
Expand All @@ -1559,7 +1559,7 @@ class LinkTest extends LinkTestBase {
@Test
def retrieveDependentRowsOfTableWithoutLinks(implicit c: TestContext): Unit = okTest {
for {
table1 <- createEmptyDefaultTable("Table 1", 1)
table1 <- createEmptyDefaultTable("Table 1")
row11 <- addRow(table1)
row12 <- addRow(table1)
row13 <- addRow(table1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.campudus.tableaux.database.domain.DomainObject
import com.campudus.tableaux.database.model.SystemModel
import com.campudus.tableaux.database.model.TableauxModel.{ColumnId, RowId, TableId}
import com.campudus.tableaux.helper.FileUtils
import com.campudus.tableaux.router.auth.KeycloakAuthHandler
import com.campudus.tableaux.router.auth.permission.{RoleModel, TableauxUser}
import com.campudus.tableaux.testtools.RequestCreation.ColumnType

import io.vertx.core.Handler
import io.vertx.core.buffer.Buffer
import io.vertx.core.http.HttpMethod
import io.vertx.ext.unit.TestContext
Expand All @@ -21,11 +21,9 @@ import io.vertx.scala.core.{DeploymentOptions, Vertx}
import io.vertx.scala.core.file.{AsyncFile, OpenOptions}
import io.vertx.scala.core.http._
import io.vertx.scala.core.streams.Pump
import io.vertx.scala.ext.web.RoutingContext
import org.vertx.scala.core.json.{JsonObject, _}

import scala.collection.JavaConverters._
import scala.collection.mutable.HashMap
import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -122,7 +120,7 @@ trait TableauxTestBase
val system = SystemModel(dbConnection)
for {
_ <- system.uninstall()
_ <- system.installShortCutFunction()
_ <- system.installShortCutFunction()
_ <- system.install()
} yield {
vertx
Expand Down Expand Up @@ -152,7 +150,10 @@ trait TableauxTestBase
def initRoleModel(roleConfig: String): RoleModel = {
val roleModel: RoleModel = RoleModel(Json.fromObjectString(roleConfig.stripMargin))

setRequestRoles(roleModel.role2permissions.keySet.toSeq)
val roles = collection.immutable.Seq(roleModel.role2permissions.keySet.toSeq: _*)

setRequestRoles(roles)

roleModel
}

Expand All @@ -172,7 +173,7 @@ trait TableauxTestBase
*/
protected def asDevUser[A](function: => Future[A]): Future[A] = {
// format: on
val userRolesFromTest: Seq[String] = user.roles
val userRolesFromTest: Seq[String] = collection.immutable.Seq(user.roles: _*)

setRequestRoles(Seq("dev"))

Expand Down Expand Up @@ -270,23 +271,25 @@ trait TableauxTestBase
p: Promise[A],
function: String => A
): (HttpClient, HttpClientResponse) => Unit = { (client: HttpClient, resp: HttpClientResponse) =>
def bodyHandler(buf: Buffer): Unit = {
val body = buf.toString()

client.close()

if (resp.statusCode() != 200) {
p.failure(TestCustomException(body, resp.statusMessage(), resp.statusCode()))
} else {
try {
p.success(function(body))
} catch {
case ex: Exception => p.failure(ex)
def bodyHandler = new Handler[Buffer] {
override def handle(buf: Buffer): Unit = {
val body = buf.toString()

client.close()

if (resp.statusCode() != 200) {
p.failure(TestCustomException(body, resp.statusMessage(), resp.statusCode()))
} else {
try {
p.success(function(body))
} catch {
case ex: Exception => p.failure(ex)
}
}
}
}

resp.bodyHandler(bodyHandler(_: Buffer))
resp.bodyHandler(bodyHandler)
}

private def createExceptionHandler[A](p: Promise[A]): (HttpClient, Throwable) => Unit = {
Expand Down Expand Up @@ -338,8 +341,12 @@ trait TableauxTestBase
client
.request(_method, port, host, path)
.putHeader("Authorization", s"Bearer $token")
.handler(responseHandler(client, _: HttpClientResponse))
.exceptionHandler(exceptionHandler(client, _: Throwable))
.handler(new Handler[HttpClientResponse] {
override def handle(resp: HttpClientResponse): Unit = responseHandler(client, resp)
})
.exceptionHandler(new Handler[Throwable] {
override def handle(x: Throwable): Unit = exceptionHandler(client, x)
})
}

protected def uploadFile(method: String, url: String, file: String, mimeType: String): Future[JsonObject] = {
Expand Down Expand Up @@ -369,23 +376,27 @@ trait TableauxTestBase
asyncFile.map({ file =>
val pump = Pump.pump(file, req)

file.exceptionHandler({ e: Throwable =>
pump.stop()
req.end("")
p.failure(e)
file.exceptionHandler(new Handler[Throwable] {
override def handle(e: Throwable): Unit = {
pump.stop()
req.end("")
p.failure(e)
}
})

file.endHandler({ _ =>
file
.closeFuture()
.onComplete({
case Success(_) =>
logger.info(s"File loaded, ending request, ${pump.numberPumped()} bytes pumped.")
req.end(footer)
case Failure(e) =>
req.end("")
p.failure(e)
})
file.endHandler(new Handler[Unit] {
override def handle(event: Unit): Unit = {
file
.closeFuture()
.onComplete({
case Success(_) =>
logger.info(s"File loaded, ending request, ${pump.numberPumped()} bytes pumped.")
req.end(footer)
case Failure(e) =>
req.end("")
p.failure(e)
})
}
})

pump.start()
Expand Down Expand Up @@ -423,12 +434,11 @@ trait TableauxTestBase
Json.obj("columns" -> Json.arr(Json.obj("kind" -> "boolean", "name" -> "Test Column 4")))

for {
column1 <- sendRequest("POST", s"/tables/$tableId/columns", createShortTextColumnJson)
column2 <- sendRequest("POST", s"/tables/$tableId/columns", createRichTextColumnJson)
column3 <- sendRequest("POST", s"/tables/$tableId/columns", createNumberColumnJson)
column4 <- sendRequest("POST", s"/tables/$tableId/columns", createBooleanColumnJson)
_ <- sendRequest("POST", s"/tables/$tableId/columns", createShortTextColumnJson)
_ <- sendRequest("POST", s"/tables/$tableId/columns", createRichTextColumnJson)
_ <- sendRequest("POST", s"/tables/$tableId/columns", createNumberColumnJson)
_ <- sendRequest("POST", s"/tables/$tableId/columns", createBooleanColumnJson)
} yield ()

}

protected def createFullStatusTestTable(): Future[TableId] = {
Expand All @@ -454,7 +464,6 @@ trait TableauxTestBase

protected def createEmptyDefaultTable(
name: String = "Test Table 1",
tableNum: Int = 1,
displayName: Option[JsonObject] = None,
description: Option[JsonObject] = None
): Future[TableId] = {
Expand Down Expand Up @@ -498,7 +507,7 @@ trait TableauxTestBase
val fillNumberCellJson2 = Json.obj("value" -> 2)

for {
tableId <- createEmptyDefaultTable(name, tableNum, displayName, description)
tableId <- createEmptyDefaultTable(name, displayName, description)
_ <- sendRequest("POST", s"/tables/$tableId/rows")
_ <- sendRequest("POST", s"/tables/$tableId/rows")
_ <- sendRequest("POST", s"/tables/$tableId/columns/1/rows/1", fillStringCellJson)
Expand Down Expand Up @@ -562,7 +571,7 @@ trait TableauxTestBase
rows <- sendRequest("POST", s"/tables/$tableId/rows", valuesRow(columnIds))
_ = logger.info(s"Row is $rows")
rowIds = rows.getJsonArray("rows").asScala.map(_.asInstanceOf[JsonObject].getLong("id").toLong).toSeq
} yield (tableId, columnIds, rowIds)
} yield (tableId, columnIds, collection.immutable.Seq(rowIds: _*))
}

protected def createSimpleTableWithMultilanguageColumn(
Expand Down Expand Up @@ -605,7 +614,7 @@ trait TableauxTestBase
columns <- sendRequest("POST", s"/tables/$tableId/columns", createMultilanguageColumn)
columnIds = columns.getJsonArray("columns").asScala.map(_.asInstanceOf[JsonObject].getLong("id").toLong).toSeq
} yield {
(tableId.toLong, columnIds)
(tableId.toLong, collection.immutable.Seq(columnIds: _*))
}
}

Expand Down

0 comments on commit cba1dda

Please sign in to comment.