Skip to content

Commit

Permalink
refactor: Make Project.shortcode typed
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jan 23, 2025
1 parent b58e89b commit ebe7934
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
private val testPass = SharedTestDataADM.testPass
private val projectIri = SharedTestDataADM.imagesProject.id
private val projectIriEnc = URLEncoder.encode(projectIri, "utf-8")
private val projectShortname = SharedTestDataADM.imagesProject.shortname.value
private val projectShortname = SharedTestDataADM.imagesProject.shortname
private val projectShortcode = SharedTestDataADM.imagesProject.shortcode

override lazy val rdfDataObjects: List[RdfDataObject] = List(
Expand Down Expand Up @@ -170,7 +170,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
result.id should be(customProjectIri)

// check the rest of project info
result.shortcode should be("3333")
result.shortcode.value should be("3333")
result.shortname.value should be("newprojectWithIri")
result.longname should be(Some("new project with a custom IRI"))
result.keywords should be(Seq("projectIRI"))
Expand Down Expand Up @@ -230,7 +230,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {

val result = AkkaHttpUtils.httpResponseToJson(response).fields("project").convertTo[Project]
result.shortname.value should be("newproject")
result.shortcode should be("1111")
result.shortcode.value should be("1111")
result.longname should be(Some("project longname"))
result.description should be(Seq(StringLiteralV2.from(value = "project description", language = Some("en"))))
result.keywords should be(Seq("keywords"))
Expand Down Expand Up @@ -355,7 +355,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {

val result: Project = AkkaHttpUtils.httpResponseToJson(response).fields("project").convertTo[Project]
result.shortname.value should be("newproject")
result.shortcode should be("1111")
result.shortcode.value should be("1111")
result.longname should be(Some("updated project longname"))
result.description should be(
Seq(StringLiteralV2.from(value = "updated project description", language = Some("en"))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import org.knora.webapi.slice.admin.domain.model.Group
import org.knora.webapi.slice.admin.domain.model.KnoraProject.CopyrightAttribution
import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseText
import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseUri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortname
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.common.Value.StringValue
Expand Down Expand Up @@ -221,6 +222,10 @@ object IntegrationTestAdminJsonProtocol extends TriplestoreJsonProtocol {
override val from: String => Either[String, Shortname] = Shortname.from
}

implicit object ShortcodeFormat extends StringValueFormat[Shortcode] {
override val from: String => Either[String, Shortcode] = Shortcode.from
}

implicit val groupFormat: JsonFormat[Group] = jsonFormat6(Group.apply)

implicit val projectAdminMembersGetResponseADMFormat: RootJsonFormat[ProjectAdminMembersGetResponseADM] = rootFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.knora.webapi.slice.admin.api.model.Project
import org.knora.webapi.slice.admin.domain.model.KnoraProject.CopyrightAttribution
import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseText
import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseUri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode

sealed abstract case class UploadFileRequest private (
fileType: FileType,
Expand All @@ -39,7 +40,7 @@ sealed abstract case class UploadFileRequest private (
* @return JSON-LD serialization of the request.
*/
def toJsonLd(
shortcode: String = "0001",
shortcode: Shortcode = Shortcode.unsafeFrom("0001"),
ontologyName: String = "knora-api",
className: Option[String] = None,
ontologyIRI: Option[String] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.knora.webapi.messages.v2.responder.resourcemessages.CreateValueInNewR
import org.knora.webapi.messages.v2.responder.valuemessages.DocumentFileValueContentV2
import org.knora.webapi.messages.v2.responder.valuemessages.FileValueV2
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode

class FileModelsSpec extends CoreSpec {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
Expand Down Expand Up @@ -414,8 +415,8 @@ class FileModelsSpec extends CoreSpec {

"creating a ChangeFileRequest," should {
"create a valid representation of a DocumentRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.DocumentFile(),
Expand All @@ -438,8 +439,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a DocumentRepresentation with custom values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val pageCount = Some(33)
val dimX = Some(44)
val dimY = Some(55)
Expand Down Expand Up @@ -473,8 +474,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a StillImageRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.StillImageFile(),
Expand All @@ -496,8 +497,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a MovingImageRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.MovingImageFile(),
Expand All @@ -519,8 +520,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a AudioRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.AudioFile,
Expand All @@ -537,8 +538,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a TextRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.TextFile,
Expand All @@ -555,8 +556,8 @@ class FileModelsSpec extends CoreSpec {
}

"create a valid representation of a ArchiveRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("0000")
val valueIRI = stringFormatter.makeRandomResourceIri("0000")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("0000"))

val change = ChangeFileRequest.make(
fileType = FileType.ArchiveFile,
Expand All @@ -575,8 +576,8 @@ class FileModelsSpec extends CoreSpec {

"generating a JSON-LD representation of a ChangeFileRequest," should {
"correctly serialize a DocumentRepresentation with default values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("7777")
val valueIRI = stringFormatter.makeRandomResourceIri("7777")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("7777"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("7777"))

val documentRepresentation = ChangeFileRequest.make(
fileType = FileType.DocumentFile(),
Expand Down Expand Up @@ -604,8 +605,8 @@ class FileModelsSpec extends CoreSpec {
}

"correctly serialize a DocumentRepresentation with custom values" in {
val resourceIRI = stringFormatter.makeRandomResourceIri("7777")
val valueIRI = stringFormatter.makeRandomResourceIri("7777")
val resourceIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("7777"))
val valueIRI = stringFormatter.makeRandomResourceIri(Shortcode.unsafeFrom("7777"))
val className = "CustomDocumentRepresentation"
val prefix = "onto"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
"return restricted view settings using project SHORTCODE" in {
val actual = UnsafeZioRun.runOrThrow(
ProjectRestService(
_.getProjectRestrictedViewSettingsByShortcode(SharedTestDataADM.imagesProject.getShortcode),
_.getProjectRestrictedViewSettingsByShortcode(SharedTestDataADM.imagesProject.shortcode),
),
)
actual shouldEqual expectedResult
Expand Down Expand Up @@ -169,7 +169,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
)

received.project.shortname.value should be("newproject")
received.project.shortcode should be(shortcode.toUpperCase) // upper case
received.project.shortcode.value should be(shortcode.toUpperCase) // upper case
received.project.longname should contain("project longname")
received.project.description should be(
Seq(StringLiteralV2.from(value = "project description", language = Some("en"))),
Expand Down Expand Up @@ -263,7 +263,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
)

received.project.shortname.value should be("newproject2")
received.project.shortcode should be("1112")
received.project.shortcode.value should be("1112")
received.project.longname should contain("project longname")
received.project.description should be(
Seq(StringLiteralV2.from(value = "project description", language = Some("en"))),
Expand Down Expand Up @@ -383,7 +383,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
),
)
received.project.shortname.value should be("newproject")
received.project.shortcode should be("111C")
received.project.shortcode.value should be("111C")
received.project.longname should be(Some("updated project longname"))
received.project.description should be(
Seq(
Expand Down Expand Up @@ -453,7 +453,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
"return all members of a project identified by shortcode" in {
val actual = UnsafeZioRun.runOrThrow(
ProjectRestService(
_.getProjectMembersByShortcode(SharedTestDataADM.rootUser, SharedTestDataADM.imagesProject.getShortcode),
_.getProjectMembersByShortcode(SharedTestDataADM.rootUser, SharedTestDataADM.imagesProject.shortcode),
),
)

Expand Down Expand Up @@ -530,7 +530,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
ProjectRestService(
_.getProjectAdminMembersByShortcode(
SharedTestDataADM.rootUser,
SharedTestDataADM.imagesProject.getShortcode,
SharedTestDataADM.imagesProject.shortcode,
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ object ProjectInfo {
ProjectInfo(
id = project.id,
shortname = project.shortname.value,
shortcode = project.shortcode,
shortcode = project.shortcode.value,
longname = project.longname,
description = project.description.headOption.map(_.value),
keywords = project.keywords.headOption.map(_ => project.keywords.mkString(", ")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.slice.admin.api.model.Project
import org.knora.webapi.slice.admin.domain.model.Group
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortname
import org.knora.webapi.slice.admin.domain.model.Permission
import org.knora.webapi.slice.admin.domain.model.User
Expand Down Expand Up @@ -157,7 +158,7 @@ object SharedTestDataADM {
def systemProject: Project = Project(
id = KnoraProjectRepo.builtIn.SystemProject.id.value,
shortname = Shortname.unsafeFrom("SystemProject"),
shortcode = "FFFF",
shortcode = Shortcode.unsafeFrom("FFFF"),
longname = Some("Knora System Project"),
description = Seq(StringLiteralV2.from(value = "Knora System Project", language = Some("en"))),
keywords = Seq.empty[String],
Expand All @@ -176,7 +177,7 @@ object SharedTestDataADM {
def defaultSharedOntologiesProject: Project = Project(
id = OntologyConstants.KnoraAdmin.DefaultSharedOntologiesProject,
shortname = Shortname.unsafeFrom("DefaultSharedOntologiesProject"),
shortcode = "0000",
shortcode = Shortcode.unsafeFrom("0000"),
longname = Some("Default Knora Shared Ontologies Project"),
description = Seq(StringLiteralV2.from(value = "Default Knora Shared Ontologies Project", language = Some("en"))),
keywords = Seq.empty[String],
Expand Down Expand Up @@ -288,7 +289,7 @@ object SharedTestDataADM {
def imagesProject: Project = Project(
id = imagesProjectIri,
shortname = Shortname.unsafeFrom("images"),
shortcode = "00FF",
shortcode = Shortcode.unsafeFrom("00FF"),
longname = Some("Image Collection Demo"),
description = Seq(StringLiteralV2.from(value = "A demo project of a collection of images", language = Some("en"))),
keywords = Seq("images", "collection").sorted,
Expand All @@ -302,7 +303,7 @@ object SharedTestDataADM {
def imagesProjectExternal: Project = Project(
id = imagesProjectIri,
shortname = Shortname.unsafeFrom("images"),
shortcode = "00FF",
shortcode = Shortcode.unsafeFrom("00FF"),
longname = Some("Image Collection Demo"),
description = Seq(StringLiteralV2.from(value = "A demo project of a collection of images", language = Some("en"))),
keywords = Seq("images", "collection").sorted,
Expand Down Expand Up @@ -444,7 +445,7 @@ object SharedTestDataADM {
def incunabulaProject: Project = Project(
id = incunabulaProjectIri,
shortname = Shortname.unsafeFrom("incunabula"),
shortcode = "0803",
shortcode = Shortcode.unsafeFrom("0803"),
longname = Some("Bilderfolgen Basler Frühdrucke"),
description = Seq(
StringLiteralV2.from(
Expand Down Expand Up @@ -479,7 +480,7 @@ object SharedTestDataADM {
def incunabulaProjectExternal: Project = Project(
id = incunabulaProjectIri,
shortname = Shortname.unsafeFrom("incunabula"),
shortcode = "0803",
shortcode = Shortcode.unsafeFrom("0803"),
longname = Some("Bilderfolgen Basler Frühdrucke"),
description = Seq(
StringLiteralV2.from(
Expand Down Expand Up @@ -612,7 +613,7 @@ object SharedTestDataADM {
def anythingProject: Project = Project(
id = anythingProjectIri,
shortname = Shortname.unsafeFrom("anything"),
shortcode = "0001",
shortcode = Shortcode.unsafeFrom("0001"),
longname = Some("Anything Project"),
description = Seq(StringLiteralV2.from(value = "Anything Project", language = None)),
keywords = Seq("things", "arbitrary test data").sorted,
Expand All @@ -625,7 +626,7 @@ object SharedTestDataADM {
def anythingProjectExternal: Project = Project(
id = anythingProjectIri,
shortname = Shortname.unsafeFrom("anything"),
shortcode = "0001",
shortcode = Shortcode.unsafeFrom("0001"),
longname = Some("Anything Project"),
description = Seq(StringLiteralV2.from(value = "Anything Project", language = None)),
keywords = Seq("things", "arbitrary test data").sorted,
Expand Down Expand Up @@ -660,7 +661,7 @@ object SharedTestDataADM {
def beolProject: Project = Project(
id = beolProjectIri,
shortname = Shortname.unsafeFrom("beol"),
shortcode = "0801",
shortcode = Shortcode.unsafeFrom("0801"),
longname = Some("Bernoulli-Euler Online"),
description = Seq(StringLiteralV2.from(value = "Bernoulli-Euler Online", language = None)),
keywords = Seq.empty[String],
Expand Down Expand Up @@ -714,7 +715,7 @@ object SharedTestDataADM {
def dokubibProject: Project = Project(
id = dokubibProjectIri,
shortname = Shortname.unsafeFrom("dokubib"),
shortcode = "0804",
shortcode = Shortcode.unsafeFrom("0804"),
longname = Some("Dokubib"),
description = Seq(StringLiteralV2.from(value = "Dokubib", language = None)),
keywords = Seq.empty[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,9 +1590,9 @@ class StringFormatter private (
* @param projectShortcode the project's shortcode.
* @return a new resource IRI.
*/
def makeRandomResourceIri(projectShortcode: String): IRI = {
def makeRandomResourceIri(shortcode: Shortcode): IRI = {
val knoraResourceID = UuidUtil.makeRandomBase64EncodedUuid
s"http://$IriDomain/$projectShortcode/$knoraResourceID"
s"http://$IriDomain/$shortcode/$knoraResourceID"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.knora.webapi.slice.admin.domain.model.GroupName
import org.knora.webapi.slice.admin.domain.model.GroupSelfJoin
import org.knora.webapi.slice.admin.domain.model.GroupStatus
import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortname
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.model.UserIri
Expand Down Expand Up @@ -86,7 +87,7 @@ object Examples {
private val project: Project = Project(
id = ProjectExample.projectIri.value,
shortname = Shortname.unsafeFrom("example"),
shortcode = "0001",
shortcode = Shortcode.unsafeFrom("0001"),
longname = Some("Example Project"),
description = Seq(StringLiteralV2.from("An example project", Some("en"))),
keywords = Seq("example", "project"),
Expand Down
Loading

0 comments on commit ebe7934

Please sign in to comment.