Skip to content

Commit

Permalink
feat(application_setting): Added test cases for application settings pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-newrelic committed Feb 17, 2025
1 parent 51c0f03 commit 342e4d7
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .tutone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1676,9 +1676,9 @@ packages:
field_type_override: "*nrtime.EpochMilliseconds"
skip_type_create: true

- name: apm
path: pkg/apm
import_path: github.com/newrelic/newrelic-client-go/v2/pkg/apm
- name: applicationSettings
path: pkg/applicationSettings
import_path: github.com/newrelic/newrelic-client-go/v2/pkg/applicationSettings
generators:
- typegen
- nerdgraphclient
Expand Down
5 changes: 2 additions & 3 deletions pkg/apm/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type ApplicationsInterface interface {

// Application represents information about a New Relic application.
type Application struct {
GUID EntityGUID `json:"guid,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Language string `json:"language,omitempty"`
Expand Down Expand Up @@ -76,8 +75,8 @@ type ListApplicationsParams struct {
// UpdateApplicationParams represents a set of parameters to be
// used when updating New Relic applications.
type UpdateApplicationParams struct {
Name string `json:"name,omitempty"`
Settings ApplicationSettings `json:"settings,omitempty"`
Name string
Settings ApplicationSettings
}

// ListApplications is used to retrieve New Relic applications.
Expand Down
1 change: 1 addition & 0 deletions pkg/apm/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
EnableRealUserMonitoring: true,
UseServerSideConfig: false,
}

testApplicationLinks = ApplicationLinks{
ServerIDs: []int{},
HostIDs: []int{204260579},
Expand Down
46 changes: 46 additions & 0 deletions pkg/applicationSettings/applicationSettings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package applicationsettings

import (
"github.com/newrelic/newrelic-client-go/v2/internal/http"
"github.com/newrelic/newrelic-client-go/v2/pkg/config"
"github.com/newrelic/newrelic-client-go/v2/pkg/entities"
"github.com/newrelic/newrelic-client-go/v2/pkg/logging"
mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers"
"testing"
)

type ApplicationSettings struct {
client http.Client
logger logging.Logger
config config.Config
}

func New(config config.Config) ApplicationSettings {
client := http.NewClient(config)
pkg := ApplicationSettings{
client: client,
logger: config.GetLogger(),
config: config,
}
return pkg
}

func newIntegrationTestClient(t *testing.T) ApplicationSettings {
tc := mock.NewIntegrationTestConfig(t)
return New(tc)
}

func newMockResponseApm(t *testing.T, mockJSONResponse string, statusCode int) ApplicationSettings {
ts := mock.NewMockServer(t, mockJSONResponse, statusCode)
tc := mock.NewTestConfig(t, ts)

return New(tc)
}

// nolint
func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) entities.Entities {
ts := mock.NewMockServer(t, mockJSONResponse, statusCode)
tc := mock.NewTestConfig(t, ts)

return entities.New(tc)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions pkg/applicationSettings/applicationSettings_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build integration
// +build integration

package applicationsettings

import (
"github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers"
"github.com/stretchr/testify/require"
"testing"
)

func TestIntegrationAgentApplicationSettings_All(t *testing.T) {
t.Parallel()
client := newIntegrationTestClient(t)

aliasName := testhelpers.IntegrationTestApplicationEntityNameNew
// updating an existing application setting
// this is expected to throw no error, and successfully updating application setting
applicationSettingTestResult, err := client.AgentApplicationSettingsUpdate(
testhelpers.IntegrationTestApplicationEntityGUIDNew,
AgentApplicationSettingsUpdateInput{
Alias: &aliasName,
},
)

require.NoError(t, err)
require.NotNil(t, applicationSettingTestResult)
require.Equal(t, aliasName, applicationSettingTestResult.Alias)

// updating an existing application setting
// this is expected to throw no error, and successfully updating application setting
applicationSettingTestResult, err = client.AgentApplicationSettingsUpdate(
testhelpers.IntegrationTestApplicationEntityGUIDNew,
AgentApplicationSettingsUpdateInput{
ApmConfig: &AgentApplicationSettingsApmConfigInput{
ApdexTarget: 0.5,
},
},
)

require.NoError(t, err)
require.NotNil(t, applicationSettingTestResult)
require.Equal(t, aliasName, applicationSettingTestResult.Alias)
require.Equal(t, applicationSettingTestResult.ApmSettings.ApmConfig.ApdexTarget, 0.5)

}

func TestIntegrationAgentApplicationSettingsError(t *testing.T) {
t.Parallel()
client := newIntegrationTestClient(t)

TransactionValue := 0.5
// updating an existing application setting
// this is expected to throw no error, and successfully updating application setting
_, err := client.AgentApplicationSettingsUpdate(
testhelpers.IntegrationTestApplicationEntityGUIDNew,
AgentApplicationSettingsUpdateInput{
TransactionTracer: &AgentApplicationSettingsTransactionTracerInput{
TransactionThresholdValue: &TransactionValue,
},
},
)

require.Error(t, err)
}
96 changes: 96 additions & 0 deletions pkg/applicationSettings/applicationSettings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build unit
// +build unit

package applicationsettings

import (
"fmt"
mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

var (
testApplicationSettings = AgentApplicationSettingsUpdateInput{
Alias: func(s string) *string { return &s }("tf_test_updated"),
ApmConfig: &AgentApplicationSettingsApmConfigInput{
ApdexTarget: 0,
UseServerSideConfig: func(b bool) *bool { return &b }(false),
},
ThreadProfiler: &AgentApplicationSettingsThreadProfilerInput{
Enabled: func(b bool) *bool { return &b }(false),
},
ErrorCollector: &AgentApplicationSettingsErrorCollectorInput{
Enabled: func(b bool) *bool { return &b }(false),
ExpectedErrorCodes: nil,
ExpectedErrorClasses: nil,
IgnoredErrorCodes: nil,
IgnoredErrorClasses: nil,
},
TransactionTracer: &AgentApplicationSettingsTransactionTracerInput{
Enabled: func(b bool) *bool { return &b }(false),
TransactionThresholdValue: func(f float64) *float64 { return &f }(0),
TransactionThresholdType: "",
RecordSql: "",
LogSql: func(b bool) *bool { return &b }(false),
StackTraceThreshold: func(f float64) *float64 { return &f }(0),
ExplainEnabled: func(b bool) *bool { return &b }(false),
ExplainThresholdValue: func(f float64) *float64 { return &f }(0),
ExplainThresholdType: "",
},
TracerType: &AgentApplicationSettingsTracerTypeInput{"NONE"},
}

testApplicationJson = `{
"apmSettings": {
"Alias": "tf_test_updated",
"ApmConfig": {
"ApdexTarget": 0,
"UseServerSideConfig": false
},
"ThreadProfilerEnabled": false,
"ErrorCollector": {
"Enabled": false,
"ExpectedErrorCodes": [],
"ExpectedErrorClasses": [],
"IgnoredErrorCodes": [],
"IgnoredErrorClasses": []
},
"TransactionTracing": {
"Enabled": false,
"TransactionThresholdValue": 0,
"TransactionThresholdType": "off",
"RecordSql": "off",
"LogSql": false,
"StackTraceThresholdValue": 0,
"ExplainQueryPlanEnabled": false,
"ExplainQueryPlanThresholdValue": 0,
"ExplainQueryPlanThresholdType": "off"
},
"TracerType": "NONE"
}
}`
)

func TestGetApmApplicationDetails(t *testing.T) {
t.Parallel()
responseJSON := fmt.Sprintf(`{"application": %s}`, testApplicationJson)
client := newMockResponse(t, responseJSON, http.StatusOK)

actual, err := client.GetEntity(mock.IntegrationTestApplicationEntityGUIDNew)

assert.NoError(t, err)
assert.NotNil(t, actual)
}

func TestUpdateApmApplicationDetails(t *testing.T) {
t.Parallel()
responseJSON := fmt.Sprintf(`{ "application": %s}`, testApplicationJson)
client := newMockResponseApm(t, responseJSON, http.StatusOK)

actual, err := client.AgentApplicationSettingsUpdate(mock.IntegrationTestApplicationEntityGUIDNew, testApplicationSettings)

assert.NoError(t, err)
assert.NotNil(t, actual)
}
4 changes: 3 additions & 1 deletion pkg/apm/types.go → pkg/applicationSettings/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Code generated by tutone: DO NOT EDIT
package apm
// NOTE: This file has been generated by Tutone and had to be manually edited for some structs to meet specific requirements.
// These changes should not be overridden by future Tutone generations.
package applicationsettings

// AgentApplicationSettingsBrowserLoader - Determines which browser loader will be configured. Some allowed return values are specified for backwards-compatibility and do not represent currently allowed values for new applications.
// See [documentation](https://docs.newrelic.com/docs/browser/browser-monitoring/installation/install-browser-monitoring-agent/#agent-types) for further information.
Expand Down

0 comments on commit 342e4d7

Please sign in to comment.