|
1 | 1 | /* |
2 | 2 | * Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
3 | 3 | */ |
4 | | -var should = require('should'); |
| 4 | +const should = require('should'); |
5 | 5 |
|
6 | | -var fs = require('fs'); |
7 | | -var stream = require('stream'); |
8 | | -var util = require('util'); |
| 6 | +const fs = require('fs'); |
| 7 | +const stream = require('stream'); |
| 8 | +const util = require('util'); |
9 | 9 |
|
10 | | -var concatStream = require('concat-stream'); |
11 | | -var valcheck = require('core-util-is'); |
12 | | -var testconfig = require('../etc/test-config-qa.js'); |
| 10 | +const concatStream = require('concat-stream'); |
| 11 | +const valcheck = require('core-util-is'); |
| 12 | +const testconfig = require('../etc/test-config-qa.js'); |
13 | 13 |
|
14 | | -var marklogic = require('../'); |
15 | | -var q = marklogic.queryBuilder; |
| 14 | +const marklogic = require('../'); |
| 15 | +const q = marklogic.queryBuilder; |
16 | 16 |
|
17 | | -var dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection); |
18 | | -var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection); |
19 | | -var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection); |
| 17 | +const dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection); |
| 18 | +const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection); |
| 19 | +const dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection); |
| 20 | + |
| 21 | +const uri = '/test/write/somePdfFile.pdf'; |
20 | 22 |
|
21 | 23 | describe('Binary documents test', function () { |
22 | | - var binaryPath = __dirname + '/data/somePdfFile.pdf'; |
23 | | - var uri = '/test/binary/somePdfFile.pdf'; |
24 | | - var binaryValue = null; |
25 | | - before(function (done) { |
26 | | - this.timeout(10000); |
27 | | - fs.createReadStream(binaryPath). |
28 | | - pipe(concatStream({ encoding: 'buffer' }, function (value) { |
29 | | - binaryValue = value; |
30 | | - done(); |
31 | | - })); |
32 | | - }); |
| 24 | + const binaryPath = __dirname + '/data/somePdfFile.pdf'; |
| 25 | + let binaryValue = null; |
33 | 26 |
|
34 | | - it('should write the binary with Readable stream', function (done) { |
35 | | - this.timeout(10000); |
36 | | - var uri = '/test/write/somePdfFile.pdf'; |
37 | | - var readableBinary = new ValueStream(binaryValue); |
38 | | - //readableBinary.pause(); |
39 | | - dbWriter.documents.write({ |
40 | | - uri: uri, |
41 | | - contentType: 'application/pdf', |
42 | | - quality: 25, |
43 | | - properties: { prop1: 'foo' }, |
44 | | - content: readableBinary |
45 | | - }). |
46 | | - result(function (response) { |
47 | | - response.should.have.property('documents'); |
48 | | - done(); |
49 | | - }, done); |
| 27 | + before(async function () { |
| 28 | + binaryValue = await new Promise((resolve, reject) => { |
| 29 | + fs.createReadStream(binaryPath) |
| 30 | + .pipe(concatStream({ encoding: 'buffer' }, function (value) { |
| 31 | + resolve(value); |
| 32 | + })) |
| 33 | + .on('error', reject); |
| 34 | + }); |
50 | 35 | }); |
51 | 36 |
|
52 | | - it('should read the binary with Readable stream', function (done) { |
53 | | - this.timeout(10000); |
54 | | - var uri = '/test/write/somePdfFile.pdf'; |
55 | | - dbReader.documents.read(uri). |
56 | | - result(function (documents) { |
57 | | - //console.log(JSON.stringify(documents, null, 2)); |
58 | | - JSON.stringify(binaryValue).should.equal( |
59 | | - JSON.stringify(documents[0].content)); |
60 | | - done(); |
61 | | - }, done); |
62 | | - }); |
| 37 | + it('should write, read, read metadata, verify and delete the binary pdf content', async function () { |
63 | 38 |
|
64 | | - it('should read the binary document metadata', function (done) { |
65 | | - this.timeout(10000); |
66 | | - var uri = '/test/write/somePdfFile.pdf'; |
67 | | - dbReader.documents.read({ uris: uri, categories: ['metadata'] }) |
68 | | - .result(function (documents) { |
69 | | - var document = documents[0]; |
70 | | - document.quality.should.equal(25); |
71 | | - document.properties.prop1.should.equal('foo'); |
72 | | - done(); |
73 | | - }, done); |
74 | | - }); |
| 39 | + const readableBinary = new ValueStream(binaryValue); |
| 40 | + try { |
| 41 | + const writeResponse = await dbWriter.documents.write({ |
| 42 | + uri: uri, |
| 43 | + contentType: 'application/pdf', |
| 44 | + quality: 25, |
| 45 | + properties: { prop1: 'foo' }, |
| 46 | + content: readableBinary |
| 47 | + }).result(); |
| 48 | + writeResponse.should.have.property('documents'); |
75 | 49 |
|
76 | | - it('should delete the pdf file', function (done) { |
77 | | - dbAdmin.documents.removeAll({ |
78 | | - all: true |
79 | | - }). |
80 | | - result(function (response) { |
81 | | - done(); |
82 | | - }, done); |
83 | | - }); |
| 50 | + const docReadResp = await dbReader.documents.read(uri).result(); |
| 51 | + docReadResp[0].contentType.should.equal('application/pdf'); |
| 52 | + docReadResp[0].content.length.should.equal(binaryValue.length); |
| 53 | + Buffer.compare(binaryValue, docReadResp[0].content).should.equal(0); |
| 54 | + |
| 55 | + const docReadRespMeta = await dbReader.documents.read( |
| 56 | + { uris: uri, categories: ['metadata'] } |
| 57 | + ).result(); |
| 58 | + docReadRespMeta[0].quality.should.equal(25); |
| 59 | + docReadRespMeta[0].properties.prop1.should.equal('foo'); |
84 | 60 |
|
| 61 | + // original test deleted all documents. This one just deletes the one we wrote. |
| 62 | + await dbAdmin.documents.remove(uri).result(); |
| 63 | + |
| 64 | + } catch (error) { |
| 65 | + console.error(error); |
| 66 | + throw error; |
| 67 | + } |
| 68 | + }); |
85 | 69 | }); |
86 | 70 |
|
87 | 71 | function ValueStream(value) { |
|
0 commit comments