[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / red / storage / index_spec.js
1 /**
2  * Copyright 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 var should = require("should");
17
18 describe("red/storage/index", function() {
19     
20     it('rejects the promise when settings suggest loading a bad module', function(done) {
21         
22         var wrongModule = {
23                 storageModule : "thisaintloading"
24         };
25         
26         var storage = require("../../../red/storage/index");
27        storage.init(wrongModule).then( function() {
28            var one = 1;
29            var zero = 0;
30            try {
31                zero.should.equal(one, "The initialization promise should never get resolved");   
32            } catch(err) {
33                done(err);
34            }
35        }).catch(function(e) {
36            done(); //successfully rejected promise
37        });
38     });
39     
40     it('non-string storage module', function(done) {
41         var initSetsMeToTrue = false;
42         
43         var moduleWithBooleanSettingInit = {
44                 init : function() {
45                     initSetsMeToTrue = true;
46                 }
47         };
48         
49         var setsBooleanModule = {
50                 storageModule : moduleWithBooleanSettingInit
51         };
52         
53         var storage = require("../../../red/storage/index");
54         storage.init(setsBooleanModule);
55         initSetsMeToTrue.should.be.true;
56         done();
57     });
58     
59     it('respects storage interface', function(done) {
60         var calledFlagGetFlows = false;
61         var calledFlagGetCredentials = false;
62         var calledFlagGetAllFlows = false;
63         var calledInit = false;
64         
65         var interfaceCheckerModule = {
66                 init : function (settings) {
67                     settings.should.be.an.Object;
68                     calledInit = true;
69                 },
70                 getFlows : function() {
71                     calledFlagGetFlows = true;
72                 },
73                 saveFlows : function (flows) {
74                     flows.should.be.true;
75                 },
76                 getCredentials : function() {
77                     calledFlagGetCredentials = true;
78                 },
79                 saveCredentials : function(credentials) {
80                     credentials.should.be.true;
81                 },
82                 getAllFlows : function() {
83                     calledFlagGetAllFlows = true;
84                 },
85                 getFlow : function(fn) {
86                     fn.should.equal("name");
87                 },
88                 saveFlow : function(fn, data) {
89                     fn.should.equal("name");
90                     data.should.be.true;
91                 },
92                 getLibraryEntry : function(type, path) {
93                     type.should.be.true;
94                     path.should.equal("name");
95                 },
96                 saveLibraryEntry : function(type, path, meta, body) {
97                     type.should.be.true;
98                     path.should.equal("name");
99                     meta.should.be.true;
100                     body.should.be.true;
101                 }
102         };
103         
104         var moduleToLoad = {
105                 storageModule : interfaceCheckerModule
106         };
107         var storage = require("../../../red/storage/index");
108         
109         storage.init(moduleToLoad);
110         storage.getFlows();
111         storage.saveFlows(true);
112         storage.getCredentials(); 
113         storage.saveCredentials(true);
114         storage.getAllFlows();
115         storage.getFlow("name");
116         storage.saveFlow("name", true);
117         storage.getLibraryEntry(true, "name");
118         storage.saveLibraryEntry(true, "name", true, true);
119         
120         calledInit.should.be.true;
121         calledFlagGetFlows.should.be.true;
122         calledFlagGetCredentials.should.be.true;
123         calledFlagGetAllFlows.should.be.true;
124         
125         done();
126     });
127     
128 });