Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(candeletecardinalities): return correct response on route negative case (DEV-36) #1910

Merged
merged 9 commits into from Sep 29, 2021
Expand Up @@ -129,9 +129,6 @@ object Cardinalities {

submittedPropertyToDelete: SmartIri = cardinalitiesToDelete.head._1
propertyIsUsed: Boolean <- isPropertyUsedInResources(settings, storeManager, submittedPropertyToDelete)
_ = if (propertyIsUsed) {
throw BadRequestException("Property is used in data. The cardinality cannot be deleted.")
}

// Make an update class definition in which the cardinality to delete is removed

Expand Down Expand Up @@ -170,7 +167,7 @@ object Cardinalities {
throw BadRequestException(msg)
}
)
} yield CanDoResponseV2(true)
} yield CanDoResponseV2(!propertyIsUsed)
}

/**
Expand Down
Expand Up @@ -2864,6 +2864,77 @@ class OntologyV2R2RSpec extends R2RSpec {
assert(resourceIri.toSmartIri.isKnoraDataIri)
}

// payload to test cardinality can't be deleted
val cardinalityCantBeDeletedPayload =
s"""
|{
| "@id" : "${SharedOntologyTestDataADM.FREETEST_ONTOLOGY_IRI_LocalHost}",
| "@type" : "owl:Ontology",
| "knora-api:lastModificationDate" : {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "$freetestLastModDate"
| },
| "@graph" : [ {
| "@id" : "freetest:FreeTest",
| "@type" : "owl:Class",
| "rdfs:subClassOf" : {
| "@type": "owl:Restriction",
| "owl:minCardinality" : 1,
| "owl:onProperty" : {
| "@id" : "freetest:hasText"
| }
| }
| } ],
| "@context" : {
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
| "owl" : "http://www.w3.org/2002/07/owl#",
| "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
| "xsd" : "http://www.w3.org/2001/XMLSchema#",
| "freetest" : "${SharedOntologyTestDataADM.FREETEST_ONTOLOGY_IRI_LocalHost}#"
| }
|}
""".stripMargin

clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "candeletecardinalities-false-request",
fileExtension = "json"
),
text = cardinalityCantBeDeletedPayload
)
)

// Expect cardinality can't be deleted - endpoint should return CanDo response with value false
Post(
"/v2/ontologies/candeletecardinalities",
HttpEntity(RdfMediaTypes.`application/ld+json`, cardinalityCantBeDeletedPayload)
) ~>
addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
val responseStr = responseAs[String]
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = JsonLDUtil.parseJsonLD(responseStr)
assert(
!responseJsonDoc.body
.value(OntologyConstants.KnoraApiV2Complex.CanDo)
.asInstanceOf[JsonLDBoolean]
.value
)

clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "candeletecardinalities-false-response",
fileExtension = "json"
),
text = responseStr
)
)
}

// Prepare the JsonLD payload to check if a cardinality can be deleted and
// then to also actually delete it.
val params =
Expand Down Expand Up @@ -2897,6 +2968,17 @@ class OntologyV2R2RSpec extends R2RSpec {
|}
""".stripMargin

clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "candeletecardinalities-true-request",
fileExtension = "json"
),
text = params
)
)

// Successfully check if the cardinality can be deleted
Post(
"/v2/ontologies/candeletecardinalities",
Expand All @@ -2908,6 +2990,17 @@ class OntologyV2R2RSpec extends R2RSpec {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = JsonLDUtil.parseJsonLD(responseStr)
assert(responseJsonDoc.body.value(OntologyConstants.KnoraApiV2Complex.CanDo).asInstanceOf[JsonLDBoolean].value)

clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "candeletecardinalities-true-response",
fileExtension = "json"
),
text = responseStr
)
)
}

// Successfully remove the (unused) text value cardinality from the class.
Expand Down