github.com/polarismesh/polaris@v1.17.8/apiserver/grpcserver/discover/server_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package discover
    19  
    20  import (
    21  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  
    25  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    26  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    27  	"google.golang.org/protobuf/types/known/wrapperspb"
    28  
    29  	"github.com/polarismesh/polaris/apiserver/grpcserver"
    30  )
    31  
    32  func Test_discoverCacheConvert(t *testing.T) {
    33  	type args struct {
    34  		m interface{}
    35  	}
    36  	tests := []struct {
    37  		name string
    38  		args args
    39  		want *grpcserver.CacheObject
    40  	}{
    41  		{
    42  			name: "DiscoverResponse_INSTANCE",
    43  			args: args{
    44  				m: &apiservice.DiscoverResponse{
    45  					Code: &wrapperspb.UInt32Value{
    46  						Value: uint32(apimodel.Code_ExecuteSuccess),
    47  					},
    48  					Type: apiservice.DiscoverResponse_INSTANCE,
    49  					Service: &apiservice.Service{
    50  						Name: &wrapperspb.StringValue{
    51  							Value: "test",
    52  						},
    53  						Namespace: &wrapperspb.StringValue{
    54  							Value: "test",
    55  						},
    56  						Revision: &wrapperspb.StringValue{
    57  							Value: "test",
    58  						},
    59  					},
    60  					Instances: []*apiservice.Instance{},
    61  				},
    62  			},
    63  			want: &grpcserver.CacheObject{
    64  				OriginVal: &apiservice.DiscoverResponse{
    65  					Code: &wrapperspb.UInt32Value{
    66  						Value: uint32(apimodel.Code_ExecuteSuccess),
    67  					},
    68  					Type: apiservice.DiscoverResponse_INSTANCE,
    69  					Service: &apiservice.Service{
    70  						Name: &wrapperspb.StringValue{
    71  							Value: "test",
    72  						},
    73  						Namespace: &wrapperspb.StringValue{
    74  							Value: "test",
    75  						},
    76  						Revision: &wrapperspb.StringValue{
    77  							Value: "test",
    78  						},
    79  					},
    80  					Instances: []*apiservice.Instance{},
    81  				},
    82  				CacheType: apiservice.DiscoverResponse_INSTANCE.String(),
    83  				Key:       fmt.Sprintf("%s-%s-%s", "test", "test", "test"),
    84  			},
    85  		},
    86  		{
    87  			name: "DiscoverResponse_SERVICES",
    88  			args: args{
    89  				m: &apiservice.DiscoverResponse{
    90  					Code: &wrapperspb.UInt32Value{
    91  						Value: uint32(apimodel.Code_ExecuteSuccess),
    92  					},
    93  					Type: apiservice.DiscoverResponse_SERVICES,
    94  					Service: &apiservice.Service{
    95  						Name: &wrapperspb.StringValue{
    96  							Value: "",
    97  						},
    98  						Namespace: &wrapperspb.StringValue{
    99  							Value: "test",
   100  						},
   101  						Revision: &wrapperspb.StringValue{
   102  							Value: "123",
   103  						},
   104  					},
   105  				},
   106  			},
   107  			want: &grpcserver.CacheObject{
   108  				OriginVal: &apiservice.DiscoverResponse{
   109  					Code: &wrapperspb.UInt32Value{
   110  						Value: uint32(apimodel.Code_ExecuteSuccess),
   111  					},
   112  					Type: apiservice.DiscoverResponse_SERVICES,
   113  					Service: &apiservice.Service{
   114  						Name: &wrapperspb.StringValue{
   115  							Value: "",
   116  						},
   117  						Namespace: &wrapperspb.StringValue{
   118  							Value: "test",
   119  						},
   120  						Revision: &wrapperspb.StringValue{
   121  							Value: "123",
   122  						},
   123  					},
   124  				},
   125  				CacheType: apiservice.DiscoverResponse_SERVICES.String(),
   126  				Key:       fmt.Sprintf("%s-%s-%s", "test", "", "123"),
   127  			},
   128  		},
   129  		{
   130  			name: "DiscoverResponse_RATE_LIMIT",
   131  			args: args{
   132  				m: &apiservice.DiscoverResponse{
   133  					Code: &wrapperspb.UInt32Value{
   134  						Value: uint32(apimodel.Code_ExecuteSuccess),
   135  					},
   136  					Type: apiservice.DiscoverResponse_RATE_LIMIT,
   137  					Service: &apiservice.Service{
   138  						Name: &wrapperspb.StringValue{
   139  							Value: "test",
   140  						},
   141  						Namespace: &wrapperspb.StringValue{
   142  							Value: "test",
   143  						},
   144  						Revision: &wrapperspb.StringValue{
   145  							Value: "test",
   146  						},
   147  					},
   148  				},
   149  			},
   150  			want: &grpcserver.CacheObject{
   151  				OriginVal: &apiservice.DiscoverResponse{
   152  					Code: &wrapperspb.UInt32Value{
   153  						Value: uint32(apimodel.Code_ExecuteSuccess),
   154  					},
   155  					Type: apiservice.DiscoverResponse_RATE_LIMIT,
   156  					Service: &apiservice.Service{
   157  						Name: &wrapperspb.StringValue{
   158  							Value: "test",
   159  						},
   160  						Namespace: &wrapperspb.StringValue{
   161  							Value: "test",
   162  						},
   163  						Revision: &wrapperspb.StringValue{
   164  							Value: "test",
   165  						},
   166  					},
   167  				},
   168  				CacheType: apiservice.DiscoverResponse_RATE_LIMIT.String(),
   169  				Key:       fmt.Sprintf("%s-%s-%s", "test", "test", "test"),
   170  			},
   171  		},
   172  	}
   173  	for _, tt := range tests {
   174  		t.Run(tt.name, func(t *testing.T) {
   175  			if got := discoverCacheConvert(tt.args.m); !reflect.DeepEqual(got, tt.want) {
   176  				t.Errorf("discoverCacheConvert() = %v, want %v", got, tt.want)
   177  			}
   178  		})
   179  	}
   180  }