forked from ShaftHQ/SHAFT_ENGINE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileActions.java
executable file
·790 lines (712 loc) · 34.3 KB
/
FileActions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package com.shaft.cli;
import com.google.common.hash.Hashing;
import com.shaft.tools.io.PropertyFileManager;
import com.shaft.tools.io.ReportManager;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.SystemUtils;
import org.sikuli.basics.FileManager;
import org.testng.Assert;
import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
@SuppressWarnings("UnusedReturnValue")
public class FileActions {
private static final String ERROR_CANNOT_CREATE_DIRECTORY = "Could not create directory: ";
private FileActions() {
throw new IllegalStateException("Utility class");
}
/**
* Copies a file from sourceFilePath to destinationFilePath on the local storage
*
* @param sourceFilePath the full (absolute) path of the source file that
* will be copied
* @param destinationFilePath the full (absolute) path of the desired location
* and file name for the newly created copied file
*/
public static void copyFile(String sourceFilePath, String destinationFilePath) {
File sourceFile = new File(sourceFilePath);
File destinationFile = new File(destinationFilePath);
copyFile(sourceFile, destinationFile);
passAction("Source File: \"" + sourceFilePath + "\" | Destination File: \"" + destinationFilePath + "\"");
}
/**
* Copies files from sourceDirectory to destinationDirectory using the provided
* terminalSession. References: https://www.computerhope.com/unix/ucp.htm
* https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
*
* @param terminalSession an object that determines the type of
* terminalSession which will be used to execute
* this File Action
* @param sourceDirectory full path to the sourceDirectory
* @param destinationDirectory full path to the destinationDirectory
* @param fileName target fileName
* @return a string value that holds the result of this terminal command
*/
public static String copyFile(TerminalActions terminalSession, String sourceDirectory, String destinationDirectory,
String fileName) {
String command;
if (isTargetOSUnixBased()) {
if (fileName.trim().equals("")) {
command = "rsync --verbose --recursive " + sourceDirectory + File.separator + " "
+ destinationDirectory;
} else {
command = "rsync --verbose --recursive " + sourceDirectory + File.separator + fileName + " "
+ destinationDirectory + File.separator;
}
} else {
command = "robocopy /e /v /fp " + sourceDirectory + " " + destinationDirectory + " " + fileName;
}
String terminalLog = terminalSession.performTerminalCommand(command);
passAction("Source Directory: \"" + sourceDirectory + "\" | Destination Directory: \"" + destinationDirectory
+ "\" | File Name: \"" + fileName + "\"", terminalLog);
return terminalLog;
}
public static String listFilesInDirectory(String targetDirectory) {
StringBuilder files = new StringBuilder();
try {
Collection<File> filesList = FileUtils.listFiles(new File(targetDirectory), TrueFileFilter.TRUE,
TrueFileFilter.TRUE);
filesList.forEach(file -> files.append(file.getName()).append(System.lineSeparator()));
} catch (IllegalArgumentException rootCauseException) {
ReportManager.log(rootCauseException);
failAction("Failed to list files in this directory: \"" + targetDirectory + "\"", rootCauseException);
}
passAction("Target Directory: \"" + targetDirectory + "\"", files.toString().trim());
return files.toString().trim();
}
/**
* Lists all files inside the targetDirectory
*
* @param terminalSession an object that determines the type of terminalSession
* which will be used to execute this File Action
* @param targetDirectory full path to the targetDirectory
* @return a string value that holds the result of this terminal command
*/
public static String listFilesInDirectory(TerminalActions terminalSession, String targetDirectory) {
List<String> commands;
if (isTargetOSUnixBased()) {
commands = Collections.singletonList("ls " + targetDirectory);
} else {
commands = Collections.singletonList("dir " + targetDirectory);
}
String log = terminalSession.performTerminalCommands(commands);
passAction("TargetDirectory: \"" + targetDirectory + "\"", log);
return log;
}
/**
* This method is used to compute the SHA256 checksum for any file. This
* checksum can be used to compare two files and confirm that they are
* identical, regardless of the file type.
*
* @param terminalSession provides information about the
* machine which contains the target
* file
* @param targetFileFolderPath the full absolute path of the
* folder that contains the target
* file, must end with a slash "/"
* @param targetFileName the name and extension of the
* target file
* @param pathToTempDirectoryOnRemoteMachine [OPTIONAL] to be used in case of
* dockerized target machine. This is
* a temporary directory that will be
* created on the remote machine to
* extract a file from inside a
* docker, and will be deleted
* afterwards
* @return a string that holds the SHA256 checksum for the target file
*/
@SuppressWarnings("UnstableApiUsage")
public static String getFileChecksum(TerminalActions terminalSession, String targetFileFolderPath,
String targetFileName, String... pathToTempDirectoryOnRemoteMachine) {
String targetFilePath = copyFileToLocalMachine(terminalSession, targetFileFolderPath, targetFileName,
pathToTempDirectoryOnRemoteMachine);
// read file
byte[] fileBytes;
String sha256 = "";
try {
fileBytes = Files.readAllBytes(Paths.get(targetFilePath));
sha256 = Hashing.sha256().hashBytes(fileBytes).toString();
} catch (IOException rootCauseException) {
ReportManager.log(rootCauseException);
failAction("Failed to read file \"" + targetFilePath + "\"", rootCauseException);
}
passAction("Target File: \"" + targetFilePath + "\" | SHA-256: \"" + sha256 + "\"");
return sha256;
}
/**
* This method is used to copy a certain file from a remote machine (dockerized
* or not) to the current execution machine.
*
* @param terminalSession provides information about the
* machine which contains the target
* file
* @param targetFileFolderPath the full absolute path of the
* folder that contains the target
* file, must end with a slash "/"
* @param targetFileName the name and extension of the
* target file
* @param pathToTempDirectoryOnRemoteMachine [OPTIONAL] to be used in case of
* dockerized target machine. This is
* a temporary directory that will be
* created on the remote machine to
* extract a file from inside a
* docker, and will be deleted
* afterwards
* @return a string that holds the full absolute path (inside a temporary
* folder) for the file that was copied to the local machine
*/
public static String copyFileToLocalMachine(TerminalActions terminalSession, String targetFileFolderPath,
String targetFileName, String... pathToTempDirectoryOnRemoteMachine) {
String targetFilePath = targetFileFolderPath + targetFileName;
TerminalActions terminalSessionForRemoteMachine = new TerminalActions();
// fetch file from inside docker to the machine itself
if (terminalSession.isDockerizedTerminal()) {
terminalSessionForRemoteMachine = new TerminalActions(terminalSession.getSshHostName(),
terminalSession.getSshPortNumber(), terminalSession.getSshUsername(),
terminalSession.getSshKeyFileFolderName(), terminalSession.getSshKeyFileName());
// copy from docker to machine
terminalSessionForRemoteMachine.performTerminalCommand("rm -r " + pathToTempDirectoryOnRemoteMachine[0]);
terminalSessionForRemoteMachine
.performTerminalCommand("mkdir -p " + pathToTempDirectoryOnRemoteMachine[0] + targetFileFolderPath);
terminalSessionForRemoteMachine.performTerminalCommand("docker cp " + terminalSession.getDockerName() + ":"
+ targetFilePath + " " + pathToTempDirectoryOnRemoteMachine[0] + targetFilePath);
targetFilePath = pathToTempDirectoryOnRemoteMachine[0] + targetFilePath;
}
// fetch file from terminal session to local machine
if (terminalSession.isRemoteTerminal()) {
// remote regular
String sshParameters = "-i " + FileActions.getAbsolutePath(terminalSession.getSshKeyFileFolderName(),
terminalSession.getSshKeyFileName()) + " -P " + terminalSession.getSshPortNumber();
String pathToRemoteFileThatWillBeCopied = targetFilePath;
String source = terminalSession.getSshUsername() + "@" + terminalSession.getSshHostName() + ":"
+ pathToRemoteFileThatWillBeCopied;
// creating local temp directory
String pathToLocalParentFolder = FileActions.getAbsolutePath("target/temp");
FileActions.deleteFolder(pathToLocalParentFolder);
FileActions.createFolder(pathToLocalParentFolder);
String destination = pathToLocalParentFolder + "/" + targetFileName;
targetFilePath = destination;
String command = "scp -v -o StrictHostKeyChecking=no " + sshParameters + " -r " + source + " "
+ destination;
// restricting file access to bypass jenkins issue
(new TerminalActions()).performTerminalCommand("chmod 400 " + FileActions
.getAbsolutePath(terminalSession.getSshKeyFileFolderName(), terminalSession.getSshKeyFileName()));
(new TerminalActions()).performTerminalCommand(command);
}
// else local regular
// it's already on the local machine so no need to do anything here
// clean temp directory on remote machine
if (terminalSession.isDockerizedTerminal() && terminalSession.isRemoteTerminal()) {
terminalSessionForRemoteMachine.performTerminalCommand("rm -r " + Arrays.toString(pathToTempDirectoryOnRemoteMachine));
}
passAction("Target File Path: \"" + targetFilePath + "\"");
return targetFilePath;
}
/**
* Deletes a file from the local storage
*
* @param targetFilePath the full (absolute) path of the source file that will
* be deleted
*/
public static void deleteFile(String targetFilePath) {
FileUtils.deleteQuietly(new File(targetFilePath));
passAction("Target File Path: \"" + targetFilePath + "\"");
}
public static void writeToFile(String fileFolderName, String fileName, List<String> text) {
byte[] textToBytes = String.join(System.lineSeparator(), text).getBytes();
writeToFile(fileFolderName, fileName, textToBytes);
}
public static void writeToFile(String fileFolderName, String fileName, byte[] content) {
String absoluteFilePath = getAbsolutePath(fileFolderName, fileName);
try {
Path filePath = Paths.get(absoluteFilePath);
Path parentDir = filePath.getParent();
if (!parentDir.toFile().exists()) {
Files.createDirectories(parentDir);
}
Files.write(filePath, content);
passAction("Target File Path: \"" + filePath + "\"", Arrays.toString(content));
} catch (InvalidPathException | IOException rootCauseException) {
ReportManager.log(rootCauseException);
failAction("Folder Name: \"" + fileFolderName + "\", File Name \"" + fileName + "\".", rootCauseException);
}
}
public static void writeToFile(String fileFolderName, String fileName, String text) {
byte[] textToBytes = text.getBytes();
writeToFile(fileFolderName, fileName, textToBytes);
}
public static String readFromFile(String fileFolderName, String fileName) {
return readFromFile(fileFolderName + fileName);
}
public static byte[] readFromImageFile(String pathToTargetImage) {
byte[] data = new byte[0];
String absoluteFilePath = getAbsolutePath(pathToTargetImage);
Path filePath = Paths.get(absoluteFilePath);
try {
data = Files.readAllBytes(filePath);
passAction("File Path: \"" + filePath + "\"");
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
return data;
}
public static String readFromFile(String pathToTargetFile) {
String absoluteFilePath = getAbsolutePath(pathToTargetFile);
String text = FileManager.readFileToString(new File(absoluteFilePath));
passAction("File Path: \"" + absoluteFilePath + "\"", text);
return text;
}
/**
* Tests whether the file or directory denoted by this abstract pathname exists.
*
* @param fileFolderName The location of the folder that contains the target
* file, relative to the project's root folder, ending
* with a /
* @param fileName The name of the target file (including its extension
* if any)
* @param numberOfRetries number of times to try to find the file, given that
* each retry is separated by a 500 millisecond wait time
* @return true if the file exists, false if it doesn't
*/
public static boolean doesFileExist(String fileFolderName, String fileName, int numberOfRetries) {
boolean doesFileExit = false;
int i = 0;
while (i < numberOfRetries) {
try {
doesFileExit = (new File(fileFolderName + fileName)).getAbsoluteFile().exists();
} catch (Exception e) {
ReportManager.log(e);
}
if (Boolean.FALSE.equals(doesFileExit)) {
try {
Thread.sleep(500);
} catch (Exception e1) {
ReportManager.log(e1);
}
}
i++;
}
passAction("File Path: \"" + fileFolderName + fileName + "\"");
return doesFileExit;
}
public static boolean doesFileExist(String targetFile) {
boolean doesFileExit = false;
try {
doesFileExit = (new File(targetFile)).getAbsoluteFile().exists();
} catch (Exception e) {
ReportManager.log(e);
failAction(e);
}
passAction("File Path: \"" + targetFile + "\"");
return doesFileExit;
}
/**
* Returns the full (absolute) file/folder path using the project-relative
* fileFolderName and the fileName
*
* @param fileFolderName The location of the folder that contains the target
* file, relative to the project's root folder, ending
* with a /
* @param fileName The name of the target file (including its extension if
* any)
* @return a string value that represents the full/absolute file/folder path
*/
public static String getAbsolutePath(String fileFolderName, String fileName) {
String filePath = "";
try {
filePath = (new File(fileFolderName + fileName)).getAbsolutePath();
passAction("Relative File Path: \"" + fileFolderName + fileName + "\"", filePath);
} catch (Exception e) {
ReportManager.log(e);
failAction(e);
}
return filePath;
}
public static String getAbsolutePath(String fileFolderName) {
String filePath = "";
try {
filePath = (new File(fileFolderName)).getAbsolutePath();
passAction("Relative Folder Path: \"" + fileFolderName + "\"", filePath);
} catch (Exception e) {
ReportManager.log(e);
failAction(e);
}
return filePath;
}
public static void copyFolder(String sourceFolderPath, String destinationFolderPath) {
File sourceFolder = new File(sourceFolderPath);
File destinationFolder = new File(destinationFolderPath);
try {
FileUtils.copyDirectory(sourceFolder, destinationFolder);
passAction(
"Source Folder: \"" + sourceFolderPath + "\" | Destination Folder: \"" + destinationFolder + "\"");
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
public static void copyFolderFromJar(String sourceFolderPath, String destinationFolderPath) {
try {
URL url = new URL(sourceFolderPath.replace("file:", "jar:file:"));
JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarConnection.getJarFile();
/*
* Iterate all entries in the jar file.
*/
for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) {
JarEntry jarEntry = e.nextElement();
String jarEntryName = jarEntry.getName();
String jarConnectionEntryName = jarConnection.getEntryName();
/*
* Extract files only if they match the path.
*/
if (jarEntryName.startsWith(jarConnectionEntryName)) {
String filename = jarEntryName.startsWith(jarConnectionEntryName) ? jarEntryName.substring(jarConnectionEntryName.length()) : jarEntryName;
File currentFile = new File(destinationFolderPath, filename);
if (jarEntry.isDirectory()) {
boolean success = currentFile.mkdirs();
if (success) {
ReportManager.logDiscrete("Directory Created successfully...");
}
} else {
InputStream is = jarFile.getInputStream(jarEntry);
OutputStream out = FileUtils.openOutputStream(currentFile);
IOUtils.copy(is, out);
is.close();
out.close();
}
}
}
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
public static void deleteFolder(String folderPath) {
File directory = new File(folderPath);
try {
FileUtils.forceDelete(directory);
passAction("Target Folder: \"" + folderPath + "\"");
} catch (FileNotFoundException e) {
// file is already deleted or was not found
ReportManager.log("Folder [" + folderPath + "] was not found, it may have already been deleted.");
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
public static void createFolder(String folderPath) {
try {
FileUtils.forceMkdir(new File(folderPath));
passAction("Target Folder: \"" + folderPath + "\"");
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
public static void createFile(String folderPath, String fileName) {
try {
FileUtils.forceMkdir(new File(folderPath));
FileUtils.touch(new File(folderPath + fileName));
passAction("Target Folder: \"" + folderPath + "\", Target File: \"" + fileName + "\"");
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
@SuppressWarnings("UnusedReturnValue")
public static boolean zipFiles(String srcFolder, String destZipFile) {
boolean result = false;
try {
zipFolder(srcFolder, destZipFile);
result = true;
passAction("Target Folder: \"" + srcFolder + "\" | Destination Archive: \"" + destZipFile + "\"");
} catch (Exception e) {
ReportManager.log(e);
failAction(e);
}
return result;
}
@SuppressWarnings("UnusedReturnValue")
public static File unpackArchive(URL url, String destinationFolderPath) {
File targetDir = new File(destinationFolderPath);
if (!targetDir.exists() && !targetDir.mkdirs()) {
failAction("file: " + url.toString() + " to directory: " + destinationFolderPath);
}
File unpacked = null;
try (InputStream in = new BufferedInputStream(url.openStream(), 1024)) {
// make sure we get the actual file
File zip = File.createTempFile("arc", ".zip", targetDir);
OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
copyInputStream(in, out);
out.close();
unpacked = unpackArchive(zip, targetDir);
FileActions.deleteFile(zip.getAbsolutePath());
passAction("Target URL\"" + url.toString() + "\" | Destination Folder: \"" + destinationFolderPath + "\"");
} catch (IOException rootCauseException) {
ReportManager.log(rootCauseException);
failAction("file: " + url.toString() + " to directory: " + destinationFolderPath, rootCauseException);
}
return unpacked;
}
public static URL downloadFile(String targetFileURL, String destinationFilePath) {
return downloadFile(targetFileURL, destinationFilePath, 0, 0);
}
public static URL downloadFile(String targetFileURL, String destinationFilePath, int connectionTimeout,
int readTimeout) {
if (targetFileURL != null && destinationFilePath != null) {
// force logging
boolean initialLoggingState = ReportManager.isDiscreteLogging();
ReportManager.setDiscreteLogging(false);
try {
ReportManager.log("Downloading a file from this url [" + targetFileURL + "] to this directory ["
+ destinationFilePath + "], please wait as downloading may take some time...");
FileUtils.copyURLToFile(new URL(targetFileURL), new File(destinationFilePath), connectionTimeout,
readTimeout);
ReportManager.logDiscrete("Downloading completed successfully.");
URL downloadedFile = new File(destinationFilePath).toURI().toURL();
passAction("Target File URL\"" + targetFileURL + "\" | Destination Folder: \"" + destinationFilePath
+ "\" | Connection Timeout: \"" + connectionTimeout + "\" | Read Timeout: \"" + readTimeout
+ "\"");
return downloadedFile;
} catch (IOException rootCauseException) {
ReportManager.log(rootCauseException);
failAction("Target File URL: [" + targetFileURL + "], and Destination File Path: ["
+ destinationFilePath + "]", rootCauseException);
return null;
} finally {
ReportManager.setDiscreteLogging(initialLoggingState);
}
} else {
failAction("Target File URL: [" + targetFileURL + "], and Destination File Path: [" + destinationFilePath
+ "]");
return null;
}
}
private static void passAction(String testData) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
reportActionResult(actionName, testData, null, true);
}
private static void passAction(String testData, String log) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
reportActionResult(actionName, testData, log, true);
}
private static void failAction(String testData, Exception... rootCauseException) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
failAction(actionName, testData, rootCauseException);
}
private static void failAction(Exception... rootCauseException) {
String actionName = Thread.currentThread().getStackTrace()[2].getMethodName();
failAction(actionName, null, rootCauseException);
}
private static void failAction(String actionName, String testData, Exception... rootCauseException) {
String message = reportActionResult(actionName, testData, null, false);
if (rootCauseException != null && rootCauseException.length >= 1) {
Assert.fail(message, rootCauseException[0]);
} else {
Assert.fail(message);
}
}
private static String reportActionResult(String actionName, String testData, String log, Boolean passFailStatus) {
String message;
if (Boolean.TRUE.equals(passFailStatus)) {
message = "File Action [" + actionName + "] successfully performed.";
} else {
message = "File Action [" + actionName + "] failed.";
}
List<List<Object>> attachments = new ArrayList<>();
if (testData != null && !testData.isEmpty() && testData.length() >= 500) {
List<Object> actualValueAttachment = Arrays.asList("File Action Test Data - " + actionName, "Actual Value",
testData);
attachments.add(actualValueAttachment);
} else if (testData != null && !testData.isEmpty()) {
message = message + " With the following test data [" + testData + "].";
}
if (log != null && !log.trim().equals("")) {
attachments.add(Arrays.asList("File Action Actual Result", "Command Log", log));
}
// Minimize File Action log steps and move them to discrete logs if called
// within SHAFT_Engine itself
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement parentMethod = stackTrace[4];
if (parentMethod.getClassName().contains("com.shaft")) {
ReportManager.logDiscrete(message);
} else {
if (!attachments.equals(new ArrayList<>())) {
ReportManager.log(message, attachments);
} else {
ReportManager.log(message);
}
}
return message;
}
private static boolean isTargetOSUnixBased() {
if (System.getProperty("executionAddress") == null) {
PropertyFileManager.readPropertyFiles();
}
if (System.getProperty("executionAddress").trim().equals("local")) {
// local execution
if (SystemUtils.IS_OS_WINDOWS) {
return false;
} else if (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) {
return true;
} else {
ReportManager.logDiscrete("Unsupported OS type, will assume it's unix based.");
return true;
}
} else {
// remote execution
String targetOS = System.getProperty("targetOperatingSystem");
if ("Windows-64".equals(targetOS)) {
return false;
} else if ("Linux-64".equals(targetOS) || "Mac-64".equals(targetOS)) {
return true;
} else {
ReportManager.logDiscrete("Unsupported OS type, will assume it's unix based.");
return true;
}
}
}
private static void copyFile(File sourceFile, File destinationFile) {
try {
FileUtils.copyFile(sourceFile, destinationFile);
} catch (IOException rootCauseException) {
ReportManager.log(rootCauseException);
failAction(rootCauseException);
}
}
private static void zipFolder(String srcFolder, String destZipFile) {
/*
* create the output stream to zip file result
*/
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter)) {
/*
* add the folder to the zip
*/
addFolderToZip("", srcFolder, zip);
/*
* close the zip objects
*/
zip.flush();
} catch (IOException e) {
ReportManager.log(e);
failAction(e);
}
}
private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag)
throws IOException {
/*
* create the file object for inputs
*/
File folder = new File(srcFile);
/*
* if the folder is empty add empty folder to the Zip file
*/
if (flag) {
zip.putNextEntry(new ZipEntry(path + FileSystems.getDefault().getSeparator() + folder.getName()
+ FileSystems.getDefault().getSeparator()));
} else { /*
* if the current name is directory, recursively traverse it to get the files
*/
if (folder.isDirectory()) {
/*
* if folder is not empty
*/
addFolderToZip(path, srcFile, zip);
} else {
/*
* write the file to the output
*/
try (FileInputStream in = new FileInputStream(srcFile)) {
byte[] buf = new byte[1024];
int len;
zip.putNextEntry(new ZipEntry(path + FileSystems.getDefault().getSeparator() + folder.getName()));
while ((len = in.read(buf)) > 0) {
/*
* Write the Result
*/
zip.write(buf, 0, len);
}
} catch (Exception e) {
ReportManager.log(e);
failAction(e);
}
}
}
}
private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
File folder = new File(srcFolder);
/*
* check the empty folder
*/
if (Objects.requireNonNull(folder.list()).length == 0) {
addFileToZip(path, srcFolder, zip, true);
} else {
/*
* list the files in the folder
*/
for (String fileName : Objects.requireNonNull(folder.list())) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + FileSystems.getDefault().getSeparator() + fileName, zip,
false);
} else {
addFileToZip(path + FileSystems.getDefault().getSeparator() + folder.getName(),
srcFolder + FileSystems.getDefault().getSeparator() + fileName, zip, false);
}
}
}
}
private static File unpackArchive(File theFile, File targetDir) throws IOException {
if (!theFile.exists()) {
throw new IOException(theFile.getAbsolutePath() + " does not exist");
}
if (buildDirectory(targetDir)) {
throw new IOException(ERROR_CANNOT_CREATE_DIRECTORY + targetDir);
}
try (ZipFile zipFile = new ZipFile(theFile)) {
for (Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = entries.nextElement();
File file = new File(targetDir, File.separator + entry.getName());
if (buildDirectory(file.getParentFile())) {
throw new IOException(ERROR_CANNOT_CREATE_DIRECTORY + file.getParentFile());
}
if (!entry.isDirectory()) {
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(file)));
} else {
if (buildDirectory(file)) {
throw new IOException(ERROR_CANNOT_CREATE_DIRECTORY + file);
}
}
}
} catch (IOException e) {
throw new IOException(e);
}
passAction("Target File\"" + theFile.getAbsolutePath() + "\" | Destination Folder: \"" + targetDir + "\"");
return theFile;
}
private static void copyInputStream(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len >= 0) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
in.close();
out.close();
}
private static boolean buildDirectory(File file) {
return !file.exists() && !file.mkdirs();
}
}