tests/testthat/test.SystemMetadata.R

test_that("dataone library loads", {
	expect_true(require(dataone))
})
test_that("SystemMetadata constructors", {
    library(datapack)
    sysmeta <- new("SystemMetadata")
    expect_equal(sysmeta@serialVersion, 1)
    expect_true(is.na(sysmeta@identifier))
    sysmeta <- new("SystemMetadata", identifier="TestId", formatId="text/csv")
    expect_equal(sysmeta@identifier, "TestId")
    expect_equal(sysmeta@formatId, "text/csv")
})
test_that("XML SystemMetadata parsing works", {
  library(dataone)
  library(XML)
  testid <- "doi:10.xxyy/AA/tesdoc123456789"
  sysmeta <- new("SystemMetadata")
  expect_equal(sysmeta@serialVersion, 1)
  doc <- xmlParseDoc(system.file("testfiles/sysmeta.xml", package="dataone"), asText=FALSE)
  expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
  xml <- xmlRoot(doc)
  #getEncoding(doc)
  sysmeta <- parseSystemMetadata(sysmeta, xmlRoot(xml))
  expect_match(sysmeta@identifier, testid)
  expect_equal(nrow(sysmeta@accessPolicy), 5)
  expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
  expect_true(sysmeta@archived)
  csattrs <- xmlAttrs(xml[["checksum"]])
  expect_match(sysmeta@checksumAlgorithm, csattrs[[1]])
  expect_true(grep("urn:node:KNB", sysmeta@preferredNodes) > 0)
  expect_true(grep("urn:node:mnUNM1", sysmeta@preferredNodes) > 0)
  expect_true(grep("urn:node:BADNODE", sysmeta@blockedNodes) > 0)
})

test_that("XML SystemMetadata serialization works", {
    library(dataone)
    testid <- "doi:10.xxyy/AA/tesdoc123456789"
    sysmeta <- new("SystemMetadata")
    expect_equal(sysmeta@serialVersion, 1)
    xml <- xmlParseDoc(system.file("testfiles/sysmeta.xml", package="dataone"), asText=FALSE)
    expect_match(xmlValue(xmlRoot(xml)[["identifier"]]), testid)
    sysmeta <- parseSystemMetadata(sysmeta, xmlRoot(xml))
    expect_match(sysmeta@identifier, testid)
    expect_true(sysmeta@archived)
    xml <- serializeSystemMetadata(sysmeta)
    #cat(xml)
    expect_match(xml, "<d1:systemMetadata")
    expect_match(xml, "<blockedMemberNode>urn:node:BADNODE</blockedMemberNode>")
    expect_match(xml, "<preferredMemberNode>urn:node:KNB</preferredMemberNode>")
    expect_match(xml, "<subject>public</subject>")
    expect_match(xml, "<permission>read</permission>")
    expect_match(xml, "<subject>CN=Subject2,O=Google,C=US,DC=cilogon,DC=org</subject>")
    expect_match(xml, "<permission>changePermission</permission>")
    sysmeta@obsoletes <- ""
    sysmeta <- new("SystemMetadata")
    xml <- serializeSystemMetadata(sysmeta)
    foundObsoletes <- grep("<obsoletes>", xml, invert=TRUE)
    expect_true(as.logical(foundObsoletes))
    # TODO: check tree equivalence with original XML document
})
test_that("SystemMetadata XML constructor works", {
    library(dataone)
    library(XML)
    testid <- "doi:10.xxyy/AA/tesdoc123456789"
    doc <- xmlParseDoc(system.file("testfiles/sysmeta.xml", package="dataone"), asText=FALSE)
    expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
    xml <- xmlRoot(doc)
    sysmeta <- SystemMetadata(xmlRoot(xml))
    expect_match(sysmeta@identifier, testid)
    expect_equal(nrow(sysmeta@accessPolicy), 5)
    expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
    expect_true(sysmeta@archived)
    csattrs <- xmlAttrs(xml[["checksum"]])
    expect_match(sysmeta@checksumAlgorithm, csattrs[[1]])
    expect_true(grep("urn:node:KNB", sysmeta@preferredNodes) > 0)
    expect_true(grep("urn:node:mnUNM1", sysmeta@preferredNodes) > 0)
    expect_true(grep("urn:node:BADNODE", sysmeta@blockedNodes) > 0)
})
test_that("SystemMetadata validation works", {
    library(dataone)
    sysmeta <- new("SystemMetadata", identifier="foo", formatId="text/csv", size=59, checksum="jdhdjhfd", rightsHolder="ff")
    isValid <- validate(sysmeta)
    expect_true(isValid)
    isValid <- validObject(sysmeta)
    expect_true(isValid)
    sysmeta <- new("SystemMetadata", identifier="foo", checksum="jdhdjhfd", rightsHolder="ff")
    errors <- validate(sysmeta)
    expect_equal(length(errors), 2)
})

test_that("SystemMetadata accessPolicy can be constructed and changed", {
    library(dataone)
    apolicy=data.frame(list("public", "read"))
    colnames(apolicy) <- c("subject", "permission")
    sysmeta <- new("SystemMetadata", identifier="foo", formatId="text/csv", size=59, checksum="jdhdjhfd", rightsHolder="ff", accessPolicy=apolicy)
    expect_equal(sysmeta@serialVersion, 1)
    expect_equal(nrow(sysmeta@accessPolicy), 1)
    expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
    
    sysmeta <- addAccessRule(sysmeta, "foo", "write")
    expect_equal(nrow(sysmeta@accessPolicy), 2)
    expect_match(as.character(sysmeta@accessPolicy$permission[[2]]), "write")
    
    apolicy=data.frame(subject=c("bar", "baz"), permission= c("changePermission", "write"))
    sysmeta <- addAccessRule(sysmeta, apolicy)
    expect_equal(nrow(sysmeta@accessPolicy), 4)
    expect_match(as.character(sysmeta@accessPolicy$permission[[4]]), "write")
    expect_match(as.character(sysmeta@accessPolicy$subject[[4]]), "baz")
})

Try the dataone package in your browser

Any scripts or data that you put into this service are public.

dataone documentation built on June 11, 2022, 1:06 a.m.