unit TestuHTTPRequests_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.SysUtils, IdBaseComponent, IdHTTP, IdException, IdTCPClient,
  IdComponent, Vcl.Dialogs, uHTTPRequests_TPS_ChunkUploader, System.Classes,
  IdTCPConnection,System.IOUtils,Winapi.Windows, Vcl.Forms,

  //myUnits

  uMain_TPS_ChunkUploader

  ;

type
  // Test methods for class THTTPRequests_TPS_ChunkUploader

  TestTHTTPRequests_TPS_ChunkUploader = class(TTestCase)
  strict private
    FHTTPRequests_TPS_ChunkUploader: THTTPRequests_TPS_ChunkUploader;

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

  public
    procedure SetUp; override;
    procedure TearDown; override;

  published


    procedure TestIsHTTPConnectionOk();

    procedure TestCreateDirOnServer();
    procedure TestDeleteDirOnServer();

    procedure TestSendOneWholeFile();
    procedure TestSendManyFiles();
    procedure TestIsCheckChunksIntegrityOk();
    procedure TestGatherFileFromChunks();


  end;

implementation

procedure TestTHTTPRequests_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;



end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TearDown;
begin
  FHTTPRequests_TPS_ChunkUploader.Free;
  FHTTPRequests_TPS_ChunkUploader := nil;


  FPS_ChunkUploader.Free();
  FPS_ChunkUploader:=nil;

end;



procedure TestTHTTPRequests_TPS_ChunkUploader.TestCreateDirOnServer;

var
  ReturnValue: Boolean;

begin

//

ReturnValue := FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTPRequests.CreateDirOnServer('SomeTestDir');
  // TODO: Validate method results
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTPRequests.DeleteDirOnServer('SomeTestDir');

  CheckEquals(true,ReturnValue,' CreateDirOnServer Failed');
  Status('Success');

end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TestDeleteDirOnServer;

var ReturnValue:Boolean;

begin
//

  with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTPRequests do begin

   CreateDirOnServer('SomeTestDir');
   ReturnValue:=DeleteDirOnServer('SomeTestDir');
   CheckTrue(ReturnValue,'SomeTestDir');

  end;


end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TestGatherFileFromChunks;

var OriginFileName:string;
    Filename,Dirname,AppPath:string;
    RelativeTempDirOnServer:string;
    AbsoluteTempDirOnClient:string;
    ChunksInfoFileName:string;
    ReturnValue: Boolean;
    FileSize:Int64;
    RelativeOutputDirOnServer: String;
    idUserString:String;

begin

//
CreateRandomDirAndFile(Filename,Dirname,AppPath);



try

    with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader do begin

     idUserString:=HTTP_ConnectionParams.IDUser;

     with Directories do begin

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

          RelativeOutputDirOnServer:=CreateOutputDirOnServer();
          RelativeTempDirOnServer:=CreateTempDirOnServer();
          AbsoluteTempDirOnClient:=CreateTempDirOnClient();

     end;

     ChunksInfoFileName:='ChunksInfo.txt';

     with SendFileInChunks do begin

     FileSize:=GetFileSize(Filename);
     DivideAndGatherFile.DivideFileInChunks(Filename,1,AbsoluteTempDirOnClient);
     GatherInfoAboutChunksToFile(AbsoluteTempDirOnClient,ChunksInfoFileName,FileSize);


     end;


     with HTTPRequests do begin



     SendManyFiles(AbsoluteTempDirOnClient,RelativeTempDirOnServer);
     IsCheckChunksIntegrityOk(RelativeTempDirOnServer,ChunksInfoFileName);
     ReturnValue:=GatherFileFromChunks
     (RelativeTempDirOnServer,RelativeOutputDirOnServer,Directories.OriginFileName,idUserString);

     end;



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

    end;


finally

DeleteRandomDirAndFile(Filename,Dirname);

//deleting temp dir on server
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
HTTPRequests.DeleteDirOnServer(RelativeTempDirOnServer);

end;


end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TestIsCheckChunksIntegrityOk;
var OriginFileName:string;
    Filename,Dirname,AppPath:string;
    RelativeTempDirOnServer:string;
    AbsoluteTempDirOnClient:string;
    ChunksInfoFileName:string;
    ReturnValue: Boolean;
    FileSize:Int64;

begin

//
CreateRandomDirAndFile(Filename,Dirname,AppPath);

FileSize:=FPS_ChunkUploader.
VisualFrame_TPS_ChunkUploader.SendFileInChunks.GetFileSize(Filename);

try

    with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader do begin


     with Directories do begin

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

          RelativeTempDirOnServer:=CreateTempDirOnServer();
          AbsoluteTempDirOnClient:=CreateTempDirOnClient();

     end;

     ChunksInfoFileName:='ChunksInfo.txt';

     with SendFileInChunks do begin

     DivideAndGatherFile.DivideFileInChunks(Filename,1,AbsoluteTempDirOnClient);
     GatherInfoAboutChunksToFile(AbsoluteTempDirOnClient,ChunksInfoFileName,FileSize);


     end;


     with HTTPRequests do begin

     SendManyFiles(AbsoluteTempDirOnClient,RelativeTempDirOnServer);
     ReturnValue:=IsCheckChunksIntegrityOk(RelativeTempDirOnServer,ChunksInfoFileName);

     end;



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

    end;


finally

DeleteRandomDirAndFile(Filename,Dirname);

//deleting temp dir on server
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
HTTPRequests.DeleteDirOnServer(RelativeTempDirOnServer);

end;


end;


procedure TestTHTTPRequests_TPS_ChunkUploader.TestIsHTTPConnectionOk;
var
  ReturnValue: Boolean;
begin

ReturnValue:=
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTPRequests.IsHTTPConnectionOk();

  // TODO: Validate method results

  CheckEquals(true,ReturnValue,' IsHTTPConnectionOk Failed');
  Status('Success');

end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TestSendManyFiles();

var OriginFileName:string;
    Filename,Dirname,AppPath:string;
    RelativeTempDirOnServer:string;
    AbsoluteTempDirOnClient:string;
    ChunksInfoFileName:string;
    ReturnValue: Boolean;
    FileSize:Int64;

begin

//
CreateRandomDirAndFile(Filename,Dirname,AppPath);

FileSize:=FPS_ChunkUploader.
VisualFrame_TPS_ChunkUploader.SendFileInChunks.GetFileSize(Filename);

try

    with FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader do begin


     with Directories do begin

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

          RelativeTempDirOnServer:=CreateTempDirOnServer();
          AbsoluteTempDirOnClient:=CreateTempDirOnClient();

     end;

     ChunksInfoFileName:='ChunksInfo.txt';

     with SendFileInChunks do begin

     DivideAndGatherFile.DivideFileInChunks(Filename,1,AbsoluteTempDirOnClient);
     GatherInfoAboutChunksToFile(AbsoluteTempDirOnClient,ChunksInfoFileName,FileSize);


     end;


     with HTTPRequests do begin

     ReturnValue:=SendManyFiles(AbsoluteTempDirOnClient,RelativeTempDirOnServer);

     end;

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

    end;


finally

DeleteRandomDirAndFile(Filename,Dirname);

//deleting temp dir on server
FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.
HTTPRequests.DeleteDirOnServer(RelativeTempDirOnServer);

end;


end;

procedure TestTHTTPRequests_TPS_ChunkUploader.TestSendOneWholeFile;

var OutPutDirOnServer:String;
    idUserString:String;
    ReturnValue:Boolean;
    FileName:string;
    FS:TFileStream;

begin

FileName:='SomeTestFile.txt';

//who will Send?
idUserString:=FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTP_ConnectionParams.IDUser;

// where to save On Server?
OutPutDirOnServer:=
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;

TFile.WriteAllText(FileName,'SomeTestContent');


    ReturnValue:=
    FPS_ChunkUploader.VisualFrame_TPS_ChunkUploader.HTTPRequests.SendOneWholeFile(

    FileName,
    OutPutDirOnServer,
    FileName,
    idUserString

    );


    TFile.Delete('SomeTestFile.txt');



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

end;




procedure TestTHTTPRequests_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;


procedure TestTHTTPRequests_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;



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

