blob: ac4921283f4bdfd20fc412ab90a7099d65f5823b [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
34#include <grpc++/channel_arguments.h>
35#include <grpc++/channel_interface.h>
36#include <grpc++/client_context.h>
37#include <grpc++/create_channel.h>
38#include <grpc++/server.h>
39#include <grpc++/server_builder.h>
40#include <grpc++/server_context.h>
Craig Tiller42bc87c2015-02-23 08:50:19 -080041#include <grpc++/server_credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080042#include <grpc++/status.h>
43#include <gtest/gtest.h>
44
Chen wang84232512015-02-12 17:29:18 -080045#include "examples/pubsub/publisher.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080046#include "test/core/util/port.h"
47#include "test/core/util/test_config.h"
48
49using grpc::ChannelInterface;
50
Chen Wang86af8cf2015-01-21 18:05:40 -080051namespace grpc {
52namespace testing {
53namespace {
54
Chen Wang405392c2015-02-03 10:28:39 -080055const char kProjectId[] = "project id";
Chen Wang86af8cf2015-01-21 18:05:40 -080056const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080057const char kMessageData[] = "test message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080058
Chen Wang04f1aa82015-01-30 18:26:16 -080059class PublisherServiceImpl : public tech::pubsub::PublisherService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080060 public:
61 Status CreateTopic(::grpc::ServerContext* context,
62 const ::tech::pubsub::Topic* request,
Craig Tillercf133f42015-02-26 14:05:56 -080063 ::tech::pubsub::Topic* response) GRPC_OVERRIDE {
Chen Wang86af8cf2015-01-21 18:05:40 -080064 EXPECT_EQ(request->name(), kTopic);
65 return Status::OK;
66 }
Chen Wang04f1aa82015-01-30 18:26:16 -080067
68 Status Publish(ServerContext* context,
69 const ::tech::pubsub::PublishRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080070 ::proto2::Empty* response) GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -080071 EXPECT_EQ(request->message().data(), kMessageData);
72 return Status::OK;
73 }
74
75 Status GetTopic(ServerContext* context,
76 const ::tech::pubsub::GetTopicRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080077 ::tech::pubsub::Topic* response) GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -080078 EXPECT_EQ(request->topic(), kTopic);
79 return Status::OK;
80 }
81
Craig Tillercf133f42015-02-26 14:05:56 -080082 Status ListTopics(
83 ServerContext* context, const ::tech::pubsub::ListTopicsRequest* request,
84 ::tech::pubsub::ListTopicsResponse* response) GRPC_OVERRIDE {
Yang Gao884ed082015-03-25 10:45:23 -070085 std::ostringstream ss;
86 ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")";
87 EXPECT_EQ(request->query(), ss.str());
88 response->add_topic()->set_name(kTopic);
89 return Status::OK;
90 }
Chen Wang04f1aa82015-01-30 18:26:16 -080091
Yang Gao884ed082015-03-25 10:45:23 -070092 Status DeleteTopic(ServerContext* context,
93 const ::tech::pubsub::DeleteTopicRequest* request,
94 ::proto2::Empty* response) GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -080095 EXPECT_EQ(request->topic(), kTopic);
96 return Status::OK;
Yang Gao884ed082015-03-25 10:45:23 -070097 }
Chen Wang86af8cf2015-01-21 18:05:40 -080098};
99
Chen Wang04f1aa82015-01-30 18:26:16 -0800100class PublisherTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -0800101 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800102 // Setup a server and a client for PublisherService.
Craig Tillercf133f42015-02-26 14:05:56 -0800103 void SetUp() GRPC_OVERRIDE {
Chen Wang86af8cf2015-01-21 18:05:40 -0800104 int port = grpc_pick_unused_port_or_die();
105 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800106 ServerBuilder builder;
Yang Gao884ed082015-03-25 10:45:23 -0700107 builder.AddListeningPort(server_address_.str(),
108 grpc::InsecureServerCredentials());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800109 builder.RegisterService(&service_);
Chen Wang86af8cf2015-01-21 18:05:40 -0800110 server_ = builder.BuildAndStart();
111
Yang Gao884ed082015-03-25 10:45:23 -0700112 channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(),
113 ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800114
Chen wang84232512015-02-12 17:29:18 -0800115 publisher_.reset(new grpc::examples::pubsub::Publisher(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800116 }
117
Craig Tillercf133f42015-02-26 14:05:56 -0800118 void TearDown() GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -0800119 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
Chen wang84232512015-02-12 17:29:18 -0800129 std::unique_ptr<grpc::examples::pubsub::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());
vjpaidbbdda52015-02-19 17:02:54 -0800141 EXPECT_EQ(topics.size(), static_cast<size_t>(1));
Chen Wang405392c2015-02-03 10:28:39 -0800142 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;
Craig Tiller190d3602015-02-18 09:23:38 -0800157}