C++ Library to handle BagIt structures. BagIt is a standard format to create transfer packages for digital preservation purposes. See https://en.wikipedia.org/wiki/BagIt for details http://andreas-romeyke.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
9.7 KiB

  1. #define BOOST_TEST_MODULE TestBagMetadata
  2. #include <boost/test/unit_test.hpp>
  3. #include <iostream>
  4. #include <string>
  5. #include <utility>
  6. #include <boost/filesystem.hpp>
  7. namespace fs = boost::filesystem;
  8. #include "bagmetadata.hpp"
  9. #include "checksum.hpp"
  10. BOOST_AUTO_TEST_SUITE(testbag_suite);
  11. using namespace std;
  12. void debug( Bagmetadata * p) {
  13. stringstream log;
  14. p->get_logstream( log );
  15. cout << "##############################################" << endl;
  16. cout << "DEBUG Bagmetadata" << endl;
  17. cout << log.str() << endl;
  18. cout << "##############################################" << endl;
  19. }
  20. bool check_constructor(string dir) {
  21. try {
  22. Bagmetadata p( std::move(dir) );
  23. return true;
  24. } catch (exception &e) {
  25. return false;
  26. }
  27. }
  28. bool check_validation(string dir) {
  29. Bagmetadata p( std::move(dir) );
  30. bool res = p.validate();
  31. return res;
  32. }
  33. bool check_get_metadata(string dir) {
  34. Bagmetadata p( std::move(dir) );
  35. map<string,string> results = p.get_metadata();
  36. map<string,string> expected = {
  37. {"External-Identifier", "testbag-01"},
  38. {"Payload-Oxum", "38.4"},
  39. {"Bagging-Date", "2015-12-28"},
  40. {"Bag-Size", "16 B"},
  41. {"External-Description", "3.dat"},
  42. {"Foo", "bar baz\n blurp"},
  43. };
  44. map<string, string>::iterator i;
  45. map<string, string>::iterator j;
  46. bool is_valid = true;
  47. for (i=expected.begin(), j=results.begin(); i!=expected.end() && j!=results.end(); i++,j++) {
  48. if (
  49. ( i->first != j-> first) ||
  50. ( i->second != j->second)
  51. ) {
  52. cout << "Expected: (" << (i->first) << ";" << (i->second) << ")" << endl;
  53. cout << "Result: ("<< j->first << ";" << j->second << ")" << endl;
  54. is_valid=false; break;
  55. }
  56. }
  57. return is_valid;
  58. }
  59. bool check_has_metadata(string dir) {
  60. Bagmetadata p( std::move(dir) );
  61. return p.has_metadata();
  62. }
  63. /*
  64. bool check_has_PayloadOxum(string dir) {
  65. Bagmetadata p( std::move(dir) );
  66. return p.has_PayloadOxum();
  67. }
  68. oxum_t check_get_PayloadOxum(string dir) {
  69. Bagmetadata p( std::move(dir) );
  70. return p.get_PayloadOxum();
  71. }
  72. */
  73. bool check_store( string dir_load_from, string dir_store_to ) {
  74. Bagmetadata p( std::move(dir_load_from) );
  75. fs::path f{ dir_store_to };
  76. fs::create_directory(f);
  77. bool result = p.store( std::move(dir_store_to) );
  78. fs::remove_all( f );
  79. debug( &p );
  80. return result;
  81. }
  82. //
  83. bool check_set_has_get_SourceOrganization(string dir, string expected) {
  84. Bagmetadata p( std::move(dir) );
  85. p.set_SourceOrganization( expected );
  86. if (!p.has_SourceOrganization() ) {
  87. return false;
  88. }
  89. string result = p.get_SourceOrganization();
  90. return (0 == expected.compare( result ) );
  91. }
  92. bool check_set_has_get_OrganizationAddress(string dir, string expected) {
  93. Bagmetadata p( std::move(dir) );
  94. p.set_OrganizationAddress( expected );
  95. if (!p.has_OrganizationAddress() ) {
  96. return false;
  97. }
  98. string result = p.get_OrganizationAddress();
  99. return (0 == expected.compare( result ) );
  100. }
  101. bool check_set_has_get_ContactName(string dir, string expected) {
  102. Bagmetadata p( std::move(dir) );
  103. p.set_ContactName( expected );
  104. if (!p.has_ContactName() ) {
  105. return false;
  106. }
  107. string result = p.get_ContactName();
  108. return (0 == expected.compare( result ) );
  109. }
  110. bool check_set_has_get_ContactPhone(string dir, string expected) {
  111. Bagmetadata p( std::move(dir) );
  112. p.set_ContactPhone( expected );
  113. if (!p.has_ContactPhone() ) {
  114. return false;
  115. }
  116. string result = p.get_ContactPhone();
  117. return (0 == expected.compare( result ) );
  118. }
  119. bool check_set_has_get_ContactEmail(string dir, string expected) {
  120. Bagmetadata p( std::move(dir) );
  121. p.set_ContactEmail( expected );
  122. if (!p.has_ContactEmail() ) {
  123. return false;
  124. }
  125. string result = p.get_ContactEmail();
  126. return (0 == expected.compare( result ) );
  127. }
  128. bool check_set_has_get_ExternalDescription(string dir, string expected) {
  129. Bagmetadata p( std::move(dir) );
  130. p.set_ExternalDescription( expected );
  131. if (!p.has_ExternalDescription() ) {
  132. return false;
  133. }
  134. string result = p.get_ExternalDescription();
  135. return (0 == expected.compare( result ) );
  136. }
  137. bool check_set_has_get_BaggingDate(string dir, string expected) {
  138. Bagmetadata p( std::move(dir) );
  139. p.set_BaggingDate( expected );
  140. if (!p.has_BaggingDate() ) {
  141. return false;
  142. }
  143. string result = p.get_BaggingDate();
  144. return (0 == expected.compare( result ) );
  145. }
  146. bool check_set_has_get_ExternalIdentifier(string dir, string expected) {
  147. Bagmetadata p( std::move(dir) );
  148. p.set_ExternalIdentifier( expected );
  149. if (!p.has_ExternalIdentifier() ) {
  150. return false;
  151. }
  152. string result = p.get_ExternalIdentifier();
  153. return (0 == expected.compare( result ) );
  154. }
  155. bool check_set_has_get_BagSize(string dir, string expected) {
  156. Bagmetadata p( std::move(dir) );
  157. p.set_BagSize( expected );
  158. if (!p.has_BagSize() ) {
  159. return false;
  160. }
  161. string result = p.get_BagSize();
  162. return (0 == expected.compare( result ) );
  163. }
  164. bool check_set_has_get_PayloadOxum(string dir, oxum_t expected) {
  165. Bagmetadata p( std::move(dir) );
  166. p.set_PayloadOxum( expected );
  167. if (!p.has_PayloadOxum() ) {
  168. return false;
  169. }
  170. oxum_t result = p.get_PayloadOxum();
  171. return (expected.octetcount == result.octetcount
  172. && expected.streamcount == result.streamcount
  173. );
  174. }
  175. bool check_set_has_get_BagGroupIdentifier(string dir, string expected) {
  176. Bagmetadata p( std::move(dir) );
  177. p.set_BagGroupIdentifier( expected );
  178. if (!p.has_BagGroupIdentifier() ) {
  179. return false;
  180. }
  181. string result = p.get_BagGroupIdentifier();
  182. return (0 == expected.compare( result ) );
  183. }
  184. bool check_set_has_get_BagCount(string dir, string expected) {
  185. Bagmetadata p( std::move(dir) );
  186. p.set_BagCount( expected );
  187. if (!p.has_BagCount() ) {
  188. return false;
  189. }
  190. string result = p.get_BagCount();
  191. return (0 == expected.compare( result ) );
  192. }
  193. bool check_set_has_get_InternalSenderIdentifier(string dir, string expected) {
  194. Bagmetadata p( std::move(dir) );
  195. p.set_InternalSenderIdentifier( expected );
  196. if (!p.has_InternalSenderIdentifier() ) {
  197. return false;
  198. }
  199. string result = p.get_InternalSenderIdentifier();
  200. return (0 == expected.compare( result ) );
  201. }
  202. bool check_set_has_get_InternalSenderDescription(string dir, string expected) {
  203. Bagmetadata p( std::move(dir) );
  204. p.set_InternalSenderDescription( expected );
  205. if (!p.has_InternalSenderDescription() ) {
  206. return false;
  207. }
  208. string result = p.get_InternalSenderDescription();
  209. return (0 == expected.compare( result ) );
  210. }
  211. //
  212. //
  213. //
  214. //
  215. BOOST_AUTO_TEST_CASE(constructor1) {BOOST_TEST(check_constructor(""));};
  216. BOOST_AUTO_TEST_CASE(constructor2) {BOOST_TEST(check_constructor("./"));};
  217. BOOST_AUTO_TEST_CASE(constructor3) {BOOST_TEST(check_constructor("/tmp/"));};
  218. BOOST_AUTO_TEST_CASE(check_metadata_exists) {BOOST_TEST( check_has_metadata("../testbags/bag_minimal_ok/")); };
  219. BOOST_AUTO_TEST_CASE(check_metadata) {BOOST_TEST( check_get_metadata("../testbags/bag_minimal_ok/")); };
  220. BOOST_AUTO_TEST_CASE(check_oxum) {
  221. oxum_t expected{};
  222. expected.octetcount=38;
  223. expected.streamcount=4;
  224. BOOST_TEST( check_set_has_get_PayloadOxum("../testbags/bag_minimal_ok/", expected));
  225. };
  226. BOOST_AUTO_TEST_CASE(check_validation_ok) {BOOST_TEST(check_validation("../testbags/bag_minimal_ok/"));};
  227. BOOST_AUTO_TEST_CASE(check_store_ok) {BOOST_TEST(check_store("../testbags/bag_minimal_ok/", "/tmp/testbag/"));};
  228. //BOOST_AUTO_TEST_CASE(check_has_PayloadOxum_ok) {BOOST_TEST(check_has_PayloadOxum("../testbags_bag_minimal_ok/"));};
  229. //BOOST_AUTO_TEST_CASE(check_get_PayloadOxum_ok) {BOOST_TEST(check_get_PayloadOxum("../testbags_bag_minimal_ok/"));};
  230. //BOOST_AUTO_TEST_CASE(check_store_ok) {BOOST_TEST(check_store("../testbags_bag_minimal_ok/"));};
  231. BOOST_AUTO_TEST_CASE(check_set_has_get_SourceOrganization_ok) {BOOST_TEST(check_set_has_get_SourceOrganization("../testbags_bag_minimal_ok/", "test"));};
  232. BOOST_AUTO_TEST_CASE(check_set_has_get_OrganizationAddress_ok) {BOOST_TEST(check_set_has_get_OrganizationAddress("../testbags_bag_minimal_ok/", "test"));};
  233. BOOST_AUTO_TEST_CASE(check_set_has_get_ContactName_ok) {BOOST_TEST(check_set_has_get_ContactName("../testbags_bag_minimal_ok/", "test"));};
  234. BOOST_AUTO_TEST_CASE(check_set_has_get_ContactPhone_ok) {BOOST_TEST(check_set_has_get_ContactPhone("../testbags_bag_minimal_ok/", "test"));};
  235. BOOST_AUTO_TEST_CASE(check_set_has_get_ContactEmail_ok) {BOOST_TEST(check_set_has_get_ContactEmail("../testbags_bag_minimal_ok/", "test"));};
  236. BOOST_AUTO_TEST_CASE(check_set_has_get_ExternalDescription_ok) {BOOST_TEST(check_set_has_get_ExternalDescription("../testbags_bag_minimal_ok/", "test"));};
  237. BOOST_AUTO_TEST_CASE(check_set_has_get_BaggingDate_ok) {BOOST_TEST(check_set_has_get_BaggingDate("../testbags_bag_minimal_ok/", "test"));};
  238. BOOST_AUTO_TEST_CASE(check_set_has_get_ExternalIdentifier_ok) {BOOST_TEST(check_set_has_get_ExternalIdentifier("../testbags_bag_minimal_ok/", "test"));};
  239. BOOST_AUTO_TEST_CASE(check_set_has_get_BagSize_ok) {BOOST_TEST(check_set_has_get_BagSize("../testbags_bag_minimal_ok/", "test"));};
  240. BOOST_AUTO_TEST_CASE(check_set_has_get_PayloadOxum_ok) {BOOST_TEST(check_set_has_get_PayloadOxum("../testbags_bag_minimal_ok/", {2,1}));};
  241. BOOST_AUTO_TEST_CASE(check_set_has_get_BagGroupIdentifier_ok) {BOOST_TEST(check_set_has_get_BagGroupIdentifier("../testbags_bag_minimal_ok/", "test"));};
  242. BOOST_AUTO_TEST_CASE(check_set_has_get_BagCount_ok) {BOOST_TEST(check_set_has_get_BagCount("../testbags_bag_minimal_ok/", "test"));};
  243. BOOST_AUTO_TEST_CASE(check_set_has_get_InternalSenderIdentifier_ok) {BOOST_TEST(check_set_has_get_InternalSenderIdentifier("../testbags_bag_minimal_ok/", "test"));};
  244. BOOST_AUTO_TEST_CASE(check_set_has_get_InternalSenderDescription_ok) {BOOST_TEST(check_set_has_get_InternalSenderDescription("../testbags_bag_minimal_ok/", "test"));};
  245. BOOST_AUTO_TEST_SUITE_END();
  246. // vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab