unit TestuSendFileInChunks_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.Classes, uDirectories_TPS_ChunkUploader,
  System.NetEncoding, uSendFileInChunks_TPS_ChunkUploader, System.SysUtils,
  System.IOUtils, Winapi.Windows,Vcl.Forms,

  //myUnits

  uMain_TPS_ChunkUploader


  ;

type
  // Test methods for class TSendFileInChunks_TPS_ChunkUploader

  TestTSendFileInChunks_TPS_ChunkUploader = class(TTestCase)
  strict private

    FSendFileInChunks_TPS_ChunkUploader: TSendFileInChunks_TPS_ChunkUploader;
    FPS_ChunkUploader:TPS_ChunkUploader;
  private

    procedure CreateRandomDirAndFile(var FileName, DirName, AppPath: string);
    procedure DeleteRandomDirAndFile(FileName, DirName: string);


    function GetFileSize(AFilePath: String): Int64;

  public
    procedure SetUp; override;
    procedure TearDown; override;
  published

    procedure TestSendOneWholeFile();
    procedure TestGatherInfoAboutChunksToFile();
    procedure TestSendChunksInfoFile();
    procedure TestSendFileInChunks();

  end;

implementation

procedure TestTSendFileInChunks_TPS_ChunkUploader.SetUp;
begin


  FPS_ChunkUploader:=TPS_ChunkUploader.Create(nil);

     with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader do begin


      with HTTP_ConnectionParams do begin

      Protocol:='http';
      Host:='localhost';//'40.85.142.196';//'192.168.1.102';//'localhost';
      Port:='40000';
      IdUser:='1'; // << Critical important to use ID that are in DataBase coffeetest.users
      User:='SomeUser';

      DefineServerToRequest();

      end;


 end;


FSendFileInChunks_TPS_ChunkUploader :=
TSendFileInChunks_TPS_ChunkUploader.Create(FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader);

end;

procedure TestTSendFileInChunks_TPS_ChunkUploader.TearDown;
begin

  FSendFileInChunks_TPS_ChunkUploader.Free;
  FSendFileInChunks_TPS_ChunkUploader := nil;

  FPS_ChunkUploader.Free;
  FPS_ChunkUploader:=nil;

end;

procedure TestTSendFileInChunks_TPS_ChunkUploader.TestGatherInfoAboutChunksToFile;

var ReturnValue:Boolean;
    FS:TFileStream;
    DirForChunks:string;
    FileName:string;
    DirName:string;
    AppPath:string;
    RelativeDirForChunks:string;
    FileSize:Int64;

begin

ReturnValue:=false;


CreateRandomDirAndFile(FileName,DirName,AppPath);

try


RelativeDirForChunks:=DirName;

    // dividing in chunks
    FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
    SendFileInChunks.DivideAndGatherFile.DivideFileInChunks(

                              FileName,
                              1, // 1 byte
                              RelativeDirForChunks

                  );
//---

FileSize:=GetFileSize(FileName);


ReturnValue:=

      FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
      SendFileInChunks.GatherInfoAboutChunksToFile(

              RelativeDirForChunks,
              'ChunksInfo.txt',
              FileSize

      );


CheckTrue(ReturnValue,' Failed GatherInfoAboutChunksToFile ');

finally
DeleteRandomDirAndFile(FileName,DirName);
end;



end;



procedure TestTSendFileInChunks_TPS_ChunkUploader.TestSendChunksInfoFile();

var ReturnValue:Boolean;
    Filename:string;
    Dir:string;
    AppPath:string;

    RelativeOutputDirOnServerLocal:string;
    RelativeTempDirOnServerLocal:string;
    AbsoluteTempDirOnClientLocal:string;

    ChunksInfoFileName:string;
    ChunkSize:Int64;
    FileSizeBytes:Int64;

begin

 CreateRandomDirAndFile(Filename,Dir,AppPath);


 //create all dirs

 try

      //create all dirs
      with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.Directories do begin

      OriginFileName:=ExtractFileName(Filename);
      CreateUniqueNameFromOriginName();

      RelativeOutputDirOnServerLocal:=CreateOutputDirOnServer();
      RelativeTempDirOnServerLocal:=CreateTempDirOnServer();
      AbsoluteTempDirOnClientLocal:=CreateTempDirOnClient();


      end;


      with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.SendFileInChunks do begin

      ChunksInfoFileName:='ChunksInfo.txt';
      ChunkSize:=1;

      FileSizeBytes:=GetFileSize(Filename);

      DivideAndGatherFile.DivideFileInChunks(Filename,ChunkSize,AbsoluteTempDirOnClientLocal);
      GatherInfoAboutChunksToFile(AbsoluteTempDirOnClientLocal,ChunksInfoFileName,FileSizeBytes);

      ReturnValue:=
      SendChunksInfoFile(RelativeTempDirOnServerLocal,AbsoluteTempDirOnClientLocal+ChunksInfoFileName);

      end;


      CheckTrue(ReturnValue,' Failed SendChunksInfoFile');


 finally

 DeleteRandomDirAndFile(Filename,Dir);

 // delete all dirs
 if TDirectory.Exists(AbsoluteTempDirOnClientLocal)
 then TDirectory.Delete(AbsoluteTempDirOnClientLocal,true);

 // delete relative temp dir on Server
 FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
 HTTPRequests.DeleteDirOnServer(RelativeTempDirOnServerLocal);

 end;


end;



procedure TestTSendFileInChunks_TPS_ChunkUploader.TestSendFileInChunks;

var Filename,Dir,AppPath:string;
    ReturnValue:Boolean;

begin

   CreateRandomDirAndFile(Filename,Dir,AppPath);
   try

   ReturnValue:=
   FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.SendFileInChunks.
   SendFileInChunks(Filename,1);

   finally

   DeleteRandomDirAndFile(Filename,Dir);

   end;


   CheckTrue(ReturnValue,' Failed TestSendFileInChunks ');

end;


procedure TestTSendFileInChunks_TPS_ChunkUploader.TestSendOneWholeFile;
var

  ReturnValue:Boolean;
  FS:TFileStream;
  FileName:String;

  RelativeOutputDirectory:string;

begin


ReturnValue:=False;

// TODO: Setup method call parameters

FileName:='SomeTestFile.txt';

// where to save On Server?
RelativeOutputDirectory:=
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.Directories.CreateOutputDirOnServer();

// which file to send?
if not TFile.Exists(FileName) then

  begin
  FS:=TFile.Create(FileName);
  CloseHandle(FS.Handle);
  end

else TFile.WriteAllText(FileName,'SomeTestContent');


ReturnValue:= FSendFileInChunks_TPS_ChunkUploader.SendOneWholeFile('SomeTestFile.txt',RelativeOutputDirectory);

 // TODO: Validate method results
CheckTrue(ReturnValue,' Failed SendOneWholeFile ');

TFile.Delete('SomeTestFile.txt');

end;


// additional

procedure TestTSendFileInChunks_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 TestTSendFileInChunks_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).ToString;
  // 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;




function TestTSendFileInChunks_TPS_ChunkUploader.GetFileSize(AFilePath: String): Int64;
var
  FS: TFileStream;


begin


  try

     Result:=-1;

     FS := TFileStream.Create(AFilePath, fmOpenRead);

      try
        Result:=FS.Size;
      finally
        FS.Free;
      end;

  except

         on E:EStreamError do

         begin

         Result := -1;

          raise Exception.Create('This is EStreamError EClassName'+E.ClassName+' '
          +'EMessage '+E.Message);

         end;


  on E:Exception do

        begin
           Result := -1;




           raise Exception.Create(E.ClassName+' Exception Raised : '
         +#13#10+#13#10+E.Message);

        end;

  end;

end;



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

