Skip to content

Commit

Permalink
Also serialize empty catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
torleifg committed Nov 28, 2024
1 parent d95233c commit e4f6c95
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RDFController(private val handler: RDFHandler) {
@PathVariable catalogId: String, @PathVariable dataServiceId: String
): ResponseEntity<String> {
return getRDFLang(acceptHeader)
.let { handler.findById(catalogId, dataServiceId, it) }
.let { handler.findDataServiceById(catalogId, dataServiceId, it) }
.let { ResponseEntity.ok(it) }
}

Expand Down
47 changes: 20 additions & 27 deletions src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,41 @@ import java.io.StringWriter
class RDFHandler(private val repository: DataServiceRepository, private val properties: ApplicationProperties) {

fun findAll(lang: Lang): String {
val dataServices = repository.findAllByStatus(Status.PUBLISHED).groupBy(DataService::catalogId)

if (dataServices.isEmpty()) return ""

val model = createModel()

dataServices.forEach { (catalogId, dataServices) ->
catalogId?.let { model.addCatalog(it, getCatalogUri(), getOrganizationUri(), getPublisherUri()) }

dataServices.forEach { dataService ->
model.addDataServiceToCatalog(dataService, getCatalogUri(), getDataServiceUri())
model.addDataService(dataService, getDataServiceUri())
repository.findAllByStatus(Status.PUBLISHED)
.groupBy(DataService::catalogId)
.forEach { (catalogId, dataServices) ->
catalogId?.let {
model.addCatalog(it, getCatalogUri(), getOrganizationUri(), getPublisherUri())
}

Check warning on line 29 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L28-L29

Added lines #L28 - L29 were not covered by tests

dataServices.forEach { dataService ->
model.addDataServiceToCatalog(dataService, getCatalogUri(), getDataServiceUri())
model.addDataService(dataService, getDataServiceUri())
}
}

Check warning on line 35 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L31-L35

Added lines #L31 - L35 were not covered by tests
}

return model.serialize(lang)
}

fun findById(catalogId: String, lang: Lang): String {
val dataServices = repository.findAllByCatalogIdAndStatus(catalogId, Status.PUBLISHED)

if (dataServices.isEmpty()) return ""

val model = createModel()

Check warning on line 41 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L41

Added line #L41 was not covered by tests

dataServices.forEach { dataService ->
dataService.catalogId?.let {
model.addCatalog(
it,
getCatalogUri(),
getOrganizationUri(),
getPublisherUri()
)
repository.findAllByCatalogIdAndStatus(catalogId, Status.PUBLISHED)
.forEach { dataService ->

Check warning on line 44 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L43-L44

Added lines #L43 - L44 were not covered by tests
dataService.catalogId?.let {
model.addCatalog(it, getCatalogUri(), getOrganizationUri(), getPublisherUri())
}

Check warning on line 47 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L46-L47

Added lines #L46 - L47 were not covered by tests

model.addDataServiceToCatalog(dataService, getCatalogUri(), getDataServiceUri())
model.addDataService(dataService, getDataServiceUri())
}

Check warning on line 51 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L49-L51

Added lines #L49 - L51 were not covered by tests
model.addDataServiceToCatalog(dataService, getCatalogUri(), getDataServiceUri())
model.addDataService(dataService, getDataServiceUri())
}

return model.serialize(lang)

Check warning on line 53 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L53

Added line #L53 was not covered by tests
}

fun findById(catalogId: String, dataServiceId: String, lang: Lang): String {
fun findDataServiceById(catalogId: String, dataServiceId: String, lang: Lang): String {
val dataService = repository.findDataServiceById(dataServiceId)
?.takeIf { it.catalogId == catalogId }
?: throw NotFoundException("Data Service with id: $dataServiceId not found in Catalog with id: $catalogId")

Check warning on line 59 in src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/dataservicecatalog/handler/RDFHandler.kt#L59

Added line #L59 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class RDFControllerTest(@Autowired val mockMvc: MockMvc) {
val dataServiceId = "5678"

handler.stub {
on { findById(catalogId, dataServiceId, Lang.N3) } doReturn "turtle"
on { findDataServiceById(catalogId, dataServiceId, Lang.N3) } doReturn "turtle"
}

mockMvc.get("/catalogs/$catalogId/data-services/$dataServiceId") {
Expand All @@ -141,7 +141,7 @@ class RDFControllerTest(@Autowired val mockMvc: MockMvc) {

handler.stub {
on {
findById(catalogId, dataServiceId, Lang.N3)
findDataServiceById(catalogId, dataServiceId, Lang.N3)
} doThrow NotFoundException("Data Service $dataServiceId not found")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import no.fdk.dataservicecatalog.handler.RDFHandler
import no.fdk.dataservicecatalog.repository.DataServiceRepository
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.riot.Lang
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
Expand All @@ -30,12 +31,28 @@ class RDFHandlerTest {
lateinit var handler: RDFHandler

@Test
fun `find should respond with empty string`() {
fun `find should respond with empty turtle rdf`() {
val rdf = """
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
"""

val expectedModel = ModelFactory.createDefaultModel()
expectedModel.read(StringReader(rdf), null, Lang.TURTLE.name)

repository.stub {
on { findAllByStatus(Status.PUBLISHED) } doReturn emptyList()
}

assertEquals("", handler.findAll(Lang.TURTLE))
val catalogs = handler.findAll(Lang.TURTLE)

val actualModel = ModelFactory.createDefaultModel()
actualModel.read(StringReader(catalogs), null, Lang.TURTLE.name)

assertTrue(expectedModel.isIsomorphicWith(actualModel))
}

@Test
Expand Down Expand Up @@ -91,7 +108,7 @@ class RDFHandlerTest {
"""

val expectedModel = ModelFactory.createDefaultModel()
expectedModel.read(StringReader(rdf), null, "TURTLE");
expectedModel.read(StringReader(rdf), null, Lang.TURTLE.name)

repository.stub {
on { findAllByStatus(Status.PUBLISHED) } doReturn listOf(dataService(dataServiceId, catalogId))
Expand All @@ -102,21 +119,39 @@ class RDFHandlerTest {
on { this.organizationCatalogBaseUri } doReturn organizationCatalogBaseUri
}

val catalogs = handler.findAll(Lang.TURTLE)

val actualModel = ModelFactory.createDefaultModel()
actualModel.read(StringReader(handler.findAll(Lang.TURTLE)), null, Lang.TURTLE.name)
actualModel.read(StringReader(catalogs), null, Lang.TURTLE.name)

assertTrue(expectedModel.isIsomorphicWith(actualModel))
}

@Test
fun `find by id should respond with empty string`() {
fun `find by id should respond with empty turtle rdf`() {
val catalogId = "1234"

val rdf = """
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
"""

val expectedModel = ModelFactory.createDefaultModel()
expectedModel.read(StringReader(rdf), null, Lang.TURTLE.name)

repository.stub {
on { findAllByCatalogIdAndStatus(catalogId, Status.PUBLISHED) } doReturn emptyList()
}

assertEquals("", handler.findById(catalogId, Lang.TURTLE))
val catalogs = handler.findById(catalogId, Lang.TURTLE)

val actualModel = ModelFactory.createDefaultModel()
actualModel.read(StringReader(catalogs), null, Lang.TURTLE.name)

assertTrue(expectedModel.isIsomorphicWith(actualModel))
}

@Test
Expand Down Expand Up @@ -172,7 +207,7 @@ class RDFHandlerTest {
"""

val expectedModel = ModelFactory.createDefaultModel()
expectedModel.read(StringReader(rdf), null, "TURTLE")
expectedModel.read(StringReader(rdf), null, Lang.TURTLE.name)

repository.stub {
on { findAllByCatalogIdAndStatus(catalogId, Status.PUBLISHED) } doReturn listOf(
Expand All @@ -188,8 +223,10 @@ class RDFHandlerTest {
on { this.organizationCatalogBaseUri } doReturn organizationCatalogBaseUri
}

val catalogs = handler.findById(catalogId, Lang.TURTLE)

val actualModel = ModelFactory.createDefaultModel()
actualModel.read(StringReader(handler.findById(catalogId, Lang.TURTLE)), null, Lang.TURTLE.name)
actualModel.read(StringReader(catalogs), null, Lang.TURTLE.name)

assertTrue(expectedModel.isIsomorphicWith(actualModel))
}
Expand All @@ -201,12 +238,13 @@ class RDFHandlerTest {

repository.stub {
on { findDataServiceById(dataServiceId) } doReturn DataService(
id = dataServiceId, catalogId = "invalid catalog id"
id = dataServiceId,
catalogId = "invalid catalog id"
)
}

assertThrows(NotFoundException::class.java) {
handler.findById(catalogId, dataServiceId, Lang.TURTLE)
handler.findDataServiceById(catalogId, dataServiceId, Lang.TURTLE)
}
}

Expand Down Expand Up @@ -261,8 +299,10 @@ class RDFHandlerTest {
on { this.baseUri } doReturn baseUri
}

val dataService = handler.findDataServiceById(catalogId, dataServiceId, Lang.TURTLE)

val actualModel = ModelFactory.createDefaultModel()
actualModel.read(StringReader(handler.findById(catalogId, dataServiceId, Lang.TURTLE)), null, Lang.TURTLE.name)
actualModel.read(StringReader(dataService), null, Lang.TURTLE.name)

assertTrue(expectedModel.isIsomorphicWith(actualModel))
}
Expand All @@ -276,14 +316,18 @@ class RDFHandlerTest {
endpointDescriptions = listOf("http://endpoint-description.com"),
formats = listOf("http://format.com"),
contactPoint = ContactPoint(
name = "name", phone = "phone", email = "email", url = "url"
name = "name",
phone = "phone",
email = "email",
url = "url"
),
servesDataset = listOf("http://serves-dataset.com"),
description = LanguageString("en", "description"),
pages = listOf("http://page.com"),
landingPage = "http://landing-page.com",
license = License(
name = "name", url = "http://license.com"
name = "name",
url = "http://license.com"
),
mediaTypes = listOf(
"https://www.iana.org/assignments/media-types/application/json", "application/xml"
Expand Down

0 comments on commit e4f6c95

Please sign in to comment.