Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ endif ()

add_subdirectory(site/testing_http)
add_subdirectory(site/testing_pubsub)
add_subdirectory(site/testing_storage)
2 changes: 1 addition & 1 deletion examples/site/hello_world_storage/hello_world_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void hello_world_storage(gcf::CloudEvent event) { // NOLINT
BOOST_LOG_TRIVIAL(info) << "Event: " << event.id();
BOOST_LOG_TRIVIAL(info) << "Event Type: " << event.type();
BOOST_LOG_TRIVIAL(info) << "Bucket: " << payload.value("bucket", "");
BOOST_LOG_TRIVIAL(info) << "File: " << payload.value("name", "");
BOOST_LOG_TRIVIAL(info) << "Object: " << payload.value("name", "");
BOOST_LOG_TRIVIAL(info) << "Metageneration: "
<< payload.value("metageneration", "");
BOOST_LOG_TRIVIAL(info) << "Created: " << payload.value("timeCreated", "");
Expand Down
50 changes: 50 additions & 0 deletions examples/site/testing_storage/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ~~~
# Copyright 2020 Google LLC
#
# Licensed 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.
# ~~~

if (BUILD_TESTING)
find_package(GTest CONFIG REQUIRED)
set(googleapis_functions_framework_examples_unit_tests # cmake-format: sort
storage_unit_test.cc)

set(googleapis_functions_framework_examples_programs # cmake-format: sort
)

foreach (fname ${googleapis_functions_framework_examples_unit_tests})
string(REPLACE "/" "_" target "${fname}")
string(REPLACE ".cc" "" target "${target}")
add_executable("${target}" ${fname})
target_link_libraries(
${target}
PRIVATE functions_framework_examples
googleapis_functions_framework
Boost::filesystem
Boost::log
GTest::gmock_main
GTest::gmock
GTest::gtest)
add_test(NAME ${target} COMMAND ${target})
endforeach ()

foreach (fname ${googleapis_functions_framework_examples_programs})
string(REPLACE "/" "_" target "${fname}")
string(REPLACE ".cc" "" target "${target}")
add_executable("${target}" ${fname})
target_link_libraries(
${target}
PRIVATE functions_framework_examples googleapis_functions_framework
Boost::filesystem Boost::log)
endforeach ()
endif ()
86 changes: 86 additions & 0 deletions examples/site/testing_storage/storage_unit_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2020 Google LLC
//
// Licensed 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.

// [START functions_storage_unit_test]
#include <google/cloud/functions/cloud_event.h>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/shared_ptr.hpp>
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>
#include <memory>
#include <sstream>

namespace gcf = ::google::cloud::functions;
extern void hello_world_storage(gcf::CloudEvent event);
Comment thread
coryan marked this conversation as resolved.

namespace {

using ::testing::HasSubstr;

TEST(StorageUnitTest, Basic) {
auto core = boost::log::core::get();
auto stream = boost::make_shared<std::ostringstream>();
auto be = [core, stream]() {
auto backend =
boost::make_shared<boost::log::sinks::text_ostream_backend>();
backend->add_stream(stream);

// Enable auto-flushing after each log record written
backend->auto_flush(true);
using sink_t = boost::log::sinks::synchronous_sink<
boost::log::sinks::text_ostream_backend>;
auto be = boost::make_shared<sink_t>(backend);
core->add_sink(be);
return be;
}();

struct TestCases {
std::string name;
std::string expected;
} cases[]{
{"object1.txt", "Object: object1.txt"},
{"object/with/longer/name.txt", "Object: object/with/longer/name.txt"},
};

auto const base = nlohmann::json::parse(R"js({
"bucket": "some-bucket",
"name": "--set-later--",
"generation": "1587627537231057",
"contentType": "text/plain",
"timeCreated": "2020-04-23T07:38:57.230Z",
"updated": "2020-04-23T07:38:57.230Z"
})js");

for (auto const& test : cases) {
SCOPED_TRACE("Testing for " + test.expected);
gcf::CloudEvent event(
/*id=*/"test-id-0001", /*source=*/"https://test-source.example.com",
/*type=*/"google.cloud.pubsub.topic.v1.messagePublished");
event.set_data_content_type("application/json");
auto data = base;
data["name"] = test.name;
event.set_data(data.dump());
stream->str({});
EXPECT_NO_THROW(hello_world_storage(event));
auto log_lines = stream->str();
EXPECT_THAT(log_lines, HasSubstr(test.expected));
}

core->remove_sink(be);
}

} // namespace
// [END functions_storage_unit_test]