[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / red / storage / localfilesystem_spec.js
1 /**
2  * Copyright 2013, 2014 IBM Corp.
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
17 var should = require("should");
18 var fs = require('fs-extra');
19 var path = require('path');
20
21 var localfilesystem = require("../../../red/storage/localfilesystem");
22
23 describe('LocalFileSystem', function() {
24     var userDir = path.join(__dirname,".testUserHome");
25     var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
26     beforeEach(function(done) {
27         fs.remove(userDir,function(err) {
28             fs.mkdir(userDir,done);
29         });
30     });
31     afterEach(function(done) {
32         fs.remove(userDir,done);
33     });
34
35     it('should initialise the user directory',function(done) {
36         localfilesystem.init({userDir:userDir}).then(function() {
37             fs.existsSync(path.join(userDir,"lib")).should.be.true;
38             fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true;
39             done();
40         }).otherwise(function(err) {
41             done(err);
42         });
43     });
44
45     it('should handle missing flow file',function(done) {
46         localfilesystem.init({userDir:userDir}).then(function() {
47             var flowFile = 'flows_'+require('os').hostname()+'.json';
48             var flowFilePath = path.join(userDir,flowFile);
49             fs.existsSync(flowFilePath).should.be.false;
50             localfilesystem.getFlows().then(function(flows) {
51                 flows.should.eql([]);
52                 done();
53             }).otherwise(function(err) {
54                 done(err);
55             });
56         }).otherwise(function(err) {
57             done(err);
58         });
59     });
60
61     it('should save flows to the default file',function(done) {
62         localfilesystem.init({userDir:userDir}).then(function() {
63             var flowFile = 'flows_'+require('os').hostname()+'.json';
64             var flowFilePath = path.join(userDir,flowFile);
65             var flowFileBackupPath = path.join(userDir,"flows.backup");
66             fs.existsSync(flowFilePath).should.be.false;
67             fs.existsSync(flowFileBackupPath).should.be.false;
68             localfilesystem.saveFlows(testFlow).then(function() {
69                 fs.existsSync(flowFilePath).should.be.true;
70                 fs.existsSync(flowFileBackupPath).should.be.false;
71                 localfilesystem.getFlows().then(function(flows) {
72                     flows.should.eql(testFlow);
73                     done();
74                 }).otherwise(function(err) {
75                     done(err);
76                 });
77             }).otherwise(function(err) {
78                 done(err);
79             });
80         }).otherwise(function(err) {
81             done(err);
82         });
83     });
84
85     it('should save flows to the specified file',function(done) {
86         var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
87         var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
88         var flowFile = 'test.json';
89         var flowFilePath = path.join(userDir,flowFile);
90
91         localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
92             fs.existsSync(defaultFlowFilePath).should.be.false;
93             fs.existsSync(flowFilePath).should.be.false;
94
95             localfilesystem.saveFlows(testFlow).then(function() {
96                 fs.existsSync(defaultFlowFilePath).should.be.false;
97                 fs.existsSync(flowFilePath).should.be.true;
98                 localfilesystem.getFlows().then(function(flows) {
99                     flows.should.eql(testFlow);
100                     done();
101                 }).otherwise(function(err) {
102                     done(err);
103                 });
104             }).otherwise(function(err) {
105                 done(err);
106             });
107         }).otherwise(function(err) {
108             done(err);
109         });
110     });
111
112     it('should backup the flows file', function(done) {
113         var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
114         var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
115         var flowFile = 'test.json';
116         var flowFilePath = path.join(userDir,flowFile);
117         var flowFileBackupPath = path.join(userDir,"flows.backup");
118
119         localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
120             fs.existsSync(defaultFlowFilePath).should.be.false;
121             fs.existsSync(flowFilePath).should.be.false;
122             fs.existsSync(flowFileBackupPath).should.be.false;
123
124             localfilesystem.saveFlows(testFlow).then(function() {
125                 fs.existsSync(flowFileBackupPath).should.be.false;
126                 fs.existsSync(defaultFlowFilePath).should.be.false;
127                 fs.existsSync(flowFilePath).should.be.true;
128                 var content = fs.readFileSync(flowFilePath,'utf8');
129                 var testFlow2 = [{"type":"tab","id":"bc5672ad.2741d8","label":"Sheet 2"}];
130                 
131                 localfilesystem.saveFlows(testFlow2).then(function() {
132                     fs.existsSync(flowFileBackupPath).should.be.true;
133                     fs.existsSync(defaultFlowFilePath).should.be.false;
134                     fs.existsSync(flowFilePath).should.be.true;
135                     var backupContent = fs.readFileSync(flowFileBackupPath,'utf8');
136                     content.should.equal(backupContent);
137                     var content2 = fs.readFileSync(flowFilePath,'utf8');
138                     content2.should.not.equal(backupContent);
139                     done();
140                     
141                 }).otherwise(function(err) {
142                     done(err);
143                 });
144                 
145             }).otherwise(function(err) {
146                 done(err);
147             });
148         }).otherwise(function(err) {
149             done(err);
150         });
151             
152             
153     });
154     
155     it('should handle missing credentials', function(done) {
156         var flowFile = 'test.json';
157         var flowFilePath = path.join(userDir,flowFile);
158         var credFile = path.join(userDir,"test_cred.json");
159         localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
160             fs.existsSync(credFile).should.be.false;
161
162             localfilesystem.getCredentials().then(function(creds) {
163                 creds.should.eql({});
164                 done();
165             }).otherwise(function(err) {
166                 done(err);
167             });
168         }).otherwise(function(err) {
169             done(err);
170         });
171     });
172
173     it('should handle credentials', function(done) {
174         var flowFile = 'test.json';
175         var flowFilePath = path.join(userDir,flowFile);
176         var credFile = path.join(userDir,"test_cred.json");
177
178         localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
179
180             fs.existsSync(credFile).should.be.false;
181
182             var credentials = {"abc":{"type":"creds"}};
183
184             localfilesystem.saveCredentials(credentials).then(function() {
185                 fs.existsSync(credFile).should.be.true;
186                 localfilesystem.getCredentials().then(function(creds) {
187                     creds.should.eql(credentials);
188                     done();
189                 }).otherwise(function(err) {
190                     done(err);
191                 });
192             }).otherwise(function(err) {
193                 done(err);
194             });
195         }).otherwise(function(err) {
196             done(err);
197         });
198     });
199
200     it('should return an empty list of library flows',function(done) {
201         localfilesystem.init({userDir:userDir}).then(function() {
202             localfilesystem.getAllFlows().then(function(flows) {
203                 flows.should.eql({});
204                 done();
205             }).otherwise(function(err) {
206                 done(err);
207             });
208         }).otherwise(function(err) {
209             done(err);
210         });
211     });
212
213     it('should return a valid list of library flows',function(done) {
214         localfilesystem.init({userDir:userDir}).then(function() {
215             var flowLib = path.join(userDir,"lib","flows");
216             fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w"));
217             fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w"));
218             fs.mkdirSync(path.join(flowLib,"C"));
219             fs.closeSync(fs.openSync(path.join(flowLib,"C","D.json"),"w"));
220             var testFlowsList = {"d":{"C":{"f":["D"]}},"f":["A","B"]};
221
222             localfilesystem.getAllFlows().then(function(flows) {
223                 flows.should.eql(testFlowsList);
224                 done();
225             }).otherwise(function(err) {
226                 done(err);
227             });
228         }).otherwise(function(err) {
229             done(err);
230         });
231     });
232
233     it('should fail a non-existent flow', function(done) {
234         localfilesystem.init({userDir:userDir}).then(function() {
235             localfilesystem.getFlow("a/b/c.json").then(function(flow) {
236                 should.fail(flow,"No flow","Flow found");
237             }).otherwise(function(err) {
238                 // err should be null, so this will pass
239                 done(err);
240             });
241         }).otherwise(function(err) {
242             done(err);
243         });
244     });
245
246     it('should return a flow',function(done) {
247         localfilesystem.init({userDir:userDir}).then(function() {
248             var testflowString = JSON.stringify(testFlow);
249             localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() {
250                 localfilesystem.getFlow("a/b/c/d.json").then(function(flow) {
251                     flow.should.eql(testflowString);
252                     done();
253                 }).otherwise(function(err) {
254                     done(err);
255                 });
256             }).otherwise(function(err) {
257                 done(err);
258             });
259         }).otherwise(function(err) {
260             done(err);
261         });
262     });
263
264     it('should return an empty list of library objects',function(done) {
265         localfilesystem.init({userDir:userDir}).then(function() {
266             localfilesystem.getLibraryEntry('object','').then(function(flows) {
267                 flows.should.eql({});
268                 done();
269             }).otherwise(function(err) {
270                 done(err);
271             });
272         }).otherwise(function(err) {
273             done(err);
274         });
275     });
276
277     it('should return an error for a non-existent library object',function(done) {
278         localfilesystem.init({userDir:userDir}).then(function() {
279             localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
280                 should.fail(null,null,"non-existent flow");
281             }).otherwise(function(err) {
282                 should.exist(err);
283                 done();
284             });
285         }).otherwise(function(err) {
286             done(err);
287         });
288     });
289
290     function createObjectLibrary() {
291         var objLib = path.join(userDir,"lib","object");
292         fs.mkdirSync(objLib);
293         fs.mkdirSync(path.join(objLib,"A"));
294         fs.mkdirSync(path.join(objLib,"B"));
295         fs.mkdirSync(path.join(objLib,"B","C"));
296         fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
297         fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
298     }
299
300     it('should return a directory listing of library objects',function(done) {
301         localfilesystem.init({userDir:userDir}).then(function() {
302             createObjectLibrary();
303
304             localfilesystem.getLibraryEntry('object','').then(function(flows) {
305                 flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
306                 localfilesystem.getLibraryEntry('object','B').then(function(flows) {
307                     flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
308                     localfilesystem.getLibraryEntry('object','B/C').then(function(flows) {
309                         flows.should.eql([]);
310                         done();
311                     }).otherwise(function(err) {
312                         done(err);
313                     });
314                 }).otherwise(function(err) {
315                     done(err);
316                 });
317             }).otherwise(function(err) {
318                 done(err);
319             });
320         }).otherwise(function(err) {
321             done(err);
322         });
323     });
324
325     it('should return a library object',function(done) {
326         localfilesystem.init({userDir:userDir}).then(function() {
327             createObjectLibrary();
328             localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) {
329                 body.should.eql("// not a metaline \n\n Hi");
330                 done();
331             }).otherwise(function(err) {
332                 done(err);
333             });
334         }).otherwise(function(err) {
335             done(err);
336         });
337     });
338
339     it('should return a newly saved library object',function(done) {
340         localfilesystem.init({userDir:userDir}).then(function() {
341             createObjectLibrary();
342             localfilesystem.getLibraryEntry('object','B').then(function(flows) {
343                 flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
344                 localfilesystem.saveLibraryEntry('object','B/D/file3.js',{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
345                     localfilesystem.getLibraryEntry('object','B/D').then(function(flows) {
346                         flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
347                         localfilesystem.getLibraryEntry('object','B/D/file3.js').then(function(body) {
348                             body.should.eql("// another non meta line\n\n Hi There");
349                             done();
350                         }).otherwise(function(err) {
351                             done(err);
352                         });
353                     }).otherwise(function(err) {
354                         done(err);
355                     });
356                 }).otherwise(function(err) {
357                     done(err);
358                 });
359             }).otherwise(function(err) {
360                 done(err);
361             });
362         }).otherwise(function(err) {
363             done(err);
364         });
365     });
366
367 });