lordgamez commented on code in PR #1695:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1695#discussion_r1439542044


##########
extensions/grafana-loki/tests/PushGrafanaLokiRESTTest.cpp:
##########
@@ -0,0 +1,296 @@
+/**
+ * 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.
+ */
+
+#include "../PushGrafanaLokiREST.h"
+#include "MockGrafanaLoki.h"
+#include "SingleProcessorTestController.h"
+#include "Catch.h"
+#include "utils/StringUtils.h"
+#include "utils/TestUtils.h"
+
+namespace org::apache::nifi::minifi::extensions::grafana::loki::test {
+
+TEST_CASE("Url property is required", "[PushGrafanaLokiREST]") {
+  auto push_grafana_loki_rest = 
std::make_shared<PushGrafanaLokiREST>("PushGrafanaLokiREST");
+  minifi::test::SingleProcessorTestController 
test_controller(push_grafana_loki_rest);
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::Url, ""));
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::StreamLabels, "job=minifi,directory=/opt/minifi/logs/"));
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::LogLineBatchSize, "1"));
+  REQUIRE_THROWS_AS(test_controller.trigger(), minifi::Exception);
+}
+
+TEST_CASE("Valid stream labels need to be set", "[PushGrafanaLokiREST]") {
+  auto push_grafana_loki_rest = 
std::make_shared<PushGrafanaLokiREST>("PushGrafanaLokiREST");
+  minifi::test::SingleProcessorTestController 
test_controller(push_grafana_loki_rest);
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::Url, "localhost:10990"));
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::LogLineBatchSize, "1"));
+  SECTION("Stream labels cannot be empty") {
+    test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::StreamLabels, "");
+  }
+  SECTION("Stream labels need to be valid") {
+    test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::StreamLabels, "invalidlabels,invalidlabels2");
+  }
+  REQUIRE_THROWS_AS(test_controller.trigger(), minifi::Exception);
+}
+
+TEST_CASE("Log Line Batch Size cannot be 0", "[PushGrafanaLokiREST]") {
+  auto push_grafana_loki_rest = 
std::make_shared<PushGrafanaLokiREST>("PushGrafanaLokiREST");
+  minifi::test::SingleProcessorTestController 
test_controller(push_grafana_loki_rest);
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::Url, "localhost:10990"));
+  CHECK(test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::StreamLabels, "job=minifi,directory=/opt/minifi/logs/"));
+  test_controller.plan->setProperty(push_grafana_loki_rest, 
PushGrafanaLokiREST::LogLineBatchSize, "0");
+  REQUIRE_THROWS_AS(test_controller.trigger(), minifi::Exception);
+}
+
+class PushGrafanaLokiRESTTestFixture {
+ public:
+  PushGrafanaLokiRESTTestFixture()
+      : mock_loki_("10990"),
+        
push_grafana_loki_rest_(std::make_shared<PushGrafanaLokiREST>("PushGrafanaLokiREST")),
+        test_controller_(push_grafana_loki_rest_) {
+    LogTestController::getInstance().setDebug<TestPlan>();
+    LogTestController::getInstance().setDebug<minifi::core::Processor>();
+    LogTestController::getInstance().setTrace<minifi::core::ProcessSession>();
+    LogTestController::getInstance().setTrace<PushGrafanaLokiREST>();
+    CHECK(test_controller_.plan->setProperty(push_grafana_loki_rest_, 
PushGrafanaLokiREST::Url, "localhost:10990"));
+    CHECK(test_controller_.plan->setProperty(push_grafana_loki_rest_, 
PushGrafanaLokiREST::StreamLabels, "job=minifi,directory=/opt/minifi/logs/"));
+  }
+
+  void setProperty(const auto& property, const std::string& property_value) {
+    CHECK(test_controller_.plan->setProperty(push_grafana_loki_rest_, 
property, property_value));
+  }
+
+  void verifyLastRequestIsEmpty() {
+    const auto& request = mock_loki_.getLastRequest();
+    REQUIRE(request.IsNull());
+  }
+
+  void verifyTenantId(const std::string& tenant_id) {
+    REQUIRE(mock_loki_.getLastTenantId() == tenant_id);
+  }
+
+  void verifyBasicAuthorization(const std::string& 
expected_username_and_password) {
+    auto last_authorization = mock_loki_.getLastAuthorization();
+    std::string expected_authorization = "Basic ";
+    REQUIRE(minifi::utils::StringUtils::startsWith(last_authorization, 
expected_authorization));
+    std::string username_and_password_decoded = 
minifi::utils::StringUtils::from_base64(last_authorization.substr(expected_authorization.size()),
 minifi::utils::as_string_tag_t{});
+    REQUIRE(username_and_password_decoded == expected_username_and_password);
+  }
+
+  void verifyBearerTokenAuthorization(const std::string& 
expected_bearer_token) {
+    auto last_authorization = mock_loki_.getLastAuthorization();
+    std::string expected_authorization = "Bearer ";
+    REQUIRE(minifi::utils::StringUtils::startsWith(last_authorization, 
expected_authorization));
+    auto bearer_token = 
last_authorization.substr(expected_authorization.size());
+    REQUIRE(bearer_token == expected_bearer_token);
+  }
+
+  void verifyStreamLabels() {
+    const auto& request = mock_loki_.getLastRequest();
+    REQUIRE(request.HasMember("streams"));
+    const auto& stream_array = request["streams"].GetArray();
+    REQUIRE(stream_array.Size() == 1);
+    REQUIRE(stream_array[0].HasMember("stream"));
+    const auto& stream = stream_array[0]["stream"].GetObject();
+    REQUIRE(stream.HasMember("job"));
+    std::string job_string = stream["job"].GetString();
+    REQUIRE(job_string == "minifi");
+    REQUIRE(stream.HasMember("directory"));
+    std::string directory_string = stream["directory"].GetString();
+    REQUIRE(directory_string == "/opt/minifi/logs/");
+  }
+
+  void verifySentRequestToLoki(uint64_t start_timestamp, const 
std::vector<std::string>& expected_log_values,
+      const std::vector<std::map<std::string, std::string>>& 
expected_log_line_attribute_values = {}) {
+    const auto& request = mock_loki_.getLastRequest();
+    REQUIRE(request.HasMember("streams"));
+    const auto& stream_array = request["streams"].GetArray();
+    REQUIRE(stream_array[0].HasMember("values"));
+    const auto& value_array = stream_array[0]["values"].GetArray();
+    REQUIRE(value_array.Size() == expected_log_values.size());
+    for (size_t i = 0; i < expected_log_values.size(); ++i) {
+      const auto& log_line_array = value_array[i].GetArray();
+      if (!expected_log_line_attribute_values.empty()) {
+        REQUIRE(log_line_array.Size() == 3);
+      } else {
+        REQUIRE(log_line_array.Size() == 2);
+      }
+      std::string timestamp_str = log_line_array[0].GetString();
+      REQUIRE(start_timestamp <= std::stoull(timestamp_str));
+      std::string value = log_line_array[1].GetString();
+      REQUIRE(value == expected_log_values[i]);

Review Comment:
   Updated in 083c28ee59f96352e89d5675d635ea3b91bd9942



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to