|
| 1 | +#include "pubsub_binder.h" |
| 2 | +#include "http_binder.h" |
| 3 | + |
| 4 | +#include <google/protobuf/util/message_differencer.h> |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +namespace cloudevents { |
| 8 | +namespace binding { |
| 9 | + |
| 10 | +using ::google::protobuf::util::MessageDifferencer; |
| 11 | +using ::google::pubsub::v1::PubsubMessage; |
| 12 | +using ::boost::beast::http::message; |
| 13 | +using ::boost::beast::http::string_body; |
| 14 | +using ::io::cloudevents::v1::CloudEvent; |
| 15 | +using ::cloudevents::format::Format; |
| 16 | + |
| 17 | +typedef io::cloudevents::v1::CloudEvent_CloudEventAttribute CeAttr; |
| 18 | +typedef boost::beast::http::request<string_body> HttpRequest; |
| 19 | +typedef boost::beast::http::response<string_body> HttpResponse; |
| 20 | + |
| 21 | +// Setup a valid CloudEvent |
| 22 | +class BindUnbindTest : public ::testing::Test { |
| 23 | + protected: |
| 24 | + void SetUp() override { |
| 25 | + ce.set_id("1"); |
| 26 | + ce.set_source("2"); |
| 27 | + ce.set_spec_version("3"); |
| 28 | + ce.set_type("4"); |
| 29 | + ce.set_binary_data("1010"); |
| 30 | + } |
| 31 | + CloudEvent ce; |
| 32 | +}; |
| 33 | + |
| 34 | +TEST_F(BindUnbindTest, Pubsub_Binary) { |
| 35 | + PubsubBinder pubsub_binder; |
| 36 | + cloudevents_absl::StatusOr<PubsubMessage> bind = pubsub_binder.Bind(ce); |
| 37 | + cloudevents_absl::StatusOr<CloudEvent> unbind = pubsub_binder.Unbind(*bind); |
| 38 | + |
| 39 | + ASSERT_TRUE(unbind.ok()); |
| 40 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 41 | +} |
| 42 | + |
| 43 | +TEST_F(BindUnbindTest, Pubsub_Structured) { |
| 44 | + PubsubBinder pubsub_binder; |
| 45 | + cloudevents_absl::StatusOr<PubsubMessage> bind = pubsub_binder.Bind(ce, Format::kJson); |
| 46 | + cloudevents_absl::StatusOr<CloudEvent> unbind = pubsub_binder.Unbind(*bind); |
| 47 | + |
| 48 | + ASSERT_TRUE(unbind.ok()); |
| 49 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(BindUnbindTest, HttpReq_Binary) { |
| 53 | + HttpReqBinder http_req_binder; |
| 54 | + cloudevents_absl::StatusOr<HttpRequest> bind = http_req_binder.Bind(ce); |
| 55 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_req_binder.Unbind(*bind); |
| 56 | + |
| 57 | + ASSERT_TRUE(unbind.ok()); |
| 58 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(BindUnbindTest, HttpReq_Structured) { |
| 62 | + HttpReqBinder http_req_binder; |
| 63 | + cloudevents_absl::StatusOr<HttpRequest> bind = http_req_binder.Bind(ce, Format::kJson); |
| 64 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_req_binder.Unbind(*bind); |
| 65 | + |
| 66 | + ASSERT_TRUE(unbind.ok()); |
| 67 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(BindUnbindTest, HttpRes_Binary) { |
| 71 | + HttpResBinder http_res_binder; |
| 72 | + cloudevents_absl::StatusOr<HttpResponse> bind = http_res_binder.Bind(ce, Format::kJson); |
| 73 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_res_binder.Unbind(*bind); |
| 74 | + |
| 75 | + ASSERT_TRUE(unbind.ok()); |
| 76 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 77 | +} |
| 78 | + |
| 79 | +TEST_F(BindUnbindTest, HttpRes_Structured) { |
| 80 | + HttpResBinder http_res_binder; |
| 81 | + cloudevents_absl::StatusOr<HttpResponse> bind = http_res_binder.Bind(ce); |
| 82 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_res_binder.Unbind(*bind); |
| 83 | + |
| 84 | + ASSERT_TRUE(unbind.ok()); |
| 85 | + ASSERT_TRUE(MessageDifferencer::Equals(ce, *unbind)); |
| 86 | +} |
| 87 | + |
| 88 | +TEST(UnbindBindTest, Pubsub_Binary) { |
| 89 | + PubsubBinder binder; |
| 90 | + PubsubMessage pubsub_msg; |
| 91 | + (*pubsub_msg.mutable_attributes())["ce-id"] = "1"; |
| 92 | + (*pubsub_msg.mutable_attributes())["ce-source"] = "2"; |
| 93 | + (*pubsub_msg.mutable_attributes())["ce-specversion"] = "3"; |
| 94 | + (*pubsub_msg.mutable_attributes())["ce-type"] = "4"; |
| 95 | + pubsub_msg.set_data("1010"); |
| 96 | + cloudevents_absl::StatusOr<CloudEvent> unbind = binder.Unbind(pubsub_msg); |
| 97 | + cloudevents_absl::StatusOr<PubsubMessage> bind = binder.Bind(*unbind); |
| 98 | + |
| 99 | + ASSERT_TRUE(bind.ok()); |
| 100 | + ASSERT_TRUE(MessageDifferencer::Equals(pubsub_msg, *bind)); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(UnbindBindTest, Pubsub_Structured) { |
| 104 | + PubsubBinder binder; |
| 105 | + PubsubMessage pubsub_msg; |
| 106 | + (*pubsub_msg.mutable_attributes())["content-type"] = "application/cloudevents+json"; |
| 107 | + pubsub_msg.set_data("{\n\t\"data_base64\" : \"1010\",\n\t\"id\" : \"1\",\n\t\"source\" : \"/test\",\n\t\"specversion\" : \"1.0\",\n\t\"type\" : \"test\"\n}"); |
| 108 | + |
| 109 | + cloudevents_absl::StatusOr<CloudEvent> unbind = binder.Unbind(pubsub_msg); |
| 110 | + cloudevents_absl::StatusOr<PubsubMessage> bind = binder.Bind(*unbind, Format::kJson); |
| 111 | + |
| 112 | + ASSERT_TRUE(bind.ok()); |
| 113 | + ASSERT_TRUE(MessageDifferencer::Equals(pubsub_msg, *bind)); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(UnbindBindTest, HttpReq_Binary) { |
| 117 | + HttpReqBinder http_req_binder; |
| 118 | + HttpRequest http_req; |
| 119 | + http_req.base().set("ce-id", "1"); |
| 120 | + http_req.base().set("ce-source", "2"); |
| 121 | + http_req.base().set("ce-specversion", "3"); |
| 122 | + http_req.base().set("ce-type", "4"); |
| 123 | + http_req.body() = "1010"; |
| 124 | + |
| 125 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_req_binder.Unbind(http_req); |
| 126 | + cloudevents_absl::StatusOr<HttpRequest> bind = http_req_binder.Bind(*unbind); |
| 127 | + |
| 128 | + ASSERT_TRUE(bind.ok()); |
| 129 | + ASSERT_EQ(http_req.base()["ce-id"], (*bind).base()["ce-id"]); |
| 130 | + ASSERT_EQ(http_req.base()["ce-source"], (*bind).base()["ce-source"]); |
| 131 | + ASSERT_EQ(http_req.base()["ce-specversion"], (*bind).base()["ce-specversion"]); |
| 132 | + ASSERT_EQ(http_req.base()["ce-type"], (*bind).base()["ce-type"]); |
| 133 | + ASSERT_EQ(http_req.body(), (*bind).body()); |
| 134 | +} |
| 135 | + |
| 136 | +TEST(UnbindBindTest, HttpReq_Structured) { |
| 137 | + HttpReqBinder http_req_binder; |
| 138 | + HttpRequest http_req; |
| 139 | + http_req.base().set("content-type", "application/cloudevents+json"); |
| 140 | + http_req.body() = "{\n\t\"id\" : \"1\",\n\t\"source\" : \"/test\",\n\t\"specversion\" : \"1.0\",\n\t\"type\" : \"test\"\n}"; |
| 141 | + |
| 142 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_req_binder.Unbind(http_req); |
| 143 | + cloudevents_absl::StatusOr<HttpRequest> bind = http_req_binder.Bind(*unbind, Format::kJson); |
| 144 | + |
| 145 | + ASSERT_TRUE(bind.ok()); |
| 146 | + ASSERT_EQ(http_req.base()["content-type"], (*bind).base()["content-type"]); |
| 147 | + ASSERT_EQ(http_req.body(), (*bind).body()); |
| 148 | +} |
| 149 | + |
| 150 | +TEST(UnbindBindTest, HttpRes_Binary) { |
| 151 | + HttpResBinder http_res_binder; |
| 152 | + HttpResponse http_res; |
| 153 | + http_res.base().set("ce-id", "1"); |
| 154 | + http_res.base().set("ce-source", "2"); |
| 155 | + http_res.base().set("ce-specversion", "3"); |
| 156 | + http_res.base().set("ce-type", "4"); |
| 157 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_res_binder.Unbind(http_res); |
| 158 | + cloudevents_absl::StatusOr<HttpResponse> bind = http_res_binder.Bind(*unbind); |
| 159 | + |
| 160 | + ASSERT_TRUE(bind.ok()); |
| 161 | + ASSERT_EQ(http_res.base()["ce-id"], (*bind).base()["ce-id"]); |
| 162 | + ASSERT_EQ(http_res.base()["ce-source"], (*bind).base()["ce-source"]); |
| 163 | + ASSERT_EQ(http_res.base()["ce-specversion"], (*bind).base()["ce-specversion"]); |
| 164 | + ASSERT_EQ(http_res.base()["ce-type"], (*bind).base()["ce-type"]); |
| 165 | +} |
| 166 | + |
| 167 | +TEST(UnbindBindTest, HttpRes_Structured) { |
| 168 | + HttpResBinder http_res_binder; |
| 169 | + HttpResponse http_res; |
| 170 | + http_res.base().set("content-type", "application/cloudevents+json"); |
| 171 | + http_res.body() = "{\n\t\"id\" : \"1\",\n\t\"source\" : \"/test\",\n\t\"specversion\" : \"1.0\",\n\t\"type\" : \"test\"\n}"; |
| 172 | + |
| 173 | + cloudevents_absl::StatusOr<CloudEvent> unbind = http_res_binder.Unbind(http_res); |
| 174 | + cloudevents_absl::StatusOr<HttpResponse> bind = http_res_binder.Bind(*unbind, Format::kJson); |
| 175 | + |
| 176 | + ASSERT_TRUE(bind.ok()); |
| 177 | + ASSERT_EQ(http_res.base()["content-type"], (*bind).base()["content-type"]); |
| 178 | + ASSERT_EQ(http_res.body(), (*bind).body()); |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace binding |
| 182 | +} // namespace cloudevents |
0 commit comments