// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by gapic-generator. DO NOT EDIT.

package asset

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"strings"
	"testing"

	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes"
	emptypb "github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/api/option"
	assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1p2beta1"
	field_maskpb "google.golang.org/genproto/protobuf/field_mask"

	status "google.golang.org/genproto/googleapis/rpc/status"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"

	gstatus "google.golang.org/grpc/status"
)

var _ = io.EOF
var _ = ptypes.MarshalAny
var _ status.Status

type mockAssetServer struct {
	// Embed for forward compatibility.
	// Tests will keep working if more methods are added
	// in the future.
	assetpb.AssetServiceServer

	reqs []proto.Message

	// If set, all calls return this error.
	err error

	// responses to return if err == nil
	resps []proto.Message
}

func (s *mockAssetServer) CreateFeed(ctx context.Context, req *assetpb.CreateFeedRequest) (*assetpb.Feed, error) {
	md, _ := metadata.FromIncomingContext(ctx)
	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
	}
	s.reqs = append(s.reqs, req)
	if s.err != nil {
		return nil, s.err
	}
	return s.resps[0].(*assetpb.Feed), nil
}

func (s *mockAssetServer) GetFeed(ctx context.Context, req *assetpb.GetFeedRequest) (*assetpb.Feed, error) {
	md, _ := metadata.FromIncomingContext(ctx)
	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
	}
	s.reqs = append(s.reqs, req)
	if s.err != nil {
		return nil, s.err
	}
	return s.resps[0].(*assetpb.Feed), nil
}

func (s *mockAssetServer) ListFeeds(ctx context.Context, req *assetpb.ListFeedsRequest) (*assetpb.ListFeedsResponse, error) {
	md, _ := metadata.FromIncomingContext(ctx)
	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
	}
	s.reqs = append(s.reqs, req)
	if s.err != nil {
		return nil, s.err
	}
	return s.resps[0].(*assetpb.ListFeedsResponse), nil
}

func (s *mockAssetServer) UpdateFeed(ctx context.Context, req *assetpb.UpdateFeedRequest) (*assetpb.Feed, error) {
	md, _ := metadata.FromIncomingContext(ctx)
	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
	}
	s.reqs = append(s.reqs, req)
	if s.err != nil {
		return nil, s.err
	}
	return s.resps[0].(*assetpb.Feed), nil
}

func (s *mockAssetServer) DeleteFeed(ctx context.Context, req *assetpb.DeleteFeedRequest) (*emptypb.Empty, error) {
	md, _ := metadata.FromIncomingContext(ctx)
	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
	}
	s.reqs = append(s.reqs, req)
	if s.err != nil {
		return nil, s.err
	}
	return s.resps[0].(*emptypb.Empty), nil
}

// clientOpt is the option tests should use to connect to the test server.
// It is initialized by TestMain.
var clientOpt option.ClientOption

var (
	mockAsset mockAssetServer
)

func TestMain(m *testing.M) {
	flag.Parse()

	serv := grpc.NewServer()
	assetpb.RegisterAssetServiceServer(serv, &mockAsset)

	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		log.Fatal(err)
	}
	go serv.Serve(lis)

	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	clientOpt = option.WithGRPCConn(conn)

	os.Exit(m.Run())
}

func TestAssetServiceCreateFeed(t *testing.T) {
	var name string = "name3373707"
	var expectedResponse = &assetpb.Feed{
		Name: name,
	}

	mockAsset.err = nil
	mockAsset.reqs = nil

	mockAsset.resps = append(mockAsset.resps[:0], expectedResponse)

	var parent string = "parent-995424086"
	var feedId string = "feedId-976011428"
	var feed *assetpb.Feed = &assetpb.Feed{}
	var request = &assetpb.CreateFeedRequest{
		Parent: parent,
		FeedId: feedId,
		Feed:   feed,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.CreateFeed(context.Background(), request)

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockAsset.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
		t.Errorf("wrong response %q, want %q)", got, want)
	}
}

func TestAssetServiceCreateFeedError(t *testing.T) {
	errCode := codes.PermissionDenied
	mockAsset.err = gstatus.Error(errCode, "test error")

	var parent string = "parent-995424086"
	var feedId string = "feedId-976011428"
	var feed *assetpb.Feed = &assetpb.Feed{}
	var request = &assetpb.CreateFeedRequest{
		Parent: parent,
		FeedId: feedId,
		Feed:   feed,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.CreateFeed(context.Background(), request)

	if st, ok := gstatus.FromError(err); !ok {
		t.Errorf("got error %v, expected grpc error", err)
	} else if c := st.Code(); c != errCode {
		t.Errorf("got error code %q, want %q", c, errCode)
	}
	_ = resp
}
func TestAssetServiceGetFeed(t *testing.T) {
	var name2 string = "name2-1052831874"
	var expectedResponse = &assetpb.Feed{
		Name: name2,
	}

	mockAsset.err = nil
	mockAsset.reqs = nil

	mockAsset.resps = append(mockAsset.resps[:0], expectedResponse)

	var formattedName string = fmt.Sprintf("projects/%s/feeds/%s", "[PROJECT]", "[FEED]")
	var request = &assetpb.GetFeedRequest{
		Name: formattedName,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.GetFeed(context.Background(), request)

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockAsset.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
		t.Errorf("wrong response %q, want %q)", got, want)
	}
}

func TestAssetServiceGetFeedError(t *testing.T) {
	errCode := codes.PermissionDenied
	mockAsset.err = gstatus.Error(errCode, "test error")

	var formattedName string = fmt.Sprintf("projects/%s/feeds/%s", "[PROJECT]", "[FEED]")
	var request = &assetpb.GetFeedRequest{
		Name: formattedName,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.GetFeed(context.Background(), request)

	if st, ok := gstatus.FromError(err); !ok {
		t.Errorf("got error %v, expected grpc error", err)
	} else if c := st.Code(); c != errCode {
		t.Errorf("got error code %q, want %q", c, errCode)
	}
	_ = resp
}
func TestAssetServiceListFeeds(t *testing.T) {
	var expectedResponse *assetpb.ListFeedsResponse = &assetpb.ListFeedsResponse{}

	mockAsset.err = nil
	mockAsset.reqs = nil

	mockAsset.resps = append(mockAsset.resps[:0], expectedResponse)

	var parent string = "parent-995424086"
	var request = &assetpb.ListFeedsRequest{
		Parent: parent,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.ListFeeds(context.Background(), request)

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockAsset.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
		t.Errorf("wrong response %q, want %q)", got, want)
	}
}

func TestAssetServiceListFeedsError(t *testing.T) {
	errCode := codes.PermissionDenied
	mockAsset.err = gstatus.Error(errCode, "test error")

	var parent string = "parent-995424086"
	var request = &assetpb.ListFeedsRequest{
		Parent: parent,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.ListFeeds(context.Background(), request)

	if st, ok := gstatus.FromError(err); !ok {
		t.Errorf("got error %v, expected grpc error", err)
	} else if c := st.Code(); c != errCode {
		t.Errorf("got error code %q, want %q", c, errCode)
	}
	_ = resp
}
func TestAssetServiceUpdateFeed(t *testing.T) {
	var name string = "name3373707"
	var expectedResponse = &assetpb.Feed{
		Name: name,
	}

	mockAsset.err = nil
	mockAsset.reqs = nil

	mockAsset.resps = append(mockAsset.resps[:0], expectedResponse)

	var feed *assetpb.Feed = &assetpb.Feed{}
	var updateMask *field_maskpb.FieldMask = &field_maskpb.FieldMask{}
	var request = &assetpb.UpdateFeedRequest{
		Feed:       feed,
		UpdateMask: updateMask,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.UpdateFeed(context.Background(), request)

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockAsset.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
		t.Errorf("wrong response %q, want %q)", got, want)
	}
}

func TestAssetServiceUpdateFeedError(t *testing.T) {
	errCode := codes.PermissionDenied
	mockAsset.err = gstatus.Error(errCode, "test error")

	var feed *assetpb.Feed = &assetpb.Feed{}
	var updateMask *field_maskpb.FieldMask = &field_maskpb.FieldMask{}
	var request = &assetpb.UpdateFeedRequest{
		Feed:       feed,
		UpdateMask: updateMask,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := c.UpdateFeed(context.Background(), request)

	if st, ok := gstatus.FromError(err); !ok {
		t.Errorf("got error %v, expected grpc error", err)
	} else if c := st.Code(); c != errCode {
		t.Errorf("got error code %q, want %q", c, errCode)
	}
	_ = resp
}
func TestAssetServiceDeleteFeed(t *testing.T) {
	var expectedResponse *emptypb.Empty = &emptypb.Empty{}

	mockAsset.err = nil
	mockAsset.reqs = nil

	mockAsset.resps = append(mockAsset.resps[:0], expectedResponse)

	var formattedName string = fmt.Sprintf("projects/%s/feeds/%s", "[PROJECT]", "[FEED]")
	var request = &assetpb.DeleteFeedRequest{
		Name: formattedName,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	err = c.DeleteFeed(context.Background(), request)

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockAsset.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

}

func TestAssetServiceDeleteFeedError(t *testing.T) {
	errCode := codes.PermissionDenied
	mockAsset.err = gstatus.Error(errCode, "test error")

	var formattedName string = fmt.Sprintf("projects/%s/feeds/%s", "[PROJECT]", "[FEED]")
	var request = &assetpb.DeleteFeedRequest{
		Name: formattedName,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	err = c.DeleteFeed(context.Background(), request)

	if st, ok := gstatus.FromError(err); !ok {
		t.Errorf("got error %v, expected grpc error", err)
	} else if c := st.Code(); c != errCode {
		t.Errorf("got error code %q, want %q", c, errCode)
	}
}
