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

     1  //go:build unit
     2  // +build unit
     3  
     4  package alerts
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  var (
    15  	testPluginsConditionJSON = `{
    16  		"id": 333444,
    17  		"name": "Connected Clients (High)",
    18  		"enabled": true,
    19  		"entities": [
    20  			"111222"
    21  		],
    22  		"metric_description": "Connected Clients",
    23  		"metric": "Component/Connection/Clients[connections]",
    24  		"value_function": "average",
    25  		"runbook_url": "https://example.com/runbook",
    26  		"terms": [
    27  			{
    28  				"duration": "5",
    29  				"operator": "above",
    30  				"priority": "critical",
    31  				"threshold": "10",
    32  				"time_function": "all"
    33  			}
    34  		],
    35  		"plugin": {
    36  			"id": "222555",
    37  			"guid": "net.example.newrelic_redis_plugin"
    38  		}
    39  	}`
    40  
    41  	testPluginsCondition = PluginsCondition{
    42  		ID:                333444,
    43  		Name:              "Connected Clients (High)",
    44  		Enabled:           true,
    45  		Entities:          []string{"111222"},
    46  		Metric:            "Component/Connection/Clients[connections]",
    47  		MetricDescription: "Connected Clients",
    48  		RunbookURL:        "https://example.com/runbook",
    49  		Terms: []ConditionTerm{
    50  			{
    51  				Duration:     5,
    52  				Operator:     "above",
    53  				Priority:     "critical",
    54  				Threshold:    10,
    55  				TimeFunction: "all",
    56  			},
    57  		},
    58  		ValueFunction: "average",
    59  		Plugin: AlertPlugin{
    60  			ID:   "222555",
    61  			GUID: "net.example.newrelic_redis_plugin",
    62  		},
    63  	}
    64  )
    65  
    66  func TestListPluginsConditions(t *testing.T) {
    67  	t.Parallel()
    68  	responseJSON := fmt.Sprintf(`{"plugins_conditions": [%s]}`, testPluginsConditionJSON)
    69  	client := newMockResponse(t, responseJSON, http.StatusOK)
    70  
    71  	expected := []*PluginsCondition{
    72  		&testPluginsCondition,
    73  	}
    74  
    75  	actual, err := client.ListPluginsConditions(123)
    76  
    77  	assert.NoError(t, err)
    78  	assert.NotNil(t, actual)
    79  	assert.Equal(t, expected, actual)
    80  }
    81  
    82  func TestGetPluginsCondition(t *testing.T) {
    83  	t.Parallel()
    84  	responseJSON := fmt.Sprintf(`{"plugins_conditions": [%s]}`, testPluginsConditionJSON)
    85  	client := newMockResponse(t, responseJSON, http.StatusOK)
    86  
    87  	actual, err := client.GetPluginsCondition(123, 333444)
    88  
    89  	assert.NoError(t, err)
    90  	assert.NotNil(t, actual)
    91  	assert.Equal(t, &testPluginsCondition, actual)
    92  }
    93  
    94  func TestCreatePluginsCondition(t *testing.T) {
    95  	t.Parallel()
    96  	responseJSON := fmt.Sprintf(`{"plugins_condition": %s}`, testPluginsConditionJSON)
    97  	client := newMockResponse(t, responseJSON, http.StatusCreated)
    98  
    99  	actual, err := client.CreatePluginsCondition(123, testPluginsCondition)
   100  
   101  	assert.NoError(t, err)
   102  	assert.NotNil(t, actual)
   103  	assert.Equal(t, &testPluginsCondition, actual)
   104  }
   105  
   106  func TestUpdatePluginsCondition(t *testing.T) {
   107  	t.Parallel()
   108  	responseJSON := fmt.Sprintf(`{"plugins_condition": %s}`, testPluginsConditionJSON)
   109  	client := newMockResponse(t, responseJSON, http.StatusOK)
   110  
   111  	actual, err := client.UpdatePluginsCondition(testPluginsCondition)
   112  
   113  	assert.NoError(t, err)
   114  	assert.NotNil(t, actual)
   115  	assert.Equal(t, &testPluginsCondition, actual)
   116  }
   117  
   118  func TestDeletePluginsCondition(t *testing.T) {
   119  	t.Parallel()
   120  	responseJSON := fmt.Sprintf(`{"plugins_condition": %s}`, testPluginsConditionJSON)
   121  	client := newMockResponse(t, responseJSON, http.StatusOK)
   122  
   123  	expected := PluginsCondition{
   124  		ID:                333444,
   125  		Name:              "Connected Clients (High)",
   126  		Enabled:           true,
   127  		Entities:          []string{"111222"},
   128  		Metric:            "Component/Connection/Clients[connections]",
   129  		MetricDescription: "Connected Clients",
   130  		RunbookURL:        "https://example.com/runbook",
   131  		Terms: []ConditionTerm{
   132  			{
   133  				Duration:     5,
   134  				Operator:     "above",
   135  				Priority:     "critical",
   136  				Threshold:    10,
   137  				TimeFunction: "all",
   138  			},
   139  		},
   140  		ValueFunction: "average",
   141  		Plugin: AlertPlugin{
   142  			ID:   "222555",
   143  			GUID: "net.example.newrelic_redis_plugin",
   144  		},
   145  	}
   146  
   147  	actual, err := client.DeletePluginsCondition(333444)
   148  
   149  	assert.NoError(t, err)
   150  	assert.NotNil(t, actual)
   151  	assert.Equal(t, &expected, actual)
   152  }