Skip to content

Commit

Permalink
test: Simplify OntologyFormatsE2ESpec (#3146)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Mar 26, 2024
1 parent fcd2f13 commit 6770b25
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 9,084 deletions.
3 changes: 3 additions & 0 deletions integration/src/test/scala/org/knora/webapi/E2ESpec.scala
Expand Up @@ -174,6 +174,9 @@ abstract class E2ESpec
protected def parseRdfXml(rdfXmlStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = RdfXml)

protected def parseJsonLd(rdfXmlStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = JsonLD)

/**
* Reads or writes a test data file.
*
Expand Down
Expand Up @@ -50,23 +50,11 @@ class OntologyFormatsE2ESpec extends E2ESpec {
fileBasename: String,
clientTestDataBasename: Option[String] = None,
) {
private val jsonLd = "jsonld"
private val jsonLdReceived = s"received.$jsonLd"
private def makeFile(suffix: String): Path =
Paths.get("..", "test_data", "generated_test_data", "ontologyR2RV2", s"$fileBasename.$suffix")

/**
* Writes the expected response file.
*
* @param responseStr the contents of the file to be written.
* @param mediaType the media type of the response.
*/
def writeFile(responseStr: String, mediaType: MediaType.NonBinary): Unit = {
val newOutputFile = makeFile(mediaType.fileExtensions.head)

Files.createDirectories(newOutputFile.getParent)
FileUtil.writeTextFile(newOutputFile, responseStr)
()
}

/**
* If `maybeClientTestDataBasename` is defined, stores the response string in [[org.knora.webapi.e2e.ClientTestDataCollector]].
*/
Expand All @@ -85,11 +73,11 @@ class OntologyFormatsE2ESpec extends E2ESpec {
def readFile(mediaType: MediaType.NonBinary): String =
FileUtil.readTextFile(makeFile(mediaType.fileExtensions.head))

def fileExists(mediaType: MediaType.NonBinary): Boolean =
Files.exists(makeFile(mediaType.fileExtensions.head))
def fileExists: Boolean =
Files.exists(makeFile(jsonLd))

def writeReceived(responseStr: String, mediaType: MediaType.NonBinary): Unit = {
val newOutputFile = makeFile(s"received.${mediaType.fileExtensions.head}")
def writeReceived(responseStr: String): Unit = {
val newOutputFile = makeFile(jsonLdReceived)

Files.createDirectories(newOutputFile.getParent)
FileUtil.writeTextFile(newOutputFile, responseStr)
Expand All @@ -115,73 +103,43 @@ class OntologyFormatsE2ESpec extends E2ESpec {
private def urlEncodeIri(iri: IRI): String =
URLEncoder.encode(iri, "UTF-8")

private def checkJsonLdTestCase(httpGetTest: HttpGetTest) = {
val mediaType = RdfMediaTypes.`application/ld+json`
val responseStr = getResponse(httpGetTest.urlPath, mediaType)
if (!httpGetTest.fileExists(mediaType)) {
if (writeTestDataFiles) httpGetTest.writeReceived(responseStr, mediaType)
throw new AssertionError(s"No approved data available in file ${httpGetTest.fileBasename}")
}
if (JsonParser(responseStr) != JsonParser(httpGetTest.readFile(mediaType))) {
if (writeTestDataFiles) httpGetTest.writeReceived(responseStr, mediaType)
throw new AssertionError(
s"""|
|The response did not equal the approved data.
|
|Response:
|
|$responseStr
|
|
|${"=" * 120}
|
|
|Approved data:
|
|${httpGetTest.readFile(mediaType)}
|
|""".stripMargin,
)
}
httpGetTest.storeClientTestData(responseStr)
}
private def checkTestCase(httpGetTest: HttpGetTest) = {
val responseJsonLd = getResponse(httpGetTest.urlPath, RdfMediaTypes.`application/ld+json`)
val responseTtl = getResponse(httpGetTest.urlPath, RdfMediaTypes.`text/turtle`)
val responseRdfXml = getResponse(httpGetTest.urlPath, RdfMediaTypes.`application/rdf+xml`)

private def checkTurleTestCase(httpGetTest: HttpGetTest) = {
val mediaType = RdfMediaTypes.`text/turtle`
val responseStr = getResponse(httpGetTest.urlPath, mediaType)
if (!httpGetTest.fileExists(mediaType)) {
if (writeTestDataFiles) httpGetTest.writeReceived(responseStr, mediaType)
if (!httpGetTest.fileExists) {
httpGetTest.writeReceived(responseJsonLd)
throw new AssertionError(s"No approved data available in file ${httpGetTest.fileBasename}")
}
if (parseTurtle(responseStr) != parseTurtle(httpGetTest.readFile(mediaType))) {
if (writeTestDataFiles) httpGetTest.writeReceived(responseStr, mediaType)

val approvedJsonLd = httpGetTest.readFile(RdfMediaTypes.`application/ld+json`)
if (JsonParser(responseJsonLd) != JsonParser(approvedJsonLd)) {
httpGetTest.writeReceived(responseJsonLd)
throw new AssertionError(
s"""|
|The response did not equal the approved data.
|
|Response:
|
|$responseStr
|$responseJsonLd
|
|
|${"=" * 120}
|
|
|Approved data:
|
|${httpGetTest.readFile(mediaType)}
|$approvedJsonLd
|
|""".stripMargin,
)
}
}

private def checkRdfXmlTestCase(httpGetTest: HttpGetTest) = {
val mediaType = RdfMediaTypes.`application/rdf+xml`
val responseStr = getResponse(httpGetTest.urlPath, mediaType)
// RDF XML can be compared agains the persisted turtle file, so does not need to br written to a file.
if (writeTestDataFiles) ()
else assert(parseRdfXml(responseStr) == parseTurtle(httpGetTest.readFile(RdfMediaTypes.`text/turtle`)))
assert(parseTurtle(responseTtl) == parseJsonLd(responseJsonLd))
assert(parseRdfXml(responseRdfXml) == parseJsonLd(responseJsonLd))

httpGetTest.storeClientTestData(responseJsonLd)
}

private def getResponse(url: String, mediaType: MediaType.NonBinary) = {
Expand Down Expand Up @@ -321,18 +279,10 @@ class OntologyFormatsE2ESpec extends E2ESpec {
)
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// If true, the existing expected response files are overwritten with the HTTP GET responses from the server.
// If false, the responses from the server are compared to the contents fo the expected response files.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
private val writeTestDataFiles = false

"The Ontologies v2 Endpoint" should {
"serve the ontologies in JSON-LD, turtle and RDF-XML" in {
forEvery(TestCases.testCases) { testCase =>
checkTurleTestCase(testCase)
checkJsonLdTestCase(testCase)
checkRdfXmlTestCase(testCase)
checkTestCase(testCase)
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6770b25

Please sign in to comment.