Skip to content

Commit

Permalink
refactor: Make Project.status typed (#3499)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone authored Feb 7, 2025
1 parent bb9f5d1 commit ec29048
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.admin.api.model.*
import org.knora.webapi.slice.admin.api.model.ProjectMembersGetResponseADM
import org.knora.webapi.slice.admin.domain.model.KnoraProject.SelfJoin
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Status
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.util.AkkaHttpUtils
import org.knora.webapi.util.MutableTestIri
Expand Down Expand Up @@ -236,7 +237,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
result.description should be(Seq(StringLiteralV2.from(value = "project description", language = Some("en"))))
result.keywords should be(Seq("keywords"))
result.logo.map(_.value) should be(Some("/fu/bar/baz.jpg"))
result.status should be(true)
result.status should be(Status.Active)
result.selfjoin should be(SelfJoin.CannotJoin)

newProjectIri.set(result.id)
Expand Down Expand Up @@ -363,7 +364,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
)
result.keywords.sorted should be(Seq("updated", "keywords").sorted)
result.logo.map(_.value) should be(Some("/fu/bar/baz-updated.jpg"))
result.status should be(true)
result.status should be(Status.Active)
result.selfjoin should be(SelfJoin.CanJoin)
}

Expand Down Expand Up @@ -399,7 +400,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
response.status should be(StatusCodes.OK)

val result: Project = AkkaHttpUtils.httpResponseToJson(response).fields("project").convertTo[Project]
result.status should be(false)
result.status should be(Status.Inactive)
}
}

Expand All @@ -412,8 +413,7 @@ class ProjectsADME2ESpec extends E2ESpec with SprayJsonSupport {
assert(response.status === StatusCodes.OK)
val jsObject = AkkaHttpUtils.httpResponseToJson(response)
val prjMembersResp = jsonReader[ProjectMembersGetResponseADM].read(jsObject)
// val prjMembersResp = jsObject.convertTo[ProjectMembersGetResponseADM]
val members = prjMembersResp.members
val members = prjMembersResp.members
members.size should be(4)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import org.knora.webapi.slice.admin.domain.model.KnoraProject.Longname
import org.knora.webapi.slice.admin.domain.model.KnoraProject.SelfJoin
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.KnoraProject.Status
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.common.Value.BooleanValue
import org.knora.webapi.slice.common.Value.StringValue
Expand Down Expand Up @@ -239,15 +240,19 @@ object IntegrationTestAdminJsonProtocol extends TriplestoreJsonProtocol {
}

trait BooleanValueFormat[T <: BooleanValue] extends JsonFormat[T] { self =>
def from: Boolean => Either[String, T]
override def write(v: T): JsValue = JsString(v.value.toString)
def from: Boolean => T
override def write(v: T): JsValue = JsBoolean(v.value)
override def read(json: JsValue): T = json match
case JsBoolean(bool) => self.from(bool).fold(err => throw DeserializationException(err), identity)
case JsBoolean(bool) => self.from(bool)
case _ => throw DeserializationException("Must be a json Boolean")
}

implicit object SelfJoinValueFormat extends BooleanValueFormat[SelfJoin] {
override val from: Boolean => Either[String, SelfJoin] = b => Right(SelfJoin.from(b))
override val from: Boolean => SelfJoin = SelfJoin.from
}

implicit object StatusFormat extends BooleanValueFormat[Status] {
override val from: Boolean => Status = Status.from
}

implicit val groupFormat: JsonFormat[Group] = jsonFormat6(Group.apply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
)
received.project.keywords.sorted should be(Seq("updated", "keywords").sorted)
received.project.logo.map(_.value) should be(Some("/fu/bar/baz-updated.jpg"))
received.project.status should be(true)
received.project.status should be(Status.Active)
received.project.selfjoin should be(SelfJoin.CanJoin)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object ProjectInfo {
keywords = project.keywords.headOption.map(_ => project.keywords.mkString(", ")),
logo = project.logo.map(_.value),
ontologies = project.ontologies,
status = project.status,
status = project.status.value,
selfjoin = project.selfjoin.value,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.SelfJoin
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.KnoraProject.Status
import org.knora.webapi.slice.admin.domain.model.Permission
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.service.KnoraGroupRepo
Expand Down Expand Up @@ -173,7 +174,7 @@ object SharedTestDataADM {
SalsahGui.SalsahGuiOntologyIri,
OntologyConstants.Standoff.StandoffOntologyIri,
),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand All @@ -187,7 +188,7 @@ object SharedTestDataADM {
keywords = Seq.empty[String],
logo = None,
ontologies = Seq.empty[IRI],
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -299,7 +300,7 @@ object SharedTestDataADM {
keywords = Seq("images", "collection").sorted,
logo = None,
ontologies = Seq(SharedOntologyTestDataADM.IMAGES_ONTOLOGY_IRI),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand All @@ -313,7 +314,7 @@ object SharedTestDataADM {
keywords = Seq("images", "collection").sorted,
logo = None,
ontologies = Seq(SharedOntologyTestDataADM.IMAGES_ONTOLOGY_IRI_LocalHost),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -476,7 +477,7 @@ object SharedTestDataADM {
).sorted,
logo = Some(Logo.unsafeFrom("incunabula_logo.png")),
ontologies = Seq(SharedOntologyTestDataADM.INCUNABULA_ONTOLOGY_IRI),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -511,7 +512,7 @@ object SharedTestDataADM {
).sorted,
logo = Some(Logo.unsafeFrom("incunabula_logo.png")),
ontologies = Seq(SharedOntologyTestDataADM.INCUNABULA_ONTOLOGY_IRI_LocalHost),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -623,7 +624,7 @@ object SharedTestDataADM {
keywords = Seq("things", "arbitrary test data").sorted,
logo = None,
ontologies = Seq(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI, SharedOntologyTestDataADM.SomethingOntologyIri),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand All @@ -639,7 +640,7 @@ object SharedTestDataADM {
SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost,
SharedOntologyTestDataADM.SomethingOntologyIriLocalhost,
),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -676,7 +677,7 @@ object SharedTestDataADM {
"http://www.knora.org/ontology/0801/leibniz",
"http://www.knora.org/ontology/0801/newton",
),
status = true,
status = Status.Active,
selfjoin = SelfJoin.CannotJoin,
)

Expand Down Expand Up @@ -725,7 +726,7 @@ object SharedTestDataADM {
keywords = Seq.empty[String],
logo = None,
ontologies = Seq("http://www.knora.org/ontology/0804/dokubib"),
status = false,
status = Status.Inactive,
selfjoin = SelfJoin.CannotJoin,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.SelfJoin.CannotJoin
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.KnoraProject.Status.Active
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.model.UserIri
import org.knora.webapi.slice.admin.domain.model.UserStatus
Expand Down Expand Up @@ -94,7 +95,7 @@ object Examples {
description = Seq(StringLiteralV2.from("An example project", Some("en"))),
keywords = Seq("example", "project"),
logo = None,
status = true,
status = Active,
ontologies = Seq.empty,
selfjoin = CannotJoin,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class Project(
keywords: Seq[String],
logo: Option[Logo],
ontologies: Seq[IRI],
status: Boolean,
status: Status,
selfjoin: SelfJoin,
) extends Ordered[Project] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final case class ProjectService(
knoraProject.keywords.map(_.value),
knoraProject.logo,
ontologies,
knoraProject.status.value,
knoraProject.status,
knoraProject.selfjoin,
),
)
Expand All @@ -70,7 +70,7 @@ final case class ProjectService(
.map(Description.unsafeFrom),
keywords = project.keywords.map(Keyword.unsafeFrom).toList,
logo = project.logo,
status = Status.from(project.status),
status = project.status,
selfjoin = project.selfjoin,
restrictedView,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object ProjectServiceSpec extends ZIOSpecDefault {
keywords = List.empty,
logo = None,
ontologies = List.empty,
status = true,
status = Status.Active,
selfjoin = SelfJoin.CanJoin,
)
assertTrue(
Expand Down

0 comments on commit ec29048

Please sign in to comment.