Skip to content

Commit

Permalink
refactor: Simplify GroupStatus model (#2741)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jul 7, 2023
1 parent 746b3ba commit 0b6d102
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 33 deletions.
Expand Up @@ -5,9 +5,7 @@

package org.knora.webapi.e2e.v1

import akka.actor.ActorSystem
import akka.http.javadsl.model.StatusCodes
import akka.http.scaladsl.testkit.RouteTestTimeout
import org.scalatest.Assertion
import org.scalatest.DoNotDiscover
import spray.json._
Expand All @@ -18,8 +16,6 @@ import org.knora.webapi._
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.routing.v1.SearchRouteV1

import scala.concurrent.duration.{FiniteDuration, NANOSECONDS}

/**
* End-to-end test specification for the search endpoint. This specification uses the Spray Testkit as documented
* here: http://spray.io/documentation/1.2.2/spray-testkit/
Expand Down
Expand Up @@ -8,7 +8,6 @@ package org.knora.webapi.responders.admin
import akka.actor.Status.Failure

import java.util.UUID
import scala.concurrent.duration._

import dsp.errors.BadRequestException
import dsp.errors.DuplicateValueException
Expand All @@ -29,7 +28,6 @@ import org.knora.webapi.util.MutableTestIri
class GroupsResponderADMSpec extends CoreSpec {
private val imagesProject = SharedTestDataADM.imagesProject
private val imagesReviewerGroup = SharedTestDataADM.imagesReviewerGroup
private val rootUser = SharedTestDataADM.rootUser

"The GroupsResponder " when {
"asked about all groups" should {
Expand Down Expand Up @@ -81,7 +79,7 @@ class GroupsResponderADMSpec extends CoreSpec {
)
.fold(e => throw e.head, v => v),
project = ProjectIri.make(SharedTestDataADM.imagesProjectIri).fold(e => throw e.head, v => v),
status = GroupStatus.make(true).fold(e => throw e.head, v => v),
status = GroupStatus.active,
selfjoin = GroupSelfJoin.make(false).fold(e => throw e.head, v => v)
),
requestingUser = SharedTestDataADM.imagesUser01,
Expand Down Expand Up @@ -114,7 +112,7 @@ class GroupsResponderADMSpec extends CoreSpec {
.make(Seq(V2.StringLiteralV2(value = "NewGroupDescription", language = Some("en"))))
.fold(e => throw e.head, v => v),
project = ProjectIri.make(SharedTestDataADM.imagesProjectIri).fold(e => throw e.head, v => v),
status = GroupStatus.make(true).fold(e => throw e.head, v => v),
status = GroupStatus.active,
selfjoin = GroupSelfJoin.make(false).fold(e => throw e.head, v => v)
),
requestingUser = SharedTestDataADM.imagesUser01,
Expand Down
Expand Up @@ -7,8 +7,6 @@ package org.knora.webapi.responders.v2

import akka.testkit.ImplicitSender

import scala.concurrent.duration._

import org.knora.webapi.ApiV2Complex
import org.knora.webapi.CoreSpec
import org.knora.webapi.SchemaOptions
Expand Down
Expand Up @@ -5,7 +5,6 @@

package org.knora.webapi.responders.v2.ontology

import akka.util.Timeout
import zio.ZIO

import org.knora.webapi.CoreSpec
Expand Down
Expand Up @@ -6,7 +6,6 @@
package org.knora.webapi.routing

import akka.testkit.ImplicitSender
import akka.util.Timeout
import org.scalatest.PrivateMethodTester

import dsp.errors.BadCredentialsException
Expand Down
Expand Up @@ -5,7 +5,6 @@

package org.knora.webapi.slice.admin.domain.service

import dsp.valueobjects.Project
import dsp.valueobjects.Project.Shortcode
import org.knora.webapi.config.{Fuseki, Triplestore}
import org.knora.webapi.testcontainers.FusekiTestContainer
Expand Down
10 changes: 3 additions & 7 deletions webapi/src/main/scala/dsp/valueobjects/Group.scala
Expand Up @@ -60,13 +60,9 @@ object Group {
*/
sealed abstract case class GroupStatus private (value: Boolean)
object GroupStatus { self =>
def make(value: Boolean): Validation[Throwable, GroupStatus] =
Validation.succeed(new GroupStatus(value) {})
def make(value: Option[Boolean]): Validation[Throwable, Option[GroupStatus]] =
value match {
case Some(v) => self.make(v).map(Some(_))
case None => Validation.succeed(None)
}
val active: GroupStatus = new GroupStatus(true) {}
val inactive: GroupStatus = new GroupStatus(false) {}
def make(value: Boolean): GroupStatus = if (value) active else inactive
}

/**
Expand Down
Expand Up @@ -506,11 +506,7 @@ final case class GroupsResponderADMLive(
!userPermissions.isSystemAdmin
}

maybeStatus = changeGroupRequest.status match {
case Some(value) =>
Some(GroupStatus.make(value).fold(e => throw e.head, v => v))
case None => None
}
maybeStatus = changeGroupRequest.status.map(GroupStatus.make)

/* create the update request */
groupUpdatePayload = GroupUpdatePayloadADM(status = maybeStatus)
Expand Down
Expand Up @@ -77,7 +77,7 @@ final case class GroupsRouteADM(
val name: Validation[Throwable, GroupName] = GroupName.make(apiRequest.name)
val descriptions: Validation[Throwable, GroupDescriptions] = GroupDescriptions.make(apiRequest.descriptions)
val project: Validation[Throwable, ProjectIri] = ProjectIri.make(apiRequest.project)
val status: Validation[Throwable, GroupStatus] = GroupStatus.make(apiRequest.status)
val status: Validation[Throwable, GroupStatus] = Validation.succeed(GroupStatus.make(apiRequest.status))
val selfjoin: Validation[Throwable, GroupSelfJoin] = GroupSelfJoin.make(apiRequest.selfjoin)
val payloadValidation: Validation[Throwable, GroupCreatePayloadADM] =
Validation.validateWith(id, name, descriptions, project, status, selfjoin)(GroupCreatePayloadADM)
Expand Down Expand Up @@ -108,7 +108,7 @@ final case class GroupsRouteADM(
.when(apiRequest.status.nonEmpty)
name = GroupName.make(apiRequest.name)
descriptions = GroupDescriptions.make(apiRequest.descriptions)
status = GroupStatus.make(apiRequest.status)
status = Validation.succeed(apiRequest.status.map(GroupStatus.make))
selfjoin = GroupSelfJoin.make(apiRequest.selfjoin)
validatedPayload = Validation.validateWith(name, descriptions, status, selfjoin)(GroupUpdatePayloadADM)
iri <- Iri
Expand Down
11 changes: 5 additions & 6 deletions webapi/src/test/scala/dsp/valueobjects/GroupSpec.scala
Expand Up @@ -91,13 +91,12 @@ object GroupSpec extends ZIOSpecDefault {
)

private val groupStatusTest = suite("GroupSpec - GroupStatus")(
test("pass a valid object and successfully create value object") {
assertTrue(GroupStatus.make(true).toOption.get.value == true) &&
assertTrue(GroupStatus.make(Some(false)).getOrElse(null).get.value == false)
},
test("successfully validate passing None") {
test("creating a GroupStatus") {
assertTrue(
GroupStatus.make(None) == Validation.succeed(None)
!GroupStatus.inactive.value,
GroupStatus.active.value,
GroupStatus.make(true).value,
!GroupStatus.make(false).value
)
}
)
Expand Down

0 comments on commit 0b6d102

Please sign in to comment.