unit TestuDivideAndGatherFile_TPS_ChunkUploader;
{

  Delphi DUnit Test Case
  ----------------------
  This unit contains a skeleton test case class generated by the Test Case Wizard.
  Modify the generated code to correctly setup and call the methods from the unit 
  being tested.

}

interface

uses
  TestFramework, System.Variants, uDivideAndGatherFile_TPS_ChunkUploader,
  uCreateUniqueName_TPS_ChunkUploader, Contnrs, Winapi.Windows, System.Types,
  Vcl.Dialogs, System.IOUtils, Vcl.Forms, Vcl.Controls, System.Classes, System.SysUtils,
  Winapi.Messages, Vcl.Graphics, uSortStringList_TPS_ChunkUploader, Vcl.StdCtrls;

type
  // Test methods for class TDivideAndGatherFile_TPS_ChunkUploader

  TestTDivideAndGatherFile_TPS_ChunkUploader = class(TTestCase)
  strict private
    FDivideAndGatherFile_TPS_ChunkUploader: TDivideAndGatherFile_TPS_ChunkUploader;
  private
    procedure CreateRandomDirAndFile(var FileName: string; var DirName: string; var AppPath: string);
    procedure DeleteRandomDirAndFile(FileName: string; DirName: string);



  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestDivideFileInChunks;
    procedure TestGatherFileFromChunks;
  end;

implementation

procedure TestTDivideAndGatherFile_TPS_ChunkUploader.SetUp;
begin

  FDivideAndGatherFile_TPS_ChunkUploader := TDivideAndGatherFile_TPS_ChunkUploader.Create(nil);


end;

procedure TestTDivideAndGatherFile_TPS_ChunkUploader.TearDown;
begin
  FDivideAndGatherFile_TPS_ChunkUploader.Free;
  FDivideAndGatherFile_TPS_ChunkUploader := nil;
end;



procedure TestTDivideAndGatherFile_TPS_ChunkUploader.TestDivideFileInChunks;
var
  ReturnValue: Boolean;
  DirForChunks: string;
  AppPath:String;
  FileName:string;
  DirName:string;

begin

ReturnValue:=false;

  CreateRandomDirAndFile(FileName, DirName, AppPath);

try

         //Setting Params
         DirForChunks:=AppPath+DirName;

          ReturnValue :=

          FDivideAndGatherFile_TPS_ChunkUploader.DivideFileInChunks(

                          FileName,
                          1, // 1 byte
                          DirForChunks

              );

          // TODO: Validate method results

          CheckTrue(ReturnValue,'Failed DivideFileInChunks');
          Status('Success');

finally

  DeleteRandomDirAndFile(FileName, DirName);

end;



end;





procedure TestTDivideAndGatherFile_TPS_ChunkUploader.TestGatherFileFromChunks;
var
  ReturnValue: Boolean;
  OutputFilename: string;
  RelativeOutputDir: string;
  AChunksDir: string;

  FileName:string;
  DirName:string;
  AppPath:string;
  DirForChunks:string;

begin

ReturnValue:=false;


  // dividing in chunks
    CreateRandomDirAndFile(FileName, DirName, AppPath);
    DirForChunks:=AppPath+DirName;

    FDivideAndGatherFile_TPS_ChunkUploader.DivideFileInChunks(

                              FileName,
                              1, // 1 byte
                              DirForChunks

                  );
//---


  OutputFilename:='GatheredFromChunks'+Random(1000000).ToString+'.txt';
  RelativeOutputDir:='';

  // TODO: Setup method call parameters
  ReturnValue := FDivideAndGatherFile_TPS_ChunkUploader.

    GatherFileFromChunks(

         DirForChunks,
         RelativeOutputDir,
         OutputFilename

     );

  // TODO: Validate method results

  CheckTrue(ReturnValue,'Failed GatherFileFromChunks');
  Status('Success');


//Deleting file and dirs

DeleteRandomDirAndFile(FileName, DirName);

if TFile.Exists('SomeTestFile_GatheredFromChunks.txt') then TFile.Delete('SomeTestFile_GatheredFromChunks.txt');


end;

procedure TestTDivideAndGatherFile_TPS_ChunkUploader.DeleteRandomDirAndFile(FileName: string; DirName: string);
begin
  if TDirectory.Exists(DirName) then
    TDirectory.Delete(DirName, true);
  if TFile.Exists(FileName) then
    TFile.Delete(FileName);
end;

procedure TestTDivideAndGatherFile_TPS_ChunkUploader.CreateRandomDirAndFile(var FileName: string; var DirName: string; var AppPath: string);
var
  FS: TFileStream;
begin

  FileName := 'SomeTestFileName' + Random(1000000).ToString + '.txt';
  DirName := 'SomeTestDir' + Random(1000000);
  // TODO: Setup method call parameters
  //which file to divide?
  if not TFile.Exists(FileName) then
  begin
    FS := TFile.Create(FileName);
    CloseHandle(FS.Handle);
  end
  else
    TFile.WriteAllText(FileName, 'SomeTestContent');
  // in which directory to put files?
  AppPath := ExtractFilePath(Application.ExeName);
  if not TDirectory.Exists(DirName) then
    TDirectory.CreateDirectory(AppPath + DirName);
end;

initialization
  // Register any test cases with the test runner
  RegisterTest(TestTDivideAndGatherFile_TPS_ChunkUploader.Suite);
end.

