Skip to content
Permalink
Browse files
feat(ontologies): make comments optional for property and class creat…
…ion (DEV-342) (#1996)

* feat: make comments optional for property and class creation

* fix: failing tests

* refactor: comment handling method

* test: add class and property creation w/o comments

* test: fix name

* test: typo
  • Loading branch information
mpro7 committed Feb 11, 2022
1 parent 5749c87 commit a3c286c3ab90e713c5a7929d69517d1dbb848d40
@@ -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,
@@ -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,
@@ -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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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
}
}
}
@@ -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)
@@ -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",
@@ -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"
@@ -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"""
@@ -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}"

0 comments on commit a3c286c

Please sign in to comment.