/*! * * Copyright (c) 2024-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file tst_fileHandler.cpp * \author (last) Behrouz NematiPour * \date (last) 07-Mar-2024 * \author (original) Behrouz NematiPour * \date (original) 07-Mar-2024 * */ #include "tst_fileHandler.h" // Qt // Project #include "VAdjustmentResponseBase.h" #include "VTreatmentAdjustmentUltrafiltrationState.h" #include "FileHandler.h" #include "VPreTreatmentDisposablesPrimeData.h" #include "VAlarmStatus.h" tst_fileHandler::tst_fileHandler(QObject *parent) : QObject(parent) { } void tst_fileHandler::tst_read_append_return() { int filesCreated = helperFunction_createFolderFiles("/tmp/tst_read_append_return/", "mock_folder", 1, 1); if(filesCreated <=0) QSKIP("could not create test file"); const QString fileToRead = "/tmp/tst_read_append_return/mock_folder_0/mock_out0.out"; const QString controlString = "TestContent"; QString contentRead = controlString; bool readResults = FileHandler::read(fileToRead, contentRead, true); QVERIFY(readResults); QVERIFY(contentRead.contains(controlString) && (contentRead.length() > controlString.length())); } void tst_fileHandler::tst_removeFiles() { const int testCount = 15; int filesCreated = helperFunction_createFolderFiles("/tmp/tst_removeFiles/", "mock_folder", 1, testCount); if(filesCreated != testCount) { QSKIP("creation of temp files failed"); } QStringList folderList = {"/tmp/tst_removeFiles/mock_folder_0/"}; QDate dateOlderThanFilter; int countRemoved = FileHandler::removeFiles(folderList, {}, dateOlderThanFilter); QCOMPARE(countRemoved, testCount); } void tst_fileHandler::tst_isMounted() { bool isReadOnly = false; bool isMounted = false; // mount location is not mounted isMounted = FileHandler::isMounted("/media/sd-card", &isReadOnly); QCOMPARE(isMounted, false); // Expecting a fail here QCOMPARE(isReadOnly, false); // Expecting a fail here // empty path case isMounted = FileHandler::isMounted("", &isReadOnly); QCOMPARE(isMounted, false); // Expecting a fail here QCOMPARE(isReadOnly, false); // Expecting a fail here //Coverage call since we remove the trailing slash isMounted = FileHandler::isMounted("/media/sd-card/", &isReadOnly); QCOMPARE(isReadOnly, false); // Expecting a fail here } void tst_fileHandler::tst_tmpUsable() { bool isTmpUsable = FileHandler::tmpUsable(); QVERIFY(isTmpUsable); isTmpUsable = FileHandler::tmpUsable(); QVERIFY(isTmpUsable); } void tst_fileHandler::tst_find() { // tests for find(QString, QStringList, quint8) const QString folderPath = "/tmp/tst_find/mock_folder_0/"; int filesCreatedCount = helperFunction_createFolderFiles(folderPath, "mock_folder", 1, 0); QFileInfoList list = FileHandler::find(folderPath, {}, 100); QCOMPARE(list.length(), 0); // expect 0 files to remove when the percent retained is 100% list = FileHandler::find(folderPath+"_not_exist", {}, 80); QCOMPARE(list.length(), 0); // expect 0 files to remove when the folder path doesn't exist list = FileHandler::find(folderPath, {}, 80); QCOMPARE(list.length(), 0); // expecting 0 files to remove when the folder exist, but no files filesCreatedCount = helperFunction_createFolderFiles(folderPath, "mock_folder", 1, 10); list = FileHandler::find(folderPath, {}, 99); QCOMPARE(list.length(), 0); // expecting empty list when retaining 99% of files // list = FileHandler::find(folderPath, {}, 1); // QVERIFY(list.length()!=0); // expecting a non-empty list when not keeping everything in path list = FileHandler::find(folderPath, {}, 0); QCOMPARE(list.length(), filesCreatedCount); // expecting a non-empty list when not keeping everything in path // second find(QString, QStringList) function list = FileHandler::find(folderPath+"_not_exist", {}); QCOMPARE(list.length(), 0); // expect 0 files to remove since the path does not exist } /*! * \brief tst_fileHandler::helperFunction_createFolderFiles * \details Creates vNumberOfFolders in the vDirPath with vNumberOfFiles in each folder. The files created contain "TestContent" text * and their filenames are "VBaseFileNamesvBaseFileExt" the I is the index from 0...vNumberOfFiles. * Folders are also automatically indexed from 0...vNumberOfFolders * \return The count of files created */ int tst_fileHandler::helperFunction_createFolderFiles(const QString vDirPath, const QString vBaseFolderName, const int vNumberOfFolders, const int vNumberOfFiles, const QString VBaseFileNames, const QString vBaseFileExt) { int filesCreatedCount = 0; QDir rootPath = QDir::root(); for(quint8 folderIndex = 0; folderIndex < vNumberOfFolders; folderIndex++) { const QString currentFolder = QString("%1/%2_%3/").arg(vDirPath).arg(vBaseFolderName).arg(folderIndex); bool isCreatedDir = rootPath.mkpath(currentFolder); if(!isCreatedDir) { return -1;} for(quint8 i=0; i