Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions internal/services/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func findApplicationAndUpdate(applicationName string, applicationsWrapper wrappe
return errors.Errorf("%s: %s", errorConstants.ApplicationNotFound, applicationName)
}

// Check if project is already associated (prevents unnecessary API calls for both when flag enabled/disabled)
for _, id := range applicationResp.ProjectIds {
if id == projectID {
logger.PrintfIfVerbose("Project is already associated with the application. Skipping association")
return nil
}
}

isEnabled, err := checkDirectAssociationEnabled(featureFlagsWrapper, tenantWrapper)
if err != nil {
return errors.Wrap(err, "error while checking if direct association is enabled")
Expand Down Expand Up @@ -140,12 +148,6 @@ func updateApplication(applicationModel *wrappers.ApplicationConfiguration, appl
}

func associateProjectToApplication(applicationID, projectID string, associatedProjectIds []string, applicationsWrapper wrappers.ApplicationsWrapper) error {
for _, id := range associatedProjectIds {
if id == projectID {
logger.PrintfIfVerbose("Project is already associated with the application. Skipping association")
return nil
}
}
associateProjectsModel := &wrappers.AssociateProjectModel{
ProjectIds: []string{projectID},
}
Expand Down
46 changes: 41 additions & 5 deletions internal/services/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"gotest.tools/assert"
)

const (
mockApplicationName = "MOCK"
testProjectName = "test-project"
)

func Test_createApplicationIds(t *testing.T) {
type args struct {
applicationID []string
Expand Down Expand Up @@ -89,11 +94,42 @@ func Test_ProjectAssociation_ToApplicationWithoutDirectAssociation(t *testing.T)
}
}

func Test_AssociateProjectToApplication_ProjectAlreadyAssociated(t *testing.T) {
projectID := "project-123"
associatedProjectIds := []string{"project-123", "project-456"}
applicationName := "app-1"
func Test_FindApplicationAndUpdate_ProjectAlreadyAssociated_FlagEnabled(t *testing.T) {
applicationName := mockApplicationName
projectID := "ProjectID1" // This ID is already in the mock application's ProjectIds
projectName := testProjectName

// Setup mocks
applicationWrapper := &mock.ApplicationsMockWrapper{}
err := associateProjectToApplication(applicationName, projectID, associatedProjectIds, applicationWrapper)
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
tenantWrapper := &mock.TenantConfigurationMockWrapper{}

// Set flag to ENABLED
mock.Flag = wrappers.FeatureFlagResponseModel{
Name: "directAssociationEnabled",
Status: true,
}
mock.Flags = wrappers.FeatureFlagsResponseModel{} // Empty to use single Flag

err := findApplicationAndUpdate(applicationName, applicationWrapper, projectName, projectID, featureFlagsWrapper, tenantWrapper)
assert.NilError(t, err)
}

func Test_FindApplicationAndUpdate_ProjectAlreadyAssociated_FlagDisabled(t *testing.T) {
applicationName := mockApplicationName
projectID := "ProjectID2" // This ID is already in the mock application's ProjectIds
projectName := testProjectName

// Setup mocks
applicationWrapper := &mock.ApplicationsMockWrapper{}
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
tenantWrapper := &mock.TenantConfigurationMockWrapper{}

mock.Flag = wrappers.FeatureFlagResponseModel{
Name: "directAssociationEnabled",
Status: false,
}
mock.Flags = wrappers.FeatureFlagsResponseModel{}
err := findApplicationAndUpdate(applicationName, applicationWrapper, projectName, projectID, featureFlagsWrapper, tenantWrapper)
assert.NilError(t, err)
}
4 changes: 3 additions & 1 deletion internal/wrappers/mock/application-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ func (a ApplicationsMockWrapper) Get(params map[string]string) (*wrappers.Applic
Name: "MOCK",
Description: "This is a mock application",
Criticality: 2,
ProjectIds: []string{"ProjectID1", "ProjectID2", "MOCK", "test_project", "ID-new-project-name", "ID-newProject"},
ProjectIds: []string{"ProjectID1", "ProjectID2", "test_project", "ID-new-project-name"},
CreatedAt: time.Now(),
}
if params["name"] == ExistingApplication {
mockApplication.Name = ExistingApplication
mockApplication.ID = "ID-newProject"
// For ExistingApplication, include "ID-newProject" for polling tests
mockApplication.ProjectIds = []string{"ProjectID1", "ProjectID2", "test_project", "ID-new-project-name", "ID-newProject"}
return &wrappers.ApplicationsResponseModel{
TotalCount: 1,
Applications: []wrappers.Application{mockApplication},
Expand Down
Loading