forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatalogInfoTest.h
159 lines (139 loc) · 7.98 KB
/
CatalogInfoTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidKernel/CatalogInfo.h"
#include "MantidKernel/ConfigService.h"
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeList.h>
#include <memory>
using namespace Mantid::Kernel;
class CatalogInfoTest : public CxxTest::TestSuite {
public:
/// Tests that the related attributes get set when object is created.
void testConstructCatalogInfo() {
const std::string facilitiesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
"<facility name=\"ISIS\">"
"<catalog name=\"ICat3Catalog\">"
"<soapendpoint "
"url=\"https://facilities01.esc.rl.ac.uk:443/ICATService/ICAT\"></"
"soapendpoint>"
"<filelocation>"
"<prefix regex=\"\\\\\\\\isis\\\\inst\\$\\\\Instruments\\$\"></prefix>"
"<windows replacement=\"\"></windows>"
"<linux replacement=\"/archive\"></linux>"
"<mac replacement=\"/archive\"></mac>"
"</filelocation>"
"</catalog>"
"</facility>"
"</facilities>";
std::unique_ptr<CatalogInfo> catalogInfo = nullptr;
TS_ASSERT_THROWS_NOTHING(catalogInfo = getCatalogFromXML(facilitiesXml));
TS_ASSERT_EQUALS(catalogInfo->catalogName(), "ICat3Catalog");
TS_ASSERT_EQUALS(catalogInfo->soapEndPoint(), "https://facilities01.esc.rl.ac.uk:443/ICATService/ICAT");
// The regex needs escaped in order to work correctly. The output should be
// same as Facilities.xml (\\\\isis\\inst\$\\Instruments\$)
TS_ASSERT_EQUALS(catalogInfo->catalogPrefix(), "\\\\\\\\isis\\\\inst\\$\\\\Instruments\\$");
TS_ASSERT_EQUALS(catalogInfo->windowsPrefix(), "");
TS_ASSERT_EQUALS(catalogInfo->macPrefix(), "/archive");
TS_ASSERT_EQUALS(catalogInfo->linuxPrefix(), "/archive");
}
/// Test transformation of possible combinations of archive paths.
/// Ensures the path is transformed into the correct OS dependent path.
void testTransformPath() {
const std::string facilitiesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
"<facility name=\"ISIS\">"
"<catalog name=\"ICat3Catalog\">"
"<soapendpoint "
"url=\"https://facilities01.esc.rl.ac.uk:443/"
"ICATService/ICAT\"></soapendpoint>"
"<filelocation>"
"<prefix "
"regex=\"\\\\\\\\isis\\\\inst\\$\\\\Instruments\\$\"></"
"prefix>" // Same regex from facilities.xml, lots of "\"
// character escaping.
"<windows "
"replacement=\"\\\\\\\\isis\\.cclrc\\.ac\\.uk\\\\inst\\$"
"\\\\Instruments\\$\"></windows>"
"<linux replacement=\"/archive\"></linux>"
"<mac replacement=\"/archive\"></mac>"
"</filelocation>"
"</catalog>"
"</facility>"
"</facilities>";
std::unique_ptr<CatalogInfo> catalogInfo = nullptr;
TS_ASSERT_THROWS_NOTHING(catalogInfo = getCatalogFromXML(facilitiesXml));
// Set the paths to test against.
std::string linuxPrefixPath = "/archive/NDXSANDALS/Instrument/data/cycle_05_3/ALF06716.LOG";
std::string macPrefixPath = "/archive/NDXSANDALS/Instrument/data/cycle_05_3/ALF06716.LOG";
std::string winPrefixPath =
R"(\\isis.cclrc.ac.uk\inst$\Instruments$\NDXSANDALS\Instrument\data\cycle_05_3\ALF06716.LOG)";
std::string winDefaultPath = R"(\\isis\inst$\Instruments$\NDXSANDALS\Instrument\data\cycle_05_3\ALF06716.LOG)";
// Perform the transformation of each path prior to assertions for
// re-usability of code.
std::string transformLin = catalogInfo->transformArchivePath(linuxPrefixPath);
std::string transformMac = catalogInfo->transformArchivePath(macPrefixPath);
std::string transformWin = catalogInfo->transformArchivePath(winDefaultPath);
// Test each variation
#ifdef __linux__
TS_ASSERT_EQUALS(linuxPrefixPath, transformMac);
TS_ASSERT_EQUALS(linuxPrefixPath, transformWin);
TS_ASSERT_EQUALS(linuxPrefixPath, transformLin);
#elif __APPLE__
TS_ASSERT_EQUALS(macPrefixPath, transformMac);
TS_ASSERT_EQUALS(macPrefixPath, transformWin);
TS_ASSERT_EQUALS(macPrefixPath, transformLin);
#elif _WIN32
TS_ASSERT_EQUALS(winPrefixPath, transformWin);
TS_ASSERT_EQUALS(winPrefixPath, transformMac);
TS_ASSERT_EQUALS(winPrefixPath, transformLin);
#endif
}
/// Parse the XML string and create a catalog Object.
std::unique_ptr<CatalogInfo> getCatalogFromXML(const std::string &xmlStr) const {
Poco::XML::DOMParser parser;
Poco::AutoPtr<Poco::XML::Document> documentParser = parser.parseString(xmlStr);
TS_ASSERT(documentParser);
Poco::XML::Element *rootElement = documentParser->documentElement();
Poco::AutoPtr<Poco::XML::NodeList> elementTag = rootElement->getElementsByTagName("facility");
TS_ASSERT(elementTag->length() > 0);
Poco::XML::Element *element = dynamic_cast<Poco::XML::Element *>(elementTag->item(0));
TS_ASSERT(element);
return (std::make_unique<CatalogInfo>(element));
}
void testCopyConstructor() {
const std::string facilitiesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
"<facility name=\"ISIS\">"
"<catalog name=\"ICat3Catalog\">"
"<soapendpoint "
"url=\"https://facilities01.esc.rl.ac.uk:443/ICATService/ICAT\"></"
"soapendpoint>"
"<filelocation>"
"<prefix regex=\"\\\\\\\\isis\\\\inst\\$\\\\Instruments\\$\"></prefix>"
"<windows replacement=\"\"></windows>"
"<linux replacement=\"/archive\"></linux>"
"<mac replacement=\"/archive\"></mac>"
"</filelocation>"
"</catalog>"
"</facility>"
"</facilities>";
std::unique_ptr<CatalogInfo> original(getCatalogFromXML(facilitiesXml));
CatalogInfo copy = *original;
TS_ASSERT_EQUALS(original->catalogName(), copy.catalogName());
TS_ASSERT_EQUALS(original->catalogPrefix(), copy.catalogPrefix());
TS_ASSERT_EQUALS(original->externalDownloadURL(), copy.externalDownloadURL());
TS_ASSERT_EQUALS(original->linuxPrefix(), copy.linuxPrefix());
TS_ASSERT_EQUALS(original->macPrefix(), copy.macPrefix());
TS_ASSERT_EQUALS(original->windowsPrefix(), copy.windowsPrefix());
TS_ASSERT_EQUALS(original->soapEndPoint(), copy.soapEndPoint());
}
};