|
| 1 | +/* |
| 2 | + * Copyright 2014-2015 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +var should = require('should'); |
| 17 | +var fs = require('fs'); |
| 18 | + |
| 19 | +var testconfig = require('../etc/test-config-qa.js'); |
| 20 | + |
| 21 | +var marklogic = require('../'); |
| 22 | +var q = marklogic.queryBuilder; |
| 23 | +var db = marklogic.createDatabaseClient(testconfig.restWriterConnection); |
| 24 | + |
| 25 | +describe('Graphs transaction remove test', function() { |
| 26 | + var graphUri = 'marklogic.com/tx/peoplerem'; |
| 27 | + var graphPath = './node-client-api/test-complete/data/people3.ttl'; |
| 28 | + var graphPath2 = './node-client-api/test-complete/data/people4.ttl'; |
| 29 | + |
| 30 | + var tid = 0; |
| 31 | + var tid2 = 0; |
| 32 | + |
| 33 | + it('should do graph transaction remove combo', function(done) { |
| 34 | + db.transactions.open({transactionName: 'sparqlTx', timeLimit: 30}) |
| 35 | + .result(function(response) { |
| 36 | + //console.log('Opening transaction 1'); |
| 37 | + //console.log(JSON.stringify(response, null, 2)); |
| 38 | + tid = response.txid; |
| 39 | + return db.graphs.write({ |
| 40 | + txid: tid, |
| 41 | + uri: graphUri, |
| 42 | + contentType: 'text/turtle', |
| 43 | + data: fs.createReadStream(graphPath), |
| 44 | + permissions: [ |
| 45 | + {'role-name':'app-user', capabilities:['read']}, |
| 46 | + {'role-name':'app-builder', capabilities:['read', 'update']} |
| 47 | + ] |
| 48 | + }).result(); |
| 49 | + }) |
| 50 | + .then(function(response) { |
| 51 | + //console.log('Write graph with transaction 1'); |
| 52 | + //console.log(JSON.stringify(response, null, 2)); |
| 53 | + response.graph.should.equal('marklogic.com/tx/peoplerem'); |
| 54 | + return db.graphs.read({ |
| 55 | + uri: graphUri, |
| 56 | + contentType: 'application/json', |
| 57 | + txid: tid, |
| 58 | + category: 'content' |
| 59 | + }).result(); |
| 60 | + }) |
| 61 | + .then(function(response) { |
| 62 | + //console.log('Read graph content transaction 1'); |
| 63 | + //console.log(JSON.stringify(response, null, 2)); |
| 64 | + response.should.have.property('http://people.org/person1'); |
| 65 | + return db.graphs.read({ |
| 66 | + uri: graphUri, |
| 67 | + contentType: 'application/json', |
| 68 | + txid: tid, |
| 69 | + category: 'metadata' |
| 70 | + }).result(); |
| 71 | + }) |
| 72 | + .then(function(response) { |
| 73 | + //console.log('Read graph metadata transaction 1'); |
| 74 | + //console.log(JSON.stringify(response, null, 2)); |
| 75 | + var permissionsFound = 0; |
| 76 | + response.permissions.forEach(function(permission){ |
| 77 | + switch (permission['role-name']) { |
| 78 | + case 'app-user': |
| 79 | + permissionsFound++; |
| 80 | + permission.capabilities.length.should.equal(1); |
| 81 | + permission.capabilities[0].should.equal('read'); |
| 82 | + break; |
| 83 | + case 'app-builder': |
| 84 | + permissionsFound++; |
| 85 | + permission.capabilities.length.should.equal(2); |
| 86 | + permission.capabilities.should.containEql('read'); |
| 87 | + permission.capabilities.should.containEql('update'); |
| 88 | + break; |
| 89 | + } |
| 90 | + }); |
| 91 | + permissionsFound.should.equal(2); |
| 92 | + return db.graphs.probe({uri: graphUri, txid: tid}).result(); |
| 93 | + }) |
| 94 | + .then(function(response) { |
| 95 | + //console.log('Probe graph transaction 1'); |
| 96 | + //console.log(JSON.stringify(response, null, 2)); |
| 97 | + response.exists.should.equal(true); |
| 98 | + response.graph.should.equal('marklogic.com/tx/peoplerem'); |
| 99 | + return db.graphs.list({txid: tid}).result(); |
| 100 | + }) |
| 101 | + .then(function(response) { |
| 102 | + //console.log('List graph transaction 1'); |
| 103 | + //console.log(JSON.stringify(response, null, 2)); |
| 104 | + response.should.containEql('marklogic.com/tx/peoplerem'); |
| 105 | + return db.graphs.merge({ |
| 106 | + txid: tid, |
| 107 | + uri: graphUri, |
| 108 | + contentType: 'text/turtle', |
| 109 | + data: fs.createReadStream(graphPath2), |
| 110 | + permissions: [ |
| 111 | + {'role-name':'app-user', capabilities:['read']}, |
| 112 | + {'role-name':'app-builder', capabilities:['read', 'update']} |
| 113 | + ] |
| 114 | + }).result(); |
| 115 | + }) |
| 116 | + .then(function(response) { |
| 117 | + //console.log('Merge graph transaction 1'); |
| 118 | + //console.log(JSON.stringify(response, null, 2)); |
| 119 | + response.graph.should.equal('marklogic.com/tx/peoplerem'); |
| 120 | + return db.graphs.read({uri: graphUri, contentType: 'application/json', txid: tid}).result(); |
| 121 | + }) |
| 122 | + .then(function(response) { |
| 123 | + //console.log('Read merged graph transaction 1'); |
| 124 | + //console.log(JSON.stringify(response, null, 2)); |
| 125 | + response.should.have.property('http://people.org/person9'); |
| 126 | + response.should.have.property('http://people.org/person12'); |
| 127 | + return db.transactions.rollback(tid).result(); |
| 128 | + }) |
| 129 | + .then(function(response) { |
| 130 | + //console.log('Rollback transaction 1'); |
| 131 | + //console.log(JSON.stringify(response, null, 2)); |
| 132 | + response.finished.should.equal('rollback'); |
| 133 | + return db.transactions.open({transactionName: 'sparqlTx2', timeLimit: 60}).result(); |
| 134 | + }) |
| 135 | + .then(function(response) { |
| 136 | + //console.log('Opening transaction 2'); |
| 137 | + //console.log(JSON.stringify(response, null, 2)); |
| 138 | + tid2 = response.txid; |
| 139 | + return db.graphs.write({ |
| 140 | + txid: tid2, |
| 141 | + uri: graphUri, |
| 142 | + contentType: 'text/turtle', |
| 143 | + data: fs.createReadStream(graphPath), |
| 144 | + permissions: [ |
| 145 | + {'role-name':'app-user', capabilities:['read']}, |
| 146 | + {'role-name':'app-builder', capabilities:['read', 'update']} |
| 147 | + ] |
| 148 | + }).result(); |
| 149 | + }) |
| 150 | + .then(function(response) { |
| 151 | + //console.log('Write graph transaction 2'); |
| 152 | + //console.log(JSON.stringify(response, null, 2)); |
| 153 | + response.graph.should.equal('marklogic.com/tx/peoplerem'); |
| 154 | + return db.graphs.read({uri: graphUri, contentType: 'application/json', txid: tid2}).result(); |
| 155 | + }) |
| 156 | + .then(function(response) { |
| 157 | + //console.log('Read graph content transaction 2'); |
| 158 | + //console.log(JSON.stringify(response, null, 2)); |
| 159 | + response.should.have.property('http://people.org/person1'); |
| 160 | + response.should.have.property('http://people.org/person2'); |
| 161 | + return db.graphs.read({uri: graphUri, contentType: 'application/json', txid: tid2, category: 'permissions'}).result(); |
| 162 | + }) |
| 163 | + .then(function(response) { |
| 164 | + //console.log('Read graph permissions transaction 2'); |
| 165 | + //console.log(JSON.stringify(response, null, 2)); |
| 166 | + var permissionsFound = 0; |
| 167 | + response.permissions.forEach(function(permission){ |
| 168 | + switch (permission['role-name']) { |
| 169 | + case 'app-user': |
| 170 | + permissionsFound++; |
| 171 | + permission.capabilities.length.should.equal(1); |
| 172 | + permission.capabilities[0].should.equal('read'); |
| 173 | + break; |
| 174 | + case 'app-builder': |
| 175 | + permissionsFound++; |
| 176 | + permission.capabilities.length.should.equal(2); |
| 177 | + permission.capabilities.should.containEql('read'); |
| 178 | + permission.capabilities.should.containEql('update'); |
| 179 | + break; |
| 180 | + } |
| 181 | + }); |
| 182 | + permissionsFound.should.equal(2); |
| 183 | + return db.graphs.remove({uri: graphUri, txid: tid2}).result(); |
| 184 | + }) |
| 185 | + .then(function(response) { |
| 186 | + //console.log('Remove graph transaction 2'); |
| 187 | + //console.log(JSON.stringify(response, null, 2)); |
| 188 | + response.graph.should.equal('marklogic.com/tx/peoplerem'); |
| 189 | + return db.graphs.probe({uri: graphUri, txid: tid2}).result(); |
| 190 | + }) |
| 191 | + .then(function(response) { |
| 192 | + //console.log('Probe removed graph'); |
| 193 | + //console.log(JSON.stringify(response, null, 2)); |
| 194 | + response.exists.should.equal(false); |
| 195 | + return db.transactions.commit(tid2).result(); |
| 196 | + }) |
| 197 | + .then(function(response) { |
| 198 | + //console.log('Commit transaction 2'); |
| 199 | + //console.log(JSON.stringify(response, null, 2)); |
| 200 | + response.finished.should.equal('commit'); |
| 201 | + return db.graphs.probe(graphUri).result(); |
| 202 | + }) |
| 203 | + .then(function(response) { |
| 204 | + //console.log('Probe removed committed graph'); |
| 205 | + //console.log(JSON.stringify(response, null, 2)); |
| 206 | + response.exists.should.equal(false); |
| 207 | + done(); |
| 208 | + }, done); |
| 209 | + /*.catch(function(error) { |
| 210 | + console.log(error); |
| 211 | + done(); |
| 212 | + }, done);*/ |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
0 commit comments