fgerlits commented on a change in pull request #1137:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1137#discussion_r684258522



##########
File path: extensions/standard-processors/processors/AttributesToJSON.h
##########
@@ -0,0 +1,103 @@
+/**
+ * @file AttributesToJSON.h
+ * AttributesToJSON class declaration
+ *
+ * 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.
+ */
+#pragma once
+
+#include <vector>
+#include <string>
+#include <set>
+#include <unordered_set>
+#include <memory>
+#include <map>
+#include <regex>
+
+#include "rapidjson/document.h"
+#include "core/Processor.h"
+#include "core/Property.h"
+#include "core/logging/Logger.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+class AttributesToJSON : public core::Processor {
+ public:
+  static const std::set<std::string> DESTINATIONS;
+
+  explicit AttributesToJSON(const std::string& name, const utils::Identifier& 
uuid = {})
+      : core::Processor(name, uuid),
+        logger_(logging::LoggerFactory<AttributesToJSON>::getLogger()),
+        
core_attributes_(core::SpecialFlowAttribute::getSpecialFlowAttributes()) {
+  }
+  static constexpr char const* ProcessorName = "AttributesToJSON";
+  // Supported Properties
+  static const core::Property AttributesList;
+  static const core::Property AttributesRegularExpression;
+  static const core::Property Destination;
+  static const core::Property IncludeCoreAttributes;
+  static const core::Property NullValue;
+
+  // Supported Relationships
+  static core::Relationship Success;
+
+  void initialize() override;
+  void onSchedule(core::ProcessContext *context, core::ProcessSessionFactory* 
sessionFactory) override;
+  void onTrigger(core::ProcessContext *context, core::ProcessSession *session) 
override;
+
+  core::annotation::Input getInputRequirement() const override {
+    return core::annotation::Input::INPUT_REQUIRED;
+  }
+
+ private:
+  class WriteCallback : public OutputStreamCallback {
+   public:
+    explicit WriteCallback(const std::string& json_data) : 
json_data_(json_data) {}
+    int64_t process(const std::shared_ptr<io::BaseStream>& stream) override {
+      const auto write_ret = stream->write(reinterpret_cast<const 
uint8_t*>(json_data_.data()), json_data_.length());
+      return io::isError(write_ret) ? -1 : gsl::narrow<int64_t>(write_ret);
+    }
+   private:
+    std::string json_data_;
+  };
+
+  bool isCoreAttributeToBeFiltered(const std::string& attribute) const;
+  bool matchesAttributeRegex(const std::string& attribute);
+  void addAttributeToJson(rapidjson::Document& document, const std::string& 
key, const std::string& value);
+  std::string buildAttributeJsonData(std::map<std::string, std::string>&& 
attributes);
+
+  std::shared_ptr<logging::Logger> logger_;
+  const std::unordered_set<std::string> core_attributes_;
+  std::vector<std::string> attribute_list_;
+  std::string attributes_regular_expression_str_;
+  std::regex attributes_regular_expression_;
+  bool write_to_attribute_ = true;

Review comment:
       it would be nicer to have a (smart) enum-valued `destination_` of either 
`FLOWFILE_ATTRIBUTE` or `FLOWFILE_CONTENT` instead of a `bool 
write_to_attribute_`

##########
File path: extensions/standard-processors/processors/AttributesToJSON.cpp
##########
@@ -0,0 +1,162 @@
+/**
+ * @file AttributesToJSON.cpp
+ * AttributesToJSON class implementation
+ *
+ * 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 "AttributesToJSON.h"
+
+#include "rapidjson/writer.h"
+#include "utils/StringUtils.h"
+#include "utils/ProcessorConfigUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+const std::set<std::string> 
AttributesToJSON::DESTINATIONS({"flowfile-attribute", "flowfile-content"});
+
+const core::Property AttributesToJSON::AttributesList(
+  core::PropertyBuilder::createProperty("Attributes List")
+    ->withDescription("Comma separated list of attributes to be included in 
the resulting JSON. "
+                      "If this value is left empty then all existing 
Attributes will be included. This list of attributes is case sensitive. "
+                      "If an attribute specified in the list is not found it 
will be be emitted to the resulting JSON with an empty string or NULL value.")
+    ->build());
+
+const core::Property AttributesToJSON::AttributesRegularExpression(
+  core::PropertyBuilder::createProperty("Attributes Regular Expression")
+    ->withDescription("Regular expression that will be evaluated against the 
flow file attributes to select the matching attributes. "
+                      "This property can be used in combination with the 
attributes list property.")
+    ->build());
+
+const core::Property AttributesToJSON::Destination(
+  core::PropertyBuilder::createProperty("Destination")
+    ->withDescription("Control if JSON value is written as a new flowfile 
attribute 'JSONAttributes' or written in the flowfile content. "
+                      "Writing to flowfile content will overwrite any existing 
flowfile content.")
+    ->isRequired(true)
+    ->withDefaultValue<std::string>("flowfile-attribute")
+    ->withAllowableValues<std::string>(DESTINATIONS)
+    ->build());
+
+const core::Property AttributesToJSON::IncludeCoreAttributes(
+  core::PropertyBuilder::createProperty("Include Core Attributes")
+    ->withDescription("Determines if the FlowFile core attributes which are 
contained in every FlowFile should be included in the final JSON value 
generated.")
+    ->isRequired(true)
+    ->withDefaultValue<bool>(true)
+    ->build());
+
+const core::Property AttributesToJSON::NullValue(
+  core::PropertyBuilder::createProperty("Null Value")
+    ->withDescription("If true a non existing or empty attribute will be NULL 
in the resulting JSON. If false an empty string will be placed in the JSON.")
+    ->isRequired(true)
+    ->withDefaultValue<bool>(false)
+    ->build());
+
+core::Relationship AttributesToJSON::Success("success", "All FlowFiles 
received are routed to success");
+
+void AttributesToJSON::initialize() {
+  setSupportedProperties({
+    AttributesList,
+    AttributesRegularExpression,
+    Destination,
+    IncludeCoreAttributes,
+    NullValue
+  });
+  setSupportedRelationships({Success});
+}
+
+void AttributesToJSON::onSchedule(core::ProcessContext* context, 
core::ProcessSessionFactory* /*sessionFactory*/) {
+  std::string attributes;
+  context->getProperty(AttributesList.getName(), attributes);
+  attribute_list_ = utils::StringUtils::splitRemovingEmpty(attributes, ",");
+  context->getProperty(AttributesRegularExpression.getName(), 
attributes_regular_expression_str_);
+  if (!attributes_regular_expression_str_.empty()) {
+    attributes_regular_expression_ = 
std::regex(attributes_regular_expression_str_);
+  }
+  write_to_attribute_ = 
utils::parsePropertyWithAllowableValuesOrThrow(*context, Destination.getName(), 
DESTINATIONS) == "flowfile-attribute";
+  context->getProperty(IncludeCoreAttributes.getName(), 
include_core_attributes_);
+  context->getProperty(NullValue.getName(), null_value_);
+}
+
+bool AttributesToJSON::isCoreAttributeToBeFiltered(const std::string& 
attribute) const {
+  return !include_core_attributes_ && core_attributes_.find(attribute) != 
core_attributes_.end();
+}
+
+bool AttributesToJSON::matchesAttributeRegex(const std::string& attribute) {
+  return attributes_regular_expression_str_.empty() || 
std::regex_search(attribute, attributes_regular_expression_);
+}
+
+void AttributesToJSON::addAttributeToJson(rapidjson::Document& document, const 
std::string& key, const std::string& value) {
+  if (isCoreAttributeToBeFiltered(key)) {
+    logger_->log_debug("Core attribute '%s' will not be included in the 
attributes JSON.", key);
+    return;
+  }
+  if (!matchesAttributeRegex(key)) {
+    logger_->log_debug("Attribute '%s' does not match the set regex, therefore 
it will not be included in the attributes JSON.", key);
+    return;
+  }
+  rapidjson::Value json_key(key.c_str(), document.GetAllocator());
+  rapidjson::Value json_val;
+  if (!value.empty() || !null_value_) {
+    json_val.SetString(value.c_str(), document.GetAllocator());
+  }
+  document.AddMember(json_key, json_val, document.GetAllocator());
+}
+
+std::string AttributesToJSON::buildAttributeJsonData(std::map<std::string, 
std::string>&& attributes) {
+  auto root = rapidjson::Document(rapidjson::kObjectType);
+  if (!attribute_list_.empty()) {
+    for (const auto& attribute : attribute_list_) {
+      addAttributeToJson(root, attribute, attributes[attribute]);
+    }
+  } else {
+    for (const auto& kvp : attributes) {

Review comment:
       I haven't tried this, but I think we can use structured bindings now:
   ```suggestion
       for (const auto& [key, value] : attributes) {
   ```

##########
File path: extensions/standard-processors/processors/AttributesToJSON.h
##########
@@ -0,0 +1,103 @@
+/**
+ * @file AttributesToJSON.h
+ * AttributesToJSON class declaration
+ *
+ * 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.
+ */
+#pragma once
+
+#include <vector>
+#include <string>
+#include <set>
+#include <unordered_set>
+#include <memory>
+#include <map>
+#include <regex>
+
+#include "rapidjson/document.h"
+#include "core/Processor.h"
+#include "core/Property.h"
+#include "core/logging/Logger.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+class AttributesToJSON : public core::Processor {
+ public:
+  static const std::set<std::string> DESTINATIONS;
+
+  explicit AttributesToJSON(const std::string& name, const utils::Identifier& 
uuid = {})
+      : core::Processor(name, uuid),
+        logger_(logging::LoggerFactory<AttributesToJSON>::getLogger()),
+        
core_attributes_(core::SpecialFlowAttribute::getSpecialFlowAttributes()) {
+  }
+  static constexpr char const* ProcessorName = "AttributesToJSON";

Review comment:
       `ProcessorName` is not used anywhere.  I know almost all processors have 
it, but we should remove it at some point, and not add it to new processors.

##########
File path: extensions/standard-processors/processors/AttributesToJSON.cpp
##########
@@ -0,0 +1,162 @@
+/**
+ * @file AttributesToJSON.cpp
+ * AttributesToJSON class implementation
+ *
+ * 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 "AttributesToJSON.h"
+
+#include "rapidjson/writer.h"
+#include "utils/StringUtils.h"
+#include "utils/ProcessorConfigUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+const std::set<std::string> 
AttributesToJSON::DESTINATIONS({"flowfile-attribute", "flowfile-content"});
+
+const core::Property AttributesToJSON::AttributesList(
+  core::PropertyBuilder::createProperty("Attributes List")
+    ->withDescription("Comma separated list of attributes to be included in 
the resulting JSON. "
+                      "If this value is left empty then all existing 
Attributes will be included. This list of attributes is case sensitive. "
+                      "If an attribute specified in the list is not found it 
will be be emitted to the resulting JSON with an empty string or NULL value.")
+    ->build());
+
+const core::Property AttributesToJSON::AttributesRegularExpression(
+  core::PropertyBuilder::createProperty("Attributes Regular Expression")
+    ->withDescription("Regular expression that will be evaluated against the 
flow file attributes to select the matching attributes. "
+                      "This property can be used in combination with the 
attributes list property.")
+    ->build());
+
+const core::Property AttributesToJSON::Destination(
+  core::PropertyBuilder::createProperty("Destination")
+    ->withDescription("Control if JSON value is written as a new flowfile 
attribute 'JSONAttributes' or written in the flowfile content. "
+                      "Writing to flowfile content will overwrite any existing 
flowfile content.")
+    ->isRequired(true)
+    ->withDefaultValue<std::string>("flowfile-attribute")
+    ->withAllowableValues<std::string>(DESTINATIONS)
+    ->build());
+
+const core::Property AttributesToJSON::IncludeCoreAttributes(
+  core::PropertyBuilder::createProperty("Include Core Attributes")
+    ->withDescription("Determines if the FlowFile core attributes which are 
contained in every FlowFile should be included in the final JSON value 
generated.")
+    ->isRequired(true)
+    ->withDefaultValue<bool>(true)
+    ->build());
+
+const core::Property AttributesToJSON::NullValue(
+  core::PropertyBuilder::createProperty("Null Value")
+    ->withDescription("If true a non existing or empty attribute will be NULL 
in the resulting JSON. If false an empty string will be placed in the JSON.")
+    ->isRequired(true)
+    ->withDefaultValue<bool>(false)
+    ->build());
+
+core::Relationship AttributesToJSON::Success("success", "All FlowFiles 
received are routed to success");
+
+void AttributesToJSON::initialize() {
+  setSupportedProperties({
+    AttributesList,
+    AttributesRegularExpression,
+    Destination,
+    IncludeCoreAttributes,
+    NullValue
+  });
+  setSupportedRelationships({Success});
+}
+
+void AttributesToJSON::onSchedule(core::ProcessContext* context, 
core::ProcessSessionFactory* /*sessionFactory*/) {
+  std::string attributes;
+  context->getProperty(AttributesList.getName(), attributes);
+  attribute_list_ = utils::StringUtils::splitRemovingEmpty(attributes, ",");
+  context->getProperty(AttributesRegularExpression.getName(), 
attributes_regular_expression_str_);
+  if (!attributes_regular_expression_str_.empty()) {
+    attributes_regular_expression_ = 
std::regex(attributes_regular_expression_str_);
+  }
+  write_to_attribute_ = 
utils::parsePropertyWithAllowableValuesOrThrow(*context, Destination.getName(), 
DESTINATIONS) == "flowfile-attribute";
+  context->getProperty(IncludeCoreAttributes.getName(), 
include_core_attributes_);
+  context->getProperty(NullValue.getName(), null_value_);
+}
+
+bool AttributesToJSON::isCoreAttributeToBeFiltered(const std::string& 
attribute) const {
+  return !include_core_attributes_ && core_attributes_.find(attribute) != 
core_attributes_.end();
+}
+
+bool AttributesToJSON::matchesAttributeRegex(const std::string& attribute) {
+  return attributes_regular_expression_str_.empty() || 
std::regex_search(attribute, attributes_regular_expression_);
+}
+
+void AttributesToJSON::addAttributeToJson(rapidjson::Document& document, const 
std::string& key, const std::string& value) {
+  if (isCoreAttributeToBeFiltered(key)) {
+    logger_->log_debug("Core attribute '%s' will not be included in the 
attributes JSON.", key);
+    return;
+  }
+  if (!matchesAttributeRegex(key)) {
+    logger_->log_debug("Attribute '%s' does not match the set regex, therefore 
it will not be included in the attributes JSON.", key);
+    return;
+  }
+  rapidjson::Value json_key(key.c_str(), document.GetAllocator());
+  rapidjson::Value json_val;
+  if (!value.empty() || !null_value_) {
+    json_val.SetString(value.c_str(), document.GetAllocator());
+  }
+  document.AddMember(json_key, json_val, document.GetAllocator());
+}
+
+std::string AttributesToJSON::buildAttributeJsonData(std::map<std::string, 
std::string>&& attributes) {
+  auto root = rapidjson::Document(rapidjson::kObjectType);
+  if (!attribute_list_.empty()) {
+    for (const auto& attribute : attribute_list_) {
+      addAttributeToJson(root, attribute, attributes[attribute]);
+    }
+  } else {
+    for (const auto& kvp : attributes) {
+      addAttributeToJson(root, kvp.first, kvp.second);
+    }
+  }

Review comment:
       So if both a list and a regex is given, we select an attribute if it is 
in the list **and** matches the regex?  I find that surprising, I would have 
expected **or**.  Is this how NiFi works?

##########
File path: libminifi/include/core/FlowFile.h
##########
@@ -310,6 +311,10 @@ struct SpecialFlowAttribute {
   static const std::string ALTERNATE_IDENTIFIER;
   // Flow identifier
   static const std::string FLOW_ID;
+
+  static std::unordered_set<std::string> getSpecialFlowAttributes() {
+    return {PATH, ABSOLUTE_PATH, FILENAME, UUID, priority, MIME_TYPE, 
DISCARD_REASON, ALTERNATE_IDENTIFIER, FLOW_ID};
+  }

Review comment:
       if it doesn't require too many changes, I would prefer to convert 
`SpecialFlowAttribute` to a `SMART_ENUM` and use its `values()` method instead 
of adding this

##########
File path: extensions/standard-processors/tests/unit/AttributesToJSONTests.cpp
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "TestBase.h"
+#include "utils/TestUtils.h"
+#include "AttributesToJSON.h"
+#include "GetFile.h"
+#include "PutFile.h"
+#include "UpdateAttribute.h"
+#include "LogAttribute.h"
+
+namespace {
+
+class AttributesToJSONTestFixture {
+ public:
+  const std::string TEST_FILE_CONTENT = "test_content";
+  const std::string TEST_FILE_NAME = "tstFile.ext";
+
+  AttributesToJSONTestFixture() {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setDebug<minifi::processors::AttributesToJSON>();
+    LogTestController::getInstance().setDebug<minifi::processors::GetFile>();
+    LogTestController::getInstance().setDebug<minifi::processors::PutFile>();
+    
LogTestController::getInstance().setDebug<minifi::processors::UpdateAttribute>();
+    
LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
+
+    dir_ = test_controller_.createTempDirectory();
+
+    plan_ = test_controller_.createPlan();
+    getfile_ = plan_->addProcessor("GetFile", "GetFile");
+    update_attribute_ = plan_->addProcessor("UpdateAttribute", 
"UpdateAttribute", core::Relationship("success", "description"), true);
+    attribute_to_json_ = plan_->addProcessor("AttributesToJSON", 
"AttributesToJSON", core::Relationship("success", "description"), true);
+    logattribute_ = plan_->addProcessor("LogAttribute", "LogAttribute", 
core::Relationship("success", "description"), true);
+    putfile_ = plan_->addProcessor("PutFile", "PutFile", 
core::Relationship("success", "description"), true);
+
+    plan_->setProperty(getfile_, 
org::apache::nifi::minifi::processors::GetFile::Directory.getName(), dir_);
+    plan_->setProperty(putfile_, 
org::apache::nifi::minifi::processors::PutFile::Directory.getName(), dir_);
+
+    update_attribute_->setDynamicProperty("my_attribute", "my_value");
+    update_attribute_->setDynamicProperty("other_attribute", "other_value");
+    update_attribute_->setDynamicProperty("empty_attribute", "");
+
+    std::fstream file;
+    std::stringstream ss;
+    ss << dir_ << utils::file::FileUtils::get_separator() << TEST_FILE_NAME;
+    file.open(ss.str(), std::ios::out);
+    file << TEST_FILE_CONTENT;
+    file.close();
+  }
+
+  std::string escapeJson(const std::string& json) const {
+    rapidjson::StringBuffer buffer;
+    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+    writer.String(json.c_str(), json.size());
+    return buffer.GetString();
+  }
+
+  std::vector<std::string> getOutputFileContents() {
+    std::vector<std::string> file_contents;
+
+    auto callback = [&file_contents](const std::string& path, const 
std::string& filename) -> bool {
+      std::ifstream is(path + utils::file::FileUtils::get_separator() + 
filename, std::ifstream::binary);
+      std::string file_content((std::istreambuf_iterator<char>(is)), 
std::istreambuf_iterator<char>());
+      file_contents.push_back(file_content);
+      return true;
+    };
+
+    utils::file::FileUtils::list_dir(dir_, callback, plan_->getLogger(), 
false);
+
+    return file_contents;
+  }
+
+ protected:
+  TestController test_controller_;
+  std::shared_ptr<TestPlan> plan_;
+  std::string dir_;
+  std::shared_ptr<core::Processor> getfile_;
+  std::shared_ptr<core::Processor> update_attribute_;
+  std::shared_ptr<core::Processor> attribute_to_json_;
+  std::shared_ptr<core::Processor> logattribute_;
+  std::shared_ptr<core::Processor> putfile_;
+};
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move all attributes to a 
flowfile attribute", "[AttributesToJSONTests]") {
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());

Review comment:
       Why do we check just the size?  I would replace this with 
`file_contents[0] == TEST_FILE_CONTENT` everywhere.  The performance difference 
must be tiny.

##########
File path: extensions/standard-processors/tests/unit/AttributesToJSONTests.cpp
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "TestBase.h"
+#include "utils/TestUtils.h"
+#include "AttributesToJSON.h"
+#include "GetFile.h"
+#include "PutFile.h"
+#include "UpdateAttribute.h"
+#include "LogAttribute.h"
+
+namespace {
+
+class AttributesToJSONTestFixture {
+ public:
+  const std::string TEST_FILE_CONTENT = "test_content";
+  const std::string TEST_FILE_NAME = "tstFile.ext";
+
+  AttributesToJSONTestFixture() {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setDebug<minifi::processors::AttributesToJSON>();
+    LogTestController::getInstance().setDebug<minifi::processors::GetFile>();
+    LogTestController::getInstance().setDebug<minifi::processors::PutFile>();
+    
LogTestController::getInstance().setDebug<minifi::processors::UpdateAttribute>();
+    
LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
+
+    dir_ = test_controller_.createTempDirectory();
+
+    plan_ = test_controller_.createPlan();
+    getfile_ = plan_->addProcessor("GetFile", "GetFile");
+    update_attribute_ = plan_->addProcessor("UpdateAttribute", 
"UpdateAttribute", core::Relationship("success", "description"), true);
+    attribute_to_json_ = plan_->addProcessor("AttributesToJSON", 
"AttributesToJSON", core::Relationship("success", "description"), true);
+    logattribute_ = plan_->addProcessor("LogAttribute", "LogAttribute", 
core::Relationship("success", "description"), true);
+    putfile_ = plan_->addProcessor("PutFile", "PutFile", 
core::Relationship("success", "description"), true);
+
+    plan_->setProperty(getfile_, 
org::apache::nifi::minifi::processors::GetFile::Directory.getName(), dir_);
+    plan_->setProperty(putfile_, 
org::apache::nifi::minifi::processors::PutFile::Directory.getName(), dir_);
+
+    update_attribute_->setDynamicProperty("my_attribute", "my_value");
+    update_attribute_->setDynamicProperty("other_attribute", "other_value");
+    update_attribute_->setDynamicProperty("empty_attribute", "");
+
+    std::fstream file;
+    std::stringstream ss;
+    ss << dir_ << utils::file::FileUtils::get_separator() << TEST_FILE_NAME;
+    file.open(ss.str(), std::ios::out);
+    file << TEST_FILE_CONTENT;
+    file.close();
+  }
+
+  std::string escapeJson(const std::string& json) const {
+    rapidjson::StringBuffer buffer;
+    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+    writer.String(json.c_str(), json.size());
+    return buffer.GetString();
+  }
+
+  std::vector<std::string> getOutputFileContents() {
+    std::vector<std::string> file_contents;
+
+    auto callback = [&file_contents](const std::string& path, const 
std::string& filename) -> bool {
+      std::ifstream is(path + utils::file::FileUtils::get_separator() + 
filename, std::ifstream::binary);
+      std::string file_content((std::istreambuf_iterator<char>(is)), 
std::istreambuf_iterator<char>());

Review comment:
       why is the `()` around `std::istreambuf_iterator<char>(is)` needed?

##########
File path: extensions/standard-processors/tests/unit/AttributesToJSONTests.cpp
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "TestBase.h"
+#include "utils/TestUtils.h"
+#include "AttributesToJSON.h"
+#include "GetFile.h"
+#include "PutFile.h"
+#include "UpdateAttribute.h"
+#include "LogAttribute.h"
+
+namespace {
+
+class AttributesToJSONTestFixture {
+ public:
+  const std::string TEST_FILE_CONTENT = "test_content";
+  const std::string TEST_FILE_NAME = "tstFile.ext";
+
+  AttributesToJSONTestFixture() {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setDebug<minifi::processors::AttributesToJSON>();
+    LogTestController::getInstance().setDebug<minifi::processors::GetFile>();
+    LogTestController::getInstance().setDebug<minifi::processors::PutFile>();
+    
LogTestController::getInstance().setDebug<minifi::processors::UpdateAttribute>();
+    
LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
+
+    dir_ = test_controller_.createTempDirectory();
+
+    plan_ = test_controller_.createPlan();
+    getfile_ = plan_->addProcessor("GetFile", "GetFile");
+    update_attribute_ = plan_->addProcessor("UpdateAttribute", 
"UpdateAttribute", core::Relationship("success", "description"), true);
+    attribute_to_json_ = plan_->addProcessor("AttributesToJSON", 
"AttributesToJSON", core::Relationship("success", "description"), true);
+    logattribute_ = plan_->addProcessor("LogAttribute", "LogAttribute", 
core::Relationship("success", "description"), true);
+    putfile_ = plan_->addProcessor("PutFile", "PutFile", 
core::Relationship("success", "description"), true);
+
+    plan_->setProperty(getfile_, 
org::apache::nifi::minifi::processors::GetFile::Directory.getName(), dir_);
+    plan_->setProperty(putfile_, 
org::apache::nifi::minifi::processors::PutFile::Directory.getName(), dir_);
+
+    update_attribute_->setDynamicProperty("my_attribute", "my_value");
+    update_attribute_->setDynamicProperty("other_attribute", "other_value");
+    update_attribute_->setDynamicProperty("empty_attribute", "");
+
+    std::fstream file;
+    std::stringstream ss;
+    ss << dir_ << utils::file::FileUtils::get_separator() << TEST_FILE_NAME;
+    file.open(ss.str(), std::ios::out);
+    file << TEST_FILE_CONTENT;
+    file.close();
+  }
+
+  std::string escapeJson(const std::string& json) const {
+    rapidjson::StringBuffer buffer;
+    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+    writer.String(json.c_str(), json.size());
+    return buffer.GetString();
+  }
+
+  std::vector<std::string> getOutputFileContents() {
+    std::vector<std::string> file_contents;
+
+    auto callback = [&file_contents](const std::string& path, const 
std::string& filename) -> bool {
+      std::ifstream is(path + utils::file::FileUtils::get_separator() + 
filename, std::ifstream::binary);
+      std::string file_content((std::istreambuf_iterator<char>(is)), 
std::istreambuf_iterator<char>());
+      file_contents.push_back(file_content);
+      return true;
+    };
+
+    utils::file::FileUtils::list_dir(dir_, callback, plan_->getLogger(), 
false);
+
+    return file_contents;
+  }
+
+ protected:
+  TestController test_controller_;
+  std::shared_ptr<TestPlan> plan_;
+  std::string dir_;
+  std::shared_ptr<core::Processor> getfile_;
+  std::shared_ptr<core::Processor> update_attribute_;
+  std::shared_ptr<core::Processor> attribute_to_json_;
+  std::shared_ptr<core::Processor> logattribute_;
+  std::shared_ptr<core::Processor> putfile_;
+};
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move all attributes to a 
flowfile attribute", "[AttributesToJSONTests]") {
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  std::string expected_json = "{\"absolute.path\":" + escapeJson(dir_ + 
utils::file::FileUtils::get_separator() + TEST_FILE_NAME) + 
",\"empty_attribute\":\"\",\"filename\":" + escapeJson(TEST_FILE_NAME) + 
",\"flow.id\":\"test\",\"my_attribute\":\"my_value\",\"other_attribute\":\"other_value\",\"path\":"
 + escapeJson(dir_ + utils::file::FileUtils::get_separator()) + "}";  // NOLINT
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:" + expected_json));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move selected attributes to a 
flowfile attribute", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "my_attribute,non_existent_attribute");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:{\"my_attribute\":\"my_value\",\"non_existent_attribute\":\"\"}"));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move selected attributes with 
special characters to a flowfile attribute", "[AttributesToJSONTests]") {
+  update_attribute_->setDynamicProperty("special_attribute", "\\\"");
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "special_attribute");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  std::string expected_json = "{\"special_attribute\":" + escapeJson("\\\"") + 
"}";
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:" + expected_json));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Non-existent or empty selected 
attributes shall be written as null in JSON", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "my_attribute,non_existent_attribute,empty_attribute");
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::NullValue.getName(), 
"true");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:{\"my_attribute\":\"my_value\",\"non_existent_attribute\":null,\"empty_attribute\":null}"));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "All non-existent or empty 
attributes shall be written as null in JSON", "[AttributesToJSONTests]") {

Review comment:
       non-existent attributes are not written in this test case
   ```suggestion
   TEST_CASE_METHOD(AttributesToJSONTestFixture, "All empty attributes shall be 
written as null in JSON", "[AttributesToJSONTests]") {
   ```

##########
File path: extensions/standard-processors/processors/AttributesToJSON.h
##########
@@ -0,0 +1,103 @@
+/**
+ * @file AttributesToJSON.h
+ * AttributesToJSON class declaration
+ *
+ * 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.
+ */
+#pragma once
+
+#include <vector>
+#include <string>
+#include <set>
+#include <unordered_set>
+#include <memory>
+#include <map>
+#include <regex>
+
+#include "rapidjson/document.h"
+#include "core/Processor.h"
+#include "core/Property.h"
+#include "core/logging/Logger.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+class AttributesToJSON : public core::Processor {
+ public:
+  static const std::set<std::string> DESTINATIONS;
+
+  explicit AttributesToJSON(const std::string& name, const utils::Identifier& 
uuid = {})
+      : core::Processor(name, uuid),
+        logger_(logging::LoggerFactory<AttributesToJSON>::getLogger()),
+        
core_attributes_(core::SpecialFlowAttribute::getSpecialFlowAttributes()) {
+  }
+  static constexpr char const* ProcessorName = "AttributesToJSON";
+  // Supported Properties
+  static const core::Property AttributesList;
+  static const core::Property AttributesRegularExpression;
+  static const core::Property Destination;
+  static const core::Property IncludeCoreAttributes;
+  static const core::Property NullValue;
+
+  // Supported Relationships
+  static core::Relationship Success;
+
+  void initialize() override;
+  void onSchedule(core::ProcessContext *context, core::ProcessSessionFactory* 
sessionFactory) override;
+  void onTrigger(core::ProcessContext *context, core::ProcessSession *session) 
override;
+
+  core::annotation::Input getInputRequirement() const override {
+    return core::annotation::Input::INPUT_REQUIRED;
+  }
+
+ private:
+  class WriteCallback : public OutputStreamCallback {
+   public:
+    explicit WriteCallback(const std::string& json_data) : 
json_data_(json_data) {}
+    int64_t process(const std::shared_ptr<io::BaseStream>& stream) override {
+      const auto write_ret = stream->write(reinterpret_cast<const 
uint8_t*>(json_data_.data()), json_data_.length());
+      return io::isError(write_ret) ? -1 : gsl::narrow<int64_t>(write_ret);
+    }
+   private:
+    std::string json_data_;
+  };
+
+  bool isCoreAttributeToBeFiltered(const std::string& attribute) const;
+  bool matchesAttributeRegex(const std::string& attribute);
+  void addAttributeToJson(rapidjson::Document& document, const std::string& 
key, const std::string& value);
+  std::string buildAttributeJsonData(std::map<std::string, std::string>&& 
attributes);
+
+  std::shared_ptr<logging::Logger> logger_;
+  const std::unordered_set<std::string> core_attributes_;
+  std::vector<std::string> attribute_list_;
+  std::string attributes_regular_expression_str_;
+  std::regex attributes_regular_expression_;

Review comment:
       I would remove `attributes_regular_expression_str_` and change the type 
of `attributes_regular_expression_` to `std::optional<std::regex>`

##########
File path: extensions/standard-processors/tests/unit/AttributesToJSONTests.cpp
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "TestBase.h"
+#include "utils/TestUtils.h"
+#include "AttributesToJSON.h"
+#include "GetFile.h"
+#include "PutFile.h"
+#include "UpdateAttribute.h"
+#include "LogAttribute.h"
+
+namespace {
+
+class AttributesToJSONTestFixture {
+ public:
+  const std::string TEST_FILE_CONTENT = "test_content";
+  const std::string TEST_FILE_NAME = "tstFile.ext";
+
+  AttributesToJSONTestFixture() {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setDebug<minifi::processors::AttributesToJSON>();
+    LogTestController::getInstance().setDebug<minifi::processors::GetFile>();
+    LogTestController::getInstance().setDebug<minifi::processors::PutFile>();
+    
LogTestController::getInstance().setDebug<minifi::processors::UpdateAttribute>();
+    
LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
+
+    dir_ = test_controller_.createTempDirectory();
+
+    plan_ = test_controller_.createPlan();
+    getfile_ = plan_->addProcessor("GetFile", "GetFile");
+    update_attribute_ = plan_->addProcessor("UpdateAttribute", 
"UpdateAttribute", core::Relationship("success", "description"), true);
+    attribute_to_json_ = plan_->addProcessor("AttributesToJSON", 
"AttributesToJSON", core::Relationship("success", "description"), true);
+    logattribute_ = plan_->addProcessor("LogAttribute", "LogAttribute", 
core::Relationship("success", "description"), true);
+    putfile_ = plan_->addProcessor("PutFile", "PutFile", 
core::Relationship("success", "description"), true);
+
+    plan_->setProperty(getfile_, 
org::apache::nifi::minifi::processors::GetFile::Directory.getName(), dir_);
+    plan_->setProperty(putfile_, 
org::apache::nifi::minifi::processors::PutFile::Directory.getName(), dir_);
+
+    update_attribute_->setDynamicProperty("my_attribute", "my_value");
+    update_attribute_->setDynamicProperty("other_attribute", "other_value");
+    update_attribute_->setDynamicProperty("empty_attribute", "");
+
+    std::fstream file;
+    std::stringstream ss;
+    ss << dir_ << utils::file::FileUtils::get_separator() << TEST_FILE_NAME;
+    file.open(ss.str(), std::ios::out);
+    file << TEST_FILE_CONTENT;
+    file.close();
+  }
+
+  std::string escapeJson(const std::string& json) const {
+    rapidjson::StringBuffer buffer;
+    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+    writer.String(json.c_str(), json.size());
+    return buffer.GetString();
+  }
+
+  std::vector<std::string> getOutputFileContents() {
+    std::vector<std::string> file_contents;
+
+    auto callback = [&file_contents](const std::string& path, const 
std::string& filename) -> bool {
+      std::ifstream is(path + utils::file::FileUtils::get_separator() + 
filename, std::ifstream::binary);
+      std::string file_content((std::istreambuf_iterator<char>(is)), 
std::istreambuf_iterator<char>());
+      file_contents.push_back(file_content);
+      return true;
+    };
+
+    utils::file::FileUtils::list_dir(dir_, callback, plan_->getLogger(), 
false);
+
+    return file_contents;
+  }
+
+ protected:
+  TestController test_controller_;
+  std::shared_ptr<TestPlan> plan_;
+  std::string dir_;
+  std::shared_ptr<core::Processor> getfile_;
+  std::shared_ptr<core::Processor> update_attribute_;
+  std::shared_ptr<core::Processor> attribute_to_json_;
+  std::shared_ptr<core::Processor> logattribute_;
+  std::shared_ptr<core::Processor> putfile_;
+};
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move all attributes to a 
flowfile attribute", "[AttributesToJSONTests]") {
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  std::string expected_json = "{\"absolute.path\":" + escapeJson(dir_ + 
utils::file::FileUtils::get_separator() + TEST_FILE_NAME) + 
",\"empty_attribute\":\"\",\"filename\":" + escapeJson(TEST_FILE_NAME) + 
",\"flow.id\":\"test\",\"my_attribute\":\"my_value\",\"other_attribute\":\"other_value\",\"path\":"
 + escapeJson(dir_ + utils::file::FileUtils::get_separator()) + "}";  // NOLINT
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:" + expected_json));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move selected attributes to a 
flowfile attribute", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "my_attribute,non_existent_attribute");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:{\"my_attribute\":\"my_value\",\"non_existent_attribute\":\"\"}"));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Move selected attributes with 
special characters to a flowfile attribute", "[AttributesToJSONTests]") {
+  update_attribute_->setDynamicProperty("special_attribute", "\\\"");
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "special_attribute");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  std::string expected_json = "{\"special_attribute\":" + escapeJson("\\\"") + 
"}";
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:" + expected_json));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "Non-existent or empty selected 
attributes shall be written as null in JSON", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::AttributesList.getName(),
 "my_attribute,non_existent_attribute,empty_attribute");
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::NullValue.getName(), 
"true");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:{\"my_attribute\":\"my_value\",\"non_existent_attribute\":null,\"empty_attribute\":null}"));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "All non-existent or empty 
attributes shall be written as null in JSON", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::NullValue.getName(), 
"true");
+  test_controller_.runSession(plan_);
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == TEST_FILE_CONTENT.size());
+  std::string expected_json = "{\"absolute.path\":" + escapeJson(dir_ + 
utils::file::FileUtils::get_separator() + TEST_FILE_NAME) + 
",\"empty_attribute\":null,\"filename\":" + escapeJson(TEST_FILE_NAME) + 
",\"flow.id\":\"test\",\"my_attribute\":\"my_value\",\"other_attribute\":\"other_value\",\"path\":"
 + escapeJson(dir_ + utils::file::FileUtils::get_separator()) + "}";  // NOLINT
+  REQUIRE(LogTestController::getInstance().contains("key:JSONAttributes 
value:" + expected_json));
+}
+
+TEST_CASE_METHOD(AttributesToJSONTestFixture, "JSON attributes are written in 
flowfile", "[AttributesToJSONTests]") {
+  plan_->setProperty(attribute_to_json_, 
org::apache::nifi::minifi::processors::AttributesToJSON::Destination.getName(), 
"flowfile-content");
+  test_controller_.runSession(plan_);
+  std::string expected_content = "{\"absolute.path\":" + escapeJson(dir_ + 
utils::file::FileUtils::get_separator() + TEST_FILE_NAME) + 
",\"empty_attribute\":\"\",\"filename\":" + escapeJson(TEST_FILE_NAME) + 
",\"flow.id\":\"test\",\"my_attribute\":\"my_value\",\"other_attribute\":\"other_value\",\"path\":"
 + escapeJson(dir_ + utils::file::FileUtils::get_separator()) + "}";  // NOLINT
+
+  auto file_contents = getOutputFileContents();
+
+  REQUIRE(file_contents.size() == 1);
+  REQUIRE(file_contents[0].size() == expected_content.size());

Review comment:
       I would remove this line

##########
File path: extensions/standard-processors/tests/unit/AttributesToJSONTests.cpp
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "TestBase.h"
+#include "utils/TestUtils.h"
+#include "AttributesToJSON.h"
+#include "GetFile.h"
+#include "PutFile.h"
+#include "UpdateAttribute.h"
+#include "LogAttribute.h"
+
+namespace {
+
+class AttributesToJSONTestFixture {
+ public:
+  const std::string TEST_FILE_CONTENT = "test_content";
+  const std::string TEST_FILE_NAME = "tstFile.ext";
+
+  AttributesToJSONTestFixture() {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setDebug<minifi::processors::AttributesToJSON>();
+    LogTestController::getInstance().setDebug<minifi::processors::GetFile>();
+    LogTestController::getInstance().setDebug<minifi::processors::PutFile>();
+    
LogTestController::getInstance().setDebug<minifi::processors::UpdateAttribute>();
+    
LogTestController::getInstance().setDebug<minifi::processors::LogAttribute>();
+
+    dir_ = test_controller_.createTempDirectory();
+
+    plan_ = test_controller_.createPlan();
+    getfile_ = plan_->addProcessor("GetFile", "GetFile");
+    update_attribute_ = plan_->addProcessor("UpdateAttribute", 
"UpdateAttribute", core::Relationship("success", "description"), true);
+    attribute_to_json_ = plan_->addProcessor("AttributesToJSON", 
"AttributesToJSON", core::Relationship("success", "description"), true);
+    logattribute_ = plan_->addProcessor("LogAttribute", "LogAttribute", 
core::Relationship("success", "description"), true);
+    putfile_ = plan_->addProcessor("PutFile", "PutFile", 
core::Relationship("success", "description"), true);
+
+    plan_->setProperty(getfile_, 
org::apache::nifi::minifi::processors::GetFile::Directory.getName(), dir_);
+    plan_->setProperty(putfile_, 
org::apache::nifi::minifi::processors::PutFile::Directory.getName(), dir_);
+
+    update_attribute_->setDynamicProperty("my_attribute", "my_value");
+    update_attribute_->setDynamicProperty("other_attribute", "other_value");
+    update_attribute_->setDynamicProperty("empty_attribute", "");
+
+    std::fstream file;
+    std::stringstream ss;
+    ss << dir_ << utils::file::FileUtils::get_separator() << TEST_FILE_NAME;
+    file.open(ss.str(), std::ios::out);
+    file << TEST_FILE_CONTENT;
+    file.close();

Review comment:
       you can use `putFileToDir()` in `TestUtils.h` for this




-- 
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