Skip to content

Commit

Permalink
Move knora-ontologies to webapi/src/main/resources and keep symbolic …
Browse files Browse the repository at this point in the history
…link to folder in project root

Remove unused parameter `appConfig` from org.knora.webapi.util.ActorUtil#zio2Message

cleanup code: remove unused code, simplify

Revert "Move knora-ontologies to webapi/src/main/resources and keep symbolic link to folder in project root"

This reverts commit 48f5e93b7e443a6867baa257db2a076e1df202b6.

fmt

Fix warnings in CardinalityHandler

Overload case class constructor and simplify SparqlAskRequest construction

Use SmartIri type in query

fix scaladoc

use InternalIri as parameter

use InternalIri in CardinalitiesSpec

wip // add isPropertyUsedInResources to CardinalityService

wip // add TriplestoreServiceFake

wip // add CardinalityService

wip // add CardinalityService

wip // add CardinalityService

wip // add canWidenCardinality method to CardinalityService

wip // add canWidenCardinality method to CardinalityService

wip // add canWidenCardinality method to CardinalityService

fmt

reorganize TestDatasetBuilder

add headers

cleanup

cleanup

cleanup

wip // Cardinality model

fmt

reorganize packages

fix compile error and turn Cardinality into sealed trait

wip add test

wip add failing tests

wip add failing tests

add OntologyCache and fake implementation

add methods to IriConverter

add methods to IriConverter

add check and tests

add check and tests

Add headers

Update webapi/src/it/scala/org/knora/webapi/slice/resourceinfo/api/IriConverterLiveSpec.scala

remove file

simplify

remove unused code

fix test labels

fmt

fix compile warnings and add type annotation to public field

Replace if else with "pattern matching"

Replace if else with "pattern matching"

Rename isStricterThan method

cleanup, remove unused code

Remove Option.get by more elaborate pattern matching

extract explanation method

prevent knora-admin and knora-base ontologies to be change

Introduce CanSetCardinalityCheckResult in order to distinguish why setting is not possible

Introduce CanSetCardinalityCheckResult in order to distinguish why setting is not possible

Introduce CanSetCardinalityCheckResult in order to distinguish why setting is not possible

Introduce CanSetCardinalityCheckResult in order to distinguish why setting is not possible

fix type in trait

Add RestCardinalityService

Use RestCardinalityService in OntologiesRouteV2 and assemble layers

fmt

add headers

extend response with optional reason of failure

fix test setup

fmt

Introduce common ReadOnlyRepository and CrudRe
traits

header && fmt

Re-add old behaviour

fmt

fixup R2R spec

Introduce canUpdateCardinality

Introduce requestcontext completion with run unsafe zio

fmt

fix error message

make private

move getStringQueryParam to RouteUtilV2

move request params checking into RestCardinalityService

fmt

finetuning

fmt
  • Loading branch information
seakayone committed Jan 13, 2023
1 parent 3a03733 commit 12e6c64
Show file tree
Hide file tree
Showing 49 changed files with 1,942 additions and 275 deletions.
6 changes: 6 additions & 0 deletions dsp-shared/src/main/scala/dsp/errors/Errors.scala
Expand Up @@ -85,6 +85,12 @@ object RequestRejectedException {
* @param message a description of the error.
*/
case class BadRequestException(message: String) extends RequestRejectedException(message)
object BadRequestException {
def invalidQueryParamValue(key: String): BadRequestException =
BadRequestException(s"Invalid value for query parameter '$key'")
def missingQueryParamValue(key: String): BadRequestException =
BadRequestException(s"Missing query parameter '$key'")
}

/**
* An exception indicating that a user has provided bad credentials.
Expand Down
19 changes: 9 additions & 10 deletions webapi/src/it/scala/org/knora/webapi/R2RSpec.scala
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi

import akka.actor.ActorRef
import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.typesafe.scalalogging.Logger
Expand All @@ -13,16 +14,17 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import zio._
import zio.logging.backend.SLF4J

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.Future

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.AppRouter
import org.knora.webapi.core.AppServer
import org.knora.webapi.core.LayersTest.DefaultTestEnvironmentWithoutSipi
import org.knora.webapi.core.TestStartupUtils
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.rdf._
Expand Down Expand Up @@ -50,7 +52,8 @@ abstract class R2RSpec
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))
lazy val effectLayers: ULayer[DefaultTestEnvironmentWithoutSipi] =
core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))

/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
Expand All @@ -63,10 +66,7 @@ abstract class R2RSpec
] = ZLayer.empty ++ Runtime.removeDefaultLoggers ++ SLF4J.slf4j ++ effectLayers

// create a configured runtime
private val runtime = Unsafe.unsafe { implicit u =>
Runtime.unsafe
.fromLayer(bootstrap)
}
val runtime: Runtime.Scoped[Environment] = Unsafe.unsafe(implicit u => Runtime.unsafe.fromLayer(bootstrap))

// An effect for getting stuff out, so that we can pass them
// to some legacy code
Expand All @@ -90,11 +90,11 @@ abstract class R2RSpec
// main difference to other specs (no own systen and executionContext defined)
lazy val rdfDataObjects = List.empty[RdfDataObject]
val log: Logger = Logger(this.getClass())
val appActor = router.ref
val appActor: ActorRef = router.ref

// needed by some tests
val routeData = KnoraRouteData(system, appActor, config)
val appConfig = config
val routeData: KnoraRouteData = KnoraRouteData(system, appActor, config)
val appConfig: AppConfig = config

final override def beforeAll(): Unit =
/* Here we start our app and initialize the repository before each suit runs */
Expand All @@ -110,7 +110,6 @@ abstract class R2RSpec
}

final override def afterAll(): Unit = {

/* Stop ZIO runtime and release resources (e.g., running docker containers) */
Unsafe.unsafe { implicit u =>
runtime.unsafe.shutdown()
Expand Down
11 changes: 11 additions & 0 deletions webapi/src/it/scala/org/knora/webapi/core/LayersTest.scala
Expand Up @@ -7,7 +7,12 @@ import org.knora.webapi.auth.JWTService
import org.knora.webapi.config.AppConfig
import org.knora.webapi.config.AppConfigForTestContainers
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.responders.ActorDeps
import org.knora.webapi.routing.ApiRoutes
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.ontology.domain.service.CardinalityService
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
import org.knora.webapi.slice.ontology.repo.service.OntologyRepoLive
import org.knora.webapi.slice.resourceinfo.api.RestResourceInfoService
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.slice.resourceinfo.domain.ResourceInfoRepo
Expand Down Expand Up @@ -44,6 +49,7 @@ object LayersTest {
with IriConverter
with RepositoryUpdater
with ResourceInfoRepo
with RestCardinalityService
with RestResourceInfoService
with State
with StringFormatter
Expand All @@ -53,15 +59,20 @@ object LayersTest {

private val commonLayersForAllIntegrationTests =
ZLayer.makeSome[CommonR0, CommonR](
ActorDeps.layer,
ApiRoutes.layer,
AppRouter.layer,
CacheServiceInMemImpl.layer,
CacheServiceManager.layer,
CardinalityService.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IriConverter.layer,
OntologyCache.layer,
OntologyRepoLive.layer,
RepositoryUpdater.layer,
ResourceInfoRepo.layer,
RestCardinalityService.layer,
RestResourceInfoService.layer,
State.layer,
StringFormatter.test,
Expand Down
24 changes: 12 additions & 12 deletions webapi/src/it/scala/org/knora/webapi/e2e/v2/OntologyV2R2RSpec.scala
Expand Up @@ -64,9 +64,9 @@ class OntologyV2R2RSpec extends R2RSpec {
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

private val ontologiesPath =
DSPApiDirectives.handleErrors(system, appConfig)(new OntologiesRouteV2(routeData).makeRoute)
DSPApiDirectives.handleErrors(system, appConfig)(new OntologiesRouteV2(routeData, runtime).makeRoute)
private val resourcesPath =
DSPApiDirectives.handleErrors(system, appConfig)(new ResourcesRouteV2(routeData, null).makeRoute)
DSPApiDirectives.handleErrors(system, appConfig)(new ResourcesRouteV2(routeData, runtime).makeRoute)

implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(
appConfig.defaultTimeoutAsDuration
Expand Down Expand Up @@ -1010,7 +1010,7 @@ class OntologyV2R2RSpec extends R2RSpec {
val expectedResponse: String =
s"""{
| "knora-api:lastModificationDate": {
| "@value": "${newFreetestLastModDate}",
| "@value": "$newFreetestLastModDate",
| "@type": "xsd:dateTimeStamp"
| },
| "rdfs:label": "freetest",
Expand Down Expand Up @@ -1092,7 +1092,7 @@ class OntologyV2R2RSpec extends R2RSpec {
val expectedResponse: String =
s"""{
| "knora-api:lastModificationDate": {
| "@value": "${newFreetestLastModDate}",
| "@value": "$newFreetestLastModDate",
| "@type": "xsd:dateTimeStamp"
| },
| "rdfs:label": "freetest",
Expand Down Expand Up @@ -1718,7 +1718,7 @@ class OntologyV2R2RSpec extends R2RSpec {
"add all IRIs to newly created link value property again" in {
val url = URLEncoder.encode(s"${SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost}", "UTF-8")
Get(
s"/v2/ontologies/allentities/${url}"
s"/v2/ontologies/allentities/$url"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -1754,7 +1754,7 @@ class OntologyV2R2RSpec extends R2RSpec {
iris should equal(expectedIris)

val isEditable = hasOtherNothingValue.requireBoolean(OntologyConstants.KnoraApiV2Complex.IsEditable)
isEditable shouldBe (true)
isEditable shouldBe true
}
}

Expand Down Expand Up @@ -1816,7 +1816,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// load back the ontology to verify that the updated property still is editable
val encodedIri = URLEncoder.encode(s"${SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost}", "UTF-8")
Get(
s"/v2/ontologies/allentities/${encodedIri}"
s"/v2/ontologies/allentities/$encodedIri"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -1889,7 +1889,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// load back the ontology to verify that the updated property still is editable
val encodedIri = URLEncoder.encode(s"${SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost}", "UTF-8")
Get(
s"/v2/ontologies/allentities/${encodedIri}"
s"/v2/ontologies/allentities/$encodedIri"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -1935,7 +1935,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// load back the ontology to verify that the updated property still is editable
val encodedIri = URLEncoder.encode(s"${SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost}", "UTF-8")
Get(
s"/v2/ontologies/allentities/${encodedIri}"
s"/v2/ontologies/allentities/$encodedIri"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -2008,7 +2008,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// load back the ontology to verify that the updated property still is editable
val encodedIri = URLEncoder.encode(s"${SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost}", "UTF-8")
Get(
s"/v2/ontologies/allentities/${encodedIri}"
s"/v2/ontologies/allentities/$encodedIri"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -3712,7 +3712,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// check the ontology to see if all worked as it should
val url = URLEncoder.encode(s"http://0.0.0.0:3333/ontology/0001/freetest/v2", "UTF-8")
Get(
s"/v2/ontologies/allentities/${url}"
s"/v2/ontologies/allentities/$url"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down Expand Up @@ -3845,7 +3845,7 @@ class OntologyV2R2RSpec extends R2RSpec {
// check the ontology to see if all worked as it should
val url = URLEncoder.encode(s"http://0.0.0.0:3333/ontology/0001/freetest/v2", "UTF-8")
Get(
s"/v2/ontologies/allentities/${url}"
s"/v2/ontologies/allentities/$url"
) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
Expand Down
Expand Up @@ -21,15 +21,14 @@ import org.xmlunit.builder.Input
import org.xmlunit.diff.Diff
import spray.json.JsValue
import spray.json.JsonParser

import java.net.URLEncoder
import java.nio.file.Paths
import java.time.Instant
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Await
import scala.concurrent.duration._

import dsp.errors.AssertionException

import org.knora.webapi._
import org.knora.webapi.e2e.ClientTestDataCollector
import org.knora.webapi.e2e.InstanceChecker
Expand Down Expand Up @@ -2113,7 +2112,7 @@ class ResourcesRouteV2E2ESpec extends E2ESpec {

"correctly update the ontology cache when adding a resource, so that the resource can afterwards be found by gravsearch" in {
val freetestLastModDate: Instant = Instant.parse("2012-12-12T12:12:12.12Z")
DSPApiDirectives.handleErrors(system, appConfig)(new OntologiesRouteV2(routeData).makeRoute)
DSPApiDirectives.handleErrors(system, appConfig)(new OntologiesRouteV2(routeData, runtime).makeRoute)
val auth = BasicHttpCredentials(SharedTestDataADM.anythingAdminUser.email, SharedTestDataADM.testPass)

// create a new resource class and add a property with cardinality to it
Expand Down
Expand Up @@ -2324,7 +2324,7 @@ class ResourcesResponderV2Spec extends CoreSpec with ImplicitSender {

val isEntityUsedSparql: String = org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.isEntityUsed(
entityIri = resourceIriToErase.get.toSmartIri,
entityIri = resourceIriToErase.get.toSmartIri.toInternalIri,
ignoreKnoraConstraints = true
)
.toString()
Expand Down
Expand Up @@ -8,7 +8,6 @@ package org.knora.webapi.responders.v2.ontology
import akka.util.Timeout

import org.knora.webapi.CoreSpec
import org.knora.webapi.InternalSchema
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
Expand All @@ -25,17 +24,17 @@ class CardinalitiesSpec extends CoreSpec {

"Cardinalities.isPropertyUsedInResources()" should {
"detect that property is in use, when used in a resource" in {
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasText").toOntologySchema(InternalSchema)
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toOntologySchema(InternalSchema)
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasText").toInternalIri
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toInternalIri
println(s"internalPropertyIri: $internalPropertyIri")

val resF = CardinalityHandler.isPropertyUsedInResources(appActor, internalClassIri, internalPropertyIri)
resF map { res => println(res); assert(res, "property is used in resource (instance of that resource class)") }
}

"detect that property is not in use, when not used in a resource" in {
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasText").toOntologySchema(InternalSchema)
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTestResourceClass").toOntologySchema(InternalSchema)
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasText").toInternalIri
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTestResourceClass").toInternalIri
println(s"internalPropertyIri: $internalPropertyIri")

val resF = CardinalityHandler.isPropertyUsedInResources(appActor, internalClassIri, internalPropertyIri)
Expand All @@ -45,8 +44,8 @@ class CardinalitiesSpec extends CoreSpec {
}

"detect that property is not in use, when not used in a resource of that class (even when used in another class)" in {
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasIntegerProperty").toOntologySchema(InternalSchema)
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toOntologySchema(InternalSchema)
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasIntegerProperty").toInternalIri
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toInternalIri
println(s"internalPropertyIri: $internalPropertyIri")

val resF = CardinalityHandler.isPropertyUsedInResources(appActor, internalClassIri, internalPropertyIri)
Expand All @@ -57,17 +56,17 @@ class CardinalitiesSpec extends CoreSpec {

"detect that link property is in use, when used in a resource" in {
val anythingOntologyIri = "http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri
val internalPropertyIri = anythingOntologyIri.makeEntityIri("isPartOfOtherThing").toOntologySchema(InternalSchema)
val internalClassIri = anythingOntologyIri.makeEntityIri("Thing").toOntologySchema(InternalSchema)
val internalPropertyIri = anythingOntologyIri.makeEntityIri("isPartOfOtherThing").toInternalIri
val internalClassIri = anythingOntologyIri.makeEntityIri("Thing").toInternalIri
println(s"internalPropertyIri: $internalPropertyIri")

val resF = CardinalityHandler.isPropertyUsedInResources(appActor, internalClassIri, internalPropertyIri)
resF map { res => println(res); assert(res, "property is used in resource (instance of resource class)") }
}

"detect that property is in use, when used in a resource of a subclass" in {
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasDecimal").toOntologySchema(InternalSchema)
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toOntologySchema(InternalSchema)
val internalPropertyIri = freetestOntologyIri.makeEntityIri("hasDecimal").toInternalIri
val internalClassIri = freetestOntologyIri.makeEntityIri("FreeTest").toInternalIri
println(s"internalPropertyIri: $internalPropertyIri")

val resF = CardinalityHandler.isPropertyUsedInResources(appActor, internalClassIri, internalPropertyIri)
Expand Down

0 comments on commit 12e6c64

Please sign in to comment.