github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/datastream/stream_activation_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_ActivateStream(t *testing.T) {
    15  	tests := map[string]struct {
    16  		request          ActivateStreamRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *DetailedStreamVersion
    21  		withError        error
    22  	}{
    23  		"200 OK": {
    24  			request:        ActivateStreamRequest{StreamID: 3},
    25  			responseStatus: http.StatusOK,
    26  			responseBody: `
    27  {
    28      "contractId": "P-1324", 
    29      "createdBy": "sample_username", 
    30      "createdDate": "2022-11-04T00:49:45Z", 
    31      "collectMidgress": true,
    32      "datasetFields": [
    33          {
    34              "datasetFieldId":1000,
    35              "datasetFieldName":"dataset_field_name_1",
    36              "datasetFieldJsonKey":"dataset_field_json_key_1"
    37          },
    38          {
    39              "datasetFieldId":1002,
    40              "datasetFieldName":"dataset_field_name_2",
    41              "datasetFieldJsonKey":"dataset_field_json_key_2"
    42          },
    43          {
    44              "datasetFieldId":1082,
    45              "datasetFieldName":"dataset_field_name_3",
    46              "datasetFieldJsonKey":"dataset_field_json_key_3"
    47          }
    48      ], 
    49      "deliveryConfiguration": {
    50          "fieldDelimiter": "SPACE", 
    51          "format": "STRUCTURED", 
    52          "frequency": {
    53              "intervalInSeconds": 30
    54          }, 
    55          "uploadFilePrefix": "ak", 
    56          "uploadFileSuffix": "ds"
    57      }, 
    58      "destination": {
    59          "bucket": "sample_bucket", 
    60          "compressLogs": true, 
    61          "destinationType": "S3", 
    62          "displayName": "sample_display_name", 
    63          "path": "/sample_path", 
    64          "region": "us-east-1"
    65      },
    66      "groupId": 1234, 
    67      "latestVersion": 2, 
    68      "modifiedBy": "sample_username2", 
    69      "modifiedDate": "2022-11-04T02:14:29Z", 
    70      "notificationEmails": [
    71          "sample_username@akamai.com"
    72      ], 
    73      "productId": "Adaptive_Media_Delivery", 
    74      "properties": [
    75          {
    76              "propertyId": 1234, 
    77              "propertyName": "sample.com"
    78          }
    79      ], 
    80      "streamId": 3, 
    81      "streamName": "ds2-sample-name", 
    82      "streamStatus": "ACTIVATING", 
    83      "streamVersion": 2
    84  }
    85  `,
    86  			expectedPath: "/datastream-config-api/v2/log/streams/3/activate",
    87  			expectedResponse: &DetailedStreamVersion{
    88  				CollectMidgress: true,
    89  				StreamStatus:    StreamStatusActivating,
    90  				DeliveryConfiguration: DeliveryConfiguration{
    91  					Delimiter: DelimiterTypePtr(DelimiterTypeSpace),
    92  					Format:    FormatTypeStructured,
    93  					Frequency: Frequency{
    94  						IntervalInSeconds: IntervalInSeconds30,
    95  					},
    96  					UploadFilePrefix: "ak",
    97  					UploadFileSuffix: "ds",
    98  				},
    99  				Destination: Destination{
   100  					CompressLogs:    true,
   101  					DisplayName:     "sample_display_name",
   102  					DestinationType: DestinationTypeS3,
   103  					Path:            "/sample_path",
   104  					Bucket:          "sample_bucket",
   105  					Region:          "us-east-1",
   106  				},
   107  				ContractID:  "P-1324",
   108  				CreatedBy:   "sample_username",
   109  				CreatedDate: "2022-11-04T00:49:45Z",
   110  				DatasetFields: []DataSetField{
   111  					{
   112  						DatasetFieldID:      1000,
   113  						DatasetFieldName:    "dataset_field_name_1",
   114  						DatasetFieldJsonKey: "dataset_field_json_key_1",
   115  					},
   116  					{
   117  						DatasetFieldID:      1002,
   118  						DatasetFieldName:    "dataset_field_name_2",
   119  						DatasetFieldJsonKey: "dataset_field_json_key_2",
   120  					},
   121  					{
   122  						DatasetFieldID:      1082,
   123  						DatasetFieldName:    "dataset_field_name_3",
   124  						DatasetFieldJsonKey: "dataset_field_json_key_3",
   125  					},
   126  				},
   127  				NotificationEmails: []string{"sample_username@akamai.com"},
   128  				GroupID:            1234,
   129  				ModifiedBy:         "sample_username2",
   130  				ModifiedDate:       "2022-11-04T02:14:29Z",
   131  				ProductID:          "Adaptive_Media_Delivery",
   132  				Properties: []Property{
   133  					{
   134  						PropertyID:   1234,
   135  						PropertyName: "sample.com",
   136  					},
   137  				},
   138  				StreamID:      3,
   139  				StreamName:    "ds2-sample-name",
   140  				StreamVersion: 2,
   141  				LatestVersion: 2,
   142  			},
   143  		},
   144  		"validation error": {
   145  			request:   ActivateStreamRequest{},
   146  			withError: ErrStructValidation,
   147  		},
   148  		"400 bad request": {
   149  			request:        ActivateStreamRequest{StreamID: 123},
   150  			responseStatus: http.StatusBadRequest,
   151  			responseBody: `
   152  {
   153  	"type": "bad-request",
   154  	"title": "Bad Request",
   155  	"detail": "",
   156  	"instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   157  	"statusCode": 400,
   158  	"errors": [
   159  		{
   160  			"type": "bad-request",
   161  			"title": "Bad Request",
   162  			"detail": "Stream does not exist. Please provide valid stream."
   163  		}
   164  	]
   165  }
   166  `,
   167  			expectedPath: "/datastream-config-api/v2/log/streams/123/activate",
   168  			withError: &Error{
   169  				Type:       "bad-request",
   170  				Title:      "Bad Request",
   171  				Instance:   "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   172  				StatusCode: http.StatusBadRequest,
   173  				Errors: []RequestErrors{
   174  					{
   175  						Type:   "bad-request",
   176  						Title:  "Bad Request",
   177  						Detail: "Stream does not exist. Please provide valid stream.",
   178  					},
   179  				},
   180  			},
   181  		},
   182  	}
   183  
   184  	for name, test := range tests {
   185  		t.Run(name, func(t *testing.T) {
   186  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   187  				assert.Equal(t, test.expectedPath, r.URL.String())
   188  				assert.Equal(t, http.MethodPost, r.Method)
   189  				w.WriteHeader(test.responseStatus)
   190  				_, err := w.Write([]byte(test.responseBody))
   191  				assert.NoError(t, err)
   192  			}))
   193  			client := mockAPIClient(t, mockServer)
   194  			result, err := client.ActivateStream(context.Background(), test.request)
   195  			if test.withError != nil {
   196  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   197  				return
   198  			}
   199  			require.NoError(t, err)
   200  			assert.Equal(t, test.expectedResponse, result)
   201  		})
   202  	}
   203  }
   204  
   205  func TestDs_DeactivateStream(t *testing.T) {
   206  	tests := map[string]struct {
   207  		request          DeactivateStreamRequest
   208  		responseStatus   int
   209  		responseBody     string
   210  		expectedPath     string
   211  		expectedResponse *DetailedStreamVersion
   212  		withError        error
   213  	}{
   214  		"200 ok": {
   215  			request:        DeactivateStreamRequest{StreamID: 3},
   216  			responseStatus: http.StatusOK,
   217  			responseBody: `
   218  {
   219      "contractId": "P-1324", 
   220      "createdBy": "sample_username", 
   221      "createdDate": "2022-11-04T00:49:45Z", 
   222      "collectMidgress": true,
   223      "datasetFields": [
   224          {
   225              "datasetFieldId":1000,
   226              "datasetFieldName":"dataset_field_name_1",
   227              "datasetFieldJsonKey":"dataset_field_json_key_1"
   228          },
   229          {
   230              "datasetFieldId":1002,
   231              "datasetFieldName":"dataset_field_name_2",
   232              "datasetFieldJsonKey":"dataset_field_json_key_2"
   233          },
   234          {
   235              "datasetFieldId":1082,
   236              "datasetFieldName":"dataset_field_name_3",
   237              "datasetFieldJsonKey":"dataset_field_json_key_3"
   238          }
   239      ], 
   240      "deliveryConfiguration": {
   241          "fieldDelimiter": "SPACE", 
   242          "format": "STRUCTURED", 
   243          "frequency": {
   244              "intervalInSeconds": 30
   245          }, 
   246          "uploadFilePrefix": "ak", 
   247          "uploadFileSuffix": "ds"
   248      }, 
   249      "destination": {
   250          "bucket": "sample_bucket", 
   251          "compressLogs": true, 
   252          "destinationType": "S3", 
   253          "displayName": "sample_display_name", 
   254          "path": "/sample_path", 
   255          "region": "us-east-1"
   256      },
   257      "groupId": 1234, 
   258      "latestVersion": 2, 
   259      "modifiedBy": "sample_username2", 
   260      "modifiedDate": "2022-11-04T02:14:29Z", 
   261      "notificationEmails": [
   262          "sample_username@akamai.com"
   263      ], 
   264      "productId": "Adaptive_Media_Delivery", 
   265      "properties": [
   266          {
   267              "propertyId": 1234, 
   268              "propertyName": "sample.com"
   269          }
   270      ], 
   271      "streamId": 3, 
   272      "streamName": "ds2-sample-name", 
   273      "streamStatus": "DEACTIVATING", 
   274      "streamVersion": 2
   275  }
   276  `,
   277  			expectedPath: "/datastream-config-api/v2/log/streams/3/deactivate",
   278  			expectedResponse: &DetailedStreamVersion{
   279  				CollectMidgress: true,
   280  				StreamStatus:    StreamStatusDeactivating,
   281  				DeliveryConfiguration: DeliveryConfiguration{
   282  					Delimiter: DelimiterTypePtr(DelimiterTypeSpace),
   283  					Format:    FormatTypeStructured,
   284  					Frequency: Frequency{
   285  						IntervalInSeconds: IntervalInSeconds30,
   286  					},
   287  					UploadFilePrefix: "ak",
   288  					UploadFileSuffix: "ds",
   289  				},
   290  				Destination: Destination{
   291  					CompressLogs:    true,
   292  					DisplayName:     "sample_display_name",
   293  					DestinationType: DestinationTypeS3,
   294  					Path:            "/sample_path",
   295  					Bucket:          "sample_bucket",
   296  					Region:          "us-east-1",
   297  				},
   298  				ContractID:  "P-1324",
   299  				CreatedBy:   "sample_username",
   300  				CreatedDate: "2022-11-04T00:49:45Z",
   301  				DatasetFields: []DataSetField{
   302  					{
   303  						DatasetFieldID:      1000,
   304  						DatasetFieldName:    "dataset_field_name_1",
   305  						DatasetFieldJsonKey: "dataset_field_json_key_1",
   306  					},
   307  					{
   308  						DatasetFieldID:      1002,
   309  						DatasetFieldName:    "dataset_field_name_2",
   310  						DatasetFieldJsonKey: "dataset_field_json_key_2",
   311  					},
   312  					{
   313  						DatasetFieldID:      1082,
   314  						DatasetFieldName:    "dataset_field_name_3",
   315  						DatasetFieldJsonKey: "dataset_field_json_key_3",
   316  					},
   317  				},
   318  				NotificationEmails: []string{"sample_username@akamai.com"},
   319  				GroupID:            1234,
   320  				ModifiedBy:         "sample_username2",
   321  				ModifiedDate:       "2022-11-04T02:14:29Z",
   322  				ProductID:          "Adaptive_Media_Delivery",
   323  				Properties: []Property{
   324  					{
   325  						PropertyID:   1234,
   326  						PropertyName: "sample.com",
   327  					},
   328  				},
   329  				StreamID:      3,
   330  				StreamName:    "ds2-sample-name",
   331  				StreamVersion: 2,
   332  				LatestVersion: 2,
   333  			},
   334  		},
   335  		"validation error": {
   336  			request:   DeactivateStreamRequest{},
   337  			withError: ErrStructValidation,
   338  		},
   339  		"400 bad request": {
   340  			request:        DeactivateStreamRequest{StreamID: 123},
   341  			responseStatus: http.StatusBadRequest,
   342  			responseBody: `
   343  {
   344  	"type": "bad-request",
   345  	"title": "Bad Request",
   346  	"detail": "",
   347  	"instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   348  	"statusCode": 400,
   349  	"errors": [
   350  		{
   351  			"type": "bad-request",
   352  			"title": "Bad Request",
   353  			"detail": "Stream does not exist. Please provide valid stream."
   354  		}
   355  	]
   356  }
   357  `,
   358  			expectedPath: "/datastream-config-api/v2/log/streams/123/deactivate",
   359  			withError: &Error{
   360  				Type:       "bad-request",
   361  				Title:      "Bad Request",
   362  				Instance:   "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   363  				StatusCode: http.StatusBadRequest,
   364  				Errors: []RequestErrors{
   365  					{
   366  						Type:   "bad-request",
   367  						Title:  "Bad Request",
   368  						Detail: "Stream does not exist. Please provide valid stream.",
   369  					},
   370  				},
   371  			},
   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.MethodPost, 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.DeactivateStream(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  }
   395  
   396  func TestDs_GetActivationHistory(t *testing.T) {
   397  	tests := map[string]struct {
   398  		request          GetActivationHistoryRequest
   399  		responseStatus   int
   400  		responseBody     string
   401  		expectedPath     string
   402  		expectedResponse []ActivationHistoryEntry
   403  		withError        error
   404  	}{
   405  		"200 OK": {
   406  			request:        GetActivationHistoryRequest{StreamID: 3},
   407  			responseStatus: http.StatusOK,
   408  			responseBody: `
   409  [
   410      {
   411          "streamId": 3,
   412          "streamVersion": 2,
   413          "modifiedBy": "user1",
   414          "modifiedDate": "16-01-2020 11:07:12 GMT",
   415          "status": "DEACTIVATED"
   416      },
   417      {
   418          "streamId": 3,
   419          "streamVersion": 2,
   420          "modifiedBy": "user2",
   421          "modifiedDate": "16-01-2020 09:31:02 GMT",
   422          "status": "ACTIVATED"
   423      }
   424  ]
   425  `,
   426  			expectedPath: "/datastream-config-api/v2/log/streams/3/activation-history",
   427  			expectedResponse: []ActivationHistoryEntry{
   428  				{
   429  					ModifiedBy:    "user1",
   430  					ModifiedDate:  "16-01-2020 11:07:12 GMT",
   431  					Status:        StreamStatusDeactivated,
   432  					StreamID:      3,
   433  					StreamVersion: 2,
   434  				},
   435  				{
   436  					ModifiedBy:    "user2",
   437  					ModifiedDate:  "16-01-2020 09:31:02 GMT",
   438  					Status:        StreamStatusActivated,
   439  					StreamID:      3,
   440  					StreamVersion: 2,
   441  				},
   442  			},
   443  		},
   444  		"validation error": {
   445  			request:   GetActivationHistoryRequest{},
   446  			withError: ErrStructValidation,
   447  		},
   448  		"400 bad request": {
   449  			request:        GetActivationHistoryRequest{StreamID: 123},
   450  			responseStatus: http.StatusBadRequest,
   451  			responseBody: `
   452  {
   453  	"type": "bad-request",
   454  	"title": "Bad Request",
   455  	"detail": "",
   456  	"instance": "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   457  	"statusCode": 400,
   458  	"errors": [
   459  		{
   460  			"type": "bad-request",
   461  			"title": "Bad Request",
   462  			"detail": "Stream does not exist. Please provide valid stream."
   463  		}
   464  	]
   465  }
   466  `,
   467  			expectedPath: "/datastream-config-api/v2/log/streams/123/activation-history",
   468  			withError: &Error{
   469  				Type:       "bad-request",
   470  				Title:      "Bad Request",
   471  				Instance:   "df22bc0f-ca8d-4bdb-afea-ffdeef819e22",
   472  				StatusCode: http.StatusBadRequest,
   473  				Errors: []RequestErrors{
   474  					{
   475  						Type:   "bad-request",
   476  						Title:  "Bad Request",
   477  						Detail: "Stream does not exist. Please provide valid stream.",
   478  					},
   479  				},
   480  			},
   481  		},
   482  	}
   483  
   484  	for name, test := range tests {
   485  		t.Run(name, func(t *testing.T) {
   486  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   487  				assert.Equal(t, test.expectedPath, r.URL.String())
   488  				assert.Equal(t, http.MethodGet, r.Method)
   489  				w.WriteHeader(test.responseStatus)
   490  				_, err := w.Write([]byte(test.responseBody))
   491  				assert.NoError(t, err)
   492  			}))
   493  			client := mockAPIClient(t, mockServer)
   494  			result, err := client.GetActivationHistory(context.Background(), test.request)
   495  			if test.withError != nil {
   496  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   497  				return
   498  			}
   499  			require.NoError(t, err)
   500  			assert.Equal(t, test.expectedResponse, result)
   501  		})
   502  	}
   503  }