adamdebreceni commented on code in PR #1340:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1340#discussion_r892432982


##########
extensions/http-curl/tests/C2MetricsTest.cpp:
##########
@@ -0,0 +1,209 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#undef NDEBUG
+#include <string>
+#include <iostream>
+#include <filesystem>
+
+#include "TestBase.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+#include "processors/TailFile.h"
+#include "state/ProcessorController.h"
+#include "utils/file/FileUtils.h"
+#include "utils/TestUtils.h"
+#include "processors/GetTCP.h"
+#include "utils/StringUtils.h"
+#include "utils/file/PathUtils.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::test {
+
+class VerifyC2Metrics : public VerifyC2Base {
+ public:
+  explicit VerifyC2Metrics(const std::atomic_bool& 
metrics_updated_successfully) : 
metrics_updated_successfully_(metrics_updated_successfully) {
+  }
+
+  void testSetup() override {
+    LogTestController::getInstance().setTrace<minifi::c2::C2Agent>();
+    LogTestController::getInstance().setTrace<minifi::c2::C2Client>();
+    LogTestController::getInstance().setDebug<minifi::c2::RESTSender>();
+    LogTestController::getInstance().setDebug<minifi::FlowController>();
+    LogTestController::getInstance().setOff<minifi::processors::GetTCP>();
+    VerifyC2Base::testSetup();
+  }
+
+  void runAssertions() override {
+    using org::apache::nifi::minifi::utils::verifyEventHappenedInPollTime;
+    assert(verifyEventHappenedInPollTime(40s, [&] { return 
metrics_updated_successfully_.load(); }, 1s));
+  }
+
+ private:
+  const std::atomic_bool& metrics_updated_successfully_;
+};
+
+class MetricsHandler: public HeartbeatHandler {
+ public:
+  explicit MetricsHandler(std::atomic_bool& metrics_updated_successfully, 
std::shared_ptr<minifi::Configure> configuration, const std::string& 
replacement_config_path)
+    : HeartbeatHandler(std::move(configuration)),
+      metrics_updated_successfully_(metrics_updated_successfully),
+      
replacement_config_(getReplacementConfigAsJsonValue(replacement_config_path)) {
+  }
+
+  void handleHeartbeat(const rapidjson::Document& root, struct mg_connection* 
conn) override {
+    switch (test_state_) {
+      case TestState::VERIFY_INITIAL_METRICS: {
+        verifyMetrics(root);
+        sendEmptyHeartbeatResponse(conn);
+        break;
+      }
+      case TestState::SEND_NEW_CONFIG: {
+        sendHeartbeatResponse("UPDATE", "configuration", "889348", conn, 
{{"configuration_data", replacement_config_}});
+        test_state_ = TestState::VERIFY_UPDATED_METRICS;
+        break;
+      }
+      case TestState::VERIFY_UPDATED_METRICS: {
+        verifyUpdatedMetrics(root);
+        sendEmptyHeartbeatResponse(conn);
+        break;
+      }
+    }
+  }
+
+ private:
+  enum class TestState {
+    VERIFY_INITIAL_METRICS,
+    SEND_NEW_CONFIG,
+    VERIFY_UPDATED_METRICS
+  };
+
+  static void sendEmptyHeartbeatResponse(struct mg_connection* conn) {
+    mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: 
text/plain\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
+  }
+
+  void verifyMetrics(const rapidjson::Document& root) {
+    auto initial_metrics_verified =
+      root.HasMember("metrics") &&
+      root["metrics"].HasMember("RuntimeMetrics") &&
+      root["metrics"].HasMember("LoadMetrics") &&
+      root["metrics"].HasMember("ProcessorMetrics");
+    if (initial_metrics_verified) {
+      test_state_ = TestState::SEND_NEW_CONFIG;
+    }
+  }
+
+  void verifyUpdatedMetrics(const rapidjson::Document& root) {
+    auto updated_metrics_verified =
+      root.HasMember("metrics") &&
+      root["metrics"].HasMember("RuntimeMetrics") &&
+      root["metrics"].HasMember("LoadMetrics") &&
+      !root["metrics"].HasMember("ProcessorMetrics") &&
+      verifyUpdatedRuntimeMetrics(root["metrics"]["RuntimeMetrics"]) &&
+      verifyUpdatedLoadMetrics(root["metrics"]["LoadMetrics"]);
+
+    if (updated_metrics_verified) {
+      metrics_updated_successfully_ = true;
+    }
+  }
+
+  static bool verifyRuntimeMetrics(const rapidjson::Value& runtime_metrics) {
+    return runtime_metrics.HasMember("deviceInfo") &&
+      runtime_metrics.HasMember("flowInfo") &&
+      runtime_metrics["flowInfo"].HasMember("versionedFlowSnapshotURI") &&
+      runtime_metrics["flowInfo"].HasMember("queues") &&
+      runtime_metrics["flowInfo"].HasMember("components") &&
+      
runtime_metrics["flowInfo"]["queues"].HasMember("2438e3c8-015a-1000-79ca-83af40ec1997")
 &&
+      runtime_metrics["flowInfo"]["components"].HasMember("FlowController") &&
+      runtime_metrics["flowInfo"]["components"].HasMember("GetTCP") &&
+      runtime_metrics["flowInfo"]["components"].HasMember("LogAttribute");
+  }

Review Comment:
   we have a `verifyJSON` test util, could that be used here? (maybe with some 
enhancements? e.g. `"__any__"` matching all values if we only care about field 
existence)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to