Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-http-metrics-for-search-endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 20, 2023
2 parents 20d87c6 + af95516 commit f1323bc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

package org.knora.webapi.responders.admin

import org.apache.pekko
import org.apache.pekko.actor.Status.Failure
import org.apache.pekko.testkit._

import java.util.UUID

Expand All @@ -28,9 +29,6 @@ import org.knora.webapi.sharedtestdata.SharedTestDataADM2._
import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri
import org.knora.webapi.util.MutableTestIri

import pekko.actor.Status.Failure
import pekko.testkit._

/**
* Tests [[ListsResponderADM]].
*/
Expand Down Expand Up @@ -196,7 +194,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
val nameWithSpecialCharacter = "a new \\\"name\\\""
appActor ! ListRootNodeCreateRequestADM(
createRootNode = ListRootNodeCreatePayloadADM(
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make(nameWithSpecialCharacter).fold(e => throw e.head, v => v)),
labels = Labels
.make(Seq(V2.StringLiteralV2(value = labelWithSpecialCharacter, language = Some("de"))))
Expand Down Expand Up @@ -236,7 +234,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
listIri = newListIri.get,
changeNodeRequest = ListNodeChangePayloadADM(
listIri = ListIri.make(newListIri.get).fold(e => throw e.head, v => v),
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make("updated name").fold(e => throw e.head, v => v)),
labels = Some(
Labels
Expand Down Expand Up @@ -290,7 +288,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {

"not update basic list information if name is duplicate" in {
val name = Some(ListName.make("sommer").fold(e => throw e.head, v => v))
val projectIRI = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v)
val projectIRI = ProjectIri.unsafeFrom(imagesProjectIri)
appActor ! NodeInfoChangeRequestADM(
listIri = newListIri.get,
changeNodeRequest = ListNodeChangePayloadADM(
Expand All @@ -314,7 +312,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
appActor ! ListChildNodeCreateRequestADM(
createChildNodeRequest = ListChildNodeCreatePayloadADM(
parentNodeIri = ListIri.make(newListIri.get).fold(e => throw e.head, v => v),
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make("first").fold(e => throw e.head, v => v)),
labels = Labels
.make(Seq(V2.StringLiteralV2(value = "New First Child List Node Value", language = Some("en"))))
Expand Down Expand Up @@ -367,7 +365,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
appActor ! ListChildNodeCreateRequestADM(
createChildNodeRequest = ListChildNodeCreatePayloadADM(
parentNodeIri = ListIri.make(newListIri.get).fold(e => throw e.head, v => v),
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make("second").fold(e => throw e.head, v => v)),
position = Some(Position.make(0).fold(e => throw e.head, v => v)),
labels = Labels
Expand Down Expand Up @@ -421,7 +419,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
appActor ! ListChildNodeCreateRequestADM(
createChildNodeRequest = ListChildNodeCreatePayloadADM(
parentNodeIri = ListIri.make(secondChildIri.get).fold(e => throw e.head, v => v),
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make("third").fold(e => throw e.head, v => v)),
labels = Labels
.make(Seq(V2.StringLiteralV2(value = "New Third Child List Node Value", language = Some("en"))))
Expand Down Expand Up @@ -475,7 +473,7 @@ class ListsResponderADMSpec extends CoreSpec with ImplicitSender {
appActor ! ListChildNodeCreateRequestADM(
createChildNodeRequest = ListChildNodeCreatePayloadADM(
parentNodeIri = ListIri.make(newListIri.get).fold(e => throw e.head, v => v),
projectIri = ProjectIri.from(imagesProjectIri).fold(e => throw e.head, v => v),
projectIri = ProjectIri.unsafeFrom(imagesProjectIri),
name = Some(ListName.make("fourth").fold(e => throw e.head, v => v)),
position = givenPosition,
labels = Labels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ case class RdfDataObject(path: String, name: String)
/**
* Represents the subject of a statement read from the triplestore.
*/
sealed trait SubjectV2
sealed trait SubjectV2 {
def value: String
}

/**
* Represents an IRI used as the subject of a statement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import org.knora.webapi.messages.twirl.queries.sparql
import org.knora.webapi.messages.util.ConstructResponseUtilV2
import org.knora.webapi.messages.util.ConstructResponseUtilV2.MappingAndXSLTransformation
import org.knora.webapi.messages.util.ErrorHandlingMap
import org.knora.webapi.messages.util.rdf.JsonLDDocument
import org.knora.webapi.messages.util.rdf.JsonLDInt
import org.knora.webapi.messages.util.rdf.JsonLDObject
import org.knora.webapi.messages.util.rdf.JsonLDString
import org.knora.webapi.messages.util.rdf.SparqlSelectResult
import org.knora.webapi.messages.util.rdf.SparqlSelectResultBody
import org.knora.webapi.messages.util.rdf.VariableResultsRow
import org.knora.webapi.messages.util.rdf.*
import org.knora.webapi.messages.util.search.*
import org.knora.webapi.messages.util.search.gravsearch.GravsearchParser
Expand Down Expand Up @@ -848,7 +855,6 @@ final case class SearchResponderV2Live(
val searchTerm = MatchStringWhileTyping(searchValue).generateLiteralForLuceneIndexWithoutExactSequence

Check warning on line 855 in webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala#L855

Added line #L855 was not covered by tests
val countSparql =
SearchQueries.selectCountByLabel(searchTerm, limitToProject.map(_.value), limitToResourceClass.map(_.toString))

Check warning on line 857 in webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala#L857

Added line #L857 was not covered by tests

for {
countResponse <- triplestore.query(countSparql)

Check warning on line 859 in webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala#L859

Added line #L859 was not covered by tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,39 @@
package org.knora.webapi.routing.v2

import com.typesafe.scalalogging.LazyLogging
import org.apache.pekko.http.scaladsl.server.Directives.*
import org.apache.pekko.http.scaladsl.server.PathMatcher
import org.apache.pekko.http.scaladsl.server.Route
import zio.*

import java.time.Instant

import dsp.errors.BadRequestException
import dsp.valueobjects.Iri
import org.apache.pekko.http.scaladsl.server.Directives.*
import org.apache.pekko.http.scaladsl.server.{PathMatcher, Route}
import org.knora.webapi.*
import org.knora.webapi.config.{AppConfig, GraphRoute, Sipi}
import org.knora.webapi.*
import org.knora.webapi.config.AppConfig
import org.knora.webapi.config.GraphRoute
import org.knora.webapi.config.Sipi
import org.knora.webapi.core.MessageRelay
import org.knora.webapi.messages.{SmartIri, StringFormatter, ValuesValidator}
import org.knora.webapi.messages.ValuesValidator.{arkTimestampToInstant, xsdDateTimeStampToInstant}
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.ValuesValidator
import org.knora.webapi.messages.ValuesValidator.arkTimestampToInstant
import org.knora.webapi.messages.ValuesValidator.xsdDateTimeStampToInstant
import org.knora.webapi.messages.util.rdf.JsonLDUtil
import org.knora.webapi.messages.v2.responder.resourcemessages.*
import org.knora.webapi.messages.v2.responder.resourcemessages.*
import org.knora.webapi.messages.v2.responder.valuemessages.*
import org.knora.webapi.messages.v2.responder.valuemessages.*
import org.knora.webapi.responders.v2.SearchResponderV2
import org.knora.webapi.routing.{Authenticator, RouteUtilV2, RouteUtilZ}
import org.knora.webapi.routing.Authenticator
import org.knora.webapi.routing.RouteUtilV2
import org.knora.webapi.routing.RouteUtilZ
import org.knora.webapi.slice.resourceinfo.api.service.RestResourceInfoService
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.slice.search.search.api.ApiV2.Headers.xKnoraAcceptSchemaHeader
import org.knora.webapi.store.iiif.api.SipiService
import zio.*

import java.time.Instant

/**
* Provides a routing function for API v2 routes that deal with resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,19 @@ object KnoraProject {

def unsafeFrom(str: String): ProjectIri = from(str).fold(e => throw e.head, identity)

def from(str: String): Validation[ValidationException, ProjectIri] =
if (str.isEmpty) Validation.fail(ValidationException(IriErrorMessages.ProjectIriMissing))
else {
val isUuid: Boolean = UuidUtil.hasValidLength(str.split("/").last)

if (!isProjectIri(str))
Validation.fail(ValidationException(IriErrorMessages.ProjectIriInvalid))
else if (isUuid && !UuidUtil.hasSupportedVersion(str))
Validation.fail(ValidationException(IriErrorMessages.UuidVersionInvalid))
else
Validation
.fromOption(validateAndEscapeProjectIri(str))
.mapError(_ => ValidationException(IriErrorMessages.ProjectIriInvalid))
.map(ProjectIri(_))
}
def from(str: String): Validation[ValidationException, ProjectIri] = str match {
case str if str.isEmpty =>
Validation.fail(ValidationException(IriErrorMessages.ProjectIriMissing))
case str if !isProjectIri(str) =>
Validation.fail(ValidationException(IriErrorMessages.ProjectIriInvalid))
case str if UuidUtil.hasValidLength(str.split("/").last) && !UuidUtil.hasSupportedVersion(str) =>
Validation.fail(ValidationException(IriErrorMessages.UuidVersionInvalid))
case _ =>
Validation
.fromOption(validateAndEscapeProjectIri(str))
.mapError(_ => ValidationException(IriErrorMessages.ProjectIriInvalid))

Check warning on line 58 in webapi/src/main/scala/org/knora/webapi/slice/admin/domain/model/KnoraProject.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/domain/model/KnoraProject.scala#L58

Added line #L58 was not covered by tests
.map(ProjectIri(_))
}
}

final case class Shortcode private (value: String) extends AnyVal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,38 @@ final case class KnoraProjectRepoLive(
}

private def toKnoraProject(subjectPropsTuple: (SubjectV2, ConstructPredicateObjects)): Task[KnoraProject] = {
val propsMap = subjectPropsTuple._2
val (subject, propertiesMap) = subjectPropsTuple

Check warning on line 65 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L65

Added line #L65 was not covered by tests
for {
projectIri <- ProjectIri.from(subjectPropsTuple._1.toString).toZIO
projectIri <- ProjectIri.from(subject.value).toZIO

Check warning on line 67 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L67

Added line #L67 was not covered by tests
shortname <- mapper
.getSingleOrFail[StringLiteralV2](ProjectShortname, propsMap)
.getSingleOrFail[StringLiteralV2](ProjectShortname, propertiesMap)

Check warning on line 69 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L69

Added line #L69 was not covered by tests
.flatMap(l => Shortname.from(l.value).toZIO)
shortcode <- mapper
.getSingleOrFail[StringLiteralV2](ProjectShortcode, propsMap)
.getSingleOrFail[StringLiteralV2](ProjectShortcode, propertiesMap)

Check warning on line 72 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L72

Added line #L72 was not covered by tests
.flatMap(l => Shortcode.from(l.value).toZIO)
longname <- mapper
.getSingleOption[StringLiteralV2](ProjectLongname, propsMap)
.getSingleOption[StringLiteralV2](ProjectLongname, propertiesMap)

Check warning on line 75 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L75

Added line #L75 was not covered by tests
.flatMap(optLit => ZIO.foreach(optLit)(l => Longname.from(l.value).toZIO))
description <- mapper
.getNonEmptyChunkOrFail[StringLiteralV2](ProjectDescription, propsMap)
.getNonEmptyChunkOrFail[StringLiteralV2](ProjectDescription, propertiesMap)

Check warning on line 78 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L78

Added line #L78 was not covered by tests
.map(_.map(l => V2.StringLiteralV2(l.value, l.language)))
.flatMap(ZIO.foreach(_)(Description.from(_).toZIO))
keywords <- mapper
.getList[StringLiteralV2](ProjectKeyword, propsMap)
.getList[StringLiteralV2](ProjectKeyword, propertiesMap)

Check warning on line 82 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L82

Added line #L82 was not covered by tests
.flatMap(l => ZIO.foreach(l.map(_.value).sorted)(Keyword.from(_).toZIO))
logo <- mapper
.getSingleOption[StringLiteralV2](ProjectLogo, propsMap)
.getSingleOption[StringLiteralV2](ProjectLogo, propertiesMap)

Check warning on line 85 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L85

Added line #L85 was not covered by tests
.flatMap(optLit => ZIO.foreach(optLit)(l => Logo.from(l.value).toZIO))
status <- mapper
.getSingleOrFail[BooleanLiteralV2](StatusProp, propsMap)
.getSingleOrFail[BooleanLiteralV2](StatusProp, propertiesMap)

Check warning on line 88 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L88

Added line #L88 was not covered by tests
.map(l => Status.from(l.value))
selfjoin <- mapper
.getSingleOrFail[BooleanLiteralV2](HasSelfJoinEnabled, propsMap)
.getSingleOrFail[BooleanLiteralV2](HasSelfJoinEnabled, propertiesMap)

Check warning on line 91 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L91

Added line #L91 was not covered by tests
.map(l => SelfJoin.from(l.value))
ontologies <- mapper
.getList[IriLiteralV2]("http://www.knora.org/ontology/knora-admin#belongsToOntology", propsMap)
.map(_.map(l => InternalIri(l.value)))
ontologies <-

Check warning on line 93 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L93

Added line #L93 was not covered by tests
mapper
.getList[IriLiteralV2]("http://www.knora.org/ontology/knora-admin#belongsToOntology", propertiesMap)
.map(_.map(l => InternalIri(l.value)))

Check warning on line 96 in webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/admin/repo/service/KnoraProjectRepoLive.scala#L95-L96

Added lines #L95 - L96 were not covered by tests
} yield KnoraProject(
projectIri,
shortname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object KnoraProjectSpec extends ZIOSpecDefault {
"http://www.knora.org/ontology/knora-admin#DefaultSharedOntologiesProject"
)
)
check(validIris)(iri => assertTrue(ProjectIri.from(iri).toOption.get.value == iri))
check(validIris)(iri => assertTrue(ProjectIri.unsafeFrom(iri).value == iri))
}
)

Expand Down

0 comments on commit f1323bc

Please sign in to comment.