Skip to content

Commit

Permalink
Add integration with clients generated by swagger-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Jun 14, 2016
1 parent 171d704 commit 3123f4e
Show file tree
Hide file tree
Showing 19 changed files with 1,189 additions and 53 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ cache:
directories:
- $HOME/local
before_install:
- ./.travis.build-protoc.sh 3.0.0-beta-3
- ./.travis/build-protoc.sh 3.0.0-beta-3
- ./.travis/install-swagger-codegen.sh 2.1.6
- go get github.com/golang/lint/golint
- go get github.com/dghubble/sling
install:
- go get github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway
- go get github.com/gengo/grpc-gateway/runtime
- go get github.com/gengo/grpc-gateway/examples
- go get github.com/gengo/grpc-gateway/examples/server
script:
- make realclean && make examples
- make realclean && make examples SWAGGER_CODEGEN="java -jar $HOME/local/swagger-codegen-cli.jar"
- if ! go version | grep devel; then test -z "$(git status --porcelain)" || (git status; git diff; exit 1); fi
- env GLOG_logtostderr=1 go test -race -v github.com/gengo/grpc-gateway/...
- golint github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/...
Expand Down
2 changes: 1 addition & 1 deletion .travis.build-protoc.sh → .travis/build-protoc.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh -eu
protoc_version=$1
if test -z "${protoc_version}"; then
echo "Usage: .travis.build-protoc.sh protoc-version"
echo "Usage: .travis/build-protoc.sh protoc-version"
exit 1
fi
if ! $HOME/local/bin/protoc-${protoc_version} --version 2>/dev/null; then
Expand Down
9 changes: 9 additions & 0 deletions .travis/install-swagger-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh -eu
codegen_version=$1
if test -z "${codegen_version}"; then
echo "Usage: .travis/install-swagger-codegen.sh codegen-version"
exit 1
fi

wget http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/${codegen_version}/swagger-codegen-cli-${codegen_version}.jar \
-O $HOME/local/swagger-codegen-cli.jar
30 changes: 29 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ EXAMPLE_GWSRCS=$(EXAMPLES:.proto=.pb.gw.go)
EXAMPLE_SWAGGERSRCS=$(EXAMPLES:.proto=.swagger.json)
EXAMPLE_DEPS=examples/sub/message.proto examples/sub2/message.proto
EXAMPLE_DEPSRCS=$(EXAMPLE_DEPS:.proto=.pb.go)

EXAMPLE_CLIENT_DIR=examples/clients
ECHO_EXAMPLE_SPEC=examples/examplepb/echo_service.swagger.json
ECHO_EXAMPLE_SRCS=$(EXAMPLE_CLIENT_DIR)/echo/EchoServiceApi.go \
$(EXAMPLE_CLIENT_DIR)/echo/ExamplepbSimpleMessage.go
ABE_EXAMPLE_SPEC=examples/examplepb/a_bit_of_everything.swagger.json
ABE_EXAMPLE_SRCS=$(EXAMPLE_CLIENT_DIR)/abe/ABitOfEverythingServiceApi.go \
$(EXAMPLE_CLIENT_DIR)/abe/ABitOfEverythingNested.go \
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbABitOfEverything.go \
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbNumericEnum.go \
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbIdMessage.go \
$(EXAMPLE_CLIENT_DIR)/abe/NestedDeepEnum.go \
$(EXAMPLE_CLIENT_DIR)/abe/ProtobufEmpty.go \
$(EXAMPLE_CLIENT_DIR)/abe/Sub2IdMessage.go \
$(EXAMPLE_CLIENT_DIR)/abe/SubStringMessage.go
EXAMPLE_CLIENT_SRCS=$(ECHO_EXAMPLE_SRCS) $(ABE_EXAMPLE_SRCS)
SWAGGER_CODEGEN=swagger-codegen

PROTOC_INC_PATH=$(dir $(shell which protoc))/../include

generate: $(OPTIONS_GO) $(RUNTIME_GO)
Expand Down Expand Up @@ -88,7 +106,16 @@ $(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
$(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,$(PKGMAP):. $(SWAGGER_EXAMPLES)

examples: $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_DEPSRCS) $(EXAMPLE_SWAGGERSRCS)
$(ECHO_EXAMPLE_SRCS): $(ECHO_EXAMPLE_SPEC)
$(SWAGGER_CODEGEN) generate -i $(ECHO_EXAMPLE_SPEC) \
-l go -o examples/clients --additional-properties packageName=echo
@rm -f $(EXAMPLE_CLIENT_DIR)/README.md $(EXAMPLE_CLIENT_DIR)/git_push.sh $(EXAMPLE_CLIENT_DIR)/.gitignore
$(ABE_EXAMPLE_SRCS): $(ABE_EXAMPLE_SPEC)
$(SWAGGER_CODEGEN) generate -i $(ABE_EXAMPLE_SPEC) \
-l go -o examples/clients --additional-properties packageName=abe
@rm -f $(EXAMPLE_CLIENT_DIR)/README.md $(EXAMPLE_CLIENT_DIR)/git_push.sh $(EXAMPLE_CLIENT_DIR)/.gitignore

examples: $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_DEPSRCS) $(EXAMPLE_SWAGGERSRCS) $(EXAMPLE_CLIENT_SRCS)
test: examples
go test -race $(PKG)/...

Expand All @@ -101,5 +128,6 @@ realclean: distclean
rm -f $(EXAMPLE_SWAGGERSRCS)
rm -f $(GO_PLUGIN)
rm -f $(SWAGGER_PLUGIN)
rm -f $(EXAMPLE_CLIENT_SRCS)

.PHONY: generate examples test clean distclean realclean
162 changes: 162 additions & 0 deletions examples/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"reflect"
"testing"

"github.com/gengo/grpc-gateway/examples/clients/abe"
"github.com/gengo/grpc-gateway/examples/clients/echo"
)

func TestClientIntegration(t *testing.T) {
}

func TestEchoClient(t *testing.T) {
if testing.Short() {
t.Skip()
return
}

cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080")
resp, err := cl.Echo("foo")
if err != nil {
t.Errorf(`cl.Echo("foo") failed with %v; want success`, err)
}
if got, want := resp.Id, "foo"; got != want {
t.Errorf("resp.Id = %q; want %q", got, want)
}
}

func TestEchoBodyClient(t *testing.T) {
if testing.Short() {
t.Skip()
return
}

cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080")
req := echo.ExamplepbSimpleMessage{Id: "foo"}
resp, err := cl.EchoBody(req)
if err != nil {
t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err)
}
if got, want := resp.Id, "foo"; got != want {
t.Errorf("resp.Id = %q; want %q", got, want)
}
}

func TestAbitOfEverythingClient(t *testing.T) {
if testing.Short() {
t.Skip()
return
}

cl := abe.NewABitOfEverythingServiceApiWithBasePath("http://localhost:8080")
testABEClientCreate(t, cl)
testABEClientCreateBody(t, cl)
}

func testABEClientCreate(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {
want := abe.ExamplepbABitOfEverything{
FloatValue: 1.5,
DoubleValue: 2.5,
Int64Value: "4294967296",
Uint64Value: "9223372036854775807",
Int32Value: -2147483648,
Fixed64Value: "9223372036854775807",
Fixed32Value: 4294967295,
BoolValue: true,
StringValue: "strprefix/foo",
Uint32Value: 4294967295,
Sfixed32Value: 2147483647,
Sfixed64Value: "-4611686018427387904",
Sint32Value: 2147483647,
Sint64Value: "4611686018427387903",
NonConventionalNameValue: "camelCase",
}
resp, err := cl.Create(
want.FloatValue,
want.DoubleValue,
want.Int64Value,
want.Uint64Value,
want.Int32Value,
want.Fixed64Value,
want.Fixed32Value,
want.BoolValue,
want.StringValue,
want.Uint32Value,
want.Sfixed32Value,
want.Sfixed64Value,
want.Sint32Value,
want.Sint64Value,
want.NonConventionalNameValue,
)
if err != nil {
t.Errorf("cl.Create(%#v) failed with %v; want success", want, err)
}
if resp.Uuid == "" {
t.Errorf("resp.Uuid is empty; want not empty")
}
resp.Uuid = ""
if got := resp; !reflect.DeepEqual(got, want) {
t.Errorf("resp = %#v; want %#v", got, want)
}
}

func testABEClientCreateBody(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {
t.Log("TODO: support enum")
return

want := abe.ExamplepbABitOfEverything{
FloatValue: 1.5,
DoubleValue: 2.5,
Int64Value: "4294967296",
Uint64Value: "9223372036854775807",
Int32Value: -2147483648,
Fixed64Value: "9223372036854775807",
Fixed32Value: 4294967295,
BoolValue: true,
StringValue: "strprefix/foo",
Uint32Value: 4294967295,
Sfixed32Value: 2147483647,
Sfixed64Value: "-4611686018427387904",
Sint32Value: 2147483647,
Sint64Value: "4611686018427387903",
NonConventionalNameValue: "camelCase",

Nested: []abe.ABitOfEverythingNested{
{
Name: "bar",
Amount: 10,
},
{
Name: "baz",
Amount: 20,
},
},
RepeatedStringValue: []string{"a", "b", "c"},
OneofString: "x",
MapValue: map[string]abe.ExamplepbNumericEnum{
// "a": abe.ExamplepbNumericEnum_ONE,
// "b": abe.ExamplepbNumericEnum_ZERO,
},
MappedStringValue: map[string]string{
"a": "x",
"b": "y",
},
MappedNestedValue: map[string]abe.ABitOfEverythingNested{
"a": {Name: "x", Amount: 1},
"b": {Name: "y", Amount: 2},
},
}
resp, err := cl.CreateBody(want)
if err != nil {
t.Errorf("cl.CreateBody(%#v) failed with %v; want success", want, err)
}
if resp.Uuid == "" {
t.Errorf("resp.Uuid is empty; want not empty")
}
resp.Uuid = ""
if got := resp; !reflect.DeepEqual(got, want) {
t.Errorf("resp = %#v; want %#v", got, want)
}
}
11 changes: 11 additions & 0 deletions examples/clients/abe/ABitOfEverythingNested.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package abe

import (
)

type ABitOfEverythingNested struct {
Amount int64 `json:"amount,omitempty"`
Name string `json:"name,omitempty"`
Ok NestedDeepEnum `json:"ok,omitempty"`

}
Loading

0 comments on commit 3123f4e

Please sign in to comment.