Skip to content

v0.2.47..v0.2.48 changeset OsmApiWriterTest.cpp

Garret Voltz edited this page Sep 27, 2019 · 1 revision
diff --git a/hoot-core-test/src/test/cpp/hoot/core/io/OsmApiWriterTest.cpp b/hoot-core-test/src/test/cpp/hoot/core/io/OsmApiWriterTest.cpp
index 8be3b24..9f8fd84 100644
--- a/hoot-core-test/src/test/cpp/hoot/core/io/OsmApiWriterTest.cpp
+++ b/hoot-core-test/src/test/cpp/hoot/core/io/OsmApiWriterTest.cpp
@@ -30,10 +30,18 @@
 #include <hoot/core/io/OsmApiWriter.h>
 #include <hoot/core/util/ConfigOptions.h>
 #include <hoot/core/util/Log.h>
+#include <hoot/core/util/FileUtils.h>
+
+#include "OsmApiWriterTestServer.h"
 
 //  Qt
 #include <QNetworkReply>
 
+//  Run tests against a local test server
+#define RUN_LOCAL_TEST_SERVER
+//  Run tests against a local copy of OSM API
+//#define RUN_LOCAL_OSM_API_SERVER
+
 namespace hoot
 {
 
@@ -44,23 +52,36 @@ class OsmApiWriterTest : public HootTestFixture
   CPPUNIT_TEST(runParseCapabilitiesTest);
   CPPUNIT_TEST(runCapabilitesTest);
   CPPUNIT_TEST(runParsePermissionsTest);
+  CPPUNIT_TEST(runPermissionsTest);
+#ifdef RUN_LOCAL_TEST_SERVER
+  CPPUNIT_TEST(runRetryConflictsTest);
+  CPPUNIT_TEST(runVersionConflictResolutionTest);
+#endif
   /* These tests are for local testing and require additional resources to complete */
-//  CPPUNIT_TEST(runPermissionsTest);
-//  CPPUNIT_TEST(runChangesetTest);
-//  CPPUNIT_TEST(runChangesetTrottleTest);
-//  CPPUNIT_TEST(runChangesetConflictTest);
-//  CPPUNIT_TEST(oauthTest);
+  CPPUNIT_TEST(runChangesetTest);
+  CPPUNIT_TEST(runChangesetTrottleTest);
+  CPPUNIT_TEST(runChangesetConflictTest);
+  CPPUNIT_TEST(oauthTest);
   CPPUNIT_TEST_SUITE_END();
 
 public:
 
   const QString OSM_API_URL = "https://www.openstreetmap.org";
-  const QString ME_API_URL = "http://ec2-34-237-221-226.compute-1.amazonaws.com";
+  const QString LOCAL_TEST_API_URL = "http://localhost:%1";
+#ifdef RUN_LOCAL_OSM_API_SERVER
+  const QString LOCAL_OSM_API_URL = "http://<Enter local OSM API URL here>";
+#endif
+
+  const int PORT_CAPABILITIES = 9800;
+  const int PORT_PERMISSIONS =  9801;
+  const int PORT_CONFLICTS =    9802;
+  const int PORT_VERSION =      9803;
 
   OsmApiWriterTest()
     : HootTestFixture("test-files/io/OsmChangesetElementTest/",
                       UNUSED_PATH)
   {
+    setResetType(ResetBasic);
   }
 
   void runParseStatusTest()
@@ -74,27 +95,7 @@ public:
   void runParseCapabilitiesTest()
   {
     OsmApiWriter writer;
-    QString xml =
-      "<?xml version='1.0' encoding='UTF-8'?>"
-      "<osm version='0.6' generator='OpenStreetMap server' copyright='OpenStreetMap and contributors' attribution='https://www.openstreetmap.org/copyright' license='http://opendatacommons.org/licenses/odbl/1-0/'>"
-      "  <api>"
-      "    <version minimum='0.6' maximum='0.6'/>"
-      "    <area maximum='0.25'/>"
-      "    <note_area maximum='25'/>"
-      "    <tracepoints per_page='5000'/>"
-      "    <waynodes maximum='2000'/>"
-      "    <changesets maximum_elements='10000'/>"
-      "    <timeout seconds='300'/>"
-      "    <status database='online' api='online' gpx='online'/>"
-      "  </api>"
-      "  <policy>"
-      "    <imagery>"
-      "      <blacklist regex='http://xdworld\\.vworld\\.kr:8080/.*'/>"
-      "      <blacklist regex='.*\\.here\\.com[/:].*'/>"
-      "    </imagery>"
-      "  </policy>"
-      "</osm>";
-    OsmApiCapabilites capabilities = writer._parseCapabilities(xml);
+    OsmApiCapabilites capabilities = writer._parseCapabilities(OsmApiSampleRequestResponse::SAMPLE_CAPABILITIES_RESPONSE);
     HOOT_STR_EQUALS(capabilities.getVersion(), QString("0.6"));
     CPPUNIT_ASSERT_EQUAL(capabilities.getTracepoints(), static_cast<long>(5000));
     CPPUNIT_ASSERT_EQUAL(capabilities.getWayNodes(), static_cast<long>(2000));
@@ -108,7 +109,13 @@ public:
   void runCapabilitesTest()
   {
     QUrl osm;
+#ifdef RUN_LOCAL_TEST_SERVER
+    osm.setUrl(LOCAL_TEST_API_URL.arg(PORT_CAPABILITIES));
+    CapabilitiesTestServer server(PORT_CAPABILITIES);
+    server.start();
+#else
     osm.setUrl(OSM_API_URL);
+#endif
 
     QList<QString> changesets;
     HootNetworkRequestPtr request(new HootNetworkRequest());
@@ -123,41 +130,44 @@ public:
     CPPUNIT_ASSERT_EQUAL(writer._capabilities.getDatabaseStatus(), OsmApiStatus::ONLINE);
     CPPUNIT_ASSERT_EQUAL(writer._capabilities.getApiStatus(), OsmApiStatus::ONLINE);
     CPPUNIT_ASSERT_EQUAL(writer._capabilities.getGpxStatus(), OsmApiStatus::ONLINE);
+#ifdef RUN_LOCAL_TEST_SERVER
+    server.wait();
+#endif
   }
 
   void runParsePermissionsTest()
   {
     OsmApiWriter writer;
-    QString xml =
-      "<?xml version='1.0' encoding='UTF-8'?>"
-      "<osm version='0.6' generator='OpenStreetMap server'>"
-      "  <permissions>"
-      "    <permission name='allow_read_prefs'/>"
-      "    <permission name='allow_read_gpx'/>"
-      "    <permission name='allow_write_api'/>"
-      "    <permission name='allow_write_gpx'/>"
-      "  </permissions>"
-      "</osm>";
-    CPPUNIT_ASSERT(writer._parsePermissions(xml));
+    CPPUNIT_ASSERT(writer._parsePermissions(OsmApiSampleRequestResponse::SAMPLE_PERMISSIONS_RESPONSE));
   }
 
   void runPermissionsTest()
   {
     QUrl osm;
-    osm.setUrl(ME_API_URL);
     osm.setUserInfo("test01:hoottest");
+#ifdef RUN_LOCAL_TEST_SERVER
+    osm.setUrl(LOCAL_TEST_API_URL.arg(PORT_PERMISSIONS));
+    PermissionsTestServer server(PORT_PERMISSIONS);
+    server.start();
+#else
+    osm.setUrl(ME_API_URL);
+#endif
 
     QList<QString> changesets;
     HootNetworkRequestPtr request(new HootNetworkRequest());
     OsmApiWriter writer(osm, changesets);
     CPPUNIT_ASSERT(writer.validatePermissions(request));
     CPPUNIT_ASSERT_EQUAL(request->getHttpStatus(), 200);
+#ifdef RUN_LOCAL_TEST_SERVER
+    server.shutdown();
+#endif
   }
 
   void runChangesetTest()
   {
+#ifdef RUN_LOCAL_OSM_API_SERVER
     QUrl osm;
-    osm.setUrl(ME_API_URL);
+    osm.setUrl(LOCAL_OSM_API_URL);
     osm.setUserInfo("test01:hoottest");
 
     QList<QString> changesets;
@@ -171,12 +181,14 @@ public:
     writer.setConfiguration(s);
 
     writer.apply();
+#endif
   }
 
   void runChangesetTrottleTest()
   {
+#ifdef RUN_LOCAL_OSM_API_SERVER
     QUrl osm;
-    osm.setUrl(ME_API_URL);
+    osm.setUrl(LOCAL_OSM_API_URL);
     osm.setUserInfo("test01:hoottest");
 
     QList<QString> changesets;
@@ -192,12 +204,14 @@ public:
     writer.setConfiguration(s);
 
     writer.apply();
+#endif
   }
 
   void runChangesetConflictTest()
   {
+#ifdef RUN_LOCAL_OSM_API_SERVER
     QUrl osm;
-    osm.setUrl(ME_API_URL);
+    osm.setUrl(LOCAL_OSM_API_URL);
     osm.setUserInfo("test01:hoottest");
 
     //  Load up the all-create ToyTestA
@@ -244,12 +258,102 @@ public:
         "</osmChange>\n",
         writer.getFailedChangeset());
     }
+#endif
+  }
+
+  void runRetryConflictsTest()
+  {
+#ifdef RUN_LOCAL_TEST_SERVER
+    //  Suppress the OsmApiWriter errors by temporarily changing the log level
+    //  when the log level is Info or above because we expect the all of the errors.
+    //  Below Info is Debug and Trace, those are set because we want to see everything
+    Log::WarningLevel logLevel = Log::getInstance().getLevel();
+    if (Log::getInstance().getLevel() >= Log::Info)
+      Log::getInstance().setLevel(Log::Fatal);
+
+    //  Setup the test
+    QUrl osm;
+    osm.setUrl(LOCAL_TEST_API_URL.arg(PORT_CONFLICTS));
+    osm.setUserInfo("test01:hoottest");
+
+    //  Kick off the conflict test server
+    RetryConflictsTestServer server(PORT_CONFLICTS);
+    server.start();
+
+    QList<QString> changesets;
+    changesets.append(_inputPath + "ToyTestAConflicts.osc");
+
+    OsmApiWriter writer(osm, changesets);
+
+    Settings s;
+    s.set(ConfigOptions::getChangesetApidbWritersMaxKey(), 1);
+    s.set(ConfigOptions::getChangesetApidbSizeMaxKey(), 100);
+    writer.setConfiguration(s);
+
+    writer.apply();
+
+    //  Wait for the test server to finish
+    server.wait();
+
+    Log::getInstance().setLevel(logLevel);
+
+    //  Make sure that some of the changes failed
+    CPPUNIT_ASSERT(writer.containsFailed());
+    HOOT_STR_EQUALS(
+          FileUtils::readFully(_inputPath + "ToyTestAConflicts.osc")
+            .replace("timestamp=\"\"", "timestamp=\"\" changeset=\"0\"")
+            .replace("    ", "\t"),
+      writer.getFailedChangeset());
+    //  Check the stats
+    checkStats(writer.getStats(), 3, 2, 0, 2, 1, 2, 5);
+#endif
+  }
+
+  void runVersionConflictResolutionTest()
+  {
+#ifdef RUN_LOCAL_TEST_SERVER
+    //  Suppress the OsmApiWriter errors by temporarily changing the log level
+    //  when the log level is Info or above because we expect the all of the errors.
+    //  Below Info is Debug and Trace, those are set because we want to see everything
+    Log::WarningLevel logLevel = Log::getInstance().getLevel();
+    if (Log::getInstance().getLevel() >= Log::Info)
+      Log::getInstance().setLevel(Log::Fatal);
+
+    //  Setup the test
+    QUrl osm;
+    osm.setUrl(LOCAL_TEST_API_URL.arg(PORT_VERSION));
+    osm.setUserInfo("test01:hoottest");
+
+    //  Kick off the version conflict test server
+    RetryVersionTestServer server(PORT_VERSION);
+    server.start();
+
+    OsmApiWriter writer(osm, OsmApiSampleRequestResponse::SAMPLE_CHANGESET_REQUEST);
+
+    Settings s;
+    s.set(ConfigOptions::getChangesetApidbWritersMaxKey(), 1);
+    s.set(ConfigOptions::getChangesetApidbSizeMaxKey(), 100);
+    writer.setConfiguration(s);
+
+    writer.apply();
+
+    //  Wait for the test server to finish
+    server.wait();
+
+    Log::getInstance().setLevel(logLevel);
+
+    //  Make sure that none of the changes failed
+    CPPUNIT_ASSERT(!writer.containsFailed());
+    //  Check the stats
+    checkStats(writer.getStats(), 0, 4, 0, 0, 4, 0, 0);
+#endif
   }
 
   void oauthTest()
   {
+#ifdef RUN_LOCAL_OSM_API_SERVER
     QUrl osm;
-    osm.setUrl(ME_API_URL);
+    osm.setUrl(LOCAL_OSM_API_URL);
 
     QList<QString> changesets;
     changesets.append("test-files/ToyTestA.osm");
@@ -267,10 +371,40 @@ public:
     writer.setConfiguration(s);
 
     writer.apply();
+#endif
+  }
+
+  void checkStats(QList<SingleStat> stats,
+                  int nodes, int ways, int relations,
+                  int created, int modified, int deleted,
+                  int errors)
+  {
+    for (int i = 0; i < stats.size(); ++i)
+    {
+      SingleStat stat = stats[i];
+      if (stat.name == "Total Nodes in Changeset")
+        testStat(stat, nodes);
+      else if (stat.name == "Total Ways in Changeset")
+        testStat(stat, ways);
+      else if (stat.name == "Total Relations in Changeset")
+        testStat(stat, relations);
+      else if (stat.name == "Total Elements Created")
+        testStat(stat, created);
+      else if (stat.name == "Total Elements Modified")
+        testStat(stat, modified);
+      else if (stat.name == "Total Elements Deleted")
+        testStat(stat, deleted);
+      else if (stat.name == "Total Errors")
+        testStat(stat, errors);
+    }
+  }
+
+  void testStat(SingleStat stat, int value)
+  {
+    HOOT_STR_EQUALS(QString("%1: %2").arg(stat.name).arg(value), stat.toString());
   }
 };
 
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(OsmApiWriterTest, "quick");
-//CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(OsmApiWriterTest, "current");
-
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(OsmApiWriterTest, "serial");
 }
Clone this wiki locally