github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/products_test.go (about)

     1  package papi
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestPapi_GetProducts(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           GetProductsRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *GetProductsResponse
    21  		withError        func(*testing.T, error)
    22  	}{
    23  		"200 OK": {
    24  			params: GetProductsRequest{
    25  				ContractID: "ctr_1-1TJZFW",
    26  			},
    27  			responseStatus: http.StatusOK,
    28  			responseBody: `{
    29      "accountId": "act_1-1TJZFB",
    30      "contractId": "ctr_1-1TJZFW",
    31      "products": {
    32          "items": [
    33              {
    34                  "productName": "Alta",
    35                  "productId": "prd_Alta"
    36              }
    37          ]
    38      }
    39  }`,
    40  			expectedPath: "/papi/v1/products?contractId=ctr_1-1TJZFW",
    41  			expectedResponse: &GetProductsResponse{
    42  				AccountID:  "act_1-1TJZFB",
    43  				ContractID: "ctr_1-1TJZFW",
    44  				Products: ProductsItems{
    45  					Items: []ProductItem{
    46  						{
    47  							ProductName: "Alta",
    48  							ProductID:   "prd_Alta",
    49  						},
    50  					},
    51  				},
    52  			},
    53  		},
    54  		"500 internal server error": {
    55  			params: GetProductsRequest{
    56  				ContractID: "ctr_1-1TJZFW",
    57  			},
    58  			responseStatus: http.StatusInternalServerError,
    59  			responseBody: `
    60  {
    61      "type": "internal_error",
    62      "title": "Internal Server Error",
    63      "detail": "Error fetching products",
    64      "status": 500
    65  }
    66  `,
    67  			expectedPath: "/papi/v1/products?contractId=ctr_1-1TJZFW",
    68  			withError: func(t *testing.T, err error) {
    69  				want := &Error{
    70  					Type:       "internal_error",
    71  					Title:      "Internal Server Error",
    72  					Detail:     "Error fetching products",
    73  					StatusCode: http.StatusInternalServerError,
    74  				}
    75  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    76  			},
    77  		},
    78  		"validation error empty contract ID": {
    79  			params: GetProductsRequest{
    80  				ContractID: "",
    81  			},
    82  			withError: func(t *testing.T, err error) {
    83  				want := ErrStructValidation
    84  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    85  				assert.Contains(t, err.Error(), "ContractID")
    86  			},
    87  		},
    88  	}
    89  
    90  	for name, test := range tests {
    91  		t.Run(name, func(t *testing.T) {
    92  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    93  				assert.Equal(t, test.expectedPath, r.URL.String())
    94  				assert.Equal(t, http.MethodGet, r.Method)
    95  				w.WriteHeader(test.responseStatus)
    96  				_, err := w.Write([]byte(test.responseBody))
    97  				assert.NoError(t, err)
    98  			}))
    99  			client := mockAPIClient(t, mockServer)
   100  			result, err := client.GetProducts(context.Background(), test.params)
   101  			if test.withError != nil {
   102  				test.withError(t, err)
   103  				return
   104  			}
   105  			require.NoError(t, err)
   106  			assert.Equal(t, test.expectedResponse, result)
   107  		})
   108  	}
   109  }