From a2f0f36675760a0d9a435803d48fe0b0ffcc6195 Mon Sep 17 00:00:00 2001
From: Federico Montesino Pouzols <federico.montesino-pouzols@stfc.ac.uk>
Date: Tue, 17 Mar 2015 16:18:25 +0000
Subject: [PATCH] finalised ComputeResourceInfo test, re #11124

---
 .../inc/MantidKernel/ComputeResourceInfo.h    |   6 +-
 .../Kernel/src/ComputeResourceInfo.cpp        |  17 +-
 .../Framework/Kernel/src/FacilityInfo.cpp     |   8 +-
 .../Kernel/test/ComputeResourceInfoTest.h     | 201 ++++++++++++++++--
 4 files changed, 207 insertions(+), 25 deletions(-)

diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ComputeResourceInfo.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ComputeResourceInfo.h
index 54022154fa7..db27c93ed68 100644
--- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ComputeResourceInfo.h
+++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ComputeResourceInfo.h
@@ -17,7 +17,8 @@ namespace Kernel {
 class FacilityInfo;
 
 /**
-Holds information about a compute resource present in a facility.
+ComputeResourceInfo holds information about / represents a compute
+resource present in a facility.
 
 At the moment (remote) compute resources are defined by their name,
 the URL they can be accessed at, and the type of remote job manager
@@ -49,6 +50,9 @@ public:
   /// constructor - from facility info and the element for this resource
   ComputeResourceInfo(const FacilityInfo *f, const Poco::XML::Element *elem);
 
+  /// Equality operator
+  bool operator==(const ComputeResourceInfo &rhs) const;
+
   /// Name of the compute resource
   std::string name() const;
 
diff --git a/Code/Mantid/Framework/Kernel/src/ComputeResourceInfo.cpp b/Code/Mantid/Framework/Kernel/src/ComputeResourceInfo.cpp
index 8306a637dac..b2e9a2fef0c 100644
--- a/Code/Mantid/Framework/Kernel/src/ComputeResourceInfo.cpp
+++ b/Code/Mantid/Framework/Kernel/src/ComputeResourceInfo.cpp
@@ -37,8 +37,8 @@ ComputeResourceInfo::ComputeResourceInfo(const FacilityInfo *fac,
         "The compute resource name is not defined, at element: " + elemStr);
   }
 
-  m_managerType = "MantidWebServiceAPI";
-  std::string type = elem->getAttribute("jobManagerType");
+  m_managerType = "MantidWebServiceAPIJobManager";
+  std::string type = elem->getAttribute("JobManagerType");
   if (!type.empty()) {
     m_managerType = type;
   }
@@ -72,6 +72,17 @@ ComputeResourceInfo::ComputeResourceInfo(const FacilityInfo *fac,
   }
 }
 
+/**
+* Equality operator. Two different resources cannot have the same name
+*
+* @param rhs object to compare this with
+*
+* @return True if the objects (names) are equal
+*/
+bool ComputeResourceInfo::operator==(const ComputeResourceInfo &rhs) const {
+  return (this->name() == rhs.name());
+}
+
 std::string ComputeResourceInfo::name() const { return m_name; }
 
 std::string ComputeResourceInfo::baseURL() const { return m_baseURL; }
@@ -94,7 +105,7 @@ const FacilityInfo &ComputeResourceInfo::facility() const {
  */
 std::ostream &operator<<(std::ostream &buffer, const ComputeResourceInfo &cr) {
   buffer << "'" + cr.name() + "', at '" + cr.baseURL() + "', of type '" +
-                cr.remoteJobManagerType();
+                cr.remoteJobManagerType() + "'";
   return buffer;
 }
 
diff --git a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
index c7bc5c4381e..dad72eec268 100644
--- a/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
+++ b/Code/Mantid/Framework/Kernel/src/FacilityInfo.cpp
@@ -275,9 +275,11 @@ FacilityInfo::computeResource(const std::string &name) const {
 
   auto it = m_computeResInfos.begin();
   for (; it != m_computeResInfos.end(); ++it) {
-    g_log.debug() << "Compute resource '" << name << "' found at facility "
-                  << this->name() << "." << std::endl;
-    return *it;
+    if (it->name() == name) {
+      g_log.debug() << "Compute resource '" << name << "' found at facility "
+                    << this->name() << "." << std::endl;
+      return *it;
+    }
   }
 
   g_log.debug() << "Could not find requested compute resource: " << name
diff --git a/Code/Mantid/Framework/Kernel/test/ComputeResourceInfoTest.h b/Code/Mantid/Framework/Kernel/test/ComputeResourceInfoTest.h
index 4ffebd50079..213a98ed209 100644
--- a/Code/Mantid/Framework/Kernel/test/ComputeResourceInfoTest.h
+++ b/Code/Mantid/Framework/Kernel/test/ComputeResourceInfoTest.h
@@ -1,24 +1,58 @@
 #ifndef COMPUTERESOURCEINFOTEST_H_
 #define COMPUTERESOURCEINFOTEST_H_
 
+#include "MantidKernel/Exception.h"
 #include "MantidKernel/FacilityInfo.h"
 
 #include <Poco/DOM/AutoPtr.h>
 #include <Poco/DOM/Document.h>
 #include <Poco/DOM/DOMParser.h>
+#include <Poco/XML/XMLException.h>
 
 using namespace Mantid::Kernel;
 
 class ComputeResourceInfoTest : public CxxTest::TestSuite {
 public:
-  void test_noComputeResource() {
+  void test_allMissing() {
     FacilityInfo *fac = NULL;
-    TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(""));
+    TS_ASSERT_THROWS_NOTHING(fac =
+                                 createCRInfoInMinimalFacility(simpleInstStr));
+    TS_ASSERT(fac);
+    std::vector<ComputeResourceInfo> cri;
+    TS_ASSERT_THROWS_NOTHING(cri = fac->computeResInfos());
+    TS_ASSERT_EQUALS(cri.size(), 0);
 
-    TS_ASSERT_THROWS_NOTHING(
-        fac = createCRInfoInMinimalFacility("<computeResource />"));
+    delete fac;
+    fac = NULL;
+    TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(
+                         "<computeResource fooAtt=\"barVal\"/>"),
+                     std::runtime_error);
+    TS_ASSERT(!fac);
+    delete fac;
+  }
 
-    ComputeResourceInfo cr = fac->computeResInfos().front();
+  void test_noURLTag() {
+    const std::string crTxt = "<computeResource name=\"foo\">"
+                              "<u>" +
+                              fermiURL + "</u>"
+                                         "</computeResource>";
+    FacilityInfo *fac = NULL;
+    TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(crTxt),
+                     std::runtime_error);
+    TS_ASSERT(!fac);
+    delete fac;
+  }
+
+  void test_wrongXML() {
+    const std::string crTxt = "<computeResource name=\"foo\">"
+                              "<u_foo>" +
+                              fermiURL + "</u_bar>"
+                                         "</compResource>";
+    FacilityInfo *fac = NULL;
+    TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(crTxt),
+                     Poco::XML::XMLException);
+    TS_ASSERT(!fac);
+    delete fac;
   }
 
   void test_normalFermi() {
@@ -30,8 +64,28 @@ public:
 
     FacilityInfo *fac = NULL;
     TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(fermi));
+    TS_ASSERT(fac);
+    TS_ASSERT_EQUALS(fac->name(), this->testFacilityName);
+
+    std::vector<ComputeResourceInfo> cri;
+    TS_ASSERT_THROWS_NOTHING(cri = fac->computeResInfos());
+    TS_ASSERT_EQUALS(cri.size(), 1);
 
     ComputeResourceInfo cr = fac->computeResInfos().front();
+    TS_ASSERT_THROWS(ComputeResourceInfo fail = fac->computeResource(scarfName),
+                     Mantid::Kernel::Exception::NotFoundError);
+    ComputeResourceInfo cr2 = fac->computeResource(fermiName);
+    TS_ASSERT_EQUALS(cr, cr2);
+    TS_ASSERT_EQUALS(cr, cri.front());
+    TS_ASSERT_EQUALS(cr2, cri.front());
+    TS_ASSERT_EQUALS(cr.name(), fermiName);
+    TS_ASSERT_EQUALS(cr2.name(), fermiName);
+    TS_ASSERT_EQUALS(cr.baseURL(), fermiURL);
+    TS_ASSERT_EQUALS(cr2.baseURL(), fermiURL);
+    TS_ASSERT_EQUALS(cr.remoteJobManagerType(), defaultType);
+    TS_ASSERT_EQUALS(cr2.remoteJobManagerType(), defaultType);
+    TS_ASSERT_EQUALS(cr.facility().name(), fac->name());
+    TS_ASSERT_EQUALS(cr2.facility().name(), fac->name());
   }
 
   void test_brokenFermi() {
@@ -45,35 +99,126 @@ public:
     TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(fermi),
                      std::runtime_error);
 
-    ComputeResourceInfo cr = fac->computeResInfos().front();
+    TS_ASSERT(!fac);
+    delete fac;
   }
 
   void test_normalSCARF() {
-    const std::string type = "SCARFLSFJobManager";
     const std::string scarf = "<computeResource name=\"" + scarfName +
-                              "\" JobManagerType=\"" + type + "\">"
-                                                              "<baseURL>" +
+                              "\" JobManagerType=\"" + scarfType + "\">"
+                                                                   "<baseURL>" +
                               scarfURL + "</baseURL>"
-                                    "</computeResource>";
+                                         "</computeResource>";
 
     FacilityInfo *fac = NULL;
     TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(scarf));
+    TS_ASSERT(fac);
+    TS_ASSERT_EQUALS(fac->name(), this->testFacilityName);
+    std::vector<ComputeResourceInfo> cri;
+    TS_ASSERT_THROWS_NOTHING(cri = fac->computeResInfos());
+    TS_ASSERT_EQUALS(cri.size(), 1);
 
     ComputeResourceInfo cr = fac->computeResInfos().front();
+    TS_ASSERT_THROWS(ComputeResourceInfo fail = fac->computeResource("inexistent!"),
+                     Mantid::Kernel::Exception::NotFoundError);
+    ComputeResourceInfo cr2 = fac->computeResource(scarfName);
+    TS_ASSERT_EQUALS(cr, cr2);
+    TS_ASSERT_EQUALS(cri.front(), cr);
+    TS_ASSERT_EQUALS(cri.front(), cr2);
+    TS_ASSERT_EQUALS(scarfName, cr.name());
+    TS_ASSERT_EQUALS(scarfName, cr2.name());
+    TS_ASSERT_EQUALS(scarfURL, cr.baseURL());
+    TS_ASSERT_EQUALS(scarfURL, cr2.baseURL());
+    TS_ASSERT_EQUALS(scarfType, cr.remoteJobManagerType());
+    TS_ASSERT_EQUALS(scarfType, cr2.remoteJobManagerType());
+    TS_ASSERT_EQUALS(fac->name(), cr.facility().name());
+    TS_ASSERT_EQUALS(fac->name(), cr2.facility().name());
+    delete fac;
   }
 
-  void test_brokenSCARF() {}
+  void test_brokenSCARF() {
+    const std::string type = "SCARFLSFJobManager";
+    const std::string err = "<computeResource foo=\"" + scarfName +
+                            "\" JobManagerType=\"" + type + "\">"
+                                                            "<URL>" +
+                            scarfURL + "</URL>"
+                                       "</computeResource>";
+    FacilityInfo *fac = NULL;
+    TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(err),
+                     std::runtime_error);
+    TS_ASSERT(!fac);
+    delete fac;
+  }
+
+  void test_equals() {
+    const std::string otherName = "other";
+    const std::string otherURL = "www.example.com/foo/baz";
+    const std::string thirdName = "third";
+    const std::string rep = "<computeResource name=\"" + fermiName +
+                            "\">"
+                            "<baseURL>" +
+                            fermiURL + "</baseURL>"
+                                       "</computeResource>"
+
+                                       "<computeResource name=\"" +
+                            otherName + "\">"
+                                        "<baseURL>" +
+                            otherURL + "</baseURL>"
+                                       "</computeResource>"
+
+                                       "<computeResource name=\"" +
+                            thirdName + "\">"
+                                        "<baseURL>" +
+                            fermiURL + "</baseURL>"
+                                       "</computeResource>"
+
+                                       "<computeResource name=\"" +
+                            fermiName + "\">"
+                                        "<baseURL>" +
+                            fermiURL + "</baseURL>"
+                                       "</computeResource>";
+
+    FacilityInfo *fac = NULL;
+    TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(rep));
+    TS_ASSERT(fac);
+    TS_ASSERT_EQUALS(fac->computeResources().size(), 3);
+    TS_ASSERT_EQUALS(fac->computeResInfos().size(), 4);
+
+    // compare names
+    TS_ASSERT(fac->computeResources()[0] == fac->computeResources()[0]);
+    TS_ASSERT(!(fac->computeResources()[0] == fac->computeResources()[1]));
+    TS_ASSERT(!(fac->computeResources()[0] == fac->computeResources()[2]));
+    TS_ASSERT(!(fac->computeResources()[1] == fac->computeResources()[2]));
+
+    // compare full comp resource info
+    TS_ASSERT(fac->computeResInfos()[0] == fac->computeResInfos()[0]);
+    TS_ASSERT(!(fac->computeResInfos()[0] == fac->computeResInfos()[1]));
+    TS_ASSERT(!(fac->computeResInfos()[0] == fac->computeResInfos()[2]));
+    TS_ASSERT(!(fac->computeResInfos()[1] == fac->computeResInfos()[2]));
+    TS_ASSERT(!(fac->computeResInfos()[2] == fac->computeResInfos()[3]));
+    TS_ASSERT(fac->computeResInfos()[0] == fac->computeResInfos()[3]);
+
+    // compare comp resource info retrieved by names
+    TS_ASSERT(
+        !(fac->computeResource(fermiName) == fac->computeResource(otherName)));
+    TS_ASSERT(
+        !(fac->computeResource(fermiName) == fac->computeResource(thirdName)));
+    TS_ASSERT(
+        !(fac->computeResource(otherName) == fac->computeResource(thirdName)));
+    delete fac;
+  }
 
 private:
   /// make a minimal facilities file/xml string includin the compute resource
   /// passed
   FacilityInfo *createCRInfoInMinimalFacility(const std::string &crStr) {
-    const std::string xmlStr =
-        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-        "<facilities>"
-        "  <facility name=\"ATestFacility\" FileExtensions=\".xyz\">" +
-        crStr + "  </facility>"
-                "</facilities>";
+    const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+                               "<facilities>"
+                               "  <facility name=\"" +
+                               testFacilityName +
+                               "\" FileExtensions=\".xyz\">" + simpleInstStr +
+                               crStr + "  </facility>"
+                                       "</facilities>";
 
     return createFacility(xmlStr);
   }
@@ -88,17 +233,37 @@ private:
   }
 
 private:
+  // a minimal instrument to create a facility info
+  static const std::string simpleInstStr;
+
+  // default remote job manager type
+  static const std::string defaultType;
+
+  static const std::string testFacilityName;
+
   static const std::string fermiName;
   static const std::string fermiURL;
   static const std::string scarfName;
   static const std::string scarfURL;
+  static const std::string scarfType;
 };
 
+const std::string ComputeResourceInfoTest::simpleInstStr =
+    "<instrument name=\"AnInst\">"
+    "  <technique>Measuring Stuff</technique>"
+    "</instrument>";
+
+const std::string ComputeResourceInfoTest::defaultType =
+    "MantidWebServiceAPIJobManager";
+
+const std::string ComputeResourceInfoTest::testFacilityName = "ATestFacility";
+
 const std::string ComputeResourceInfoTest::fermiURL =
     "https://fermi.ornl.gov/MantidRemote";
 const std::string ComputeResourceInfoTest::fermiName = "Fermi";
 const std::string ComputeResourceInfoTest::scarfURL =
     "https://portal.scarf.rl.ac.uk";
 const std::string ComputeResourceInfoTest::scarfName = "SCARF@STFC";
+const std::string ComputeResourceInfoTest::scarfType = "SCARFLSFJobManager";
 
-#endif // COMPUTERESOURCEINFOTEST_H_
\ No newline at end of file
+#endif // COMPUTERESOURCEINFOTEST_H_
-- 
GitLab