Skip to content

Commit

Permalink
Add data validation to the tests using GetGeneralEvent api.
Browse files Browse the repository at this point in the history
  • Loading branch information
gailazar300 committed Mar 31, 2024
1 parent e2e7cd4 commit 71b5429
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
37 changes: 27 additions & 10 deletions tests/xscanalyticsevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestXscAddAndUpdateGeneralEvent(t *testing.T) {
testsEventService = services.NewAnalyticsEventService(client)
testsEventService.XscDetails = xscDetails

event := services.XscAnalyticsBasicGeneralEvent{
event := services.XscAnalyticsGeneralEvent{XscAnalyticsBasicGeneralEvent: services.XscAnalyticsBasicGeneralEvent{
EventType: 1,
EventStatus: "started",
EventStatus: services.Started,
Product: "cli",
ProductVersion: "2.53.1",
IsDefaultConfig: false,
Expand All @@ -27,20 +27,37 @@ func TestXscAddAndUpdateGeneralEvent(t *testing.T) {
OsArchitecture: "arm64",
MachineId: "id",
AnalyzerManagerVersion: "1.1.1",
}
msi, err := testsEventService.AddGeneralEvent(services.XscAnalyticsGeneralEvent{XscAnalyticsBasicGeneralEvent: event})
}}
msi, err := testsEventService.AddGeneralEvent(event)
assert.NoError(t, err)
assert.True(t, isValidUUID(msi))

event = services.XscAnalyticsBasicGeneralEvent{
EventStatus: "started",
TotalFindings: 10,
TotalIgnoredFindings: 5,
TotalScanDuration: "15s",
// Validate that the event sent and saved properly in XSC.
resp, err := testsEventService.GetGeneralEvent(msi)
assert.NoError(t, err)
assert.Equal(t, event, *resp)

finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
MultiScanId: msi,
XscAnalyticsBasicGeneralEvent: services.XscAnalyticsBasicGeneralEvent{
EventStatus: services.Completed,
TotalFindings: 10,
TotalIgnoredFindings: 5,
TotalScanDuration: "15s",
},
}

err = testsEventService.UpdateGeneralEvent(services.XscAnalyticsGeneralEventFinalize{XscAnalyticsBasicGeneralEvent: event, MultiScanId: msi})
err = testsEventService.UpdateGeneralEvent(finalizeEvent)
assert.NoError(t, err)

// Validate that the event's update sent and saved properly in XSC.
resp, err = testsEventService.GetGeneralEvent(msi)
assert.NoError(t, err)
event.EventStatus = services.Completed
event.TotalFindings = 10
event.TotalIgnoredFindings = 5
event.TotalScanDuration = "15s"
assert.Equal(t, event, *resp)
}

func isValidUUID(str string) bool {
Expand Down
15 changes: 15 additions & 0 deletions xsc/services/analyticsevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ func (vs *AnalyticsEventService) UpdateGeneralEvent(event XscAnalyticsGeneralEve
return nil
}

// GetGeneralEvent returns event's data matching the provided multi scan id.
func (vs *AnalyticsEventService) GetGeneralEvent(msi string) (*XscAnalyticsGeneralEvent, error) {
httpDetails := vs.XscDetails.CreateHttpClientDetails()
resp, body, _, err := vs.client.SendGet(vs.XscDetails.GetUrl()+"api/v1/event/"+msi, true, &httpDetails)
if err != nil {
return nil, err
}
if err = errorutils.CheckResponseStatus(resp, http.StatusOK); err != nil {
return nil, errorutils.CheckError(errorutils.GenerateResponseError(resp.Status, utils.IndentJson(body)))
}
var response XscAnalyticsGeneralEvent
err = json.Unmarshal(body, &response)
return &response, errorutils.CheckError(err)
}

// XscAnalyticsGeneralEvent extend the basic struct with Frogbot related info.
type XscAnalyticsGeneralEvent struct {
XscAnalyticsBasicGeneralEvent
Expand Down

0 comments on commit 71b5429

Please sign in to comment.