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

     1  package datastream
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestDs_GetProperties(t *testing.T) {
    16  	tests := map[string]struct {
    17  		request          GetPropertiesRequest
    18  		responseStatus   int
    19  		responseBody     string
    20  		expectedPath     string
    21  		expectedResponse *PropertiesDetails
    22  		withError        error
    23  	}{
    24  		"200 OK": {
    25  			request: GetPropertiesRequest{
    26  				GroupId: 12345,
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `
    30  {
    31      "groupId": 12345,
    32      "properties": [
    33      {
    34          "contractId": "1-7KLGU",
    35          "propertyId": 382631,
    36          "propertyName": "customp.akamai.com",
    37          "productId": "Ion_Standard",
    38          "productName": "Ion Standard",
    39          "hostnames": [
    40              "customp.akamaize.net",
    41              "customp.akamaized-staging.net"
    42          ]
    43      },
    44      {
    45          "contractId": "1-7KLGU",
    46          "propertyId": 347459,
    47          "propertyName": "example.com",
    48          "productId": "Dynamic_Site_Accelerator",
    49          "productName": "Dynamic Site Accelerator",
    50          "hostnames": [
    51              "example.edgekey.net"
    52          ]
    53      }
    54  ]
    55  }
    56  `,
    57  			expectedPath: "/datastream-config-api/v2/log/groups/12345/properties",
    58  			expectedResponse: &PropertiesDetails{
    59  				GroupID: 12345,
    60  				Properties: []PropertyDetails{
    61  					{
    62  						ContractID:   "1-7KLGU",
    63  						PropertyID:   382631,
    64  						PropertyName: "customp.akamai.com",
    65  						ProductID:    "Ion_Standard",
    66  						ProductName:  "Ion Standard",
    67  						Hostnames: []string{
    68  							"customp.akamaize.net",
    69  							"customp.akamaized-staging.net",
    70  						},
    71  					},
    72  					{
    73  						ContractID:   "1-7KLGU",
    74  						PropertyID:   347459,
    75  						PropertyName: "example.com",
    76  						ProductID:    "Dynamic_Site_Accelerator",
    77  						ProductName:  "Dynamic Site Accelerator",
    78  						Hostnames: []string{
    79  							"example.edgekey.net",
    80  						},
    81  					},
    82  				},
    83  			},
    84  		},
    85  		"validation error": {
    86  			request:   GetPropertiesRequest{},
    87  			withError: ErrStructValidation,
    88  		},
    89  		"400 bad request": {
    90  			request:        GetPropertiesRequest{GroupId: 12345},
    91  			responseStatus: http.StatusBadRequest,
    92  			responseBody: `
    93  {
    94  	"type": "bad-request",
    95  	"title": "Bad Request",
    96  	"detail": "",
    97  	"instance": "baf2671f-7b3a-406d-9dd8-63ef20a01296",
    98  	"statusCode": 400,
    99  	"errors": [
   100  		{
   101  			"type": "bad-request",
   102  			"title": "Bad Request",
   103  			"detail": "Invalid Product Name"
   104  		}
   105  	]
   106  }
   107  `,
   108  			expectedPath: "/datastream-config-api/v2/log/groups/12345/properties",
   109  			withError: &Error{
   110  				Type:       "bad-request",
   111  				Title:      "Bad Request",
   112  				Instance:   "baf2671f-7b3a-406d-9dd8-63ef20a01296",
   113  				StatusCode: http.StatusBadRequest,
   114  				Errors: []RequestErrors{
   115  					{
   116  						Type:   "bad-request",
   117  						Title:  "Bad Request",
   118  						Detail: "Invalid Product Name",
   119  					},
   120  				},
   121  			},
   122  		},
   123  		"403 forbidden": {
   124  			request:        GetPropertiesRequest{GroupId: 12345},
   125  			responseStatus: http.StatusForbidden,
   126  			responseBody: `
   127  {
   128  	"type": "forbidden",
   129  	"title": "Forbidden",
   130  	"detail": "",
   131  	"instance": "28eb43a8-97ae-4c57-98aa-258081582b92",
   132  	"statusCode": 403,
   133  	"errors": [
   134  		{
   135  			"type": "forbidden",
   136  			"title": "Forbidden",
   137  			"detail": "User is not having access for the group. Access denied, please contact support."
   138  		}
   139  	]
   140  }
   141  `,
   142  			expectedPath: "/datastream-config-api/v2/log/groups/12345/properties",
   143  			withError: &Error{
   144  				Type:       "forbidden",
   145  				Title:      "Forbidden",
   146  				Instance:   "28eb43a8-97ae-4c57-98aa-258081582b92",
   147  				StatusCode: http.StatusForbidden,
   148  				Errors: []RequestErrors{
   149  					{
   150  						Type:   "forbidden",
   151  						Title:  "Forbidden",
   152  						Detail: "User is not having access for the group. Access denied, please contact support.",
   153  					},
   154  				},
   155  			},
   156  		},
   157  	}
   158  
   159  	for name, test := range tests {
   160  		t.Run(name, func(t *testing.T) {
   161  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   162  				assert.Equal(t, test.expectedPath, r.URL.String())
   163  				assert.Equal(t, http.MethodGet, r.Method)
   164  				w.WriteHeader(test.responseStatus)
   165  				_, err := w.Write([]byte(test.responseBody))
   166  				assert.NoError(t, err)
   167  			}))
   168  			client := mockAPIClient(t, mockServer)
   169  			result, err := client.GetProperties(context.Background(), test.request)
   170  			if test.withError != nil {
   171  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   172  				return
   173  			}
   174  			require.NoError(t, err)
   175  			assert.Equal(t, test.expectedResponse, result)
   176  		})
   177  	}
   178  }
   179  
   180  func TestDs_GetDatasetFields(t *testing.T) {
   181  	tests := map[string]struct {
   182  		request          GetDatasetFieldsRequest
   183  		responseStatus   int
   184  		responseBody     string
   185  		expectedPath     string
   186  		expectedResponse *DataSets
   187  		withError        error
   188  	}{
   189  		"200 OK": {
   190  			request: GetDatasetFieldsRequest{
   191  				ProductID: nil,
   192  			},
   193  			responseStatus: http.StatusOK,
   194  			responseBody: `
   195  {
   196      "datasetFields": [
   197          {
   198              "datasetFieldDescription": "datasetFieldDescription_1",
   199              "datasetFieldGroup": "datasetFieldGroup_1",
   200              "datasetFieldId": 1000,
   201              "datasetFieldJsonKey": "datasetFieldJsonKey_1",
   202              "datasetFieldName": "datasetFieldName_1"
   203          },
   204          {
   205              "datasetFieldDescription": "datasetFieldDescription_2",
   206              "datasetFieldGroup": "datasetFieldGroup_2",
   207              "datasetFieldId": 1001,
   208              "datasetFieldJsonKey": "datasetFieldJsonKey_2",
   209              "datasetFieldName": "datasetFieldName_2"
   210          },
   211          {
   212              "datasetFieldDescription": "datasetFieldDescription_3",
   213              "datasetFieldGroup": "datasetFieldGroup_3",
   214              "datasetFieldId": 1002,
   215              "datasetFieldJsonKey": "datasetFieldJsonKey_3",
   216              "datasetFieldName": "datasetFieldName_3"
   217          }
   218      ]
   219  }
   220  `,
   221  			expectedPath: "/datastream-config-api/v2/log/datasets-fields",
   222  			expectedResponse: &DataSets{
   223  				DataSetFields: []DataSetField{
   224  					{
   225  						DatasetFieldID:          1000,
   226  						DatasetFieldName:        "datasetFieldName_1",
   227  						DatasetFieldJsonKey:     "datasetFieldJsonKey_1",
   228  						DatasetFieldGroup:       "datasetFieldGroup_1",
   229  						DatasetFieldDescription: "datasetFieldDescription_1",
   230  					},
   231  					{
   232  						DatasetFieldID:          1001,
   233  						DatasetFieldName:        "datasetFieldName_2",
   234  						DatasetFieldJsonKey:     "datasetFieldJsonKey_2",
   235  						DatasetFieldGroup:       "datasetFieldGroup_2",
   236  						DatasetFieldDescription: "datasetFieldDescription_2",
   237  					},
   238  					{
   239  						DatasetFieldID:          1002,
   240  						DatasetFieldName:        "datasetFieldName_3",
   241  						DatasetFieldJsonKey:     "datasetFieldJsonKey_3",
   242  						DatasetFieldGroup:       "datasetFieldGroup_3",
   243  						DatasetFieldDescription: "datasetFieldDescription_3",
   244  					},
   245  				},
   246  			},
   247  		},
   248  		"validation error - invalid product id": {
   249  			request:        GetDatasetFieldsRequest{ProductID: tools.StringPtr("INVALID_PROD_ID")},
   250  			responseStatus: http.StatusBadRequest,
   251  			responseBody: `
   252  {
   253      "errors": [
   254          {
   255              "detail": "Invalid product ID. Provide the correct product ID and try again.", 
   256              "problemId": "800a7291-c694-434a-99b7-8940d788239a", 
   257              "title": "Bad Request", 
   258              "type": "bad-request"
   259          }
   260      ], 
   261      "instance": "6e067164-4a61-429a-abaf-87452fd47036", 
   262      "problemId": "6e067164-4a61-429a-abaf-87452fd47036", 
   263      "status": 400, 
   264      "title": "Bad Request", 
   265      "type": "bad-request"
   266  }
   267  `,
   268  			expectedPath: "/datastream-config-api/v2/log/datasets-fields?productId=INVALID_PROD_ID",
   269  			withError: &Error{
   270  				Type:       "bad-request",
   271  				Title:      "Bad Request",
   272  				Instance:   "6e067164-4a61-429a-abaf-87452fd47036",
   273  				StatusCode: http.StatusBadRequest,
   274  				Errors: []RequestErrors{
   275  					{
   276  						Type:   "bad-request",
   277  						Title:  "Bad Request",
   278  						Detail: "Invalid product ID. Provide the correct product ID and try again.",
   279  					},
   280  				},
   281  			},
   282  		},
   283  	}
   284  
   285  	for name, test := range tests {
   286  		t.Run(name, func(t *testing.T) {
   287  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   288  				assert.Equal(t, test.expectedPath, r.URL.String())
   289  				assert.Equal(t, http.MethodGet, r.Method)
   290  				w.WriteHeader(test.responseStatus)
   291  				_, err := w.Write([]byte(test.responseBody))
   292  				assert.NoError(t, err)
   293  			}))
   294  			client := mockAPIClient(t, mockServer)
   295  			result, err := client.GetDatasetFields(context.Background(), test.request)
   296  			if test.withError != nil {
   297  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   298  				return
   299  			}
   300  			require.NoError(t, err)
   301  			assert.Equal(t, test.expectedResponse, result)
   302  		})
   303  	}
   304  }