-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add sbt projects and dependencies * add role domain * add Role value objects + fix build * add LangString value object * add RoleIri value object + tests * update RoleId + LangString * add role test data + domain tests * add role repo implementation * add permission value object + update LangString * naming improvements * add interface placeholders * add RoleRepoMock * fix RoleRepoSpec name * add RoleHandler + tests * add RoleRepoLive + tests * update docs + cleanup * forgotten docs * review changes * fix merge issues
- Loading branch information
Showing
24 changed files
with
1,395 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package dsp.role.api | ||
|
||
import dsp.role.domain.Role | ||
import dsp.valueobjects.Id.RoleId | ||
import zio._ | ||
import zio.macros.accessible | ||
|
||
/** | ||
* The trait (interface) for the role repository. | ||
* The role repository is responsible for storing and retrieving roles. | ||
* Needs to be used by the role repository implementations. | ||
*/ | ||
@accessible | ||
trait RoleRepo { | ||
|
||
/** | ||
* Writes a role into the repository, while both creating or updating a role. | ||
* | ||
* @param r the [[Role]] to write | ||
* @return the [[RoleId]] | ||
*/ | ||
def storeRole(role: Role): UIO[RoleId] | ||
|
||
/** | ||
* Gets all roles from the repository. | ||
* | ||
* @return a list of [[Role]] | ||
*/ | ||
def getRoles(): UIO[List[Role]] | ||
|
||
/** | ||
* Retrieves a role from the repository. | ||
* | ||
* @param id the role's ID | ||
* @return the [[Role]] if found | ||
*/ | ||
def getRoleById(id: RoleId): IO[Option[Nothing], Role] | ||
|
||
// should the role name be unique like username??? | ||
|
||
/** | ||
* Deletes the [[Role]] from the repository by its [[RoleId]] | ||
* | ||
* @param id the role ID | ||
* @return the [[RoleId]] of the deleted role, if found | ||
*/ | ||
def deleteRole(id: RoleId): IO[Option[Nothing], RoleId] | ||
} |
122 changes: 122 additions & 0 deletions
122
dsp-role/core/src/main/scala/dsp/role/domain/RoleDomain.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package dsp.role.domain | ||
|
||
import dsp.errors.BadRequestException | ||
import dsp.valueobjects.Id.RoleId | ||
import dsp.valueobjects.Id.UserId | ||
import dsp.valueobjects.Permission | ||
import dsp.valueobjects.Role._ | ||
import zio.prelude.Validation | ||
|
||
case class RoleUser( | ||
id: UserId | ||
) | ||
|
||
/** | ||
* Role's domain entity. | ||
* | ||
* @param id the role's ID | ||
* @param name the role's name | ||
* @param description the role's description | ||
* @param users the role's users | ||
* @param permission the role's permission | ||
*/ | ||
sealed abstract case class Role private ( | ||
id: RoleId, | ||
name: LangString, | ||
description: LangString, | ||
users: List[RoleUser], // how to reperesnt the user here? | ||
permission: Permission | ||
) extends Ordered[Role] { self => | ||
|
||
/** | ||
* Allows to compare the [[Role]] instances. | ||
* | ||
* @param that [[Role]] to compare | ||
* @return [[Boolean]] value | ||
*/ | ||
def compare(that: Role): Int = self.id.iri.toString().compareTo(that.id.iri.toString()) | ||
|
||
/** | ||
* Updates the role's name with new value. | ||
* | ||
* @param newValue new role's name to update | ||
* @return updated [[Role]] | ||
*/ | ||
def updateName(newValue: LangString): Validation[BadRequestException, Role] = | ||
Role.make( | ||
self.id, | ||
newValue, | ||
self.description, | ||
self.users, | ||
self.permission | ||
) | ||
|
||
/** | ||
* Updates the role's description with new value. | ||
* | ||
* @param newValue new role's description to update | ||
* @return updated [[Role]] | ||
*/ | ||
def updateDescription(newValue: LangString): Validation[BadRequestException, Role] = | ||
Role.make( | ||
self.id, | ||
self.name, | ||
newValue, | ||
self.users, | ||
self.permission | ||
) | ||
|
||
/** | ||
* Updates the role's users with new value. | ||
* | ||
* @param newValue new role's users to update | ||
* @return updated [[Role]] | ||
*/ | ||
def updateUsers(newValue: List[RoleUser]): Validation[BadRequestException, Role] = | ||
Role.make( | ||
self.id, | ||
self.name, | ||
self.description, | ||
newValue, | ||
self.permission | ||
) | ||
|
||
/** | ||
* Updates the role's permission with new value. | ||
* | ||
* @param newValue new role's permission to update | ||
* @return updated [[Role]] | ||
*/ | ||
def updatePermission(newValue: Permission): Validation[BadRequestException, Role] = | ||
Role.make( | ||
self.id, | ||
self.name, | ||
self.description, | ||
self.users, | ||
newValue | ||
) | ||
} | ||
|
||
object Role { | ||
def make( | ||
id: RoleId, | ||
name: LangString, | ||
description: LangString, | ||
users: List[RoleUser], | ||
permission: Permission | ||
): Validation[BadRequestException, Role] = | ||
Validation.succeed( | ||
new Role( | ||
id, | ||
name, | ||
description, | ||
users, | ||
permission | ||
) {} | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
dsp-role/core/src/test/scala/dsp/role/api/RoleApiSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package dsp.role.api | ||
|
||
/** | ||
* To be implemented... | ||
*/ | ||
object placeholder {} |
111 changes: 111 additions & 0 deletions
111
dsp-role/core/src/test/scala/dsp/role/domain/RoleDomainSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package dsp.role.domain | ||
|
||
import dsp.role.sharedtestdata.RoleTestData | ||
import dsp.valueobjects.Id | ||
import dsp.valueobjects.Permission | ||
import dsp.valueobjects.Role._ | ||
import zio.test._ | ||
|
||
/** | ||
* This spec is used to test [[RoleDomain]]. | ||
*/ | ||
object RoleDomainSpec extends ZIOSpecDefault { | ||
def spec = (compareRolesTest + createRoleTest + updateRoleTest) | ||
|
||
private val compareRolesTest = suite("compareRoles")( | ||
test("compare two roles") { | ||
val role = RoleTestData.role1 | ||
val equalRole = RoleTestData.role1 | ||
val nonEqualRole = RoleTestData.role2 | ||
|
||
assertTrue(role == equalRole) && | ||
assertTrue(role != nonEqualRole) | ||
} | ||
) | ||
|
||
private val createRoleTest = suite("createRole")( | ||
test("create a role") { | ||
( | ||
for { | ||
id <- RoleTestData.id1 | ||
name <- RoleTestData.name1 | ||
description <- RoleTestData.description1 | ||
users = RoleTestData.users1 | ||
permission <- RoleTestData.permission1 | ||
|
||
role <- Role.make( | ||
id, | ||
name, | ||
description, | ||
users = users, | ||
permission | ||
) | ||
} yield assertTrue(role.id == id) && | ||
assertTrue(role.name == name) && | ||
assertTrue(role.description == description) && | ||
assertTrue(role.users == users) && | ||
assertTrue(role.permission == permission) | ||
).toZIO | ||
} | ||
) | ||
|
||
private val updateRoleTest = suite("updateRole")( | ||
test("update the name") { | ||
( | ||
for { | ||
role <- RoleTestData.role1 | ||
newValue <- LangString.make("newRoleName", "en") | ||
updatedRole <- role.updateName(newValue) | ||
} yield assertTrue(updatedRole.name == newValue) && | ||
assertTrue(updatedRole.name != role.name) && | ||
assertTrue(updatedRole.description == role.description) && | ||
assertTrue(updatedRole.users == role.users) && | ||
assertTrue(updatedRole.permission == role.permission) | ||
).toZIO | ||
}, | ||
test("update the description") { | ||
( | ||
for { | ||
role <- RoleTestData.role1 | ||
newValue <- LangString.make("New Role Description", "en") | ||
updatedRole <- role.updateDescription(newValue) | ||
} yield assertTrue(updatedRole.name == role.name) && | ||
assertTrue(updatedRole.description == newValue) && | ||
assertTrue(updatedRole.description != role.description) && | ||
assertTrue(updatedRole.users == role.users) && | ||
assertTrue(updatedRole.permission == role.permission) | ||
).toZIO | ||
}, | ||
test("update the users") { | ||
( | ||
for { | ||
role <- RoleTestData.role1 | ||
newValue = List(RoleUser(Id.UserId.make().fold(e => throw e.head, v => v))) | ||
updatedRole <- role.updateUsers(newValue) | ||
} yield assertTrue(updatedRole.name == role.name) && | ||
assertTrue(updatedRole.description == role.description) && | ||
assertTrue(updatedRole.users == newValue) && | ||
assertTrue(updatedRole.users != role.users) && | ||
assertTrue(updatedRole.permission == role.permission) | ||
).toZIO | ||
}, | ||
test("update the permission") { | ||
( | ||
for { | ||
role <- RoleTestData.role1 | ||
newValue <- Permission.make(Permission.Create) | ||
updatedRole <- role.updatePermission(newValue) | ||
} yield assertTrue(updatedRole.name == role.name) && | ||
assertTrue(updatedRole.description == role.description) && | ||
assertTrue(updatedRole.users == role.users) && | ||
assertTrue(updatedRole.permission == newValue) && | ||
assertTrue(updatedRole.permission != role.permission) | ||
).toZIO | ||
} | ||
) | ||
} |
Oops, something went wrong.