blob: 1cb5da0b8a6783f612cd3c11b38baeb8387091fa [file] [log] [blame]
Michael Lando451a3402017-02-19 10:28:42 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21describe("General", function() {
22
23
24
25 describe("File Handler", function() {
26
27 var jsHandler = new JsHandler();
28 var fileHandler = jsHandler.getFileHandler();
29 fileHandler.fileContents.fileToUpload = mysqlTypeYml;
30 fileHandler.fileContents.artifactToUpload = installMySqlSH;
31
32
33 describe("checkFile Method", function() {
34
35 it("checkFile Valid", function() {
36 spyOn(window, 'alert');
37
38 affix('#dummyElement').val('stam.zip');
39 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.chef, 'dummyElement');
40 expect(isValid).toBeTruthy();
41
42
43 $('#dummyElement').val('stam.sh');
44 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.shell, 'dummyElement');
45 expect(isValid).toBeTruthy();
46
47 $('#dummyElement').val('stam.pp');
48 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.puppet, 'dummyElement');
49 expect(isValid).toBeTruthy();
50
51 $('#dummyElement').val('stam.yang');
52 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.yang, 'dummyElement');
53 expect(isValid).toBeTruthy();
54
55
56
57 });
58
59 it("checkFile inValid", function() {
60 spyOn(window, 'alert');
61
62 affix('#dummyElement').val('stam.zip');
63 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.shell, 'dummyElement');
64 expect(isValid).toBeFalsy();
65
66 $('#dummyElement').val('stam.sh');
67 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.chef, 'dummyElement');
68 expect(isValid).toBeFalsy();
69
70 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.puppet, 'dummyElement');
71 expect(isValid).toBeFalsy();
72
73 var isValid = fileHandler.checkFile(fileHandler.filesExtensions.yang, 'dummyElement');
74 expect(isValid).toBeFalsy();
75
76 });
77
78 });
79
80
81
82 describe("getZipName Method", function() {
83 it("getZipName", function() {
84 spyOn(fileHandler, 'getFileName').and.returnValue('mysql-type.yml');
85 expect(fileHandler.getZipName() == 'mysql-type.zip').toBeTruthy();
86 });
87 });
88 /*
89 describe("generateArtifactsList Method", function() {
90 beforeEach(function() {
91 jsHandler.getDomHandler().exisitngArifactsCounter = 2;
92 });
93
94 it("generateArtifactsList", function() {
95 spyOn(fileHandler, 'getFileName').and.returnValue('mysql-type.yml');
96 var artifactList = fileHandler.generateArtifactsList();
97// expect(fileHandler.generateArtifactsList() == 'mysql-type.zip').toBeTruthy();
98 });
99 });*/
100
101 describe("getArtifactsListFromYml Method", function() {
102 it("getArtifactsListFromYml", function() {
103 var expectedArtifacts = fileHandler.getArtifactsListFromYml();
104 expect(expectedArtifacts.length == 3).toBeTruthy();
105 expect($.inArray('scripts/install_mysql.sh', expectedArtifacts) != -1).toBeTruthy();
106 expect($.inArray('scripts/start_mysql.sh', expectedArtifacts) != -1).toBeTruthy();
107 expect($.inArray('images/mysql.png', expectedArtifacts) != -1).toBeTruthy();
108 });
109 });
110
111 describe("uploadFileJson Method", function() {
112 var sentJson, sendEncodedMd5;
113
114 beforeEach(function() {
115 sentJson = '';
116 sendEncodedMd5 = '';
117 spyOn(fileHandler, 'sendAjaxToServer').and.callFake(function(strifiedJson, encodedMd5) {
118 sentJson = JSON.parse(strifiedJson);
119 sendEncodedMd5 = encodedMd5;
120
121 });
122 spyOn(fileHandler, 'getZipName').and.callFake(function() { return 'fakeZipName.zip' });
123 spyOn(fileHandler, 'generateArtifactsList').and.callFake(function() { return 'fakeArtifactsList' });
124 spyOn(fileHandler, 'generateZip').and.callFake(function() { return 'fakeEncodedZipData' });
125 });
126
127 it("sendAjaxToServer method is called", function() {
128 fileHandler.uploadFileJson();
129 expect(fileHandler.sendAjaxToServer).toHaveBeenCalled();
130 });
131
132 it("json contains relevant fields", function() {
133 fileHandler.uploadFileJson();
134
135 expect(sentJson.payloadName == 'fakeZipName.zip').toBeTruthy();
136 expect(sentJson.isEncoded).toBeTruthy();
137 expect(sentJson.isCompressed).toBeTruthy();
138 expect(sentJson.artifactList == 'fakeArtifactsList').toBeTruthy();
139 expect(sentJson.payloadData == 'fakeEncodedZipData').toBeTruthy();
140
141
142
143 });
144
145 it("md5 is validated", function() {
146 fileHandler.uploadFileJson();
147
148 var strifiedJson = JSON.stringify(sentJson);
149 var md5 = CryptoJS.MD5(strifiedJson);
150 var encodedMd5 = btoa(md5);
151 expect(encodedMd5 == sendEncodedMd5).toBeTruthy();
152
153 });
154 });
155
156 describe("generateZip Method", function() {
157
158 it("generateZip", function() {
159
160
161 spyOn(fileHandler, 'getFileName').and.callFake(function(controName) {
162 var fileName;
163 if( controName == 'fileToUpload'){
164 fileName = 'mysql-type.yml';
165 }
166 else if( controName == 'imageToUpload'){
167 fileName = 'root.png';
168 }
169 else if( controName == 'artifactToUpload'){
170 fileName = 'install_mysql.sh';
171 }
172 else{
173 fileName = controName+'.sh';
174 }
175 return fileName;
176 });
177
178 var encodedZip = fileHandler.generateZip();
179 var decodedZip = atob(encodedZip);
180 var myZip = new JSZip(decodedZip);
181
182 var componentContentInZip = myZip.file('mysql-type.yml').asText();
183 expect(componentContentInZip == mysqlTypeYml).toBeTruthy();
184
185 var artifactContentInZip = myZip.folder('scripts').file('install_mysql.sh').asText();
186 expect(artifactContentInZip == installMySqlSH).toBeTruthy();
187
188
189
190 });
191 });
192 });
193
194
195});
196
197