Include presence containers in NetconfAccess::getItems

This patch also changes how the datastore test works a little bit. Empty
strings are a good indicator of "nothing". In case of presence
containers, an empty string means that it is present, so I make use of
boost::optional to represent "nothing".

Change-Id: I820d517a21f6ee7457f698ea49ae2e6eb5a7d2de
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index 1e28bd2..fc35be6 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -23,6 +23,14 @@
     for (const auto& it : items) {
         if (!it)
             continue;
+        if (it->schema()->nodetype() == LYS_CONTAINER) {
+            if (libyang::Schema_Node_Container{it->schema()}.presence()) {
+                // The fact that the container is included in the data tree
+                // means that it is present and I don't need to check any
+                // value.
+                res.emplace(it->path(), special_{SpecialValue::PresenceContainer});
+            }
+        }
         if (it->schema()->nodetype() == LYS_LIST) {
             res.emplace(it->path(), special_{SpecialValue::List});
         }
diff --git a/tests/datastore_access.cpp b/tests/datastore_access.cpp
index ff29594..9740db6 100644
--- a/tests/datastore_access.cpp
+++ b/tests/datastore_access.cpp
@@ -19,12 +19,18 @@
 #include "sysrepo_subscription.hpp"
 #include "utils.hpp"
 
-class MockRecorder : public Recorder {
+class MockRecorder : public trompeloeil::mock_interface<Recorder> {
 public:
-    MAKE_MOCK3(write, void(const std::string&, const std::string&, const std::string&), override);
+    IMPLEMENT_MOCK3(write);
 };
 
 namespace std {
+std::ostream& operator<<(std::ostream& s, const std::optional<std::string>& opt)
+{
+    s << (opt ? *opt : "std::nullopt");
+    return s;
+}
+
 std::ostream& operator<<(std::ostream& s, const DatastoreAccess::Tree& map)
 {
     s << std::endl
@@ -51,87 +57,89 @@
 #error "Unknown backend"
 #endif
 
+    using namespace std::literals::string_literals;
+
     SECTION("set leafInt8 to -128")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafInt8", "", "-128"));
+        REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s));
         datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt16 to -32768")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafInt16", "", "-32768"));
+        REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
         datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt32 to -2147483648")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafInt32", "", "-2147483648"));
+        REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
         datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt64 to -50000000000")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafInt64", "", "-50000000000"));
+        REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
         datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt8 to 255")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafUInt8", "", "255"));
+        REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s));
         datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt16 to 65535")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafUInt16", "", "65535"));
+        REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
         datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt32 to 4294967295")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafUInt32", "", "4294967295"));
+        REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
         datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt64 to 50000000000")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafUInt64", "", "50000000000"));
+        REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
         datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
         datastore.commitChanges();
     }
 
     SECTION("set leafEnum to coze")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafEnum", "", "coze"));
+        REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s));
         datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
         datastore.commitChanges();
     }
 
     SECTION("set leafDecimal to 123.544")
     {
-        REQUIRE_CALL(mock, write("/example-schema:leafDecimal", "", "123.544"));
+        REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
         datastore.setLeaf("/example-schema:leafDecimal", 123.544);
         datastore.commitChanges();
     }
 
     SECTION("create presence container")
     {
-        REQUIRE_CALL(mock, write("/example-schema:pContainer", "", ""));
+        REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
         datastore.createPresenceContainer("/example-schema:pContainer");
         datastore.commitChanges();
     }
 
     SECTION("create a list instance")
     {
-        REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", "", ""));
-        REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "", "Nguyen"));
+        REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
+        REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
         datastore.createListInstance("/example-schema:person[name='Nguyen']");
         datastore.commitChanges();
     }
@@ -139,12 +147,12 @@
     SECTION("leafref pointing to a key of a list")
     {
         {
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", "", "Dan"));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", "", "Elfi"));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", "", "Kolafa"));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
             datastore.createListInstance("/example-schema:person[name='Dan']");
             datastore.createListInstance("/example-schema:person[name='Elfi']");
             datastore.createListInstance("/example-schema:person[name='Kolafa']");
@@ -156,21 +164,21 @@
         // SECTION.
         SECTION("Dan")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", "", "Dan"));
+            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Dan"s));
             datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"});
             datastore.commitChanges();
         }
 
         SECTION("Elfi")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", "", "Elfi"));
+            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Elfi"s));
             datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"});
             datastore.commitChanges();
         }
 
         SECTION("Kolafa")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", "", "Kolafa"));
+            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Kolafa"s));
             datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"});
             datastore.commitChanges();
         }
@@ -178,7 +186,7 @@
     SECTION("bool values get correctly represented as bools")
     {
         {
-            REQUIRE_CALL(mock, write("/example-schema:down", "", "true"));
+            REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s));
             datastore.setLeaf("/example-schema:down", bool{true});
             datastore.commitChanges();
         }
@@ -190,8 +198,8 @@
     SECTION("getting items from the whole module")
     {
         {
-            REQUIRE_CALL(mock, write("/example-schema:up", "", "true"));
-            REQUIRE_CALL(mock, write("/example-schema:down", "", "false"));
+            REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s));
+            REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s));
             datastore.setLeaf("/example-schema:up", bool{true});
             datastore.setLeaf("/example-schema:down", bool{false});
             datastore.commitChanges();
@@ -216,7 +224,7 @@
     SECTION("getItems returns correct datatypes")
     {
         {
-            REQUIRE_CALL(mock, write("/example-schema:leafEnum", "", "lol"));
+            REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
             datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
             datastore.commitChanges();
         }
@@ -228,12 +236,12 @@
     SECTION("getItems on a list")
     {
         {
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", "", "Jan"));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", "", "Michal"));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", "", ""));
-            REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", "", "Petr"));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
+            REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
             datastore.createListInstance("/example-schema:person[name='Jan']");
             datastore.createListInstance("/example-schema:person[name='Michal']");
             datastore.createListInstance("/example-schema:person[name='Petr']");
@@ -251,5 +259,32 @@
         REQUIRE(datastore.getItems("/example-schema:person") == expected);
     }
 
+    SECTION("presence containers")
+    {
+        DatastoreAccess::Tree expected;
+        // Make sure it's not there before we create it
+        REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
+
+        {
+            REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
+            datastore.createPresenceContainer("/example-schema:pContainer");
+            datastore.commitChanges();
+        }
+        expected = {
+            {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
+        };
+        REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
+
+        // Make sure it's not there after we delete it
+        {
+            REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
+            datastore.deletePresenceContainer("/example-schema:pContainer");
+            datastore.commitChanges();
+        }
+        expected = {};
+        REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
+
+    }
+
     waitForCompletionAndBitMore(seq1);
 }
diff --git a/tests/mock/sysrepo_subscription.cpp b/tests/mock/sysrepo_subscription.cpp
index 801ec1c..7cd5cf3 100644
--- a/tests/mock/sysrepo_subscription.cpp
+++ b/tests/mock/sysrepo_subscription.cpp
@@ -28,9 +28,11 @@
             return SR_ERR_OK;
 
         while (auto change = sess->get_change_next(it)) {
-            m_recorder->write(change->new_val()->xpath(),
-                              change->old_val() ? change->old_val()->val_to_string() : "",
-                              change->new_val()->val_to_string());
+            auto xpath = (change->new_val() ? change->new_val() : change->old_val())->xpath();
+
+            auto oldValue = change->old_val() ? std::optional{change->old_val()->val_to_string()} : std::nullopt;
+            auto newValue = change->new_val() ? std::optional{change->new_val()->val_to_string()} : std::nullopt;
+            m_recorder->write(xpath, oldValue, newValue);
         }
 
         return SR_ERR_OK;
diff --git a/tests/mock/sysrepo_subscription.hpp b/tests/mock/sysrepo_subscription.hpp
index 36681a6..dce6506 100644
--- a/tests/mock/sysrepo_subscription.hpp
+++ b/tests/mock/sysrepo_subscription.hpp
@@ -8,6 +8,7 @@
 
 #pragma once
 
+#include <optional>
 #include <memory>
 
 namespace sysrepo {
@@ -21,7 +22,7 @@
 class Recorder {
 public:
     virtual ~Recorder();
-    virtual void write(const std::string& xpath, const std::string& oldValue, const std::string& newValue) = 0;
+    virtual void write(const std::string& xpath, const std::optional<std::string>& oldValue, const std::optional<std::string>& newValue) = 0;
 };
 
 class SysrepoSubscription {