From 1ef6c7f53ac04458cad9e8d76ece3a8c9dd28e68 Mon Sep 17 00:00:00 2001
From: Steven Murray <Steven.Murray@cern.ch>
Date: Tue, 25 Jun 2019 12:07:21 +0200
Subject: [PATCH] Fix for cta/CTA#536 logicallibrary bad error message

---
 catalogue/CatalogueTest.cpp  | 36 ++++++++++++++++++++++++++++++++++++
 catalogue/RdbmsCatalogue.cpp | 14 ++++++++++++++
 catalogue/RdbmsCatalogue.hpp |  8 ++++++++
 3 files changed, 58 insertions(+)

diff --git a/catalogue/CatalogueTest.cpp b/catalogue/CatalogueTest.cpp
index 19f659c116..0e31d03ee7 100644
--- a/catalogue/CatalogueTest.cpp
+++ b/catalogue/CatalogueTest.cpp
@@ -3160,6 +3160,42 @@ TEST_P(cta_catalogue_CatalogueTest, createTape_many_tapes) {
     }
   }
 
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.vid = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.mediaType = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.vendor = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.logicalLibrary = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.tapePool = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
+  {
+    catalogue::TapeSearchCriteria searchCriteria;
+    searchCriteria.vo = "";
+    ASSERT_THROW(m_catalogue->getTapes(searchCriteria), exception::UserError);
+  }
+
   {
     catalogue::TapeSearchCriteria searchCriteria;
     searchCriteria.vid = "vid1";
diff --git a/catalogue/RdbmsCatalogue.cpp b/catalogue/RdbmsCatalogue.cpp
index 64bbdfba00..d01017d909 100644
--- a/catalogue/RdbmsCatalogue.cpp
+++ b/catalogue/RdbmsCatalogue.cpp
@@ -1946,6 +1946,13 @@ std::list<common::dataStructures::Tape> RdbmsCatalogue::getTapes(const TapeSearc
 //------------------------------------------------------------------------------
 std::list<common::dataStructures::Tape> RdbmsCatalogue::getTapes(rdbms::Conn &conn,
   const TapeSearchCriteria &searchCriteria) const {
+  if(isSetAndEmpty(searchCriteria.vid)) throw exception::UserError("VID cannot be an empty string");
+  if(isSetAndEmpty(searchCriteria.mediaType)) throw exception::UserError("Media type cannot be an empty string");
+  if(isSetAndEmpty(searchCriteria.vendor)) throw exception::UserError("Vendor cannot be an empty string");
+  if(isSetAndEmpty(searchCriteria.logicalLibrary)) throw exception::UserError("Logical library cannot be an empty string");
+  if(isSetAndEmpty(searchCriteria.tapePool)) throw exception::UserError("Tape pool cannot be an empty string");
+  if(isSetAndEmpty(searchCriteria.vo)) throw exception::UserError("Virtual organisation cannot be an empty string");
+
   try {
     std::list<common::dataStructures::Tape> tapes;
     std::string sql =
@@ -5865,5 +5872,12 @@ uint64_t RdbmsCatalogue::getNbTapesInPool(rdbms::Conn &conn, const std::string &
   }
 }
 
+//------------------------------------------------------------------------------
+// isSetAndEmpty
+//------------------------------------------------------------------------------
+bool RdbmsCatalogue::isSetAndEmpty(const optional<std::string> &optionalStr) const {
+  return optionalStr && optionalStr->empty();
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/RdbmsCatalogue.hpp b/catalogue/RdbmsCatalogue.hpp
index 56e6415781..d6cdbfe7f9 100644
--- a/catalogue/RdbmsCatalogue.hpp
+++ b/catalogue/RdbmsCatalogue.hpp
@@ -1280,6 +1280,14 @@ protected:
    */
   uint64_t getNbTapesInPool(rdbms::Conn &conn, const std::string &name) const;
 
+  /**
+   * Returns true if the specified optional string is both set and empty.
+   *
+   * @param optionalStr The optional string.
+   * @return True if the specified optional string is both set and empty.
+   */
+  bool isSetAndEmpty(const optional<std::string> &optionalStr) const;
+
   /**
    * Cached versions of tape copy to tape tape pool mappings for specific
    * storage classes.
-- 
GitLab