blob: 0bb4b842176a6ddee8cc43498b2ee335de4ff670 [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Chen Wang86af8cf2015-01-21 18:05:40 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Nicolas "Pixel" Noble3f36f5e2015-02-06 20:45:54 +010034#include <google/protobuf/stubs/common.h>
35
Chen Wang86af8cf2015-01-21 18:05:40 -080036#include <grpc++/channel_arguments.h>
37#include <grpc++/channel_interface.h>
38#include <grpc++/client_context.h>
39#include <grpc++/create_channel.h>
40#include <grpc++/server.h>
41#include <grpc++/server_builder.h>
42#include <grpc++/server_context.h>
Craig Tiller42bc87c2015-02-23 08:50:19 -080043#include <grpc++/server_credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080044#include <grpc++/status.h>
45#include <gtest/gtest.h>
46
Chen wang84232512015-02-12 17:29:18 -080047#include "examples/pubsub/publisher.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080048#include "test/core/util/port.h"
49#include "test/core/util/test_config.h"
50
51using grpc::ChannelInterface;
52
Chen Wang86af8cf2015-01-21 18:05:40 -080053namespace grpc {
54namespace testing {
55namespace {
56
Chen Wang405392c2015-02-03 10:28:39 -080057const char kProjectId[] = "project id";
Chen Wang86af8cf2015-01-21 18:05:40 -080058const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080059const char kMessageData[] = "test message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080060
Chen Wang04f1aa82015-01-30 18:26:16 -080061class PublisherServiceImpl : public tech::pubsub::PublisherService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080062 public:
63 Status CreateTopic(::grpc::ServerContext* context,
64 const ::tech::pubsub::Topic* request,
65 ::tech::pubsub::Topic* response) override {
66 EXPECT_EQ(request->name(), kTopic);
67 return Status::OK;
68 }
Chen Wang04f1aa82015-01-30 18:26:16 -080069
70 Status Publish(ServerContext* context,
71 const ::tech::pubsub::PublishRequest* request,
72 ::proto2::Empty* response) override {
73 EXPECT_EQ(request->message().data(), kMessageData);
74 return Status::OK;
75 }
76
77 Status GetTopic(ServerContext* context,
78 const ::tech::pubsub::GetTopicRequest* request,
79 ::tech::pubsub::Topic* response) override {
80 EXPECT_EQ(request->topic(), kTopic);
81 return Status::OK;
82 }
83
84 Status ListTopics(ServerContext* context,
85 const ::tech::pubsub::ListTopicsRequest* request,
86 ::tech::pubsub::ListTopicsResponse* response) override {
Chen Wang0b439e42015-02-03 10:52:41 -080087 std::ostringstream ss;
Chen Wang405392c2015-02-03 10:28:39 -080088 ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")";
89 EXPECT_EQ(request->query(), ss.str());
90 response->add_topic()->set_name(kTopic);
91 return Status::OK;
Chen Wang04f1aa82015-01-30 18:26:16 -080092 }
93
94 Status DeleteTopic(ServerContext* context,
95 const ::tech::pubsub::DeleteTopicRequest* request,
96 ::proto2::Empty* response) override {
97 EXPECT_EQ(request->topic(), kTopic);
98 return Status::OK;
99 }
100
Chen Wang86af8cf2015-01-21 18:05:40 -0800101};
102
Chen Wang04f1aa82015-01-30 18:26:16 -0800103class PublisherTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -0800104 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800105 // Setup a server and a client for PublisherService.
Chen Wang86af8cf2015-01-21 18:05:40 -0800106 void SetUp() override {
107 int port = grpc_pick_unused_port_or_die();
108 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800109 ServerBuilder builder;
Craig Tiller42bc87c2015-02-23 08:50:19 -0800110 builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800111 builder.RegisterService(&service_);
Chen Wang86af8cf2015-01-21 18:05:40 -0800112 server_ = builder.BuildAndStart();
113
Craig Tiller42bc87c2015-02-23 08:50:19 -0800114 channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(), ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800115
Chen wang84232512015-02-12 17:29:18 -0800116 publisher_.reset(new grpc::examples::pubsub::Publisher(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800117 }
118
Chen Wang04f1aa82015-01-30 18:26:16 -0800119 void TearDown() override {
120 server_->Shutdown();
121 publisher_->Shutdown();
122 }
Chen Wang86af8cf2015-01-21 18:05:40 -0800123
Chen Wang86af8cf2015-01-21 18:05:40 -0800124 std::ostringstream server_address_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800125 std::unique_ptr<Server> server_;
126 PublisherServiceImpl service_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800127
128 std::shared_ptr<ChannelInterface> channel_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800129
Chen wang84232512015-02-12 17:29:18 -0800130 std::unique_ptr<grpc::examples::pubsub::Publisher> publisher_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800131};
132
Chen Wang04f1aa82015-01-30 18:26:16 -0800133TEST_F(PublisherTest, TestPublisher) {
134 EXPECT_TRUE(publisher_->CreateTopic(kTopic).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800135
Chen Wang04f1aa82015-01-30 18:26:16 -0800136 EXPECT_TRUE(publisher_->Publish(kTopic, kMessageData).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800137
Chen Wang04f1aa82015-01-30 18:26:16 -0800138 EXPECT_TRUE(publisher_->GetTopic(kTopic).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800139
140 std::vector<grpc::string> topics;
141 EXPECT_TRUE(publisher_->ListTopics(kProjectId, &topics).IsOk());
vjpaidbbdda52015-02-19 17:02:54 -0800142 EXPECT_EQ(topics.size(), static_cast<size_t>(1));
Chen Wang405392c2015-02-03 10:28:39 -0800143 EXPECT_EQ(topics[0], kTopic);
Chen Wang86af8cf2015-01-21 18:05:40 -0800144}
145
146} // namespace
147} // namespace testing
148} // namespace grpc
149
150int main(int argc, char** argv) {
151 grpc_test_init(argc, argv);
152 grpc_init();
153 ::testing::InitGoogleTest(&argc, argv);
154 gpr_log(GPR_INFO, "Start test ...");
155 int result = RUN_ALL_TESTS();
156 grpc_shutdown();
157 return result;
Craig Tiller190d3602015-02-18 09:23:38 -0800158}