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

feat(ontologies): make comments optional for property and class creation (DEV-342) #1996

Merged
merged 6 commits into from Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -465,16 +465,12 @@ object CreatePropertyRequestV2 extends KnoraJsonLDRequestReaderV2[CreateProperty
throw BadRequestException(s"Invalid knora-api:objectType: $objectType")
}

// The request must provide an rdfs:label and an rdfs:comment.
// The request must provide an rdfs:label

if (!propertyInfoContent.predicates.contains(OntologyConstants.Rdfs.Label.toSmartIri)) {
throw BadRequestException("Missing rdfs:label")
}

if (!propertyInfoContent.predicates.contains(OntologyConstants.Rdfs.Comment.toSmartIri)) {
throw BadRequestException("Missing rdfs:comment")
}

CreatePropertyRequestV2(
propertyInfoContent = propertyInfoContent,
lastModificationDate = lastModificationDate,
Expand Down Expand Up @@ -553,16 +549,12 @@ object CreateClassRequestV2 extends KnoraJsonLDRequestReaderV2[CreateClassReques
val classInfoContent = classUpdateInfo.classInfoContent
val lastModificationDate = classUpdateInfo.lastModificationDate

// The request must provide an rdfs:label and an rdfs:comment.
// The request must provide an rdfs:label

if (!classInfoContent.predicates.contains(OntologyConstants.Rdfs.Label.toSmartIri)) {
throw BadRequestException("Missing rdfs:label")
}

if (!classInfoContent.predicates.contains(OntologyConstants.Rdfs.Comment.toSmartIri)) {
throw BadRequestException("Missing rdfs:comment")
}

CreateClassRequestV2(
classInfoContent = classInfoContent,
lastModificationDate = lastModificationDate,
Expand Down
Expand Up @@ -2499,7 +2499,7 @@ class OntologyV2R2RSpec extends R2RSpec {
lastModificationDate = freetestLastModDate,
className = "BlueFreeTestClass",
label = LangString("en", "A Blue Free Test class"),
comment = LangString("en", "A Blue Free Test class used for testing cardinalities")
comment = Some(LangString("en", "A Blue Free Test class used for testing cardinalities"))
)
.value

Expand Down Expand Up @@ -2527,7 +2527,7 @@ class OntologyV2R2RSpec extends R2RSpec {
subjectClassName = Some("BlueFreeTestClass"),
propertyType = PropertyValueType.TextValue,
label = LangString("en", "blue test text property"),
comment = LangString("en", "A blue test text property")
comment = Some(LangString("en", "A blue test text property"))
)
.value

Expand Down Expand Up @@ -2555,7 +2555,7 @@ class OntologyV2R2RSpec extends R2RSpec {
subjectClassName = Some("BlueFreeTestClass"),
propertyType = PropertyValueType.IntValue,
label = LangString("en", "blue test integer property"),
comment = LangString("en", "A blue test integer property")
comment = Some(LangString("en", "A blue test integer property"))
)
.value

Expand Down Expand Up @@ -2746,7 +2746,7 @@ class OntologyV2R2RSpec extends R2RSpec {
lastModificationDate = freetestLastModDate,
className = "TestClassOne",
label = LangString("en", "Test class number one"),
comment = LangString("en", "A test class used for testing cardinalities")
comment = Some(LangString("en", "A test class used for testing cardinalities"))
)
.value

Expand All @@ -2770,7 +2770,7 @@ class OntologyV2R2RSpec extends R2RSpec {
lastModificationDate = freetestLastModDate,
className = "TestClassTwo",
label = LangString("en", "Test class number two"),
comment = LangString("en", "A test class used for testing cardinalities")
comment = Some(LangString("en", "A test class used for testing cardinalities"))
)
.value

Expand All @@ -2796,7 +2796,7 @@ class OntologyV2R2RSpec extends R2RSpec {
subjectClassName = None,
propertyType = PropertyValueType.IntValue,
label = LangString("en", "Test int property"),
comment = LangString("en", "A test int property")
comment = Some(LangString("en", "A test int property"))
)
.value

Expand Down Expand Up @@ -3048,4 +3048,57 @@ class OntologyV2R2RSpec extends R2RSpec {
assert(!responseJsonDoc.body.value(OntologyConstants.KnoraApiV2Complex.CanDo).asInstanceOf[JsonLDBoolean].value)
}
}

"create a class w/o comment" in {
val request = CreateClassRequest
.make(
ontologyName = "freetest",
lastModificationDate = freetestLastModDate,
className = "testClass",
label = LangString("en", "Test label"),
comment = None
)
.value

Post(
"/v2/ontologies/classes",
HttpEntity(RdfMediaTypes.`application/ld+json`, request)
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)

val responseJsonDoc = responseToJsonLDDocument(response)
val responseAsInput: InputOntologyV2 =
InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape

freetestLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
}
}

"create a property w/o comment" in {
val request = CreatePropertyRequest
.make(
ontologyName = "freetest",
lastModificationDate = freetestLastModDate,
propertyName = "testProperty",
subjectClassName = None,
propertyType = PropertyValueType.IntValue,
label = LangString("en", "Test label"),
comment = None
)
.value

Post(
"/v2/ontologies/properties",
HttpEntity(RdfMediaTypes.`application/ld+json`, request)
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {

val response = responseAs[String]
assert(status == StatusCodes.OK, response)
val responseJsonDoc = JsonLDUtil.parseJsonLD(response)
val responseAsInput: InputOntologyV2 =
InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape

freetestLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
}
}
}
Expand Up @@ -5,11 +5,23 @@

package org.knora.webapi.models

import org.knora.webapi.sharedtestdata.SharedOntologyTestDataADM

import java.time.Instant
import scala.annotation.tailrec

object Comments {
def handleOptionalComment(comment: Option[LangString]): String =
comment match {
case Some(value) =>
s"""
| "rdfs:comment" : {
| "@language" : "${value.language}",
| "@value" : "${value.value}"
| },
|""".stripMargin
case None => ""
}
}

final case class LangString(language: String, value: String)

sealed abstract case class CreateClassRequest private (value: String)
Expand All @@ -19,9 +31,10 @@ object CreateClassRequest {
lastModificationDate: Instant,
className: String,
label: LangString,
comment: LangString
comment: Option[LangString]
): CreateClassRequest = {
val ontologyId = s"http://0.0.0.0:3333/ontology/0001/$ontologyName/v2"
val maybeComment: String = Comments.handleOptionalComment(comment)

val value = s"""{
| "@id" : "$ontologyId",
Expand All @@ -37,10 +50,7 @@ object CreateClassRequest {
| "@language" : "${label.language}",
| "@value" : "${label.value}"
| },
| "rdfs:comment" : {
| "@language" : "${comment.language}",
| "@value" : "${comment.value}"
| },
| $maybeComment
| "rdfs:subClassOf" : [
| {
| "@id": "knora-api:Resource"
Expand Down Expand Up @@ -82,10 +92,11 @@ object CreatePropertyRequest {
subjectClassName: Option[String],
propertyType: PropertyValueType,
label: LangString,
comment: LangString
comment: Option[LangString]
): CreatePropertyRequest = {
val LocalHost_Ontology = "http://0.0.0.0:3333/ontology"
val ontologyId = LocalHost_Ontology + s"/0001/$ontologyName/v2"
val maybeComment: String = Comments.handleOptionalComment(comment)

val optionalSubjectClass = subjectClassName match {
case Some(subjectName) => s"""
Expand All @@ -110,10 +121,7 @@ object CreatePropertyRequest {
| "knora-api:objectType" : {
| "@id" : "${propertyType.value}"
| },
| "rdfs:comment" : {
| "@language" : "${comment.language}",
| "@value" : "${comment.value}"
| },
| $maybeComment
| "rdfs:label" : {
| "@language" : "${label.language}",
| "@value" : "${label.value}"
Expand Down