github.1485827954.workers.dev/newrelic/newrelic-client-go@v1.1.0/pkg/workflows/workflows_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package workflows
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/newrelic/newrelic-client-go/pkg/ai"
    14  	"github.com/newrelic/newrelic-client-go/pkg/notifications"
    15  
    16  	mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
    17  )
    18  
    19  func TestWorkflows(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	n := newIntegrationTestClient(t)
    23  
    24  	notificationsClient := newNotificationsIntegrationTestClient(t)
    25  
    26  	accountID, err := mock.GetTestAccountID()
    27  	if err != nil {
    28  		t.Skipf("%s", err)
    29  	}
    30  
    31  	// Create a destination to work with in this test
    32  	testIntegrationDestinationNameRandStr := mock.RandSeq(5)
    33  	destination := notifications.AiNotificationsDestinationInput{}
    34  	destination.Type = notifications.AiNotificationsDestinationTypeTypes.WEBHOOK
    35  	destination.Properties = []notifications.AiNotificationsPropertyInput{
    36  		{
    37  			Key:          "url",
    38  			Value:        "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b",
    39  			Label:        "",
    40  			DisplayValue: "",
    41  		},
    42  	}
    43  	destination.Auth = &notifications.AiNotificationsCredentialsInput{
    44  		Type: notifications.AiNotificationsAuthTypeTypes.TOKEN,
    45  		Token: notifications.AiNotificationsTokenAuthInput{
    46  			Token:  "Token",
    47  			Prefix: "Bearer",
    48  		},
    49  	}
    50  	destination.Name = fmt.Sprintf("test-notifications-destination-%s", testIntegrationDestinationNameRandStr)
    51  
    52  	// Test: Create Destination
    53  	createDestinationResult, err := notificationsClient.AiNotificationsCreateDestination(accountID, destination)
    54  	require.NoError(t, err)
    55  	require.NotNil(t, createDestinationResult)
    56  
    57  	destinationID := createDestinationResult.Destination.ID
    58  
    59  	// Create a channel to work with in this test
    60  	testIntegrationChannelNameRandStr := mock.RandSeq(5)
    61  	channel := notifications.AiNotificationsChannelInput{}
    62  	channel.Type = notifications.AiNotificationsChannelTypeTypes.WEBHOOK
    63  	channel.Product = notifications.AiNotificationsProductTypes.IINT
    64  	channel.Properties = []notifications.AiNotificationsPropertyInput{
    65  		{
    66  			Key:          "payload",
    67  			Value:        "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}",
    68  			Label:        "Payload Template",
    69  			DisplayValue: "",
    70  		},
    71  	}
    72  	channel.DestinationId = createDestinationResult.Destination.ID
    73  	channel.Name = fmt.Sprintf("test-notifications-channel-%s", testIntegrationChannelNameRandStr)
    74  
    75  	// Test: Create Channel
    76  	createChannelResult, err := notificationsClient.AiNotificationsCreateChannel(accountID, channel)
    77  	require.NoError(t, err)
    78  	require.NotNil(t, createChannelResult)
    79  
    80  	channelId := createChannelResult.Channel.ID
    81  
    82  	// Create a workflow to work with in this test
    83  	testIntegrationWorkflowNameRandStr := mock.RandSeq(5)
    84  	workflow := AiWorkflowsCreateWorkflowInput{}
    85  	workflow.WorkflowEnabled = false
    86  	workflow.DestinationsEnabled = true
    87  	workflow.EnrichmentsEnabled = true
    88  	workflow.MutingRulesHandling = AiWorkflowsMutingRulesHandlingTypes.DONT_NOTIFY_FULLY_OR_PARTIALLY_MUTED_ISSUES
    89  	workflow.Name = fmt.Sprintf("test-workflows-workflow-%s", testIntegrationWorkflowNameRandStr)
    90  	workflow.Enrichments = &AiWorkflowsEnrichmentsInput{
    91  		NRQL: []AiWorkflowsNRQLEnrichmentInput{{
    92  			Name: "enrichment-test",
    93  			Configuration: []AiWorkflowsNRQLConfigurationInput{{
    94  				Query: "SELECT * FROM Logs",
    95  			}},
    96  		}},
    97  	}
    98  	workflow.IssuesFilter = AiWorkflowsFilterInput{
    99  		Name: "filter-test",
   100  		Type: AiWorkflowsFilterTypeTypes.FILTER,
   101  		Predicates: []AiWorkflowsPredicateInput{{
   102  			Attribute: "source",
   103  			Operator:  AiWorkflowsOperatorTypes.CONTAINS,
   104  			Values:    []string{"newrelic"},
   105  		}},
   106  	}
   107  	workflow.DestinationConfigurations = []AiWorkflowsDestinationConfigurationInput{{
   108  		ChannelId: channelId,
   109  	}}
   110  
   111  	// Test: Create Workflow
   112  	createResult, err := n.AiWorkflowsCreateWorkflow(accountID, workflow)
   113  	require.NoError(t, err)
   114  	require.NotNil(t, createResult)
   115  
   116  	id := createResult.Workflow.ID
   117  
   118  	// Test: Get Workflow
   119  	filters := ai.AiWorkflowsFilters{
   120  		ID: id,
   121  	}
   122  	getWorkflowResult, err := n.GetWorkflows(accountID, "", filters)
   123  	require.NoError(t, err)
   124  	require.NotNil(t, getWorkflowResult)
   125  	assert.Equal(t, 1, getWorkflowResult.TotalCount)
   126  
   127  	// Test: Update Workflow
   128  	updateWorkflow := AiWorkflowsUpdateWorkflowInput{}
   129  	updateWorkflow.WorkflowEnabled = true
   130  	updateWorkflow.DestinationsEnabled = false
   131  	updateWorkflow.EnrichmentsEnabled = false
   132  	updateWorkflow.MutingRulesHandling = AiWorkflowsMutingRulesHandlingTypes.NOTIFY_ALL_ISSUES
   133  	updateWorkflow.Enrichments = &AiWorkflowsUpdateEnrichmentsInput{
   134  		NRQL: []AiWorkflowsNRQLUpdateEnrichmentInput{{
   135  			Name: "enrichment-test-update",
   136  			Configuration: []AiWorkflowsNRQLConfigurationInput{{
   137  				Query: "SELECT * FROM Metric",
   138  			}},
   139  			ID: createResult.Workflow.Enrichments[0].ID,
   140  		}},
   141  	}
   142  	updateWorkflow.IssuesFilter = AiWorkflowsUpdatedFilterInput{
   143  		FilterInput: AiWorkflowsFilterInput{
   144  			Name: "filter-test-update",
   145  			Type: AiWorkflowsFilterTypeTypes.FILTER,
   146  			Predicates: []AiWorkflowsPredicateInput{{
   147  				Attribute: "source",
   148  				Operator:  AiWorkflowsOperatorTypes.CONTAINS,
   149  				Values:    []string{"servicenow"},
   150  			}},
   151  		},
   152  		ID: createResult.Workflow.IssuesFilter.ID,
   153  	}
   154  	updateWorkflow.DestinationConfigurations = []AiWorkflowsDestinationConfigurationInput{{
   155  		ChannelId: channelId,
   156  	}}
   157  	updateWorkflow.Name = fmt.Sprintf("test-workflows-update-workflow-%s", testIntegrationWorkflowNameRandStr)
   158  	updateWorkflow.ID = id
   159  
   160  	updateWorkflowResult, err := n.AiWorkflowsUpdateWorkflow(accountID, updateWorkflow)
   161  	require.NoError(t, err)
   162  	require.NotNil(t, updateWorkflowResult)
   163  
   164  	// Test: Delete Workflow (with channel)
   165  	deleteResult, err := n.AiWorkflowsDeleteWorkflow(accountID, id)
   166  	require.NoError(t, err)
   167  	require.NotNil(t, deleteResult)
   168  
   169  	// Delete Destination
   170  	deleteDestinationResult, err := notificationsClient.AiNotificationsDeleteDestination(accountID, destinationID)
   171  	require.NoError(t, err)
   172  	require.NotNil(t, deleteDestinationResult)
   173  }