package services /* TODO: TO BE DEPRECATED IN 0.25 @RS185722 */ import ( "context" "errors" "fmt" "testing" "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "edge-infra.dev/pkg/edge/api/graph/model" sqlquery "edge-infra.dev/pkg/edge/api/sql" ) var ( logClassificationLabelTestEdgeIDs = []string{"a6d0489c-56c3-47fb-84cb-0c2d8051b9f0", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f1", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f2", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f3", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f4", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f5", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f6", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f7", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f8"} classificationTestEdgeIDs = []string{"de4f477e-1f68-40f7-9f1f-f9c9dc08c250", "de4f477e-1f68-40f7-9f1f-f9c9dc08c251", "de4f477e-1f68-40f7-9f1f-f9c9dc08c252", "de4f477e-1f68-40f7-9f1f-f9c9dc08c253", "de4f477e-1f68-40f7-9f1f-f9c9dc08c254", "de4f477e-1f68-40f7-9f1f-f9c9dc08c255", "de4f477e-1f68-40f7-9f1f-f9c9dc08c256", "de4f477e-1f68-40f7-9f1f-f9c9dc08c257", "de4f477e-1f68-40f7-9f1f-f9c9dc08c258"} labelTestEdgeIDs = []string{"9c478a9c-b536-4ea9-94c7-144c87094f60", "9c478a9c-b536-4ea9-94c7-144c87094f61", "9c478a9c-b536-4ea9-94c7-144c87094f62", "9c478a9c-b536-4ea9-94c7-144c87094f63", "9c478a9c-b536-4ea9-94c7-144c87094f64", "9c478a9c-b536-4ea9-94c7-144c87094f65", "9c478a9c-b536-4ea9-94c7-144c87094f66", "9c478a9c-b536-4ea9-94c7-144c87094f67", "9c478a9c-b536-4ea9-94c7-144c87094f68"} labelTestKeys = []string{"label-1", "label-2", "label-3", "label-4", "label-5", "label-6", "label-7", "label-8", "label-9"} logClassificationLabelRows = []string{"log_classification_label_edge_id", "classification_edge_id", "banner_edge_id", "label_edge_id", "label_key", "classification", "pod", "container", "log_type", "log_class", "pattern"} ) func TestCreateClassificationLabel(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() testCases := []struct { name string labelID string classificationID string lclID string mocksQuery func() []*sqlmock.ExpectedQuery mocksExec func() []*sqlmock.ExpectedExec expectedError error }{ { name: "Test 1 - Classification Already Tied to a label", labelID: labelTestEdgeIDs[3], classificationID: classificationTestEdgeIDs[3], lclID: logClassificationLabelTestEdgeIDs[3], mocksQuery: func() []*sqlmock.ExpectedQuery { return []*sqlmock.ExpectedQuery{ mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel). WithArgs(labelTestEdgeIDs[3]). WillReturnRows(sqlmock.NewRows(logClassificationLabelRows). AddRow(logClassificationLabelTestEdgeIDs[3], classificationTestEdgeIDs[3], testClassificationBannerEdgeID, labelTestEdgeIDs[3], labelTestKeys[3], classificationDescriptions[3], classificationPods[3], classificationContainers[3], classificationTypes[3], classificationClasses[0], classificationPatterns[3])), } }, expectedError: errors.New("Error Creating Log Classification Label: Classification is already tied to this label"), }, { name: "Test 2 - Everything works", labelID: labelTestEdgeIDs[0], classificationID: classificationTestEdgeIDs[0], lclID: logClassificationLabelTestEdgeIDs[0], mocksQuery: func() []*sqlmock.ExpectedQuery { return []*sqlmock.ExpectedQuery{ mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel). WithArgs(labelTestEdgeIDs[0]). WillReturnRows(sqlmock.NewRows(logClassificationLabelRows))} }, mocksExec: func() []*sqlmock.ExpectedExec { return []*sqlmock.ExpectedExec{ mock.ExpectExec(sqlquery.CreateLogClassificationLabel). WithArgs(logClassificationLabelTestEdgeIDs[0], classificationTestEdgeIDs[0], labelTestEdgeIDs[0]). WillReturnResult(sqlmock.NewResult(1, 1))} }, expectedError: nil, }, } service := NewLogClassificationLabelService(db) for _, test := range testCases { if test.expectedError == nil { test.mocksQuery() mock.ExpectBegin() test.mocksExec() mock.ExpectCommit() t.Run(test.name, func(t *testing.T) { result, err := service.CreateLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID) assert.NoError(t, err) assert.Contains(t, result, "Entry Created Successfully") }) } else { test.mocksQuery() result, err := service.CreateLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID) t.Run(test.name, func(t *testing.T) { assert.Equal(t, result, "") assert.ErrorContains(t, err, test.expectedError.Error()) }) } } } func TestDeleteClassificationLabel(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() mock.ExpectExec(sqlquery.DeleteLogClassificationLabel). WithArgs(logClassificationLabelTestEdgeIDs[0], classificationEdgeIDs[0], labelTestEdgeIDs[0]). WillReturnResult(sqlmock.NewResult(1, 1)) service := NewLogClassificationLabelService(db) if deleted, err := service.DeleteLogClassificationLabel(context.Background(), logClassificationLabelTestEdgeIDs[0], classificationEdgeIDs[0], labelTestEdgeIDs[0]); !deleted && err != nil { t.Errorf("error was not expected while deleting classification: %s", err) } } func TestUpdateClassificationLabel(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() mock.ExpectBegin() mock.ExpectExec(sqlquery.UpdateLogClassificationLabel). WithArgs(labelTestEdgeIDs[1], classificationTestEdgeIDs[1]). WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectCommit() service := NewLogClassificationLabelService(db) returnedString, err := service.UpdateLogClassificationLabel(context.Background(), classificationTestEdgeIDs[1], labelTestEdgeIDs[1]) assert.NoError(t, err) assert.Contains(t, returnedString, "Updated Successfully.") } func TestGetClassificationLabel(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() testCases := []struct { name string labelID string classificationID string lclID string mocksQuery func() []*sqlmock.ExpectedQuery expectedReturn *model.LogClassificationLabel expectedError error }{ { name: "Test 1- Simple Get", labelID: labelTestEdgeIDs[8], classificationID: classificationTestEdgeIDs[8], lclID: logClassificationLabelTestEdgeIDs[8], mocksQuery: func() []*sqlmock.ExpectedQuery { return []*sqlmock.ExpectedQuery{ mock.ExpectQuery(sqlquery.GetLogClassificationLabel). WithArgs(labelTestEdgeIDs[8], classificationTestEdgeIDs[8], logClassificationLabelTestEdgeIDs[8]). WillReturnRows(sqlmock.NewRows(logClassificationLabelRows). AddRow(logClassificationLabelTestEdgeIDs[8], classificationTestEdgeIDs[8], testClassificationBannerEdgeID, labelTestEdgeIDs[8], labelTestKeys[8], classificationDescriptions[8], classificationPods[8], classificationContainers[8], classificationTypes[8], classificationClasses[1], classificationPatterns[8])), } }, expectedReturn: &model.LogClassificationLabel{ LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[8], ClassificationEdgeID: classificationTestEdgeIDs[8], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[8], LabelName: labelTestKeys[8], Description: classificationDescriptions[8], Pod: classificationPods[8], Container: classificationContainers[8], Type: classificationTypes[8], Class: classificationClasses[1], Pattern: classificationPatterns[8], }, expectedError: nil, }, } for _, test := range testCases { if test.expectedError == nil { fmt.Printf("ExpectedReturn %+v", test.expectedReturn) test.mocksQuery() service := NewLogClassificationLabelService(db) logClassificationLabel, err := service.GetLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID) assert.NoError(t, err) assert.Equal(t, test.expectedReturn.LogClassificationLabelEdgeID, logClassificationLabel.LogClassificationLabelEdgeID) assert.Equal(t, test.expectedReturn.ClassificationEdgeID, logClassificationLabel.ClassificationEdgeID) assert.Equal(t, testClassificationBannerEdgeID, logClassificationLabel.BannerEdgeID) assert.Equal(t, test.expectedReturn.LabelEdgeID, logClassificationLabel.LabelEdgeID) assert.Equal(t, test.expectedReturn.LabelName, logClassificationLabel.LabelName) assert.Equal(t, test.expectedReturn.Description, logClassificationLabel.Description) assert.Equal(t, test.expectedReturn.Pod, logClassificationLabel.Pod) assert.Equal(t, test.expectedReturn.Container, logClassificationLabel.Container) assert.Equal(t, test.expectedReturn.Type, logClassificationLabel.Type) assert.Equal(t, test.expectedReturn.Class, logClassificationLabel.Class) assert.Equal(t, test.expectedReturn.Pattern, logClassificationLabel.Pattern) } } } func TestGetClassificationLabelsByLabel(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() testCases := []struct { name string labelID string mocks func() []*sqlmock.ExpectedQuery expectedError error expectedLabels []*model.LogClassificationLabel }{ { name: "Test 1 - Label Edge Id is blank", labelID: "", expectedError: errors.New("Error Getting Log Classification Labels by Label: Label Edge ID not a valid UUID format"), }, { name: "Test 2 - Label Edge Id is not a valid UID", labelID: "12345", expectedError: errors.New("Error Getting Log Classification Labels by Label: Label Edge ID not a valid UUID format"), }, { name: "Test 3 - Get by Label Edge IDs", labelID: labelTestEdgeIDs[2], mocks: func() []*sqlmock.ExpectedQuery { return []*sqlmock.ExpectedQuery{ mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel). WithArgs(labelTestEdgeIDs[2]). WillReturnRows(sqlmock.NewRows(logClassificationLabelRows). AddRow(logClassificationLabelTestEdgeIDs[2], classificationTestEdgeIDs[2], testClassificationBannerEdgeID, labelTestEdgeIDs[2], labelTestKeys[2], classificationDescriptions[2], classificationPods[2], classificationContainers[2], classificationTypes[2], classificationClasses[0], classificationPatterns[2]). AddRow(logClassificationLabelTestEdgeIDs[4], classificationTestEdgeIDs[4], testClassificationBannerEdgeID, labelTestEdgeIDs[2], labelTestKeys[4], classificationDescriptions[4], classificationPods[4], classificationContainers[4], classificationTypes[4], classificationClasses[1], classificationPatterns[4])), } }, expectedError: nil, //nolint duplicate expectedLabels: []*model.LogClassificationLabel{ { LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[2], ClassificationEdgeID: classificationTestEdgeIDs[2], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[2], LabelName: labelTestKeys[2], Description: classificationDescriptions[2], Pod: classificationPods[2], Container: classificationContainers[2], Type: classificationTypes[2], Class: classificationClasses[0], Pattern: classificationPatterns[2], }, { LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[4], ClassificationEdgeID: classificationTestEdgeIDs[4], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[2], LabelName: labelTestKeys[4], Description: classificationDescriptions[4], Pod: classificationPods[4], Container: classificationContainers[4], Type: classificationTypes[4], Class: classificationClasses[1], Pattern: classificationPatterns[4], }, }, }, } //nolint duplicate lines for _, testCase := range testCases { if testCase.expectedError != nil { service := NewLogClassificationLabelService(db) t.Run(testCase.name, func(t *testing.T) { logClassificationLabels, err := service.GetLogClassificationLabelsByLabel(context.Background(), testCase.labelID) assert.Nil(t, logClassificationLabels) assert.ErrorContains(t, err, testCase.expectedError.Error()) }) } else { service := NewLogClassificationLabelService(db) testCase.mocks() t.Run(testCase.name, func(t *testing.T) { logClassificationLabels, err := service.GetLogClassificationLabelsByLabel(context.Background(), testCase.labelID) assert.NoError(t, err) logClassificationLabelHelper(t, testCase.expectedLabels, logClassificationLabels) }) } } } func TestGetClassificationLabelsByBanner(t *testing.T) { db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) require.NoError(t, err) defer db.Close() testCases := []struct { name string bannerID string mocks func() []*sqlmock.ExpectedQuery expectedError error expectedLabels []*model.LogClassificationLabel }{ { name: "Test 1 - Banner Edge Id is blank", bannerID: "", expectedError: errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID can not be blank"), }, { name: "Test 2 - Banner Edge Id is not a valid UID", bannerID: "12345", expectedError: errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID not a valid UUID format"), }, { name: "Test 3 - Get by Banner Edge IDs", bannerID: testClassificationBannerEdgeID, mocks: func() []*sqlmock.ExpectedQuery { return []*sqlmock.ExpectedQuery{ mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByBanner). WithArgs(testClassificationBannerEdgeID). WillReturnRows(sqlmock.NewRows(logClassificationLabelRows). AddRow(logClassificationLabelTestEdgeIDs[5], classificationTestEdgeIDs[5], testClassificationBannerEdgeID, labelTestEdgeIDs[5], labelTestKeys[5], classificationDescriptions[5], classificationPods[5], classificationContainers[5], classificationTypes[5], classificationClasses[0], classificationPatterns[5]). AddRow(logClassificationLabelTestEdgeIDs[6], classificationTestEdgeIDs[6], testClassificationBannerEdgeID, labelTestEdgeIDs[6], labelTestKeys[6], classificationDescriptions[6], classificationPods[6], classificationContainers[6], classificationTypes[6], classificationClasses[0], classificationPatterns[6]). AddRow(logClassificationLabelTestEdgeIDs[7], classificationTestEdgeIDs[7], testClassificationBannerEdgeID, labelTestEdgeIDs[7], labelTestKeys[7], classificationDescriptions[7], classificationPods[7], classificationContainers[7], classificationTypes[7], classificationClasses[1], classificationPatterns[7])), } }, expectedError: nil, // nolint duplicate expectedLabels: []*model.LogClassificationLabel{ { LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[5], ClassificationEdgeID: classificationTestEdgeIDs[5], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[5], LabelName: labelTestKeys[5], Description: classificationDescriptions[5], Pod: classificationPods[5], Container: classificationContainers[5], Type: classificationTypes[5], Class: classificationClasses[0], Pattern: classificationPatterns[5], }, { LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[6], ClassificationEdgeID: classificationTestEdgeIDs[6], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[6], LabelName: labelTestKeys[6], Description: classificationDescriptions[6], Pod: classificationPods[6], Container: classificationContainers[6], Type: classificationTypes[6], Class: classificationClasses[0], Pattern: classificationPatterns[6], }, { LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[7], ClassificationEdgeID: classificationTestEdgeIDs[7], BannerEdgeID: testClassificationBannerEdgeID, LabelEdgeID: labelTestEdgeIDs[7], LabelName: labelTestKeys[7], Description: classificationDescriptions[7], Pod: classificationPods[7], Container: classificationContainers[7], Type: classificationTypes[7], Class: classificationClasses[1], Pattern: classificationPatterns[7], }, }, }, } //nolint duplicate lines for _, testCase := range testCases { if testCase.expectedError != nil { service := NewLogClassificationLabelService(db) t.Run(testCase.name, func(t *testing.T) { logClassificationLabels, err := service.GetLogClassificationLabelsByBanner(context.Background(), testCase.bannerID) assert.Nil(t, logClassificationLabels) assert.ErrorContains(t, err, testCase.expectedError.Error()) }) } else { service := NewLogClassificationLabelService(db) testCase.mocks() t.Run(testCase.name, func(t *testing.T) { logClassificationLabels, err := service.GetLogClassificationLabelsByBanner(context.Background(), testCase.bannerID) assert.NoError(t, err) logClassificationLabelHelper(t, testCase.expectedLabels, logClassificationLabels) }) } } } func logClassificationLabelHelper(t *testing.T, expectedLabels []*model.LogClassificationLabel, logClassificationLabels []*model.LogClassificationLabel) { for index, logClassificationLabel := range logClassificationLabels { assert.Equal(t, expectedLabels[index].LogClassificationLabelEdgeID, logClassificationLabel.LogClassificationLabelEdgeID) assert.Equal(t, expectedLabels[index].ClassificationEdgeID, logClassificationLabel.ClassificationEdgeID) assert.Equal(t, testClassificationBannerEdgeID, logClassificationLabel.BannerEdgeID) assert.Equal(t, expectedLabels[index].LabelEdgeID, logClassificationLabel.LabelEdgeID) assert.Equal(t, expectedLabels[index].LabelName, logClassificationLabel.LabelName) assert.Equal(t, expectedLabels[index].Description, logClassificationLabel.Description) assert.Equal(t, expectedLabels[index].Pod, logClassificationLabel.Pod) assert.Equal(t, expectedLabels[index].Container, logClassificationLabel.Container) assert.Equal(t, expectedLabels[index].Type, logClassificationLabel.Type) assert.Equal(t, expectedLabels[index].Class, logClassificationLabel.Class) assert.Equal(t, expectedLabels[index].Pattern, logClassificationLabel.Pattern) } }