UNPKG

3.58 kBJavaScriptView Raw
1/* eslint-env mocha */
2process.on('unhandledRejection', (reason, p) => {
3 console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
4 // application specific logging, throwing an error, or other logic here
5})
6
7const tymly = require('@wmfs/tymly')
8const path = require('path')
9const fs = require('fs')
10const expect = require('chai').expect
11const FlattenXmlFiles = require('./../lib/components/state-resources/flatten-xml-files')
12
13const STATE_MACHINE_NAME = 'tymlyTest_simpsonsImport_1_0'
14
15describe('xmlFlatten State Resource', function () {
16 this.timeout(process.env.TIMEOUT || 5000)
17
18 const fixture = path.resolve(__dirname, 'fixtures', 'xmlflatten')
19
20 const sourceFile = path.resolve(fixture, 'input', 'simpsons.xml')
21 const expectedFile = path.resolve(fixture, 'expected', 'simpsons.csv')
22
23 function outputFileName (postfix) {
24 const outputFile = path.resolve(fixture, 'output', `simpsons-${postfix}.csv`)
25
26 if (fs.existsSync(outputFile)) fs.unlinkSync(outputFile)
27
28 return outputFile
29 } // outputFileName
30
31 function verifyOutput (outputFile, done) {
32 try {
33 const output = fs.readFileSync(outputFile, { encoding: 'utf8' }).split('\n')
34 const expected = fs.readFileSync(expectedFile, { encoding: 'utf8' }).split('\n')
35
36 expect(output).to.eql(expected)
37 } catch (err) {
38 if (done) return done(err)
39 throw err
40 }
41 } // verifyOutput
42
43 describe('state resource', () => {
44 const flattener = new FlattenXmlFiles()
45 it('initialise flattenXml state resource', () => {
46 flattener.init({
47 rootXMLElement: 'Episode',
48 pivotPath: '$.People.Person',
49 headerMap: [
50 ['$.Title', 'title', 'string'],
51 ['@.Name', 'name', 'string'],
52 [{ test: '@.Age<=16', value: 'yes' }, 'child', 'string'],
53 [{ test: '@.Age>16', select: '@.Age' }, 'age', 'integer']
54 ]
55 },
56 null
57 )
58 })
59
60 it('run flattenXml state resource', (done) => {
61 const outputFile = outputFileName('state-resource')
62
63 flattener.run({
64 xmlPath: sourceFile,
65 csvPath: outputFile
66 },
67 {
68 sendTaskSuccess: () => {
69 verifyOutput(outputFile, done)
70 done()
71 },
72 sendTaskFailure: err => done(err)
73 })
74 })
75 }) // state-resource
76
77 describe('blueprint', () => {
78 let tymlyService
79 let statebox
80
81 it('start Tymly service', async () => {
82 const tymlyServices = await tymly.boot(
83 {
84 pluginPaths: [
85 path.resolve(__dirname, './../lib'),
86 path.resolve(__dirname, '../node_modules/@wmfs/tymly-test-helpers/plugins/allow-everything-rbac-plugin')
87 ],
88 blueprintPaths: [
89 path.resolve(fixture, 'blueprints', 'xmlflatten-blueprint')
90 ]
91 }
92 )
93
94 tymlyService = tymlyServices.tymly
95 statebox = tymlyServices.statebox
96 })
97
98 it('run the execution to process the XML file', async () => {
99 const outputFile = outputFileName('execution')
100
101 const executionDescription = await statebox.startExecution(
102 {
103 xmlPath: sourceFile,
104 csvPath: outputFile
105 }, // input
106 STATE_MACHINE_NAME, // state machine name
107 {
108 sendResponse: 'COMPLETE'
109 } // options
110 )
111
112 expect(executionDescription.status).to.eql('SUCCEEDED')
113 expect(executionDescription.currentStateName).to.eql('FlattenXmlFile')
114
115 verifyOutput(outputFile)
116 })
117
118 it('shutdown Tymly', () => {
119 return tymlyService.shutdown()
120 })
121 }) // blueprint
122})