static void interactiveCustomCatalogTests(Node& node, TestRunner& tr) { Messenger* messenger = node.getMessenger(); tr.group("customcatalog+listing updater"); // generate the ware URLs that will be used throughout the test. Url waresUrl; waresUrl.format("%s/api/3.0/catalog/wares?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); Url wareUrl; wareUrl.format("%s/api/3.0/catalog/wares/%s?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_WARE_ID_2, TEST_USER_ID); // generate the files URL that will be used to prime the medialibrary Url filesUrl; filesUrl.format("%s/api/3.2/medialibrary/files?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); Url removeUrl; removeUrl.format( "%s/api/3.2/medialibrary/files/%s?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_FILE_ID_2, TEST_USER_ID); // remove any previous files from the media library messenger->deleteResource(&removeUrl, NULL, node.getDefaultUserId()); // pass even if there is an exception - this is meant to just clear any // previous entries in the medialibrary database if they existed. Exception::clear(); tr.test("add file to medialibrary (valid)"); { DynamicObject in; DynamicObject out; // create a FileInfo object string normalizedPath; File::normalizePath( (sTestDataDir + TEST_FILENAME_2).c_str(), normalizedPath); out["path"] = normalizedPath.c_str(); out["mediaId"] = 2; // prepare event waiter EventWaiter ew(node.getEventController()); ew.start("bitmunk.medialibrary.File.updated"); ew.start("bitmunk.medialibrary.File.exception"); // add the file to the media library assertNoException( messenger->post(&filesUrl, &out, &in, node.getDefaultUserId())); // wait for file ID set event assert(ew.waitForEvent(5*1000)); // ensure it has an exception Event e = ew.popEvent(); //dumpDynamicObject(e); if(e["details"]->hasMember("exception")) { ExceptionRef ex = Exception::convertToException( e["details"]["exception"]); Exception::set(ex); } } tr.passIfNoException(); tr.test("add ware without payee-scheme (valid)"); { DynamicObject in; DynamicObject out; // create the outgoing ware object FileInfo fi; fi["id"] = TEST_FILE_ID_2; out["id"] = TEST_WARE_ID_2; out["mediaId"] = 2; out["description"] = "This ware was added by test-services-customcatalog"; out["fileInfo"] = fi; out["payees"]->setType(Array); Payee p1 = out["payees"]->append(); Payee p2 = out["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "This payee is for media ID 2"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "This payee is for media ID 2"; // add the ware to the custom catalog messenger->post(&waresUrl, &out, &in, node.getDefaultUserId()); } tr.passIfNoException(); printf("\nWaiting for server info to update...\n"); Thread::sleep(2*1000); while(true) { tr.test("get server info"); { Url serverUrl; serverUrl.format("%s/api/3.0/catalog/server?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); DynamicObject in; messenger->get(&serverUrl, in, node.getDefaultUserId()); dumpDynamicObject(in); } tr.passIfNoException(); printf( "\nSleeping to allow listing updater to run. Hit CTRL+C to quit.\n"); Thread::sleep(30*1000); } tr.ungroup(); }
static void customCatalogTests(Node& node, TestRunner& tr) { resetTestEnvironment(node); Messenger* messenger = node.getMessenger(); // generate the ware URLs that will be used throughout the test. Url waresUrl; waresUrl.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); Url waresNonDefaultListUrl; waresNonDefaultListUrl.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64 "&id=%s&default=false", messenger->getSelfUrl(true).c_str(), TEST_USER_ID, TEST_WARE_ID_2); Url wareUrl; wareUrl.format( "%s/api/3.0/catalog/wares/%s?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_WARE_ID_2, TEST_USER_ID); // generate the files URL that will be used to prime the medialibrary Url filesUrl; filesUrl.format("%s/api/3.2/medialibrary/files?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); Url removeUrl; removeUrl.format( "%s/api/3.2/medialibrary/files/%s?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_FILE_ID_2, TEST_USER_ID); // Create basic and detailed test ware with id=2 Ware basicWare2; Ware detailedWare2; { basicWare2["id"] = TEST_WARE_ID_2; basicWare2["description"] = "This ware was added by test-services-customcatalog"; detailedWare2 = basicWare2.clone(); detailedWare2["mediaId"] = 2; detailedWare2["fileInfos"]->setType(Array); FileInfo fi = detailedWare2["fileInfos"]->append(); fi["id"] = TEST_FILE_ID_2; fi["mediaId"] = 2; File file((sTestDataDir + TEST_FILENAME_2).c_str()); fi["extension"] = file->getExtension() + 1; fi["contentType"] = "audio/mpeg"; fi["contentSize"] = TEST_CONTENT_SIZE_2; fi["size"] = (uint64_t)file->getLength(); fi["path"] = file->getAbsolutePath(); detailedWare2["payees"]->setType(Array); Payee p1 = detailedWare2["payees"]->append(); Payee p2 = detailedWare2["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "This payee is for media ID 2"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "This payee is for media ID 2"; } // remove any previous files from the media library messenger->deleteResource(&removeUrl, NULL, node.getDefaultUserId()); // pass even if there is an exception - this is meant to just clear any // previous entries in the medialibrary database if they existed. Exception::clear(); tr.group("customcatalog"); tr.test("add file to medialibrary (valid)"); { DynamicObject in; DynamicObject out; // create a FileInfo object string normalizedPath; File::normalizePath( (sTestDataDir + TEST_FILENAME_2).c_str(), normalizedPath); out["path"] = normalizedPath.c_str(); out["mediaId"] = 2; // prepare event waiter EventWaiter ew(node.getEventController()); ew.start("bitmunk.medialibrary.File.updated"); ew.start("bitmunk.medialibrary.File.exception"); // add the file to the media library assertNoException( messenger->post(&filesUrl, &out, &in, node.getDefaultUserId())); // wait for file ID set event assert(ew.waitForEvent(5*1000)); // ensure it has an exception Event e = ew.popEvent(); //dumpDynamicObject(e); if(e["details"]->hasMember("exception")) { ExceptionRef ex = Exception::convertToException( e["details"]["exception"]); Exception::set(ex); } else if(strcmp( e["type"]->getString(), "bitmunk.medialibrary.File.updated") == 0) { // add new mediaLibraryId to the basic and detailed wares basicWare2["mediaLibraryId"] = e["details"]["mediaLibraryId"]; detailedWare2["mediaLibraryId"] = e["details"]["mediaLibraryId"]; } } tr.passIfNoException(); tr.test("add ware without payee-scheme (valid)"); { DynamicObject in; DynamicObject out; // create the outgoing ware object FileInfo fi; fi["id"] = TEST_FILE_ID_2; out["id"] = TEST_WARE_ID_2; out["mediaId"] = 2; out["description"] = "This ware was added by test-services-customcatalog"; out["fileInfo"] = fi; out["payees"]->setType(Array); Payee p1 = out["payees"]->append(); Payee p2 = out["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "This payee is for media ID 2"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "This payee is for media ID 2"; // add the ware to the custom catalog messenger->post(&waresUrl, &out, &in, node.getDefaultUserId()); // set ware payeeSchemeIds basicWare2["payeeSchemeId"] = in["payeeSchemeId"]; detailedWare2["payeeSchemeId"] = in["payeeSchemeId"]; } tr.passIfNoException(); tr.test("get ware (valid)"); { // get a specific ware from the custom catalog Ware receivedWare; assertNoException( messenger->get(&wareUrl, receivedWare, node.getDefaultUserId())); // remove format details for comparison if(receivedWare->hasMember("fileInfos") && receivedWare["fileInfos"]->getType() == Array && receivedWare["fileInfos"]->length() == 1) { receivedWare["fileInfos"][0]->removeMember("formatDetails"); } // check to make sure that the wares match assertNamedDynoCmp( "Expected ware ResourceSet", detailedWare2, "Received ware ResourceSet", receivedWare); } tr.passIfNoException(); tr.test("get wares"); { // get a specific ware from the custom catalog Ware receivedWareSet; Url url; url.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64 "&id=%s", messenger->getSelfUrl(true).c_str(), TEST_USER_ID, TEST_WARE_ID_2); assertNoException( messenger->get(&url, receivedWareSet, node.getDefaultUserId())); // check ware set Ware expectedWareSet; expectedWareSet["resources"][0] = basicWare2.clone(); expectedWareSet["total"] = 1; expectedWareSet["num"] = 1; expectedWareSet["start"] = 0; assertNamedDynoCmp( "Expected ware ResourceSet", expectedWareSet, "Received ware ResourceSet", receivedWareSet); } tr.passIfNoException(); tr.test("get wares by fileId"); { // get a specific ware from the custom catalog Ware receivedWareSet; Url url; url.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64 "&fileId=%s", messenger->getSelfUrl(true).c_str(), TEST_USER_ID, TEST_FILE_ID_2); assertNoException( messenger->get(&url, receivedWareSet, node.getDefaultUserId())); // check ware set Ware expectedWareSet; expectedWareSet["resources"][0] = basicWare2.clone(); expectedWareSet["total"] = 1; expectedWareSet["num"] = 1; expectedWareSet["start"] = 0; assertNamedDynoCmp( "Expected ware ResourceSet", expectedWareSet, "Received ware ResourceSet", receivedWareSet); } tr.passIfNoException(); tr.test("get specific unknown wares"); { // get a specific ware from the custom catalog Ware receivedWareSet; Url url; url.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64 "&id=%s&id=INVALID", messenger->getSelfUrl(true).c_str(), TEST_USER_ID, TEST_WARE_ID_2); messenger->get(&url, receivedWareSet, node.getDefaultUserId()); // check ware set Ware expectedWareSet; expectedWareSet["resources"][0] = basicWare2.clone(); expectedWareSet["total"] = 1; expectedWareSet["num"] = 2; expectedWareSet["start"] = 0; assertNamedDynoCmp( "Expected ware ResourceSet", expectedWareSet, "Received ware ResourceSet", receivedWareSet); } tr.passIfNoException(); tr.test("get wares by dup ware+file ids"); { // This test gets the same ware by both ware id and file id and returns // duplicate results. // get a specific ware from the custom catalog Ware receivedWareSet; Url url; url.format( "%s/api/3.0/catalog/wares?nodeuser=%" PRIu64 "&id=%s&fileId=%s", messenger->getSelfUrl(true).c_str(), TEST_USER_ID, TEST_WARE_ID_2, TEST_FILE_ID_2); messenger->get(&url, receivedWareSet, node.getDefaultUserId()); // check ware set Ware expectedWareSet; expectedWareSet["resources"][0] = basicWare2.clone(); expectedWareSet["total"] = 1; expectedWareSet["num"] = 2; expectedWareSet["start"] = 0; assertNamedDynoCmp( "Expected ware ResourceSet", expectedWareSet, "Received ware ResourceSet", receivedWareSet); } tr.passIfNoException(); tr.test("remove ware (valid)"); { // remove the ware messenger->deleteResource(&wareUrl, NULL, node.getDefaultUserId()); } tr.passIfNoException(); /*************************** Payee Scheme tests **************************/ // generate the payee schemes URL Url payeeSchemesUrl; payeeSchemesUrl.format("%s/api/3.0/catalog/payees/schemes?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), TEST_USER_ID); PayeeSchemeId psId = 0; tr.test("add payee scheme (valid)"); { DynamicObject in; DynamicObject out; // create the outgoing list of payees to organize into a payee scheme out["description"] = "Payee scheme description 1"; out["payees"]->setType(Array); PayeeList payees = out["payees"]; Payee p1 = payees->append(); Payee p2 = payees->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "test-services-customcatalog test payee 1"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "test-services-customcatalog test payee 2"; // add the ware to the custom catalog messenger->post(&payeeSchemesUrl, &out, &in, node.getDefaultUserId()); if(in->hasMember("payeeSchemeId")) { psId = in["payeeSchemeId"]->getUInt32(); } } tr.passIfNoException(); // generate the payee scheme URL Url payeeSchemeIdUrl; payeeSchemeIdUrl.format("%s/api/3.0/catalog/payees/schemes/%u?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), psId, TEST_USER_ID); tr.test("get payee scheme (valid)"); { // Create the expected payee scheme PayeeScheme expectedPayeeScheme; expectedPayeeScheme["id"] = psId; expectedPayeeScheme["userId"] = node.getDefaultUserId(); expectedPayeeScheme["description"] = "Payee scheme description 1"; expectedPayeeScheme["payees"]->setType(Array); Payee p1 = expectedPayeeScheme["payees"]->append(); Payee p2 = expectedPayeeScheme["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "test-services-customcatalog test payee 1"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "test-services-customcatalog test payee 2"; // get a specific payee scheme from the custom catalog PayeeScheme receivedPayeeScheme; messenger->get( &payeeSchemeIdUrl, receivedPayeeScheme, node.getDefaultUserId()); // check payee schemes assertNamedDynoCmp( "Expected payee scheme", expectedPayeeScheme, "Received payee scheme", receivedPayeeScheme); } tr.passIfNoException(); tr.test("get all payee schemes (valid)"); { // Create the result set ResourceSet expectedResults; expectedResults["total"] = 2; expectedResults["start"] = 0; expectedResults["num"] = 2; PayeeScheme ps = expectedResults["resources"]->append(); ps["id"] = 1; ps["userId"] = node.getDefaultUserId(); ps["description"] = "This ware was added by test-services-customcatalog"; ps["payees"]->setType(Array); Payee p1 = ps["payees"]->append(); Payee p2 = ps["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.10"; p1["description"] = "This payee is for media ID 2"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.10"; p2["description"] = "This payee is for media ID 2"; ps = expectedResults["resources"]->append(); ps["id"] = 2; ps["userId"] = node.getDefaultUserId(); ps["description"] = "Payee scheme description 1"; ps["payees"]->setType(Array); Payee p3 = ps["payees"]->append(); Payee p4 = ps["payees"]->append(); p3["id"] = 900; p3["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p3["amount"] = "0.10"; p3["description"] = "test-services-customcatalog test payee 1"; p4["id"] = 900; p4["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p4["percentage"] = "0.10"; p4["description"] = "test-services-customcatalog test payee 2"; // get all payee schemes from the custom catalog ResourceSet receivedResults; messenger->get( &payeeSchemesUrl, receivedResults, node.getDefaultUserId()); // check payee schemes assertNamedDynoCmp( "Expected payee scheme ResourceSet", expectedResults, "Received payee scheme ResourceSet", receivedResults); } tr.passIfNoException(); tr.test("update payee scheme (valid)"); { DynamicObject in; DynamicObject out; // create the outgoing list of payees to organize into a payee scheme out["description"] = "Payee scheme description 2"; Payee p1 = out["payees"]->append(); Payee p2 = out["payees"]->append(); p1["id"] = 900; p1["amountType"] = PAYEE_AMOUNT_TYPE_FLATFEE; p1["amount"] = "0.15"; p1["description"] = "test-services-customcatalog test payee 1 (updated)"; p2["id"] = 900; p2["amountType"] = PAYEE_AMOUNT_TYPE_PTOTAL; p2["percentage"] = "0.14"; p2["description"] = "test-services-customcatalog test payee 2 (updated)"; // update a pre-existing payee scheme assertNoException( messenger->post( &payeeSchemeIdUrl, &out, &in, node.getDefaultUserId())); // ensure that the payee scheme was updated assert(in["payeeSchemeId"]->getUInt32() == 2); if(in->hasMember("payeeSchemeId")) { psId = in["payeeSchemeId"]->getUInt32(); } } tr.passIfNoException(); tr.test("add ware with payee scheme (valid)"); { DynamicObject in; DynamicObject out; // create the outgoing ware object FileInfo fi; fi["id"] = TEST_FILE_ID_2; out["id"] = TEST_WARE_ID_2; out["mediaId"] = 2; out["description"] = "This ware was added by test-services-customcatalog"; out["fileInfo"] = fi; out["payeeSchemeId"] = psId; // add the ware to the custom catalog messenger->post(&waresUrl, &out, &in, node.getDefaultUserId()); } tr.passIfNoException(); tr.test("remove associated payee scheme (invalid)"); { messenger->deleteResource( &payeeSchemeIdUrl, NULL, node.getDefaultUserId()); } tr.passIfException(); tr.test("remove ware associated w/ payee scheme (valid)"); { messenger->deleteResource( &wareUrl, NULL, node.getDefaultUserId()); } tr.passIfNoException(); tr.test("remove payee scheme (valid)"); { messenger->deleteResource( &payeeSchemeIdUrl, NULL, node.getDefaultUserId()); } tr.passIfNoException(); tr.test("create ware w/ invalid payee scheme (invalid)"); { DynamicObject in; DynamicObject out; // create the outgoing ware object FileInfo fi; fi["id"] = TEST_FILE_ID_2; PayeeScheme ps; ps["id"] = psId; out["id"] = TEST_WARE_ID_2; out["mediaId"] = 2; out["description"] = "This ware was added by test-services-customcatalog"; out["fileInfo"] = fi; out["payeeScheme"] = ps; // add the ware to the custom catalog messenger->post(&waresUrl, &out, &in, node.getDefaultUserId()); } tr.passIfException(); tr.test("remove ware (invalid)"); { // remove a ware that doesn't exist messenger->deleteResource(&wareUrl, NULL, node.getDefaultUserId()); } tr.passIfException(); tr.ungroup(); }
void DownloadStateEventReactor::reprocessRequired(Event& e) { // get user ID UserId userId = BM_USER_ID(e["details"]["userId"]); // ensure operation is done as the user to protect against logout Operation op = mNode->currentOperation(); if(mNode->addUserOperation(userId, op, NULL)) { // lock to prevent double-reprocessing download states // Note: Reprocessing the download state isn't fatal, it just may fail // silently or be annoying by re-assembling already assembled files, etc. mReprocessLock.lock(); { // log activity MO_CAT_DEBUG(BM_EVENTREACTOR_DS_CAT, "Event reactor handling ds 'reprocessRequired' " "event for user %" PRIu64 "...", userId); // get messenger Messenger* messenger = mNode->getMessenger(); // process all queued directives { Url url; url.format("%s/api/3.0/system/directives?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), userId); DynamicObject directives; if(messenger->get(&url, directives, userId)) { DynamicObjectIterator i = directives.getIterator(); while(i->hasNext()) { DynamicObject& directive = i->next(); // process the directive if it is of type "peerbuy": if(strcmp(directive["type"]->getString(), "peerbuy") == 0) { url.format( "%s/api/3.0/system/directives/process/%s?nodeuser=%" PRIu64, messenger->getSelfUrl(true).c_str(), directive["id"]->getString(), userId); DynamicObject in; if(!messenger->post(&url, NULL, &in, userId)) { // schedule exception event Event e2; e2["type"] = "bitmunk.eventreactor.EventReactor.exception"; e2["details"]["exception"] = Exception::getAsDynamicObject(); mNode->getEventController()->schedule(e2); } } } } } // create a list of download states to reprocess DownloadStateList list; list->setType(Array); if(e["details"]->hasMember("all") && e["details"]["all"]->getBoolean()) { // get all non-processing download states Url url; url.format("%s/api/3.0/purchase/contracts/downloadstates" "?nodeuser=%" PRIu64 "&incomplete=true&processing=false", messenger->getSelfUrl(true).c_str(), userId); DynamicObject in; if(messenger->get(&url, in, userId)) { // append states to list list.merge(in["downloadStates"], true); } else { // schedule exception event Event e2; e2["type"] = "bitmunk.eventreactor.EventReactor.exception"; e2["details"]["exception"] = Exception::getAsDynamicObject(); mNode->getEventController()->schedule(e2); } } else if(e["details"]->hasMember("downloadStateId")) { // reprocess single download state DownloadStateId dsId = e["details"]["downloadStateId"]->getUInt64(); IPurchaseModule* ipm = dynamic_cast<IPurchaseModule*>( mNode->getKernel()->getModuleApi( "bitmunk.purchase.Purchase")); DownloadState ds; ds["id"] = dsId; BM_ID_SET(ds["userId"], userId); if(ipm->populateDownloadState(ds)) { // append state to list list->append(ds); } else { // schedule exception event Event e2; e2["type"] = "bitmunk.eventreactor.EventReactor.exception"; e2["details"]["exception"] = Exception::getAsDynamicObject(); mNode->getEventController()->schedule(e2); } } // reprocess all states in the list DynamicObjectIterator i = list.getIterator(); while(i->hasNext()) { DownloadState& ds = i->next(); DownloadStateId dsId = ds["id"]->getUInt64(); if(ds["downloadStarted"]->getBoolean()) { // do purchase only if no pieces remain if(ds["remainingPieces"]->getUInt32() == 0) { if(!ds["licensePurchased"]->getBoolean() || !ds["dataPurchased"]->getBoolean()) { // do purchase postDownloadStateId(userId, dsId, "purchase"); } else if(!ds["filesAssembled"]->getBoolean()) { // do file assembly postDownloadStateId(userId, dsId, "assemble"); } } else if(!ds["downloadPaused"]->getBoolean()) { // restart download postDownloadStateId(userId, dsId, "download"); } } else if(!ds["initialized"]->getBoolean()) { // initialize download state postDownloadStateId(userId, dsId, "initialize"); } else if(!ds["licenseAcquired"]->getBoolean()) { // acquire license postDownloadStateId(userId, dsId, "license"); } } } mReprocessLock.unlock(); } }