view UnitTests/FileStorage.cpp @ 228:1af3bc092db8

removal of the old rest api
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 10:59:55 +0100
parents 4eb0c7ce86c9
children 5368bbe813cf
line wrap: on
line source

#include "gtest/gtest.h"

#include <ctype.h>
#include <glog/logging.h>

#include "../Core/FileStorage.h"
#include "../OrthancServer/ServerIndex.h"
#include "../Core/Toolbox.h"
#include "../Core/OrthancException.h"
#include "../Core/Uuid.h"
#include "../Core/HttpServer/FilesystemHttpSender.h"
#include "../Core/HttpServer/BufferHttpSender.h"
#include "../Core/FileStorage/FileStorageAccessor.h"
#include "../Core/FileStorage/CompressedFileStorageAccessor.h"

using namespace Orthanc;

TEST(FileStorage, Basic)
{
  FileStorage s("FileStorageUnitTests");

  std::string data = Toolbox::GenerateUuid();
  std::string uid = s.Create(data);
  std::string d;
  s.ReadFile(d, uid);
  ASSERT_EQ(d.size(), data.size());
  ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
}

TEST(FileStorage, EndToEnd)
{
  FileStorage s("FileStorageUnitTests");
  s.Clear();

  std::list<std::string> u;
  for (unsigned int i = 0; i < 10; i++)
  {
    u.push_back(s.Create(Toolbox::GenerateUuid()));
  }

  std::set<std::string> ss;
  s.ListAllFiles(ss);
  ASSERT_EQ(10u, ss.size());
  
  unsigned int c = 0;
  for (std::list<std::string>::iterator
         i = u.begin(); i != u.end(); i++, c++)
  {
    ASSERT_TRUE(ss.find(*i) != ss.end());
    if (c < 5)
      s.Remove(*i);
  }

  s.ListAllFiles(ss);
  ASSERT_EQ(5u, ss.size());

  s.Clear();
  s.ListAllFiles(ss);
  ASSERT_EQ(0u, ss.size());
}


TEST(FileStorageAccessor, Simple)
{
  FileStorage s("FileStorageUnitTests");
  FileStorageAccessor accessor(s);

  std::string data = "Hello world";
  std::string id = accessor.Write(data);
  
  std::string r;
  accessor.Read(r, id);

  ASSERT_EQ(data, r);
}


TEST(FileStorageAccessor, NoCompression)
{
  FileStorage s("FileStorageUnitTests");
  CompressedFileStorageAccessor accessor(s);

  accessor.SetCompressionForNextOperations(CompressionType_None);
  std::string data = "Hello world";
  std::string id = accessor.Write(data);
  
  std::string r;
  accessor.Read(r, id);

  ASSERT_EQ(data, r);
}


TEST(FileStorageAccessor, Compression)
{
  FileStorage s("FileStorageUnitTests");
  CompressedFileStorageAccessor accessor(s);

  accessor.SetCompressionForNextOperations(CompressionType_Zlib);
  std::string data = "Hello world";
  std::string id = accessor.Write(data);
  
  std::string r;
  accessor.Read(r, id);

  ASSERT_EQ(data, r);
}


TEST(FileStorageAccessor, Mix)
{
  FileStorage s("FileStorageUnitTests");
  CompressedFileStorageAccessor accessor(s);

  std::string r;
  std::string compressedData = "Hello";
  std::string uncompressedData = "HelloWorld";

  accessor.SetCompressionForNextOperations(CompressionType_Zlib);
  std::string compressedId = accessor.Write(compressedData);
  
  accessor.SetCompressionForNextOperations(CompressionType_None);
  std::string uncompressedId = accessor.Write(uncompressedData);
  
  accessor.SetCompressionForNextOperations(CompressionType_Zlib);
  accessor.Read(r, compressedId);
  ASSERT_EQ(compressedData, r);

  accessor.SetCompressionForNextOperations(CompressionType_None);
  accessor.Read(r, compressedId);
  ASSERT_NE(compressedData, r);
  
#if defined(__linux)
  // This test is too slow on Windows
  accessor.SetCompressionForNextOperations(CompressionType_Zlib);
  ASSERT_THROW(accessor.Read(r, uncompressedId), OrthancException);
#endif
}



#if 0
// TODO REMOVE THIS STUFF
namespace Orthanc
{
  class ServerStorageAccessor : public StorageAccessor
  {
  private:
    CompressedFileStorageAccessor composite_;
    ServerIndex& index_;
    AttachedFileType contentType_;

  protected:
    virtual std::string WriteInternal(const void* data,
                                      size_t size)
    {
      switch (contentType_)
      {
      case AttachedFileType_Json:
        composite_.SetCompressionForNextOperations(CompressionType_None);
        break;

      case AttachedFileType_Dicom:
        // TODO GLOBAL PARAMETER
        composite_.SetCompressionForNextOperations(CompressionType_Zlib);
        break;
        
      default:
        throw OrthancException(ErrorCode_InternalError);
      }

      std::string fileUuid = composite_.Write(data, size);

      
    }

  public: 
    ServerStorageAccessor(FileStorage& storage,
                          ServerIndex& index) :
      composite_(storage),
      index_(index)
    {
      contentType_ = AttachedFileType_Dicom;
    }

    void SetAttachmentType(AttachedFileType type)
    {
      contentType_ = type;
    }

    AttachedFileType GetAttachmentType() const
    {
      return contentType_;
    }

    virtual void Read(std::string& content,
                      const std::string& uuid)
    {
      std::string fileUuid;
      CompressionType compression;

      if (index_.GetFile(fileUuid, compression, uuid, contentType_))
      {
        composite_.SetCompressionForNextOperations(compression);
        composite_.Read(content, fileUuid);
      }
      else
      {
        throw OrthancException(ErrorCode_InternalError);
      }
    }

    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid)
    {
      std::string fileUuid;
      CompressionType compression;

      if (index_.GetFile(fileUuid, compression, uuid, contentType_))
      {
        composite_.SetCompressionForNextOperations(compression);
        return composite_.ConstructHttpFileSender(fileUuid);
      }
      else
      {
        throw OrthancException(ErrorCode_InternalError);
      }
    }
  };
}
#endif