package checkmarx import ( "testing" "github.com/SAP/jenkins-library/pkg/format" piperHttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/log" "github.com/stretchr/testify/assert" ) func TestParse(t *testing.T) { //Use a test CXXML doc testCxxml := ` test/any.ts 7 46 1 slice 5 7 dummy code test/any.ts 7 12 2 location 8 7 dummy code 2 html/other.ts 7 46 1 slice 5 7 dummycode html/other.ts 7 12 2 location 8 7 dummycode2 test/any.ts 7 46 1 slice 5 7 dummy code ` t.Run("Valid config", func(t *testing.T) { opts := piperHttp.ClientOptions{} logger := log.Entry().WithField("package", "SAP/jenkins-library/pkg/checkmarx_test") myTestClient := senderMock{responseBody: `{"shortDescription":"This is a dummy short description."}`, httpStatusCode: 200} sys := SystemInstance{serverURL: "https://cx.server.com", client: &myTestClient, logger: logger} myTestClient.SetOptions(opts) sarif, err := Parse(&sys, []byte(testCxxml), 11037) assert.NoError(t, err, "error") assert.Equal(t, len(sarif.Runs[0].Results), 3) assert.Equal(t, len(sarif.Runs[0].Tool.Driver.Rules), 2) assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolState, "Confirmed") assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolAuditMessage, "Changed status to Confirmed \n Dummy comment") assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolSeverityIndex, 3) assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolSeverity, "High") assert.Equal(t, sarif.Runs[0].Results[2].Properties.AuditRequirementIndex, format.AUDIT_REQUIREMENT_GROUP_1_INDEX) assert.Equal(t, sarif.Runs[0].Results[2].Properties.AuditRequirement, format.AUDIT_REQUIREMENT_GROUP_1_DESC) //assert.Equal(t, "This is a dummy short description.", sarif.Runs[0].Tool.Driver.Rules[0].FullDescription.Text) // ensure the existence of not applicable field (specific Fortify) assert.Equal(t, sarif.Runs[0].Results[2].Properties.InstanceSeverity, "") assert.Equal(t, sarif.Runs[0].Results[2].Properties.Confidence, "") assert.Equal(t, sarif.Runs[0].Results[2].Properties.FortifyCategory, "") }) t.Run("Missing sys", func(t *testing.T) { sarif, err := Parse(nil, []byte(testCxxml), 11037) assert.NoError(t, err, "error") assert.Equal(t, len(sarif.Runs[0].Results), 3) assert.Equal(t, len(sarif.Runs[0].Tool.Driver.Rules), 2) assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolState, "Confirmed") assert.Equal(t, sarif.Runs[0].Results[2].Properties.ToolAuditMessage, "Changed status to Confirmed \n Dummy comment") assert.Equal(t, "Dummy Categories", sarif.Runs[0].Tool.Driver.Rules[0].FullDescription.Text) }) t.Run("Missing data", func(t *testing.T) { _, err := Parse(nil, []byte{}, 11037) assert.Error(t, err, "EOF") }) }