github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.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/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestDs_GetProperties(t *testing.T) {
    15  	tests := map[string]struct {
    16  		request          GetPropertiesRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse []Property
    21  		withError        error
    22  	}{
    23  		"200 OK": {
    24  			request: GetPropertiesRequest{
    25  				GroupId:   12345,
    26  				ProductId: "Download_Delivery",
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `
    30  [
    31      {
    32          "propertyId": 382631,
    33          "propertyName": "customp.akamai.com",
    34          "productId": "Ion_Standard",
    35          "productName": "Ion Standard",
    36          "hostnames": [
    37              "customp.akamaize.net",
    38              "customp.akamaized-staging.net"
    39          ]
    40      },
    41      {
    42          "propertyId": 347459,
    43          "propertyName": "example.com",
    44          "productId": "Dynamic_Site_Accelerator",
    45          "productName": "Dynamic Site Accelerator",
    46          "hostnames": [
    47              "example.edgekey.net"
    48          ]
    49      }
    50  ]
    51  `,
    52  			expectedPath: "/datastream-config-api/v1/log/properties/product/Download_Delivery/group/12345",
    53  			expectedResponse: []Property{
    54  				{
    55  					PropertyID:   382631,
    56  					PropertyName: "customp.akamai.com",
    57  					ProductID:    "Ion_Standard",
    58  					ProductName:  "Ion Standard",
    59  					Hostnames: []string{
    60  						"customp.akamaize.net",
    61  						"customp.akamaized-staging.net",
    62  					},
    63  				},
    64  				{
    65  					PropertyID:   347459,
    66  					PropertyName: "example.com",
    67  					ProductID:    "Dynamic_Site_Accelerator",
    68  					ProductName:  "Dynamic Site Accelerator",
    69  					Hostnames: []string{
    70  						"example.edgekey.net",
    71  					},
    72  				},
    73  			},
    74  		},
    75  		"validation error": {
    76  			request:   GetPropertiesRequest{},
    77  			withError: ErrStructValidation,
    78  		},
    79  		"400 bad request": {
    80  			request:        GetPropertiesRequest{GroupId: 12345, ProductId: "testProductName"},
    81  			responseStatus: http.StatusBadRequest,
    82  			responseBody: `
    83  {
    84  	"type": "bad-request",
    85  	"title": "Bad Request",
    86  	"detail": "",
    87  	"instance": "baf2671f-7b3a-406d-9dd8-63ef20a01296",
    88  	"statusCode": 400,
    89  	"errors": [
    90  		{
    91  			"type": "bad-request",
    92  			"title": "Bad Request",
    93  			"detail": "Invalid Product Name"
    94  		}
    95  	]
    96  }
    97  `,
    98  			expectedPath: "/datastream-config-api/v1/log/properties/product/testProductName/group/12345",
    99  			withError: &Error{
   100  				Type:       "bad-request",
   101  				Title:      "Bad Request",
   102  				Instance:   "baf2671f-7b3a-406d-9dd8-63ef20a01296",
   103  				StatusCode: http.StatusBadRequest,
   104  				Errors: []RequestErrors{
   105  					{
   106  						Type:   "bad-request",
   107  						Title:  "Bad Request",
   108  						Detail: "Invalid Product Name",
   109  					},
   110  				},
   111  			},
   112  		},
   113  		"403 forbidden": {
   114  			request:        GetPropertiesRequest{GroupId: 12345, ProductId: "testProductName"},
   115  			responseStatus: http.StatusForbidden,
   116  			responseBody: `
   117  {
   118  	"type": "forbidden",
   119  	"title": "Forbidden",
   120  	"detail": "",
   121  	"instance": "28eb43a8-97ae-4c57-98aa-258081582b92",
   122  	"statusCode": 403,
   123  	"errors": [
   124  		{
   125  			"type": "forbidden",
   126  			"title": "Forbidden",
   127  			"detail": "User is not having access for the group. Access denied, please contact support."
   128  		}
   129  	]
   130  }
   131  `,
   132  			expectedPath: "/datastream-config-api/v1/log/properties/product/testProductName/group/12345",
   133  			withError: &Error{
   134  				Type:       "forbidden",
   135  				Title:      "Forbidden",
   136  				Instance:   "28eb43a8-97ae-4c57-98aa-258081582b92",
   137  				StatusCode: http.StatusForbidden,
   138  				Errors: []RequestErrors{
   139  					{
   140  						Type:   "forbidden",
   141  						Title:  "Forbidden",
   142  						Detail: "User is not having access for the group. Access denied, please contact support.",
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  
   149  	for name, test := range tests {
   150  		t.Run(name, func(t *testing.T) {
   151  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   152  				assert.Equal(t, test.expectedPath, r.URL.String())
   153  				assert.Equal(t, http.MethodGet, r.Method)
   154  				w.WriteHeader(test.responseStatus)
   155  				_, err := w.Write([]byte(test.responseBody))
   156  				assert.NoError(t, err)
   157  			}))
   158  			client := mockAPIClient(t, mockServer)
   159  			result, err := client.GetProperties(context.Background(), test.request)
   160  			if test.withError != nil {
   161  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   162  				return
   163  			}
   164  			require.NoError(t, err)
   165  			assert.Equal(t, test.expectedResponse, result)
   166  		})
   167  	}
   168  }
   169  
   170  func TestDs_GetPropertiesByGroup(t *testing.T) {
   171  	tests := map[string]struct {
   172  		request          GetPropertiesByGroupRequest
   173  		responseStatus   int
   174  		responseBody     string
   175  		expectedPath     string
   176  		expectedResponse []Property
   177  		withError        error
   178  	}{
   179  		"200 OK": {
   180  			request: GetPropertiesByGroupRequest{
   181  				GroupId: 12345,
   182  			},
   183  			responseStatus: http.StatusOK,
   184  			responseBody: `
   185  [
   186      {
   187          "propertyId": 382631,
   188          "propertyName": "customp.akamai.com",
   189          "productId": "Ion_Standard",
   190          "productName": "Ion Standard",
   191          "hostnames": [
   192              "customp.akamaize.net",
   193              "customp.akamaized-staging.net"
   194          ]
   195      },
   196      {
   197          "propertyId": 347459,
   198          "propertyName": "example.com",
   199          "productId": "Dynamic_Site_Accelerator",
   200          "productName": "Dynamic Site Accelerator",
   201          "hostnames": [
   202              "example.edgekey.net"
   203          ]
   204      }
   205  ]
   206  `,
   207  			expectedPath: "/datastream-config-api/v1/log/properties/group/12345",
   208  			expectedResponse: []Property{
   209  				{
   210  					PropertyID:   382631,
   211  					PropertyName: "customp.akamai.com",
   212  					ProductID:    "Ion_Standard",
   213  					ProductName:  "Ion Standard",
   214  					Hostnames: []string{
   215  						"customp.akamaize.net",
   216  						"customp.akamaized-staging.net",
   217  					},
   218  				},
   219  				{
   220  					PropertyID:   347459,
   221  					PropertyName: "example.com",
   222  					ProductID:    "Dynamic_Site_Accelerator",
   223  					ProductName:  "Dynamic Site Accelerator",
   224  					Hostnames: []string{
   225  						"example.edgekey.net",
   226  					},
   227  				},
   228  			},
   229  		},
   230  		"validation error": {
   231  			request:   GetPropertiesByGroupRequest{},
   232  			withError: ErrStructValidation,
   233  		},
   234  		"403 access forbidden": {
   235  			request:        GetPropertiesByGroupRequest{GroupId: 12345},
   236  			responseStatus: http.StatusForbidden,
   237  			responseBody: `
   238  {
   239  	"type": "forbidden",
   240  	"title": "Forbidden",
   241  	"detail": "",
   242  	"instance": "04fde003-428b-4c2c-94fe-6109af9d231c",
   243  	"statusCode": 403,
   244  	"errors": [
   245  		{
   246  			"type": "forbidden",
   247  			"title": "Forbidden",
   248  			"detail": "User is not having access for the group. Access denied, please contact support."
   249  		}
   250  	]
   251  }
   252  `,
   253  			expectedPath: "/datastream-config-api/v1/log/properties/group/12345",
   254  			withError: &Error{
   255  				Type:       "forbidden",
   256  				Title:      "Forbidden",
   257  				Instance:   "04fde003-428b-4c2c-94fe-6109af9d231c",
   258  				StatusCode: http.StatusForbidden,
   259  				Errors: []RequestErrors{
   260  					{
   261  						Type:   "forbidden",
   262  						Title:  "Forbidden",
   263  						Detail: "User is not having access for the group. Access denied, please contact support.",
   264  					},
   265  				},
   266  			},
   267  		},
   268  	}
   269  
   270  	for name, test := range tests {
   271  		t.Run(name, func(t *testing.T) {
   272  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   273  				assert.Equal(t, test.expectedPath, r.URL.String())
   274  				assert.Equal(t, http.MethodGet, r.Method)
   275  				w.WriteHeader(test.responseStatus)
   276  				_, err := w.Write([]byte(test.responseBody))
   277  				assert.NoError(t, err)
   278  			}))
   279  			client := mockAPIClient(t, mockServer)
   280  			result, err := client.GetPropertiesByGroup(context.Background(), test.request)
   281  			if test.withError != nil {
   282  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   283  				return
   284  			}
   285  			require.NoError(t, err)
   286  			assert.Equal(t, test.expectedResponse, result)
   287  		})
   288  	}
   289  }
   290  
   291  func TestDs_GetDatasetFields(t *testing.T) {
   292  	tests := map[string]struct {
   293  		request          GetDatasetFieldsRequest
   294  		responseStatus   int
   295  		responseBody     string
   296  		expectedPath     string
   297  		expectedResponse []DataSets
   298  		withError        error
   299  	}{
   300  		"200 OK": {
   301  			request: GetDatasetFieldsRequest{
   302  				TemplateName: TemplateNameEdgeLogs,
   303  			},
   304  			responseStatus: http.StatusOK,
   305  			responseBody: `
   306  [
   307  	{
   308  	    "datasetGroupName":"group_name_1",
   309  	    "datasetGroupDescription":"group_desc_1",
   310  	    "datasetFields":[
   311  	        {
   312  	            "datasetFieldId":1000,
   313  	            "datasetFieldName":"dataset_field_name_1",
   314  	            "datasetFieldDescription":"dataset_field_desc_1"
   315  	        },
   316  	        {
   317  	            "datasetFieldId":1002,
   318  	            "datasetFieldName":"dataset_field_name_2",
   319  	            "datasetFieldDescription":"dataset_field_desc_2"
   320  	        }
   321  	    ]
   322  	},
   323  	{
   324  	    "datasetGroupName":"group_name_2",
   325  	    "datasetFields":[
   326  	        {
   327  	            "datasetFieldId":1082,
   328  	            "datasetFieldName":"dataset_field_name_3",
   329  	            "datasetFieldDescription":"dataset_field_desc_3"
   330  	        }
   331  	    ]
   332  	}
   333  ]
   334  `,
   335  			expectedPath: "/datastream-config-api/v1/log/datasets/template/EDGE_LOGS",
   336  			expectedResponse: []DataSets{
   337  				{
   338  					DatasetGroupName:        "group_name_1",
   339  					DatasetGroupDescription: "group_desc_1",
   340  					DatasetFields: []DatasetFields{
   341  						{
   342  							DatasetFieldID:          1000,
   343  							DatasetFieldName:        "dataset_field_name_1",
   344  							DatasetFieldDescription: "dataset_field_desc_1",
   345  						},
   346  						{
   347  							DatasetFieldID:          1002,
   348  							DatasetFieldName:        "dataset_field_name_2",
   349  							DatasetFieldDescription: "dataset_field_desc_2",
   350  						},
   351  					},
   352  				},
   353  				{
   354  					DatasetGroupName: "group_name_2",
   355  					DatasetFields: []DatasetFields{
   356  						{
   357  							DatasetFieldID:          1082,
   358  							DatasetFieldName:        "dataset_field_name_3",
   359  							DatasetFieldDescription: "dataset_field_desc_3",
   360  						},
   361  					},
   362  				},
   363  			},
   364  		},
   365  		"validation error - empty request": {
   366  			request:   GetDatasetFieldsRequest{},
   367  			withError: ErrStructValidation,
   368  		},
   369  		"validation error - invalid enum value": {
   370  			request:   GetDatasetFieldsRequest{TemplateName: "invalidTemplateName"},
   371  			withError: ErrStructValidation,
   372  		},
   373  	}
   374  
   375  	for name, test := range tests {
   376  		t.Run(name, func(t *testing.T) {
   377  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   378  				assert.Equal(t, test.expectedPath, r.URL.String())
   379  				assert.Equal(t, http.MethodGet, r.Method)
   380  				w.WriteHeader(test.responseStatus)
   381  				_, err := w.Write([]byte(test.responseBody))
   382  				assert.NoError(t, err)
   383  			}))
   384  			client := mockAPIClient(t, mockServer)
   385  			result, err := client.GetDatasetFields(context.Background(), test.request)
   386  			if test.withError != nil {
   387  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   388  				return
   389  			}
   390  			require.NoError(t, err)
   391  			assert.Equal(t, test.expectedResponse, result)
   392  		})
   393  	}
   394  }