diff --git a/pkg/assembler/backends/ent/backend/occurrence.go b/pkg/assembler/backends/ent/backend/occurrence.go index eefe9f04ab..49108b4a82 100644 --- a/pkg/assembler/backends/ent/backend/occurrence.go +++ b/pkg/assembler/backends/ent/backend/occurrence.go @@ -98,9 +98,21 @@ func (b *EntBackend) IsOccurrence(ctx context.Context, query *model.IsOccurrence } func (b *EntBackend) IngestOccurrences(ctx context.Context, subjects model.PackageOrSourceInputs, artifacts []*model.ArtifactInputSpec, occurrences []*model.IsOccurrenceInputSpec) ([]*model.IsOccurrence, error) { - // funcName := "IngestOccurrences" - - return nil, nil + var models []*model.IsOccurrence + for i := range occurrences { + var subject model.PackageOrSourceInput + if len(subjects.Packages) > 0 { + subject = model.PackageOrSourceInput{Package: subjects.Packages[i]} + } else { + subject = model.PackageOrSourceInput{Source: subjects.Sources[i]} + } + modelOccurrence, err := b.IngestOccurrence(ctx, subject, *artifacts[i], *occurrences[i]) + if err != nil { + return nil, gqlerror.Errorf("IngestOccurrences failed with element #%v with err: %v", i, err) + } + models = append(models, modelOccurrence) + } + return models, nil } func (b *EntBackend) IngestOccurrence(ctx context.Context, diff --git a/pkg/assembler/backends/ent/backend/occurrence_test.go b/pkg/assembler/backends/ent/backend/occurrence_test.go index 6676faecc9..63b34a3f09 100644 --- a/pkg/assembler/backends/ent/backend/occurrence_test.go +++ b/pkg/assembler/backends/ent/backend/occurrence_test.go @@ -603,3 +603,108 @@ func (s *Suite) TestOccurrence() { }) } } + +func (s *Suite) TestIngestOccurrences() { + type call struct { + PkgSrcs model.PackageOrSourceInputs + Artifacts []*model.ArtifactInputSpec + Occurrences []*model.IsOccurrenceInputSpec + } + tests := []struct { + Name string + InPkg []*model.PkgInputSpec + InSrc []*model.SourceInputSpec + InArt []*model.ArtifactInputSpec + Calls []call + ExpOcc []*model.IsOccurrence + ExpIngestErr bool + ExpQueryErr bool + }{{ + Name: "HappyPath - packages", + InPkg: []*model.PkgInputSpec{p1, p2}, + InArt: []*model.ArtifactInputSpec{a1, a2}, + Calls: []call{ + call{ + PkgSrcs: model.PackageOrSourceInputs{ + Packages: []*model.PkgInputSpec{p1, p2}, + }, + Artifacts: []*model.ArtifactInputSpec{a1, a2}, + Occurrences: []*model.IsOccurrenceInputSpec{{ + Justification: "test justification", + }, { + Justification: "test justification", + }}, + }, + }, + ExpOcc: []*model.IsOccurrence{ + &model.IsOccurrence{ + Subject: p1out, + Artifact: a1out, + Justification: "test justification", + }, &model.IsOccurrence{ + Subject: p2out, + Artifact: a2out, + Justification: "test justification", + }, + }, + }, { + Name: "HappyPath - sources", + InSrc: []*model.SourceInputSpec{s1}, + InArt: []*model.ArtifactInputSpec{a1}, + Calls: []call{ + call{ + PkgSrcs: model.PackageOrSourceInputs{ + Sources: []*model.SourceInputSpec{s1}, + }, + Artifacts: []*model.ArtifactInputSpec{a1}, + Occurrences: []*model.IsOccurrenceInputSpec{{ + Justification: "test justification", + }}, + }, + }, + ExpOcc: []*model.IsOccurrence{ + { + Subject: s1out, + Artifact: a1out, + Justification: "test justification", + }, + }, + }} + ctx := s.Ctx + for _, test := range tests { + s.Run(test.Name, func() { + t := s.T() + b, err := GetBackend(s.Client) + if err != nil { + t.Fatalf("Could not instantiate testing backend: %v", err) + } + for _, p := range test.InPkg { + if _, err := b.IngestPackage(ctx, *p); err != nil { + t.Fatalf("Could not ingest package: %v", err) + } + } + for _, s := range test.InSrc { + if _, err := b.IngestSource(ctx, *s); err != nil { + t.Fatalf("Could not ingest source: %v", err) + } + } + for _, a := range test.InArt { + if _, err := b.IngestArtifact(ctx, a); err != nil { + t.Fatalf("Could not ingest artifact: %v", err) + } + } + for _, o := range test.Calls { + got, err := b.IngestOccurrences(ctx, o.PkgSrcs, o.Artifacts, o.Occurrences) + if (err != nil) != test.ExpIngestErr { + t.Fatalf("did not get expected ingest error, want: %v, got: %v", test.ExpIngestErr, err) + } + if err != nil { + return + } + if diff := cmp.Diff(test.ExpOcc, got, ignoreID); diff != "" { + t.Errorf("Unexpected results. (-want +got):\n%s", diff) + } + } + }) + } +}