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
feat: expose GET /admin/projects/[shortname | shortcode]/{shortname |…
… shortcode} as ZIO HTTP routes (#2365)
- Loading branch information
irinaschubert
committed
Jan 4, 2023
1 parent
1b8e74b
commit 9907cdf
Showing
6 changed files
with
171 additions
and
116 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
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
77 changes: 0 additions & 77 deletions
77
webapi/src/test/scala/org/knora/webapi/routing/admin/ProjectRouteZSpec.scala
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
webapi/src/test/scala/org/knora/webapi/routing/admin/ProjectsRouteZSpec.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,141 @@ | ||
package org.knora.webapi.routing.admin | ||
|
||
import zhttp.http._ | ||
import zio._ | ||
import zio.mock.Expectation | ||
import zio.test.ZIOSpecDefault | ||
import zio.test._ | ||
|
||
import java.net.URLEncoder | ||
|
||
import org.knora.webapi.config.AppConfig | ||
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectADM | ||
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectGetRequestADM | ||
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectGetResponseADM | ||
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierADM | ||
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2 | ||
import org.knora.webapi.responders.ActorToZioBridge | ||
import org.knora.webapi.responders.ActorToZioBridgeMock | ||
import org.knora.webapi.responders.admin.RestProjectsService | ||
|
||
object ProjectRouteZSpec extends ZIOSpecDefault { | ||
|
||
private val systemUnderTest: URIO[ProjectsRouteZ, HttpApp[Any, Nothing]] = ZIO.service[ProjectsRouteZ].map(_.route) | ||
|
||
// Test data | ||
private val projectIri: ProjectIdentifierADM.IriIdentifier = | ||
ProjectIdentifierADM.IriIdentifier | ||
.fromString("http://rdfh.ch/projects/0001") | ||
.getOrElse(throw new IllegalArgumentException()) | ||
|
||
private val projectShortname: ProjectIdentifierADM.ShortnameIdentifier = | ||
ProjectIdentifierADM.ShortnameIdentifier | ||
.fromString("shortname") | ||
.getOrElse(throw new IllegalArgumentException()) | ||
|
||
private val projectShortcode: ProjectIdentifierADM.ShortcodeIdentifier = | ||
ProjectIdentifierADM.ShortcodeIdentifier | ||
.fromString("AB12") | ||
.getOrElse(throw new IllegalArgumentException()) | ||
|
||
private val basePathIri: Path = !! / "admin" / "projects" / "iri" | ||
private val basePathShortname: Path = !! / "admin" / "projects" / "shortname" | ||
private val basePathShortcode: Path = !! / "admin" / "projects" / "shortcode" | ||
private val validIriUrlEncoded: String = URLEncoder.encode(projectIri.value.value, "utf-8") | ||
private val validShortname: String = "shortname" | ||
private val validShortcode: String = "AB12" | ||
private val expectedRequestSuccessIri: ProjectGetRequestADM = ProjectGetRequestADM(projectIri) | ||
private val expectedRequestSuccessShortname: ProjectGetRequestADM = ProjectGetRequestADM(projectShortname) | ||
private val expectedRequestSuccessShortcode: ProjectGetRequestADM = ProjectGetRequestADM(projectShortcode) | ||
private val expectedResponseSuccess: ProjectGetResponseADM = | ||
ProjectGetResponseADM( | ||
ProjectADM( | ||
id = "id", | ||
shortname = "shortname", | ||
shortcode = "AB12", | ||
longname = None, | ||
description = List(StringLiteralV2("description")), | ||
keywords = List.empty, | ||
logo = None, | ||
ontologies = List.empty, | ||
status = false, | ||
selfjoin = false | ||
) | ||
) | ||
|
||
// Expectations and layers for ActorToZioBridge mock | ||
private val commonLayers: URLayer[ActorToZioBridge, ProjectsRouteZ] = | ||
ZLayer.makeSome[ActorToZioBridge, ProjectsRouteZ](AppConfig.test, ProjectsRouteZ.layer, RestProjectsService.layer) | ||
|
||
private val expectMessageToProjectResponderIri: ULayer[ActorToZioBridge] = | ||
ActorToZioBridgeMock.AskAppActor | ||
.of[ProjectGetResponseADM] | ||
.apply(Assertion.equalTo(expectedRequestSuccessIri), Expectation.value(expectedResponseSuccess)) | ||
.toLayer | ||
|
||
private val expectMessageToProjectResponderShortname: ULayer[ActorToZioBridge] = | ||
ActorToZioBridgeMock.AskAppActor | ||
.of[ProjectGetResponseADM] | ||
.apply(Assertion.equalTo(expectedRequestSuccessShortname), Expectation.value(expectedResponseSuccess)) | ||
.toLayer | ||
|
||
private val expectMessageToProjectResponderShortcode: ULayer[ActorToZioBridge] = | ||
ActorToZioBridgeMock.AskAppActor | ||
.of[ProjectGetResponseADM] | ||
.apply(Assertion.equalTo(expectedRequestSuccessShortcode), Expectation.value(expectedResponseSuccess)) | ||
.toLayer | ||
|
||
private val expectNoInteractionWithProjectsResponderADM = ActorToZioBridgeMock.empty | ||
|
||
val spec: Spec[Any, Serializable] = | ||
suite("ProjectsRouteZSpec")( | ||
suite("get project by IRI")( | ||
test("given valid project iri should respond with success") { | ||
val urlWithValidIri = URL.empty.setPath(basePathIri / validIriUrlEncoded) | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithValidIri)).flatMap(_.body.asString) | ||
} yield assertTrue(actual == expectedResponseSuccess.toJsValue.toString()) | ||
}.provide(commonLayers, expectMessageToProjectResponderIri), | ||
test("given invalid project iri should respond with bad request") { | ||
val urlWithInvalidIri = URL.empty.setPath(basePathIri / "invalid") | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithInvalidIri)).map(_.status) | ||
} yield assertTrue(actual == Status.BadRequest) | ||
}.provide(commonLayers, expectNoInteractionWithProjectsResponderADM) | ||
), | ||
suite("get project by shortname")( | ||
test("given valid project shortname should respond with success") { | ||
val urlWithValidShortname = URL.empty.setPath(basePathShortname / validShortname) | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithValidShortname)).flatMap(_.body.asString) | ||
} yield assertTrue(actual == expectedResponseSuccess.toJsValue.toString()) | ||
}.provide(commonLayers, expectMessageToProjectResponderShortname), | ||
test("given invalid project shortname should respond with bad request") { | ||
val urlWithInvalidShortname = URL.empty.setPath(basePathShortname / "123") | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithInvalidShortname)).map(_.status) | ||
} yield assertTrue(actual == Status.BadRequest) | ||
}.provide(commonLayers, expectNoInteractionWithProjectsResponderADM) | ||
), | ||
suite("get project by shortcode")( | ||
test("given valid project shortcode should respond with success") { | ||
val urlWithValidShortcode = URL.empty.setPath(basePathShortcode / validShortcode) | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithValidShortcode)).flatMap(_.body.asString) | ||
} yield assertTrue(actual == expectedResponseSuccess.toJsValue.toString()) | ||
}.provide(commonLayers, expectMessageToProjectResponderShortcode), | ||
test("given invalid project shortcode should respond with bad request") { | ||
val urlWithInvalidShortcode = URL.empty.setPath(basePathShortcode / "invalid") | ||
for { | ||
route <- systemUnderTest | ||
actual <- route.apply(Request(url = urlWithInvalidShortcode)).map(_.status) | ||
} yield assertTrue(actual == Status.BadRequest) | ||
}.provide(commonLayers, expectNoInteractionWithProjectsResponderADM) | ||
) | ||
) | ||
} |