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

     1  package papi
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"errors"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  	"github.com/tj/assert"
    15  )
    16  
    17  func TestPapi_SearchProperties(t *testing.T) {
    18  	tests := map[string]struct {
    19  		params           SearchRequest
    20  		responseStatus   int
    21  		responseBody     string
    22  		expectedPath     string
    23  		expectedRequest  string
    24  		expectedResponse *SearchResponse
    25  		withError        func(*testing.T, error)
    26  	}{
    27  		"200 OK": {
    28  			params: SearchRequest{
    29  				Key:   "edgeHostname",
    30  				Value: "edgesuite.net",
    31  			},
    32  			responseStatus: http.StatusOK,
    33  			responseBody: `
    34  {
    35      "versions": {
    36          "items": [
    37              {
    38                  "accountId": "accountID_1",
    39                  "assetId": "assetID_1",
    40                  "contractId": "contractID_1",
    41                  "groupId": "groupID_1",
    42                  "productionStatus": "INACTIVE",
    43                  "propertyId": "propertyID_1",
    44                  "propertyName": "propertyName_1",
    45                  "propertyVersion": 1,
    46                  "stagingStatus": "INACTIVE",
    47                  "updatedByUser": "user_1",
    48                  "updatedDate": "2017-08-07T15:39:49Z"
    49              },
    50              {
    51                  "accountId": "accountID_2",
    52                  "assetId": "assetID_2",
    53                  "contractId": "contractID_2",
    54                  "groupId": "groupID_2",
    55                  "productionStatus": "INACTIVE",
    56                  "propertyId": "propertyID_2",
    57                  "propertyName": "propertyName_2",
    58                  "propertyVersion": 2,
    59                  "stagingStatus": "INACTIVE",
    60                  "updatedByUser": "user_2",
    61                  "updatedDate": "2017-08-07T15:39:49Z"
    62              }
    63          ]
    64      }
    65  }`,
    66  			expectedRequest: `
    67  {
    68  	"edgeHostname": "edgesuite.net"
    69  }`,
    70  			expectedPath: "/papi/v1/search/find-by-value",
    71  			expectedResponse: &SearchResponse{
    72  				Versions: SearchItems{
    73  					Items: []SearchItem{
    74  						{
    75  							AccountID:        "accountID_1",
    76  							AssetID:          "assetID_1",
    77  							ContractID:       "contractID_1",
    78  							GroupID:          "groupID_1",
    79  							ProductionStatus: "INACTIVE",
    80  							PropertyID:       "propertyID_1",
    81  							PropertyName:     "propertyName_1",
    82  							PropertyVersion:  1,
    83  							StagingStatus:    "INACTIVE",
    84  							UpdatedByUser:    "user_1",
    85  							UpdatedDate:      "2017-08-07T15:39:49Z",
    86  						},
    87  						{
    88  							AccountID:        "accountID_2",
    89  							AssetID:          "assetID_2",
    90  							ContractID:       "contractID_2",
    91  							GroupID:          "groupID_2",
    92  							ProductionStatus: "INACTIVE",
    93  							PropertyID:       "propertyID_2",
    94  							PropertyName:     "propertyName_2",
    95  							PropertyVersion:  2,
    96  							StagingStatus:    "INACTIVE",
    97  							UpdatedByUser:    "user_2",
    98  							UpdatedDate:      "2017-08-07T15:39:49Z",
    99  						},
   100  					},
   101  				},
   102  			},
   103  		},
   104  		"500 Internal Server Error": {
   105  			params: SearchRequest{
   106  				Key:   "edgeHostname",
   107  				Value: "edgesuite.net",
   108  			},
   109  			responseStatus: http.StatusInternalServerError,
   110  			responseBody: `
   111  {
   112  	"type": "internal_error",
   113      "title": "Internal Server Error",
   114      "detail": "Error searching for property",
   115      "status": 505
   116  }`,
   117  			expectedRequest: `
   118  {
   119  	"edgeHostname": "edgesuite.net"
   120  }`,
   121  			expectedPath: "/papi/v1/search/find-by-value",
   122  			withError: func(t *testing.T, err error) {
   123  				want := &Error{
   124  					Type:       "internal_error",
   125  					Title:      "Internal Server Error",
   126  					Detail:     "Error searching for property",
   127  					StatusCode: http.StatusInternalServerError,
   128  				}
   129  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   130  			},
   131  		},
   132  		"invalid key": {
   133  			params: SearchRequest{
   134  				Key:   "test",
   135  				Value: "edgesuite.net",
   136  			},
   137  			withError: func(t *testing.T, err error) {
   138  				want := ErrStructValidation
   139  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   140  				assert.Contains(t, err.Error(), "SearchKey")
   141  			},
   142  		},
   143  		"empty key": {
   144  			params: SearchRequest{
   145  				Key:   "",
   146  				Value: "edgesuite.net",
   147  			},
   148  			withError: func(t *testing.T, err error) {
   149  				want := ErrStructValidation
   150  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   151  				assert.Contains(t, err.Error(), "SearchKey")
   152  			},
   153  		},
   154  		"empty value": {
   155  			params: SearchRequest{
   156  				Key:   "edgeHostname",
   157  				Value: "",
   158  			},
   159  			withError: func(t *testing.T, err error) {
   160  				want := ErrStructValidation
   161  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   162  				assert.Contains(t, err.Error(), "SearchValue")
   163  			},
   164  		},
   165  	}
   166  
   167  	for name, test := range tests {
   168  		t.Run(name, func(t *testing.T) {
   169  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   170  				assert.Equal(t, test.expectedPath, r.URL.String())
   171  				assert.Equal(t, http.MethodPost, r.Method)
   172  				body, err := ioutil.ReadAll(r.Body)
   173  				require.NoError(t, err)
   174  				var compact bytes.Buffer
   175  				err = json.Compact(&compact, []byte(test.expectedRequest))
   176  				require.NoError(t, err)
   177  				assert.Equal(t, compact.String(), string(body))
   178  				w.WriteHeader(test.responseStatus)
   179  				_, err = w.Write([]byte(test.responseBody))
   180  				assert.NoError(t, err)
   181  			}))
   182  			client := mockAPIClient(t, mockServer)
   183  			result, err := client.SearchProperties(context.Background(), test.params)
   184  			if test.withError != nil {
   185  				test.withError(t, err)
   186  				return
   187  			}
   188  			require.NoError(t, err)
   189  			assert.Equal(t, test.expectedResponse, result)
   190  		})
   191  	}
   192  }