Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: implement pubsub_unit_test example #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2021
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
10 changes: 8 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
50 changes: 50 additions & 0 deletions examples/site/testing_pubsub/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
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 ()
75 changes: 75 additions & 0 deletions examples/site/testing_pubsub/pubsub_unit_test.cc
Original file line number Diff line number Diff line change
@@ -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 <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_pubsub(gcf::CloudEvent event);

namespace {

using ::testing::HasSubstr;

TEST(PubsubUnitTest, 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 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