blob: db3e3784da7bedd798f6d59ede891c70049ce279 [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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>
43#include <grpc++/status.h>
44#include <gtest/gtest.h>
45
Chen Wang04f1aa82015-01-30 18:26:16 -080046#include "examples/tips/publisher.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080047#include "test/core/util/port.h"
48#include "test/core/util/test_config.h"
49
50using grpc::ChannelInterface;
51
Chen Wang86af8cf2015-01-21 18:05:40 -080052namespace grpc {
53namespace testing {
54namespace {
55
Chen Wang405392c2015-02-03 10:28:39 -080056const char kProjectId[] = "project id";
Chen Wang86af8cf2015-01-21 18:05:40 -080057const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080058const char kMessageData[] = "test message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080059
Chen Wang04f1aa82015-01-30 18:26:16 -080060class PublisherServiceImpl : public tech::pubsub::PublisherService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080061 public:
62 Status CreateTopic(::grpc::ServerContext* context,
63 const ::tech::pubsub::Topic* request,
64 ::tech::pubsub::Topic* response) override {
65 EXPECT_EQ(request->name(), kTopic);
66 return Status::OK;
67 }
Chen Wang04f1aa82015-01-30 18:26:16 -080068
69 Status Publish(ServerContext* context,
70 const ::tech::pubsub::PublishRequest* request,
71 ::proto2::Empty* response) override {
72 EXPECT_EQ(request->message().data(), kMessageData);
73 return Status::OK;
74 }
75
76 Status GetTopic(ServerContext* context,
77 const ::tech::pubsub::GetTopicRequest* request,
78 ::tech::pubsub::Topic* response) override {
79 EXPECT_EQ(request->topic(), kTopic);
80 return Status::OK;
81 }
82
83 Status ListTopics(ServerContext* context,
84 const ::tech::pubsub::ListTopicsRequest* request,
85 ::tech::pubsub::ListTopicsResponse* response) override {
Chen Wang0b439e42015-02-03 10:52:41 -080086 std::ostringstream ss;
Chen Wang405392c2015-02-03 10:28:39 -080087 ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")";
88 EXPECT_EQ(request->query(), ss.str());
89 response->add_topic()->set_name(kTopic);
90 return Status::OK;
Chen Wang04f1aa82015-01-30 18:26:16 -080091 }
92
93 Status DeleteTopic(ServerContext* context,
94 const ::tech::pubsub::DeleteTopicRequest* request,
95 ::proto2::Empty* response) override {
96 EXPECT_EQ(request->topic(), kTopic);
97 return Status::OK;
98 }
99
Chen Wang86af8cf2015-01-21 18:05:40 -0800100};
101
Chen Wang04f1aa82015-01-30 18:26:16 -0800102class PublisherTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -0800103 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800104 // Setup a server and a client for PublisherService.
Chen Wang86af8cf2015-01-21 18:05:40 -0800105 void SetUp() override {
106 int port = grpc_pick_unused_port_or_die();
107 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800108 ServerBuilder builder;
109 builder.AddPort(server_address_.str());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800110 builder.RegisterService(&service_);
Chen Wang86af8cf2015-01-21 18:05:40 -0800111 server_ = builder.BuildAndStart();
112
113 channel_ = CreateChannel(server_address_.str(), ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800114
115 publisher_.reset(new grpc::examples::tips::Publisher(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800116 }
117
Chen Wang04f1aa82015-01-30 18:26:16 -0800118 void TearDown() override {
119 server_->Shutdown();
120 publisher_->Shutdown();
121 }
Chen Wang86af8cf2015-01-21 18:05:40 -0800122
Chen Wang86af8cf2015-01-21 18:05:40 -0800123 std::ostringstream server_address_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800124 std::unique_ptr<Server> server_;
125 PublisherServiceImpl service_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800126
127 std::shared_ptr<ChannelInterface> channel_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800128
129 std::unique_ptr<grpc::examples::tips::Publisher> publisher_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800130};
131
Chen Wang04f1aa82015-01-30 18:26:16 -0800132TEST_F(PublisherTest, TestPublisher) {
133 EXPECT_TRUE(publisher_->CreateTopic(kTopic).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800134
Chen Wang04f1aa82015-01-30 18:26:16 -0800135 EXPECT_TRUE(publisher_->Publish(kTopic, kMessageData).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800136
Chen Wang04f1aa82015-01-30 18:26:16 -0800137 EXPECT_TRUE(publisher_->GetTopic(kTopic).IsOk());
Chen Wang405392c2015-02-03 10:28:39 -0800138
139 std::vector<grpc::string> topics;
140 EXPECT_TRUE(publisher_->ListTopics(kProjectId, &topics).IsOk());
141 EXPECT_EQ(topics.size(), 1);
142 EXPECT_EQ(topics[0], kTopic);
Chen Wang86af8cf2015-01-21 18:05:40 -0800143}
144
145} // namespace
146} // namespace testing
147} // namespace grpc
148
149int main(int argc, char** argv) {
150 grpc_test_init(argc, argv);
151 grpc_init();
152 ::testing::InitGoogleTest(&argc, argv);
153 gpr_log(GPR_INFO, "Start test ...");
154 int result = RUN_ALL_TESTS();
155 grpc_shutdown();
156 return result;
157}