github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/muting_rules_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package alerts
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"net/http"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var (
    17  	testMutingRuleListResponseJSON = `{
    18  		"actor": {
    19  			"account": {
    20  				"alerts": {
    21  					"mutingRules": [
    22  						{
    23  							"id": "123",
    24  							"accountId": 400304,
    25  							"condition": {
    26  								"conditions": [
    27  									{
    28  										"attribute": "conditionName",
    29  										"operator": "EQUALS",
    30  										"values": [
    31  											"please not me"
    32  										]
    33  									}
    34  								],
    35  								"operator": "AND"
    36  							},
    37  							"createdAt": "2021-01-12T00:50:39.533Z",
    38  							"createdByUser": {
    39  								"email": "testemail@newrelic.com",
    40  								"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
    41  								"id": 1,
    42  								"name": "Test User"
    43  							},
    44  							"description": null,
    45  							"enabled": true,
    46  							"name": "Test Muting Rule",
    47  							"schedule": {
    48  								"endRepeat": null,
    49  								"endTime": "2021-07-08T14:30:00-07:00",
    50  								"nextEndTime": "2021-07-08T14:30:00-07:00",
    51  								"nextStartTime": "2021-07-08T12:30:00-07:00",
    52  								"repeat": "DAILY",
    53  								"repeatCount": 10,
    54  								"startTime": "2021-07-08T12:30:00-07:00",
    55  								"timeZone": "America/Los_Angeles",
    56  								"weeklyRepeatDays": null
    57  							},
    58  							"status": "INACTIVE",
    59  							"updatedAt": "2021-01-12T00:50:39.533Z",
    60  							"updatedByUser": {
    61  								"email": "testemail@newrelic.com",
    62  								"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
    63  								"id": 1,
    64  								"name": "Test User"
    65  							}
    66  						}
    67  					]
    68  				}
    69  			}
    70  		}
    71  	}`
    72  
    73  	testMutingRuleGetResponseJSON = `{
    74  		"actor": {
    75  			"account": {
    76  				"alerts": {
    77  					"mutingRule": {
    78  						"id": "123",
    79  						"accountId": 400304,
    80  						"condition": {
    81  							"conditions": [
    82  								{
    83  									"attribute": "conditionName",
    84  									"operator": "EQUALS",
    85  									"values": [
    86  										"please not me"
    87  									]
    88  								}
    89  							],
    90  							"operator": "AND"
    91  						},
    92  						"createdAt": "2021-01-12T00:50:39.533Z",
    93  						"createdByUser": {
    94  							"email": "testemail@newrelic.com",
    95  							"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
    96  							"id": 1,
    97  							"name": "Test User"
    98  						},
    99  						"description": null,
   100  						"enabled": true,
   101  						"name": "Test Muting Rule",
   102  						"schedule": {
   103  							"endRepeat": null,
   104  							"endTime": "2021-07-08T14:30:00-07:00",
   105  							"nextEndTime": "2021-07-08T14:30:00-07:00",
   106  							"nextStartTime": "2021-07-08T12:30:00-07:00",
   107  							"repeat": "DAILY",
   108  							"repeatCount": 10,
   109  							"startTime": "2021-07-08T12:30:00-07:00",
   110  							"timeZone": "America/Los_Angeles",
   111  							"weeklyRepeatDays": null
   112  						},
   113  						"status": "INACTIVE",
   114  						"updatedAt": "2021-01-12T00:50:39.533Z",
   115  						"updatedByUser": {
   116  							"email": "testemail@newrelic.com",
   117  							"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   118  							"id": 1,
   119  							"name": "Test User"
   120  						}
   121  					}
   122  				}
   123  			}
   124  		}
   125  	}`
   126  
   127  	testMutingRuleCreateResponseJSON = `{
   128  		"alertsMutingRuleCreate": {
   129  			"id": "123",
   130  			"accountId": 400304,
   131  			"condition": {
   132  				"conditions": [
   133  					{
   134  						"attribute": "conditionName",
   135  						"operator": "EQUALS",
   136  						"values": [
   137  							"please not me"
   138  						]
   139  					}
   140  				],
   141  				"operator": "AND"
   142  			},
   143  			"createdAt": "2021-01-12T00:50:39.533Z",
   144  			"createdByUser": {
   145  				"email": "testemail@newrelic.com",
   146  				"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   147  				"id": 1,
   148  				"name": "Test User"
   149  			},
   150  			"description": null,
   151  			"enabled": true,
   152  			"name": "Test Muting Rule",
   153  			"schedule": {
   154  				"endRepeat": null,
   155  				"endTime": "2021-07-08T14:30:00-07:00",
   156  				"nextEndTime": "2021-07-08T14:30:00-07:00",
   157  				"nextStartTime": "2021-07-08T12:30:00-07:00",
   158  				"repeat": "DAILY",
   159  				"repeatCount": 10,
   160  				"startTime": "2021-07-08T12:30:00-07:00",
   161  				"timeZone": "America/Los_Angeles",
   162  				"weeklyRepeatDays": null
   163  			},
   164  			"status": "INACTIVE",
   165  			"updatedAt": "2021-01-12T00:50:39.533Z",
   166  			"updatedByUser": {
   167  				"email": "testemail@newrelic.com",
   168  				"gravatar": "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   169  				"id": 1,
   170  				"name": "Test User"
   171  			}
   172  		}
   173  	}`
   174  
   175  	testMutingRuleUpdateResponseJSON = `{
   176  		"alertsMutingRuleUpdate": {
   177  			"id": "123",
   178  			"accountId": 400304,
   179  			"schedule": {
   180  				"endRepeat": "2021-08-08T12:30:00-07:00",
   181  				"startTime": "2021-07-08T12:30:00-07:00",
   182  				"repeat": null,
   183  				"repeatCount": null,
   184  				"weeklyRepeatDays": null
   185  			}
   186  		}
   187  	}`
   188  
   189  	testMutingRuleDeleteResponseJSON = `{
   190  		"alertsMutingRuleDelete": {
   191  			"id": "123"
   192  		}
   193  	}`
   194  )
   195  
   196  func TestListMutingRules(t *testing.T) {
   197  	t.Parallel()
   198  	respJSON := fmt.Sprintf(`{ "data":%s }`, testMutingRuleListResponseJSON)
   199  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   200  	accountID := 400304
   201  	startTime, err1 := time.Parse(time.RFC3339, "2021-07-08T12:30:00-07:00")
   202  	if err1 != nil {
   203  		t.Fatal(err1)
   204  	}
   205  	endTime, err2 := time.Parse(time.RFC3339, "2021-07-08T14:30:00-07:00")
   206  	if err2 != nil {
   207  		t.Fatal(err2)
   208  	}
   209  	repeatCount := 10
   210  
   211  	expected := []MutingRule{
   212  		{
   213  			ID:        123,
   214  			AccountID: accountID,
   215  			Condition: MutingRuleConditionGroup{
   216  				Conditions: []MutingRuleCondition{
   217  					{
   218  						Attribute: "conditionName",
   219  						Operator:  "EQUALS",
   220  						Values:    []string{"please not me"},
   221  					},
   222  				},
   223  				Operator: "AND",
   224  			},
   225  			CreatedAt: "2021-01-12T00:50:39.533Z",
   226  			CreatedByUser: ByUser{
   227  				Email:    "testemail@newrelic.com",
   228  				Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   229  				ID:       1,
   230  				Name:     "Test User",
   231  			},
   232  			Description: "",
   233  			Enabled:     true,
   234  			Name:        "Test Muting Rule",
   235  			Schedule: &MutingRuleSchedule{
   236  				EndRepeat:        nil,
   237  				EndTime:          &endTime,
   238  				Repeat:           &MutingRuleScheduleRepeatTypes.DAILY,
   239  				RepeatCount:      &repeatCount,
   240  				StartTime:        &startTime,
   241  				TimeZone:         "America/Los_Angeles",
   242  				WeeklyRepeatDays: nil,
   243  			},
   244  			UpdatedAt: "2021-01-12T00:50:39.533Z",
   245  			UpdatedByUser: ByUser{
   246  				Email:    "testemail@newrelic.com",
   247  				Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   248  				ID:       1,
   249  				Name:     "Test User",
   250  			},
   251  		},
   252  	}
   253  
   254  	actual, err := alerts.ListMutingRules(accountID)
   255  
   256  	assert.NoError(t, err)
   257  	assert.NotNil(t, actual)
   258  	assert.Equal(t, expected, actual)
   259  }
   260  
   261  func TestGetMutingRule(t *testing.T) {
   262  	t.Parallel()
   263  	respJSON := fmt.Sprintf(`{ "data":%s }`, testMutingRuleGetResponseJSON)
   264  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   265  	accountID := 400304
   266  	ruleID := 123
   267  	startTime, err1 := time.Parse(time.RFC3339, "2021-07-08T12:30:00-07:00")
   268  	if err1 != nil {
   269  		t.Fatal(err1)
   270  	}
   271  	endTime, err2 := time.Parse(time.RFC3339, "2021-07-08T14:30:00-07:00")
   272  	if err2 != nil {
   273  		t.Fatal(err2)
   274  	}
   275  	repeatCount := 10
   276  
   277  	expected := MutingRule{
   278  		ID:        123,
   279  		AccountID: accountID,
   280  		Condition: MutingRuleConditionGroup{
   281  			Conditions: []MutingRuleCondition{
   282  				{
   283  					Attribute: "conditionName",
   284  					Operator:  "EQUALS",
   285  					Values:    []string{"please not me"},
   286  				},
   287  			},
   288  			Operator: "AND",
   289  		},
   290  		CreatedAt: "2021-01-12T00:50:39.533Z",
   291  		CreatedByUser: ByUser{
   292  			Email:    "testemail@newrelic.com",
   293  			Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   294  			ID:       1,
   295  			Name:     "Test User",
   296  		},
   297  		Description: "",
   298  		Enabled:     true,
   299  		Name:        "Test Muting Rule",
   300  		Schedule: &MutingRuleSchedule{
   301  			EndRepeat:        nil,
   302  			EndTime:          &endTime,
   303  			Repeat:           &MutingRuleScheduleRepeatTypes.DAILY,
   304  			RepeatCount:      &repeatCount,
   305  			StartTime:        &startTime,
   306  			TimeZone:         "America/Los_Angeles",
   307  			WeeklyRepeatDays: nil,
   308  		},
   309  		UpdatedAt: "2021-01-12T00:50:39.533Z",
   310  		UpdatedByUser: ByUser{
   311  			Email:    "testemail@newrelic.com",
   312  			Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   313  			ID:       1,
   314  			Name:     "Test User",
   315  		},
   316  	}
   317  
   318  	actual, err := alerts.GetMutingRule(accountID, ruleID)
   319  
   320  	assert.NoError(t, err)
   321  	assert.NotNil(t, actual)
   322  	assert.Equal(t, expected, *actual)
   323  }
   324  
   325  func TestCreateMutingRule(t *testing.T) {
   326  	t.Parallel()
   327  	respJSON := fmt.Sprintf(`{ "data":%s }`, testMutingRuleCreateResponseJSON)
   328  	alerts := newMockResponse(t, respJSON, http.StatusCreated)
   329  	accountID := 400304
   330  	startTime, err1 := time.Parse(time.RFC3339, "2021-07-08T12:30:00-07:00")
   331  	if err1 != nil {
   332  		t.Fatal(err1)
   333  	}
   334  	endTime, err2 := time.Parse(time.RFC3339, "2021-07-08T14:30:00-07:00")
   335  	if err2 != nil {
   336  		t.Fatal(err2)
   337  	}
   338  	repeatCount := 10
   339  
   340  	expected := MutingRule{
   341  		ID:        123,
   342  		AccountID: accountID,
   343  		Condition: MutingRuleConditionGroup{
   344  			Conditions: []MutingRuleCondition{
   345  				{
   346  					Attribute: "conditionName",
   347  					Operator:  "EQUALS",
   348  					Values:    []string{"please not me"},
   349  				},
   350  			},
   351  			Operator: "AND",
   352  		},
   353  		CreatedAt: "2021-01-12T00:50:39.533Z",
   354  		CreatedByUser: ByUser{
   355  			Email:    "testemail@newrelic.com",
   356  			Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   357  			ID:       1,
   358  			Name:     "Test User",
   359  		},
   360  		Description: "",
   361  		Enabled:     true,
   362  		Name:        "Test Muting Rule",
   363  		Schedule: &MutingRuleSchedule{
   364  			EndRepeat:        nil,
   365  			EndTime:          &endTime,
   366  			Repeat:           &MutingRuleScheduleRepeatTypes.DAILY,
   367  			RepeatCount:      &repeatCount,
   368  			StartTime:        &startTime,
   369  			TimeZone:         "America/Los_Angeles",
   370  			WeeklyRepeatDays: nil,
   371  		},
   372  		UpdatedAt: "2021-01-12T00:50:39.533Z",
   373  		UpdatedByUser: ByUser{
   374  			Email:    "testemail@newrelic.com",
   375  			Gravatar: "https://secure.gravatar.com/avatar/692dc9742bd717014494f5093faff304",
   376  			ID:       1,
   377  			Name:     "Test User",
   378  		},
   379  	}
   380  
   381  	actual, err := alerts.CreateMutingRule(accountID, MutingRuleCreateInput{})
   382  
   383  	assert.NoError(t, err)
   384  	assert.NotNil(t, actual)
   385  	assert.Equal(t, expected, *actual)
   386  }
   387  
   388  func TestUpdateMutingRule(t *testing.T) {
   389  	t.Parallel()
   390  	respJSON := fmt.Sprintf(`{ "data":%s }`, testMutingRuleUpdateResponseJSON)
   391  	alerts := newMockResponse(t, respJSON, http.StatusCreated)
   392  	accountID := 400304
   393  	ruleID := 123
   394  	startTime, err1 := time.Parse(time.RFC3339, "2021-07-08T12:30:00-07:00")
   395  	if err1 != nil {
   396  		t.Fatal(err1)
   397  	}
   398  	endRepeat, err1 := time.Parse(time.RFC3339, "2021-08-08T12:30:00-07:00")
   399  	if err1 != nil {
   400  		t.Fatal(err1)
   401  	}
   402  
   403  	expected := MutingRule{
   404  		ID:        123,
   405  		AccountID: accountID,
   406  		Schedule: &MutingRuleSchedule{
   407  			EndRepeat: &endRepeat,
   408  			StartTime: &startTime,
   409  		},
   410  	}
   411  
   412  	actual, err := alerts.UpdateMutingRule(accountID, ruleID, MutingRuleUpdateInput{})
   413  
   414  	assert.NoError(t, err)
   415  	assert.NotNil(t, actual)
   416  	assert.Equal(t, expected, *actual)
   417  }
   418  
   419  func TestDeleteMutingRule(t *testing.T) {
   420  	t.Parallel()
   421  	respJSON := fmt.Sprintf(`{ "data":%s }`, testMutingRuleDeleteResponseJSON)
   422  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   423  	accountID := 400304
   424  	ruleID := 123
   425  
   426  	err := alerts.DeleteMutingRule(accountID, ruleID)
   427  
   428  	assert.NoError(t, err)
   429  }
   430  
   431  var (
   432  	location, _ = time.LoadLocation("America/Los_Angeles")
   433  
   434  	naiveDateTimeTests = []struct {
   435  		in           time.Time
   436  		out          string
   437  		errorMessage string
   438  	}{
   439  		{time.Date(
   440  			2006, 01, 02, 15, 04, 05, 0, time.UTC), "\"2006-01-02T15:04:05\"", ""},
   441  		{time.Date(
   442  			2006, 01, 02, 15, 04, 05, 0, location), "", "json: error calling MarshalJSON for type alerts.NaiveDateTime: time offset -28800 not allowed. You can call .UTC() on the time provided to reset the offset"},
   443  	}
   444  )
   445  
   446  func TestNaiveDateTimeMarshaling(t *testing.T) {
   447  	for _, tt := range naiveDateTimeTests {
   448  		tt := tt
   449  		expected := tt.out
   450  
   451  		t.Run(tt.in.String(), func(t *testing.T) {
   452  			t.Parallel()
   453  			naiveDateTime := NaiveDateTime{tt.in}
   454  			actual, err := json.Marshal(naiveDateTime)
   455  
   456  			if string(actual) != expected {
   457  				t.Errorf("expected %q, but got %q", expected, string(actual))
   458  			}
   459  
   460  			actualError := ""
   461  
   462  			if err != nil {
   463  				actualError = err.Error()
   464  			}
   465  
   466  			if tt.errorMessage != actualError {
   467  				t.Errorf("expected %q, but got %q", tt.errorMessage, actualError)
   468  			}
   469  		})
   470  	}
   471  }