github.com/newrelic/newrelic-client-go@v1.1.0/pkg/notifications/channels_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package notifications
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/newrelic/newrelic-client-go/pkg/ai"
    14  	"github.com/newrelic/newrelic-client-go/pkg/nrtime"
    15  )
    16  
    17  var (
    18  	accountID           = 10867072
    19  	testTimestampString = "2022-07-11T08:43:38.943355Z"
    20  	testTimestamp       = nrtime.DateTime(testTimestampString)
    21  	ID                  = "28932668-a894-41f9-bdd2-f7c094d164df"
    22  	destinationId       = "b1e90a32-23b7-4028-b2c7-ffbdfe103852"
    23  
    24  	testCreateChannelResponseJSON = `{
    25  		"aiNotificationsCreateChannel": {
    26  		  "channel": {
    27  			"accountId": 10867072,
    28  			"active": true,
    29  			"createdAt": "2022-07-11T08:43:38.943355Z",
    30  			"destinationId": "b1e90a32-23b7-4028-b2c7-ffbdfe103852",
    31  			"id": "28932668-a894-41f9-bdd2-f7c094d164df",
    32  			"name": "test-notification-channel-1",
    33  			"product": "IINT",
    34  			"properties": [
    35  			  {
    36  				"displayValue": "",
    37  				"key": "payload",
    38  				"label": "Payload Template",
    39  				"value": "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}"
    40  			  }
    41  			],
    42  			"status": "DEFAULT",
    43  			"type": "WEBHOOK",
    44  			"updatedAt": "2022-07-11T08:43:38.943355Z",
    45  			"updatedBy": 1547846
    46  		  },
    47  		  "error": null,
    48  		  "errors": []
    49  		}
    50  	}`
    51  
    52  	testUpdateChannelResponseJSON = `{
    53  		"aiNotificationsUpdateChannel": {
    54  		  "channel": {
    55  			"accountId": 10867072,
    56  			"active": false,
    57  			"createdAt": "2022-07-11T08:43:38.943355Z",
    58  			"destinationId": "b1e90a32-23b7-4028-b2c7-ffbdfe103852",
    59  			"id": "28932668-a894-41f9-bdd2-f7c094d164df",
    60  			"name": "test-notification-channel-1-update",
    61  			"product": "IINT",
    62  			"properties": [
    63  			  {
    64  				"displayValue": "",
    65  				"key": "payload",
    66  				"label": "Payload Template",
    67  				"value": "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}"
    68  			  }
    69  			],
    70  			"status": "DEFAULT",
    71  			"type": "WEBHOOK",
    72  			"updatedAt": "2022-07-11T08:43:38.943355Z",
    73  			"updatedBy": 1547846
    74  		  },
    75  		  "error": null,
    76  		  "errors": []
    77  		}
    78  	}`
    79  
    80  	testDeleteChannelResponseJSON = `{
    81  		"aiNotificationsDeleteChannel": {
    82  		  "error": null,
    83  		  "errors": [],
    84  		  "ids": [
    85  			"28932668-a894-41f9-bdd2-f7c094d164df"
    86  		  ]
    87  		}
    88  	}`
    89  
    90  	testGetChannelResponseJSON = `{
    91  		"actor": {
    92  		  "account": {
    93  			"aiNotifications": {
    94  			  "channels": {
    95  				"entities": [
    96  				  {
    97  					"accountId": 10867072,
    98  					"active": true,
    99  					"createdAt": "2022-07-11T08:43:38.943355Z",
   100  					"destinationId": "b1e90a32-23b7-4028-b2c7-ffbdfe103852",
   101  					"id": "28932668-a894-41f9-bdd2-f7c094d164df",
   102  					"name": "test-notification-channel-1",
   103  					"product": "IINT",
   104  					"properties": [
   105  					  {
   106  						"displayValue": "",
   107  						"key": "payload",
   108  						"label": "Payload Template",
   109  						"value": "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}"
   110  					  }
   111  					],
   112  					"status": "DEFAULT",
   113  					"type": "WEBHOOK",
   114  					"updatedAt": "2022-07-11T08:43:38.943355Z",
   115  					"updatedBy": 1547846
   116  				  }
   117  				],
   118  				"error": null,
   119  				"errors": [],
   120  				"nextCursor": null,
   121  				"totalCount": 1
   122  			  }
   123  			}
   124  		  }
   125  		}
   126  	}`
   127  )
   128  
   129  func TestCreateChannel(t *testing.T) {
   130  	t.Parallel()
   131  	respJSON := fmt.Sprintf(`{ "data":%s }`, testCreateChannelResponseJSON)
   132  	notifications := newMockResponse(t, respJSON, http.StatusCreated)
   133  
   134  	channelInput := AiNotificationsChannelInput{
   135  		DestinationId: destinationId,
   136  		Name:          "test-notification-channel-1",
   137  		Product:       AiNotificationsProductTypes.IINT,
   138  		Properties: []AiNotificationsPropertyInput{
   139  			{
   140  				Key:   "payload",
   141  				Label: "Payload Template",
   142  				Value: "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}",
   143  			},
   144  		},
   145  		Type: AiNotificationsChannelTypeTypes.WEBHOOK,
   146  	}
   147  
   148  	expected := &AiNotificationsChannelResponse{
   149  		Channel: AiNotificationsChannel{
   150  			AccountID:     accountID,
   151  			Active:        true,
   152  			CreatedAt:     testTimestamp,
   153  			DestinationId: destinationId,
   154  			ID:            ID,
   155  			Name:          "test-notification-channel-1",
   156  			Product:       AiNotificationsProductTypes.IINT,
   157  			Properties: []AiNotificationsProperty{
   158  				{
   159  					DisplayValue: "",
   160  					Key:          "payload",
   161  					Label:        "Payload Template",
   162  					Value:        "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}",
   163  				},
   164  			},
   165  			Status:    AiNotificationsChannelStatusTypes.DEFAULT,
   166  			Type:      AiNotificationsChannelTypeTypes.WEBHOOK,
   167  			UpdatedAt: testTimestamp,
   168  			UpdatedBy: 1547846,
   169  		},
   170  		Error:  ai.AiNotificationsError{},
   171  		Errors: []ai.AiNotificationsError{},
   172  	}
   173  
   174  	actual, err := notifications.AiNotificationsCreateChannel(accountID, channelInput)
   175  
   176  	assert.NoError(t, err)
   177  	assert.NotNil(t, actual)
   178  	assert.Equal(t, expected, actual)
   179  }
   180  
   181  func TestGetChannel(t *testing.T) {
   182  	t.Parallel()
   183  	respJSON := fmt.Sprintf(`{ "data":%s }`, testGetChannelResponseJSON)
   184  	notifications := newMockResponse(t, respJSON, http.StatusOK)
   185  
   186  	expected := &AiNotificationsChannelsResponse{
   187  		Entities: []AiNotificationsChannel{
   188  			{
   189  				AccountID:     accountID,
   190  				Active:        true,
   191  				CreatedAt:     testTimestamp,
   192  				DestinationId: destinationId,
   193  				ID:            ID,
   194  				Name:          "test-notification-channel-1",
   195  				Product:       AiNotificationsProductTypes.IINT,
   196  				Properties: []AiNotificationsProperty{
   197  					{
   198  						DisplayValue: "",
   199  						Key:          "payload",
   200  						Label:        "Payload Template",
   201  						Value:        "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}",
   202  					},
   203  				},
   204  				Status:    AiNotificationsChannelStatusTypes.DEFAULT,
   205  				Type:      AiNotificationsChannelTypeTypes.WEBHOOK,
   206  				UpdatedAt: testTimestamp,
   207  				UpdatedBy: 1547846,
   208  			},
   209  		},
   210  		Error:      AiNotificationsResponseError{},
   211  		Errors:     []AiNotificationsResponseError{},
   212  		NextCursor: "",
   213  		TotalCount: 1,
   214  	}
   215  
   216  	filters := ai.AiNotificationsChannelFilter{
   217  		ID: ID,
   218  	}
   219  	sorter := AiNotificationsChannelSorter{}
   220  
   221  	actual, err := notifications.GetChannels(accountID, "", filters, sorter)
   222  
   223  	assert.NoError(t, err)
   224  	assert.NotNil(t, actual)
   225  	assert.Equal(t, expected, actual)
   226  }
   227  
   228  func TestUpdateChannel(t *testing.T) {
   229  	t.Parallel()
   230  	respJSON := fmt.Sprintf(`{ "data":%s }`, testUpdateChannelResponseJSON)
   231  	notifications := newMockResponse(t, respJSON, http.StatusCreated)
   232  
   233  	updateChannelInput := AiNotificationsChannelUpdate{
   234  		Name: "test-notification-channel-1-update",
   235  		Properties: []AiNotificationsPropertyInput{
   236  			{
   237  				Key:   "payload",
   238  				Label: "Payload Template",
   239  				Value: "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}",
   240  			},
   241  		},
   242  		Active: false,
   243  	}
   244  
   245  	expected := &AiNotificationsChannelResponse{
   246  		Channel: AiNotificationsChannel{
   247  			AccountID:     accountID,
   248  			Active:        false,
   249  			CreatedAt:     testTimestamp,
   250  			DestinationId: destinationId,
   251  			ID:            ID,
   252  			Name:          "test-notification-channel-1-update",
   253  			Product:       AiNotificationsProductTypes.IINT,
   254  			Properties: []AiNotificationsProperty{
   255  				{
   256  					DisplayValue: "",
   257  					Key:          "payload",
   258  					Label:        "Payload Template",
   259  					Value:        "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}",
   260  				},
   261  			},
   262  			Status:    AiNotificationsChannelStatusTypes.DEFAULT,
   263  			Type:      AiNotificationsChannelTypeTypes.WEBHOOK,
   264  			UpdatedAt: testTimestamp,
   265  			UpdatedBy: 1547846,
   266  		},
   267  		Error:  ai.AiNotificationsError{},
   268  		Errors: []ai.AiNotificationsError{},
   269  	}
   270  
   271  	actual, err := notifications.AiNotificationsUpdateChannel(accountID, updateChannelInput, ID)
   272  
   273  	assert.NoError(t, err)
   274  	assert.NotNil(t, actual)
   275  	assert.Equal(t, expected, actual)
   276  }
   277  
   278  func TestDeleteChannel(t *testing.T) {
   279  	t.Parallel()
   280  	respJSON := fmt.Sprintf(`{ "data":%s }`, testDeleteChannelResponseJSON)
   281  	notifications := newMockResponse(t, respJSON, http.StatusOK)
   282  
   283  	expected := &AiNotificationsDeleteResponse{
   284  		IDs:    []string{ID},
   285  		Errors: []AiNotificationsResponseError{},
   286  		Error:  AiNotificationsResponseError{},
   287  	}
   288  
   289  	actual, err := notifications.AiNotificationsDeleteChannel(accountID, ID)
   290  
   291  	assert.NoError(t, err)
   292  	assert.NotNil(t, actual)
   293  	assert.Equal(t, expected, actual)
   294  }