From f3e3bd021be0850dbc2da0fe723f7e1992ac4eee Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Wed, 20 Jan 2021 22:15:19 +0000 Subject: [PATCH] feat: implement pubsub_unit_test example --- examples/CMakeLists.txt | 10 ++- examples/site/testing_pubsub/CMakeLists.txt | 50 +++++++++++++ .../site/testing_pubsub/pubsub_unit_test.cc | 75 +++++++++++++++++++ 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 examples/site/testing_pubsub/CMakeLists.txt create mode 100644 examples/site/testing_pubsub/pubsub_unit_test.cc diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 956ee5f7..26751b58 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -66,10 +66,16 @@ if (BUILD_TESTING) add_executable("${target}" ${fname}) target_link_libraries( ${target} - PRIVATE functions_framework_examples googleapis_functions_framework - Boost::log GTest::gmock_main GTest::gmock GTest::gtest) + 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 () endif () add_subdirectory(site/testing_http) +add_subdirectory(site/testing_pubsub) diff --git a/examples/site/testing_pubsub/CMakeLists.txt b/examples/site/testing_pubsub/CMakeLists.txt new file mode 100644 index 00000000..18ffec2e --- /dev/null +++ b/examples/site/testing_pubsub/CMakeLists.txt @@ -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 + pubsub_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 () diff --git a/examples/site/testing_pubsub/pubsub_unit_test.cc b/examples/site/testing_pubsub/pubsub_unit_test.cc new file mode 100644 index 00000000..45208e4f --- /dev/null +++ b/examples/site/testing_pubsub/pubsub_unit_test.cc @@ -0,0 +1,75 @@ +// 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. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gcf = ::google::cloud::functions; +extern void hello_world_pubsub(gcf::CloudEvent event); + +namespace { + +using ::testing::HasSubstr; + +TEST(PubsubUnitTest, Basic) { + auto core = boost::log::core::get(); + auto stream = boost::make_shared(); + auto be = [core, stream]() { + auto backend = + boost::make_shared(); + 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(backend); + core->add_sink(be); + return be; + }(); + + struct TestCases { + std::string data; + std::string expected; + } cases[]{ + // The magic string was obtained using: + // /bin/echo -n 'C++' | openssl base64 -e + {R"js({"message": {"data": "Qysr"}})js", "Hello C++"}, + {R"js({"message": {"data": ""}})js", "Hello World"}, + }; + + 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"); + event.set_data(test.data); + stream->str({}); + EXPECT_NO_THROW(hello_world_pubsub(event)); + auto log_lines = stream->str(); + EXPECT_THAT(log_lines, HasSubstr(test.expected)); + } + + core->remove_sink(be); +} + +} // namespace