github.com/newrelic/newrelic-client-go@v1.1.0/pkg/dashboards/dashboards_api_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package dashboards
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/newrelic/newrelic-client-go/pkg/common"
    13  	"github.com/newrelic/newrelic-client-go/pkg/entities"
    14  	mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
    15  )
    16  
    17  func newIntegrationTestClient(t *testing.T) Dashboards {
    18  	tc := mock.NewIntegrationTestConfig(t)
    19  
    20  	return New(tc)
    21  }
    22  
    23  func TestIntegrationDashboard_Nil(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	client := newIntegrationTestClient(t)
    27  
    28  	// Test: GetDashboardEntity
    29  	dash, err := client.GetDashboardEntity(`bad-guid`)
    30  	require.NotNil(t, err)
    31  	require.Error(t, err)
    32  	assert.Nil(t, dash)
    33  }
    34  
    35  func TestIntegrationDashboard_Basic(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	testAccountID, err := mock.GetTestAccountID()
    39  	if err != nil {
    40  		t.Skipf("%s", err)
    41  	}
    42  
    43  	client := newIntegrationTestClient(t)
    44  
    45  	// Test vars
    46  	dashboardName := "newrelic-client-go test-dashboard-" + mock.RandSeq(5)
    47  	dashboardInput := DashboardInput{
    48  		Description: "Test description",
    49  		Name:        dashboardName,
    50  		Permissions: entities.DashboardPermissionsTypes.PRIVATE,
    51  		Pages: []DashboardPageInput{
    52  			{
    53  				Description: "Test page description",
    54  				Name:        "Test Page",
    55  				Widgets: []DashboardWidgetInput{
    56  					{
    57  						Title: "Test Text Widget",
    58  						Configuration: DashboardWidgetConfigurationInput{
    59  							Markdown: &DashboardMarkdownWidgetConfigurationInput{
    60  								Text: "Test Text widget **markdown**",
    61  							},
    62  						},
    63  					},
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	// Test: DashboardCreate
    70  	result, err := client.DashboardCreate(testAccountID, dashboardInput)
    71  
    72  	require.NoError(t, err)
    73  	require.NotNil(t, result)
    74  	assert.Equal(t, 0, len(result.Errors))
    75  	require.NotNil(t, result.EntityResult.GUID)
    76  
    77  	dashGUID := result.EntityResult.GUID
    78  
    79  	// Test: GetDashboardEntity
    80  	dash, err := client.GetDashboardEntity(dashGUID)
    81  	require.NoError(t, err)
    82  	require.NotNil(t, dash)
    83  
    84  	assert.Equal(t, dashGUID, dash.GUID)
    85  	assert.Equal(t, dashboardInput.Description, dash.Description)
    86  	assert.Equal(t, dashboardInput.Name, dash.Name)
    87  	assert.Equal(t, dashboardInput.Permissions, dash.Permissions)
    88  
    89  	// Input and Pages are different types so we can not easily compare them...
    90  	assert.Equal(t, len(dashboardInput.Pages), len(dash.Pages))
    91  	require.Equal(t, 1, len(dash.Pages))
    92  	require.Equal(t, 1, len(dash.Pages[0].Widgets))
    93  
    94  	assert.Equal(t, dashboardInput.Pages[0].Widgets[0].Title, dash.Pages[0].Widgets[0].Title)
    95  	assert.Equal(t, dashboardInput.Pages[0].Widgets[0].Configuration.Markdown.Text, dash.Pages[0].Widgets[0].Configuration.Markdown.Text)
    96  	assert.Greater(t, len(dash.Pages[0].Widgets[0].RawConfiguration), 1)
    97  
    98  	// Test: DashboardUpdate
    99  	updatedDashboard := DashboardInput{
   100  		Name:        dash.Name,
   101  		Permissions: dash.Permissions,
   102  		Pages: []DashboardPageInput{
   103  			{
   104  				Name: dash.Pages[0].Name,
   105  				Widgets: []DashboardWidgetInput{
   106  					{
   107  						// Even though the config isn't changing, we have to pass it. 2021-01-11 JT
   108  						Configuration: dashboardInput.Pages[0].Widgets[0].Configuration,
   109  						ID:            dash.Pages[0].Widgets[0].ID,
   110  						Title:         "Updated Title",
   111  					},
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	upDash, err := client.DashboardUpdate(updatedDashboard, dashGUID)
   118  	require.NoError(t, err)
   119  	require.NotNil(t, upDash)
   120  
   121  	require.Equal(t, 1, len(upDash.EntityResult.Pages))
   122  	require.Equal(t, 1, len(upDash.EntityResult.Pages[0].Widgets))
   123  	assert.Equal(t, updatedDashboard.Pages[0].Widgets[0].Title, upDash.EntityResult.Pages[0].Widgets[0].Title)
   124  
   125  	// Test: DashboardDelete
   126  	delRes, err := client.DashboardDelete(dashGUID)
   127  	require.NoError(t, err)
   128  	require.NotNil(t, delRes)
   129  	assert.Equal(t, 0, len(delRes.Errors))
   130  	assert.Equal(t, DashboardDeleteResultStatusTypes.SUCCESS, delRes.Status)
   131  }
   132  
   133  func TestIntegrationDashboard_LinkedEntities(t *testing.T) {
   134  	t.Parallel()
   135  
   136  	testAccountID, err := mock.GetTestAccountID()
   137  	if err != nil {
   138  		t.Skipf("%s", err)
   139  	}
   140  
   141  	client := newIntegrationTestClient(t)
   142  
   143  	// Test vars
   144  	dashboardAName := "newrelic-client-go test-dashboard-" + mock.RandSeq(5)
   145  	dashboardAInput := DashboardInput{
   146  		Description: "Test description",
   147  		Name:        dashboardAName,
   148  		Permissions: entities.DashboardPermissionsTypes.PRIVATE,
   149  		Pages: []DashboardPageInput{
   150  			{
   151  				Description: "Test page description",
   152  				Name:        "Test Page",
   153  				Widgets: []DashboardWidgetInput{
   154  					{
   155  						Title: "Test Text Widget",
   156  						Configuration: DashboardWidgetConfigurationInput{
   157  							Markdown: &DashboardMarkdownWidgetConfigurationInput{
   158  								Text: "Test Text widget **markdown**",
   159  							},
   160  						},
   161  					},
   162  				},
   163  			},
   164  		},
   165  	}
   166  
   167  	// Create a dashboard to reference in linked entity GUIDs
   168  	resultDashA, err := client.DashboardCreate(testAccountID, dashboardAInput)
   169  	require.NoError(t, err)
   170  	require.NotNil(t, resultDashA)
   171  	defer client.DashboardDelete(resultDashA.EntityResult.GUID) // Clean up dashboard A
   172  
   173  	dashboardBName := "newrelic-client-go test-dashboard-" + mock.RandSeq(5)
   174  	dashboardBInput := DashboardInput{
   175  		Description: "Testing dashboard widget with linked entities",
   176  		Name:        dashboardBName,
   177  		Permissions: entities.DashboardPermissionsTypes.PRIVATE,
   178  		Pages: []DashboardPageInput{
   179  			{
   180  				Description: "Test page description",
   181  				Name:        "Test Page",
   182  				Widgets: []DashboardWidgetInput{
   183  					{
   184  						Title: "Widget with linked entities",
   185  						Configuration: DashboardWidgetConfigurationInput{
   186  							Bar: &DashboardBarWidgetConfigurationInput{
   187  								NRQLQueries: []DashboardWidgetNRQLQueryInput{
   188  									{
   189  										AccountID: testAccountID,
   190  										Query:     "FROM Transaction SELECT average(duration) FACET appName",
   191  									},
   192  								},
   193  							},
   194  						},
   195  						LinkedEntityGUIDs: []common.EntityGUID{
   196  							common.EntityGUID(resultDashA.EntityResult.Pages[0].GUID),
   197  						},
   198  					},
   199  				},
   200  			},
   201  		},
   202  	}
   203  
   204  	// Test: Create dashboard with a widget that includes `linkedEntityGuids`
   205  	resultDashB, err := client.DashboardCreate(testAccountID, dashboardBInput)
   206  	require.NoError(t, err)
   207  	require.NotNil(t, resultDashB)
   208  	defer client.DashboardDelete(resultDashB.EntityResult.GUID) // Clean up dashboard B
   209  
   210  	assert.Equal(t, 0, len(resultDashB.Errors))
   211  	assert.NotNil(t, resultDashB.EntityResult.GUID)
   212  
   213  	// Test: GetDashboardEntity
   214  	dashB, err := client.GetDashboardEntity(resultDashB.EntityResult.GUID)
   215  	require.NoError(t, err)
   216  	require.NotNil(t, dashB)
   217  	assert.Greater(t, len(dashB.Pages[0].Widgets[0].LinkedEntities), 0)
   218  }
   219  
   220  func TestIntegrationDashboard_InvalidInput(t *testing.T) {
   221  	t.Parallel()
   222  
   223  	testAccountID, err := mock.GetTestAccountID()
   224  	if err != nil {
   225  		t.Skipf("%s", err)
   226  	}
   227  
   228  	client := newIntegrationTestClient(t)
   229  
   230  	// Test vars
   231  	dashboardName := "newrelic-client-go test-dashboard-" + mock.RandSeq(5)
   232  	dashboardInput := DashboardInput{
   233  		Description: "Testing dashboard widget with linked entities",
   234  		Name:        dashboardName,
   235  		Permissions: entities.DashboardPermissionsTypes.PRIVATE,
   236  		Pages: []DashboardPageInput{
   237  			{
   238  				Description: "Test page description",
   239  				Name:        "Test Page",
   240  				Widgets: []DashboardWidgetInput{
   241  					{
   242  						Title: "Widget with bad NRQL",
   243  						Configuration: DashboardWidgetConfigurationInput{
   244  							Bar: &DashboardBarWidgetConfigurationInput{
   245  								NRQLQueries: []DashboardWidgetNRQLQueryInput{
   246  									{
   247  										AccountID: testAccountID,
   248  										Query:     "This is bad NRQL input",
   249  									},
   250  								},
   251  							},
   252  						},
   253  					},
   254  				},
   255  			},
   256  		},
   257  	}
   258  
   259  	// Test: DashboardCreate
   260  	dash, err := client.DashboardCreate(testAccountID, dashboardInput)
   261  
   262  	require.Nil(t, dash)
   263  	require.Error(t, err)
   264  }