github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/operation_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dsl
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/sacloud/iaas-api-go/internal/dsl/meta"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestOperation(t *testing.T) {
    26  	resources := Resources{}
    27  	resource := &Resource{
    28  		Name: "Test",
    29  	}
    30  	resources.Define(resource)
    31  
    32  	type expectOperationValues struct {
    33  		methodName                 string
    34  		requestEnvelopeStructName  string
    35  		responseEnvelopeStructName string
    36  		requestPayloadName         string
    37  		responsePayloadName        string
    38  	}
    39  
    40  	expects := []struct {
    41  		operation *Operation
    42  		expect    *expectOperationValues
    43  	}{
    44  		{
    45  			operation: &Operation{
    46  				ResourceName: resource.Name,
    47  				Name:         "Create",
    48  				PathFormat:   DefaultPathFormat,
    49  				Method:       http.MethodPost,
    50  				RequestEnvelope: RequestEnvelope(&EnvelopePayloadDesc{
    51  					Type: meta.Static(struct{}{}),
    52  					Name: "Test",
    53  				}),
    54  				Arguments: []*Argument{
    55  					{
    56  						Name:       "arg1",
    57  						MapConvTag: "Destination",
    58  						Type: &Model{
    59  							Name: "Model",
    60  							Fields: []*FieldDesc{
    61  								{
    62  									Name: "Field1",
    63  									Type: meta.Static(""),
    64  								},
    65  								{
    66  									Name: "Field2",
    67  									Type: meta.Static(""),
    68  								},
    69  							},
    70  						},
    71  					},
    72  				},
    73  				ResponseEnvelope: ResponseEnvelope(&EnvelopePayloadDesc{
    74  					Name: "Test",
    75  					Type: meta.Static(struct{}{}),
    76  				}),
    77  				Results: Results{
    78  					{
    79  						SourceField: "Test",
    80  						DestField:   "Test",
    81  						IsPlural:    false,
    82  						Model: &Model{
    83  							Name: "ResponseEnvelope",
    84  							Fields: []*FieldDesc{
    85  								{
    86  									Name: "Field3",
    87  									Type: meta.Static(""),
    88  								},
    89  								{
    90  									Name: "Field4",
    91  									Type: meta.Static(""),
    92  								},
    93  							},
    94  						},
    95  					},
    96  				},
    97  			},
    98  			expect: &expectOperationValues{
    99  				methodName:                 "Create",
   100  				requestEnvelopeStructName:  "testCreateRequestEnvelope",
   101  				responseEnvelopeStructName: "testCreateResponseEnvelope",
   102  				requestPayloadName:         "Test",
   103  				responsePayloadName:        "Test",
   104  			},
   105  		},
   106  	}
   107  
   108  	for _, tc := range expects {
   109  		resource.Operations = []*Operation{tc.operation}
   110  		require.Equal(t, tc.expect.methodName, tc.operation.MethodName())
   111  		require.Equal(t, tc.expect.requestEnvelopeStructName, tc.operation.RequestEnvelopeStructName())
   112  		require.Equal(t, tc.expect.responseEnvelopeStructName, tc.operation.ResponseEnvelopeStructName())
   113  		require.Equal(t, tc.expect.requestPayloadName, tc.operation.RequestPayloads()[0].Name)
   114  		require.Equal(t, tc.expect.responsePayloadName, tc.operation.ResponsePayloads()[0].Name)
   115  	}
   116  }