export async function getTestCaseDisplayData()

in src/tdmlEditor/utilities/tdmlXmlUtils.ts [128:239]


export async function getTestCaseDisplayData(
  xmlBuffer: string
): Promise<TDMLTestSuiteDisplay> {
  var xmlObj: Element | ElementCompact = xml2js(xmlBuffer)

  const rootNode = (xmlObj as Element).elements?.filter((node) =>
    node.name?.endsWith(testSuiteElement)
  )

  if (rootNode === undefined) {
    console.log(`No test suite found in XML buffer`)
    return {
      suiteName: '',
      testCases: [],
    }
  }

  if (rootNode.length !== 1) {
    console.log(`More than one test suite found in XML buffer`)
    return {
      suiteName: '',
      testCases: [],
    }
  }

  const retVal: TDMLTestCaseDisplay[] = []

  // Each TDML file contains at least one test case
  const testCases = rootNode[0].elements?.filter((node) =>
    node.name?.endsWith(testCaseElement)
  )

  // Each test case may contain any number of documents
  const documentParts: string[][] = []
  testCases?.forEach((testCaseNode) => {
    const documentsGrouped: string[] = []
    testCaseNode.elements
      ?.filter((childNode) => childNode.name?.endsWith(documentElement))
      .forEach((documentNode) =>
        documentNode.elements
          ?.filter((childNode) => childNode.name?.endsWith(documentPartElement))
          .forEach((documentPartNode) => {
            if (
              documentPartNode.elements !== undefined &&
              documentPartNode.elements[0].text !== undefined
            )
              documentsGrouped.push(
                documentPartNode.elements[0].text.toString()
              )
          })
      )
    documentParts.push(documentsGrouped)
  })

  // Each test case may contain any number of infosets
  const infosetParts: string[][] = []
  testCases?.forEach((testCaseNode) => {
    const infosetsGrouped: string[] = []
    testCaseNode.elements
      ?.filter((childNode) => childNode.name?.endsWith(infosetElement))
      .forEach((infosetNode) =>
        infosetNode.elements
          ?.filter((childNode) => childNode.name?.endsWith(dfdlInfosetElement))
          .forEach((dfdlInfosetNode) => {
            if (
              dfdlInfosetNode.elements !== undefined &&
              dfdlInfosetNode.elements[0].text !== undefined
            )
              infosetsGrouped.push(dfdlInfosetNode.elements[0].text.toString())
            else if (dfdlInfosetNode.elements !== undefined)
              infosetsGrouped.push('xml defined infoset')
          })
      )
    infosetParts.push(infosetsGrouped)
  })

  // Combine the extracted data info the custom types for returning
  testCases?.forEach((testCaseNode, index) => {
    if (
      testCaseNode.attributes !== undefined &&
      testCaseNode.attributes[testCaseNameAttribute] !== undefined &&
      testCaseNode.attributes[testCaseModelAttribute] !== undefined
    ) {
      retVal.push({
        testCaseName: testCaseNode.attributes[testCaseNameAttribute].toString(),
        testCaseDescription:
          testCaseNode.attributes[testCaseDescriptionAttribute] !== undefined
            ? testCaseNode.attributes[testCaseDescriptionAttribute].toString()
            : '',
        testCaseModel:
          testCaseNode.attributes[testCaseModelAttribute].toString(),
        dataDocuments: documentParts[index],
        dfdlInfosets: infosetParts[index],
      })
    }
  })

  if (rootNode[0].attributes !== undefined) {
    return {
      suiteName:
        rootNode[0].attributes[testSuiteAttribute] !== undefined
          ? rootNode[0].attributes[testSuiteAttribute].toString()
          : '',
      testCases: retVal,
    }
  } else {
    return {
      suiteName: '',
      testCases: [],
    }
  }
}