# Orthanc - A Lightweight, RESTful DICOM Store
# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
# Department, University Hospital of Liege, Belgium
# Copyright (C) 2017-2023 Osimis S.A., Belgium
# Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
# Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.


##
## This file is meant to be used only by ../SharedLibrary/CMakeLists.txt
##

cmake_minimum_required(VERSION 2.8...4.0)
project(UnitTestsProject)

set(STATIC_BUILD OFF CACHE BOOL "Static build of the third-party libraries (necessary for Windows)")
set(UNIT_TESTS_WITH_HTTP_CONNEXIONS ON CACHE BOOL "Allow unit tests to make HTTP requests")

set(USE_GOOGLE_TEST_DEBIAN_PACKAGE OFF CACHE BOOL "Use the sources of Google Test shipped with libgtest-dev (Debian only)")
set(USE_SYSTEM_GOOGLE_TEST ON CACHE BOOL "Use the system version of Google Test")

if (UNIT_TESTS_WITH_HTTP_CONNEXIONS)
  add_definitions(-DUNIT_TESTS_WITH_HTTP_CONNEXIONS=1)
else()
  add_definitions(-DUNIT_TESTS_WITH_HTTP_CONNEXIONS=0)
endif()

if (ORTHANC_FRAMEWORK_STATIC)
  include_directories(${ORTHANC_FRAMEWORK_ROOT}/..)
else()
  include(CheckIncludeFile)
  include(CheckIncludeFileCXX)

  link_libraries(jsoncpp)
  
  include(FindLua)
  if (NOT LUA_FOUND)
    message(FATAL_ERROR "Please install the liblua-dev package")
  endif()  
  include_directories(${LUA_INCLUDE_DIR})
  link_libraries(${LUA_LIBRARIES})

  check_include_file(sqlite3.h HAVE_SQLITE_H)
  if (NOT HAVE_SQLITE_H)
    message(FATAL_ERROR "Please install the libsqlite3-dev package")
  endif()
  link_libraries(sqlite3)

  check_include_file_cxx(pugixml.hpp HAVE_PUGIXML_H)
  if (NOT HAVE_PUGIXML_H)
    message(FATAL_ERROR "Please install the libpugixml-dev package")
  endif()      
  link_libraries(pugixml)

  find_package(Boost COMPONENTS filesystem thread system date_time iostreams locale regex)
  if (NOT Boost_FOUND)
      message(FATAL_ERROR "Unable to locate Boost on this system")
  endif()
  link_libraries(${Boost_LIBRARIES})

  include(FindDCMTK NO_MODULE)
  link_libraries(${DCMTK_LIBRARIES})
endif()
  
include(${CMAKE_SOURCE_DIR}/../Resources/CMake/DownloadOrthancFramework.cmake)
include(${CMAKE_SOURCE_DIR}/../Resources/CMake/GoogleTestConfiguration.cmake)

add_definitions(
  -DORTHANC_UNIT_TESTS_LINK_FRAMEWORK=1
  -DORTHANC_BUILD_UNIT_TESTS=1  # For "HierarchicalZipWriter" tests
  )

add_executable(UnitTests
  ${CMAKE_SOURCE_DIR}/SharedLibraryUnitTests.cpp

  ${CMAKE_SOURCE_DIR}/DicomMapTests.cpp
  ${CMAKE_SOURCE_DIR}/FileStorageTests.cpp
  ${CMAKE_SOURCE_DIR}/FrameworkTests.cpp
  ${CMAKE_SOURCE_DIR}/FromDcmtkTests.cpp
  ${CMAKE_SOURCE_DIR}/ImageProcessingTests.cpp
  ${CMAKE_SOURCE_DIR}/ImageTests.cpp
  ${CMAKE_SOURCE_DIR}/JobsTests.cpp
  ${CMAKE_SOURCE_DIR}/JpegLosslessTests.cpp
  ${CMAKE_SOURCE_DIR}/LoggingTests.cpp
  ${CMAKE_SOURCE_DIR}/LuaTests.cpp
  ${CMAKE_SOURCE_DIR}/MemoryCacheTests.cpp
  ${CMAKE_SOURCE_DIR}/RestApiTests.cpp
  ${CMAKE_SOURCE_DIR}/SQLiteChromiumTests.cpp
  ${CMAKE_SOURCE_DIR}/SQLiteTests.cpp
  ${CMAKE_SOURCE_DIR}/StreamTests.cpp
  ${CMAKE_SOURCE_DIR}/ToolboxTests.cpp
  ${CMAKE_SOURCE_DIR}/ZipTests.cpp

  ${AUTOGENERATED_SOURCES}
  ${BOOST_SOURCES}
  ${GOOGLE_TEST_SOURCES}
  )

DefineSourceBasenameForTarget(UnitTests)

target_link_libraries(UnitTests ${ORTHANC_FRAMEWORK_LIBRARIES})

install(TARGETS UnitTests
  DESTINATION ${ORTHANC_FRAMEWORK_LIBDIR}
  )
