From 6348ad39fd6274de356c5f3e1c330219dcdef3ac Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Tue, 28 Mar 2023 23:09:10 +0100 Subject: [PATCH] Generate factory test content Generate as much as possible of the factory test. Add some extra coverage for getters and setters in generated code. Signed-off-by: Andrea Frittoli --- pkg/api/examples_test.go | 6 +- pkg/api/factory_test.go | 591 +++---------------------- pkg/api/types.go | 2 - pkg/api/vocabulary.go | 40 -- pkg/api/zz_artifactpackaged.go | 4 - pkg/api/zz_artifactpublished.go | 4 - pkg/api/zz_branchcreated.go | 4 - pkg/api/zz_branchdeleted.go | 4 - pkg/api/zz_buildfinished.go | 4 - pkg/api/zz_buildqueued.go | 4 - pkg/api/zz_buildstarted.go | 4 - pkg/api/zz_changeabandoned.go | 4 - pkg/api/zz_changecreated.go | 4 - pkg/api/zz_changemerged.go | 4 - pkg/api/zz_changereviewed.go | 4 - pkg/api/zz_changeupdated.go | 4 - pkg/api/zz_environmentcreated.go | 4 - pkg/api/zz_environmentdeleted.go | 4 - pkg/api/zz_environmentmodified.go | 4 - pkg/api/zz_factory_test.go | 637 +++++++++++++++++++++++++++ pkg/api/zz_incidentdetected.go | 4 - pkg/api/zz_incidentreported.go | 4 - pkg/api/zz_incidentresolved.go | 4 - pkg/api/zz_pipelinerunfinished.go | 4 - pkg/api/zz_pipelinerunqueued.go | 4 - pkg/api/zz_pipelinerunstarted.go | 4 - pkg/api/zz_repositorycreated.go | 4 - pkg/api/zz_repositorydeleted.go | 4 - pkg/api/zz_repositorymodified.go | 4 - pkg/api/zz_servicedeployed.go | 4 - pkg/api/zz_servicepublished.go | 4 - pkg/api/zz_serviceremoved.go | 4 - pkg/api/zz_servicerolledback.go | 4 - pkg/api/zz_serviceupgraded.go | 4 - pkg/api/zz_taskrunfinished.go | 4 - pkg/api/zz_taskrunstarted.go | 4 - pkg/api/zz_testcasefinished.go | 4 - pkg/api/zz_testcasequeued.go | 4 - pkg/api/zz_testcasestarted.go | 4 - pkg/api/zz_testsuitefinished.go | 4 - pkg/api/zz_testsuitestarted.go | 4 - pkg/api/zz_types.go | 74 ++-- tools/generator.go | 19 +- tools/templates/event.go.tmpl | 4 - tools/templates/factory_test.go.tmpl | 45 ++ tools/templates/types.go.tmpl | 4 +- 46 files changed, 795 insertions(+), 771 deletions(-) delete mode 100644 pkg/api/vocabulary.go create mode 100644 pkg/api/zz_factory_test.go create mode 100644 tools/templates/factory_test.go.tmpl diff --git a/pkg/api/examples_test.go b/pkg/api/examples_test.go index eaa40d7..57ad0b6 100644 --- a/pkg/api/examples_test.go +++ b/pkg/api/examples_test.go @@ -325,6 +325,11 @@ func TestExamples(t *testing.T) { if !ok { t.Fatalf("missing produced event for event type: %v", name) } + // Check that the produced event is valid + err := Validate(produced) + if err != nil { + t.Errorf("produced event failed to validate: %v", err) + } consumed, err := NewFromJsonBytes(exampleConsumed) if err != nil { t.Fatalf("didn't expected it to fail, but it did: %v", err) @@ -343,7 +348,6 @@ func TestExamples(t *testing.T) { if d := cmp.Diff(consumed.GetSubject(), produced.GetSubject()); d != "" { t.Errorf("args: diff(-want,+got):\n%s", d) } - }) } } diff --git a/pkg/api/factory_test.go b/pkg/api/factory_test.go index ac77b34..c4fb0d6 100644 --- a/pkg/api/factory_test.go +++ b/pkg/api/factory_test.go @@ -19,6 +19,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "encoding/json" "fmt" "testing" "time" @@ -47,541 +48,23 @@ func testUUID() string { return fmt.Sprintf("%v", u) } +type testNewCDEventType struct { + name string + eventType string + expectedEvent CDEvent +} + +// tests is used in TestNewCDEvents. It's content is +// generated in zz_factory_tests.go +var tests []testNewCDEventType +var testContentType = "application/json" + func TestNewCDEvent(t *testing.T) { + testDataJsonBytes, err := json.Marshal(testDataJson) + if err != nil { + t.Fatalf("didn't expected it to fail, but it did: %v", err) + } - tests := []struct { - name string - eventType CDEventType - expectedEvent CDEvent - }{{ - name: "pipelinerun queued", - eventType: PipelineRunQueuedEventV1, - expectedEvent: &PipelineRunQueuedEvent{ - Context: Context{ - Type: PipelineRunQueuedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: PipelineRunQueuedSubject{ - SubjectBase: SubjectBase{ - Type: PipelineRunSubjectType, - }, - }, - }, - }, { - name: "pipelinerun started", - eventType: PipelineRunStartedEventV1, - expectedEvent: &PipelineRunStartedEvent{ - Context: Context{ - Type: PipelineRunStartedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: PipelineRunStartedSubject{ - SubjectBase: SubjectBase{ - Type: PipelineRunSubjectType, - }, - }, - }, - }, { - name: "pipelinerun finished", - eventType: PipelineRunFinishedEventV1, - expectedEvent: &PipelineRunFinishedEvent{ - Context: Context{ - Type: PipelineRunFinishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: PipelineRunFinishedSubject{ - SubjectBase: SubjectBase{ - Type: PipelineRunSubjectType, - }, - }, - }, - }, { - name: "taskrun started", - eventType: TaskRunStartedEventV1, - expectedEvent: &TaskRunStartedEvent{ - Context: Context{ - Type: TaskRunStartedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TaskRunStartedSubject{ - SubjectBase: SubjectBase{ - Type: TaskRunSubjectType, - }, - }, - }, - }, { - name: "taskrun finished", - eventType: TaskRunFinishedEventV1, - expectedEvent: &TaskRunFinishedEvent{ - Context: Context{ - Type: TaskRunFinishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TaskRunFinishedSubject{ - SubjectBase: SubjectBase{ - Type: TaskRunSubjectType, - }, - }, - }, - }, { - name: "change created", - eventType: ChangeCreatedEventV1, - expectedEvent: &ChangeCreatedEvent{ - Context: Context{ - Type: ChangeCreatedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ChangeCreatedSubject{ - SubjectBase: SubjectBase{ - Type: ChangeSubjectType, - }, - }, - }, - }, { - name: "change updated", - eventType: ChangeUpdatedEventV1, - expectedEvent: &ChangeUpdatedEvent{ - Context: Context{ - Type: ChangeUpdatedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ChangeUpdatedSubject{ - SubjectBase: SubjectBase{ - Type: ChangeSubjectType, - }, - }, - }, - }, { - name: "change reviewed", - eventType: ChangeReviewedEventV1, - expectedEvent: &ChangeReviewedEvent{ - Context: Context{ - Type: ChangeReviewedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ChangeReviewedSubject{ - SubjectBase: SubjectBase{ - Type: ChangeSubjectType, - }, - }, - }, - }, { - name: "change merged", - eventType: ChangeMergedEventV1, - expectedEvent: &ChangeMergedEvent{ - Context: Context{ - Type: ChangeMergedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ChangeMergedSubject{ - SubjectBase: SubjectBase{ - Type: ChangeSubjectType, - }, - }, - }, - }, { - name: "change abandoned", - eventType: ChangeAbandonedEventV1, - expectedEvent: &ChangeAbandonedEvent{ - Context: Context{ - Type: ChangeAbandonedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ChangeAbandonedSubject{ - SubjectBase: SubjectBase{ - Type: ChangeSubjectType, - }, - }, - }, - }, { - name: "repository created", - eventType: RepositoryCreatedEventV1, - expectedEvent: &RepositoryCreatedEvent{ - Context: Context{ - Type: RepositoryCreatedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: RepositoryCreatedSubject{ - SubjectBase: SubjectBase{ - Type: RepositorySubjectType, - }, - }, - }, - }, { - name: "repository modified", - eventType: RepositoryModifiedEventV1, - expectedEvent: &RepositoryModifiedEvent{ - Context: Context{ - Type: RepositoryModifiedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: RepositoryModifiedSubject{ - SubjectBase: SubjectBase{ - Type: RepositorySubjectType, - }, - }, - }, - }, { - name: "repository deleted", - eventType: RepositoryDeletedEventV1, - expectedEvent: &RepositoryDeletedEvent{ - Context: Context{ - Type: RepositoryDeletedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: RepositoryDeletedSubject{ - SubjectBase: SubjectBase{ - Type: RepositorySubjectType, - }, - }, - }, - }, { - name: "branch created", - eventType: BranchCreatedEventV1, - expectedEvent: &BranchCreatedEvent{ - Context: Context{ - Type: BranchCreatedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: BranchCreatedSubject{ - SubjectBase: SubjectBase{ - Type: BranchSubjectType, - }, - }, - }, - }, { - name: "branch deleted", - eventType: BranchDeletedEventV1, - expectedEvent: &BranchDeletedEvent{ - Context: Context{ - Type: BranchDeletedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: BranchDeletedSubject{ - SubjectBase: SubjectBase{ - Type: BranchSubjectType, - }, - }, - }, - }, { - name: "testcase queued", - eventType: TestCaseQueuedEventV1, - expectedEvent: &TestCaseQueuedEvent{ - Context: Context{ - Type: TestCaseQueuedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TestCaseQueuedSubject{ - SubjectBase: SubjectBase{ - Type: TestCaseSubjectType, - }, - }, - }, - }, { - name: "testcase started", - eventType: TestCaseStartedEventV1, - expectedEvent: &TestCaseStartedEvent{ - Context: Context{ - Type: TestCaseStartedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TestCaseStartedSubject{ - SubjectBase: SubjectBase{ - Type: TestCaseSubjectType, - }, - }, - }, - }, { - name: "testcase finished", - eventType: TestCaseFinishedEventV1, - expectedEvent: &TestCaseFinishedEvent{ - Context: Context{ - Type: TestCaseFinishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TestCaseFinishedSubject{ - SubjectBase: SubjectBase{ - Type: TestCaseSubjectType, - }, - }, - }, - }, { - name: "testsuite started", - eventType: TestSuiteStartedEventV1, - expectedEvent: &TestSuiteStartedEvent{ - Context: Context{ - Type: TestSuiteStartedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TestSuiteStartedSubject{ - SubjectBase: SubjectBase{ - Type: TestSuiteSubjectType, - }, - }, - }, - }, { - name: "testsuite finished", - eventType: TestSuiteFinishedEventV1, - expectedEvent: &TestSuiteFinishedEvent{ - Context: Context{ - Type: TestSuiteFinishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: TestSuiteFinishedSubject{ - SubjectBase: SubjectBase{ - Type: TestSuiteSubjectType, - }, - }, - }, - }, { - name: "build queued", - eventType: BuildQueuedEventV1, - expectedEvent: &BuildQueuedEvent{ - Context: Context{ - Type: BuildQueuedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: BuildQueuedSubject{ - SubjectBase: SubjectBase{ - Type: BuildSubjectType, - }, - }, - }, - }, { - name: "build started", - eventType: BuildStartedEventV1, - expectedEvent: &BuildStartedEvent{ - Context: Context{ - Type: BuildStartedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: BuildStartedSubject{ - SubjectBase: SubjectBase{ - Type: BuildSubjectType, - }, - }, - }, - }, { - name: "build finished", - eventType: BuildFinishedEventV1, - expectedEvent: &BuildFinishedEvent{ - Context: Context{ - Type: BuildFinishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: BuildFinishedSubject{ - SubjectBase: SubjectBase{ - Type: BuildSubjectType, - }, - }, - }, - }, { - name: "artifact packaged", - eventType: ArtifactPackagedEventV1, - expectedEvent: &ArtifactPackagedEvent{ - Context: Context{ - Type: ArtifactPackagedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ArtifactPackagedSubject{ - SubjectBase: SubjectBase{ - Type: ArtifactSubjectType, - }, - }, - }, - }, { - name: "artifact published", - eventType: ArtifactPublishedEventV1, - expectedEvent: &ArtifactPublishedEvent{ - Context: Context{ - Type: ArtifactPublishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ArtifactPublishedSubject{ - SubjectBase: SubjectBase{ - Type: ArtifactSubjectType, - }, - }, - }, - }, { - name: "environment created", - eventType: EnvironmentCreatedEventV1, - expectedEvent: &EnvironmentCreatedEvent{ - Context: Context{ - Type: EnvironmentCreatedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: EnvironmentCreatedSubject{ - SubjectBase: SubjectBase{ - Type: EnvironmentSubjectType, - }, - }, - }, - }, { - name: "environment modified", - eventType: EnvironmentModifiedEventV1, - expectedEvent: &EnvironmentModifiedEvent{ - Context: Context{ - Type: EnvironmentModifiedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: EnvironmentModifiedSubject{ - SubjectBase: SubjectBase{ - Type: EnvironmentSubjectType, - }, - }, - }, - }, { - name: "environment deleted", - eventType: EnvironmentDeletedEventV1, - expectedEvent: &EnvironmentDeletedEvent{ - Context: Context{ - Type: EnvironmentDeletedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: EnvironmentDeletedSubject{ - SubjectBase: SubjectBase{ - Type: EnvironmentSubjectType, - }, - }, - }, - }, { - name: "service deployed", - eventType: ServiceDeployedEventV1, - expectedEvent: &ServiceDeployedEvent{ - Context: Context{ - Type: ServiceDeployedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ServiceDeployedSubject{ - SubjectBase: SubjectBase{ - Type: ServiceSubjectType, - }, - }, - }, - }, { - name: "service upgraded", - eventType: ServiceUpgradedEventV1, - expectedEvent: &ServiceUpgradedEvent{ - Context: Context{ - Type: ServiceUpgradedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ServiceUpgradedSubject{ - SubjectBase: SubjectBase{ - Type: ServiceSubjectType, - }, - }, - }, - }, { - name: "service rolledback", - eventType: ServiceRolledbackEventV1, - expectedEvent: &ServiceRolledbackEvent{ - Context: Context{ - Type: ServiceRolledbackEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ServiceRolledbackSubject{ - SubjectBase: SubjectBase{ - Type: ServiceSubjectType, - }, - }, - }, - }, { - name: "service removed", - eventType: ServiceRemovedEventV1, - expectedEvent: &ServiceRemovedEvent{ - Context: Context{ - Type: ServiceRemovedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ServiceRemovedSubject{ - SubjectBase: SubjectBase{ - Type: ServiceSubjectType, - }, - }, - }, - }, { - name: "service published", - eventType: ServicePublishedEventV1, - expectedEvent: &ServicePublishedEvent{ - Context: Context{ - Type: ServicePublishedEventV1.String(), - Timestamp: timeNow(), - Id: testUUID(), - Version: CDEventsSpecVersion, - }, - Subject: ServicePublishedSubject{ - SubjectBase: SubjectBase{ - Type: ServiceSubjectType, - }, - }, - }, - }} for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { event, err := NewCDEvent(tc.eventType) @@ -591,13 +74,53 @@ func TestNewCDEvent(t *testing.T) { if d := cmp.Diff(tc.expectedEvent, event); d != "" { t.Errorf("args: diff(-want,+got):\n%s", d) } + // Check GetType + if d := cmp.Diff(tc.eventType, event.GetType().String()); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + // CustomData set and get + err = event.SetCustomData(testContentType, testDataJsonBytes) + if err != nil { + t.Fatalf("didn't expected it to fail, but it did: %v", err) + } + customDataRawGot, err := event.GetCustomDataRaw() + if err != nil { + t.Fatalf("didn't expected it to fail, but it did: %v", err) + } + if d := cmp.Diff(testDataJsonBytes, customDataRawGot); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + customDataGot, err := event.GetCustomData() + if err != nil { + t.Fatalf("didn't expected it to fail, but it did: %v", err) + } + if d := cmp.Diff(testDataJsonUnmarshalled, customDataGot); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + var customDataAsGot testData + err = event.GetCustomDataAs(&customDataAsGot) + if err != nil { + t.Fatalf("didn't expected it to fail, but it did: %v", err) + } + if d := cmp.Diff(testDataJson, customDataAsGot); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + if d := cmp.Diff(event.GetCustomDataContentType(), testContentType); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + // Subject source set and get + event.SetSubjectSource("randomSubjectSource") + gotSubjectSource := event.GetSubjectSource() + if d := cmp.Diff("randomSubjectSource", gotSubjectSource); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } }) } } func TestNewCDEventFailed(t *testing.T) { - _, err := NewCDEvent(CDEventType{Subject: "not supported"}) + _, err := NewCDEvent(CDEventType{Subject: "not supported"}.String()) if err == nil { t.Fatalf("expected it to fail, but it didn't") } diff --git a/pkg/api/types.go b/pkg/api/types.go index 968b4d1..bbf2b02 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -110,8 +110,6 @@ func (t SubjectType) String() string { } type Subject interface { - GetEventType() CDEventType - GetSubjectType() SubjectType } diff --git a/pkg/api/vocabulary.go b/pkg/api/vocabulary.go deleted file mode 100644 index 6b6a307..0000000 --- a/pkg/api/vocabulary.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2022 The CDEvents Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -SPDX-License-Identifier: Apache-2.0 -*/ - -package api - -const ( - // Core Subject Types - PipelineRunSubjectType SubjectType = "pipelineRun" - TaskRunSubjectType SubjectType = "taskRun" - - // SCM Subject Types - RepositorySubjectType SubjectType = "repository" - BranchSubjectType SubjectType = "branch" - ChangeSubjectType SubjectType = "change" - - // CI Subject Types - BuildSubjectType SubjectType = "build" - TestCaseSubjectType SubjectType = "testCase" - TestSuiteSubjectType SubjectType = "testSuite" - ArtifactSubjectType SubjectType = "artifact" - - // CD Subject Types - EnvironmentSubjectType SubjectType = "environment" - ServiceSubjectType SubjectType = "service" -) diff --git a/pkg/api/zz_artifactpackaged.go b/pkg/api/zz_artifactpackaged.go index a184695..9c68ed1 100644 --- a/pkg/api/zz_artifactpackaged.go +++ b/pkg/api/zz_artifactpackaged.go @@ -47,10 +47,6 @@ type ArtifactPackagedSubject struct { Content ArtifactPackagedSubjectContent `json:"content"` } -func (sc ArtifactPackagedSubject) GetEventType() CDEventType { - return ArtifactPackagedEventV1 -} - func (sc ArtifactPackagedSubject) GetSubjectType() SubjectType { return "artifact" } diff --git a/pkg/api/zz_artifactpublished.go b/pkg/api/zz_artifactpublished.go index 85f26be..daa2ab8 100644 --- a/pkg/api/zz_artifactpublished.go +++ b/pkg/api/zz_artifactpublished.go @@ -46,10 +46,6 @@ type ArtifactPublishedSubject struct { Content ArtifactPublishedSubjectContent `json:"content"` } -func (sc ArtifactPublishedSubject) GetEventType() CDEventType { - return ArtifactPublishedEventV1 -} - func (sc ArtifactPublishedSubject) GetSubjectType() SubjectType { return "artifact" } diff --git a/pkg/api/zz_branchcreated.go b/pkg/api/zz_branchcreated.go index 8b6af2f..9946d7c 100644 --- a/pkg/api/zz_branchcreated.go +++ b/pkg/api/zz_branchcreated.go @@ -47,10 +47,6 @@ type BranchCreatedSubject struct { Content BranchCreatedSubjectContent `json:"content"` } -func (sc BranchCreatedSubject) GetEventType() CDEventType { - return BranchCreatedEventV1 -} - func (sc BranchCreatedSubject) GetSubjectType() SubjectType { return "branch" } diff --git a/pkg/api/zz_branchdeleted.go b/pkg/api/zz_branchdeleted.go index c47a54c..3906961 100644 --- a/pkg/api/zz_branchdeleted.go +++ b/pkg/api/zz_branchdeleted.go @@ -47,10 +47,6 @@ type BranchDeletedSubject struct { Content BranchDeletedSubjectContent `json:"content"` } -func (sc BranchDeletedSubject) GetEventType() CDEventType { - return BranchDeletedEventV1 -} - func (sc BranchDeletedSubject) GetSubjectType() SubjectType { return "branch" } diff --git a/pkg/api/zz_buildfinished.go b/pkg/api/zz_buildfinished.go index 9cdb38e..245052e 100644 --- a/pkg/api/zz_buildfinished.go +++ b/pkg/api/zz_buildfinished.go @@ -47,10 +47,6 @@ type BuildFinishedSubject struct { Content BuildFinishedSubjectContent `json:"content"` } -func (sc BuildFinishedSubject) GetEventType() CDEventType { - return BuildFinishedEventV1 -} - func (sc BuildFinishedSubject) GetSubjectType() SubjectType { return "build" } diff --git a/pkg/api/zz_buildqueued.go b/pkg/api/zz_buildqueued.go index e017df5..ab75da4 100644 --- a/pkg/api/zz_buildqueued.go +++ b/pkg/api/zz_buildqueued.go @@ -46,10 +46,6 @@ type BuildQueuedSubject struct { Content BuildQueuedSubjectContent `json:"content"` } -func (sc BuildQueuedSubject) GetEventType() CDEventType { - return BuildQueuedEventV1 -} - func (sc BuildQueuedSubject) GetSubjectType() SubjectType { return "build" } diff --git a/pkg/api/zz_buildstarted.go b/pkg/api/zz_buildstarted.go index a405a09..c48670f 100644 --- a/pkg/api/zz_buildstarted.go +++ b/pkg/api/zz_buildstarted.go @@ -46,10 +46,6 @@ type BuildStartedSubject struct { Content BuildStartedSubjectContent `json:"content"` } -func (sc BuildStartedSubject) GetEventType() CDEventType { - return BuildStartedEventV1 -} - func (sc BuildStartedSubject) GetSubjectType() SubjectType { return "build" } diff --git a/pkg/api/zz_changeabandoned.go b/pkg/api/zz_changeabandoned.go index 7d074b6..2ecc95b 100644 --- a/pkg/api/zz_changeabandoned.go +++ b/pkg/api/zz_changeabandoned.go @@ -47,10 +47,6 @@ type ChangeAbandonedSubject struct { Content ChangeAbandonedSubjectContent `json:"content"` } -func (sc ChangeAbandonedSubject) GetEventType() CDEventType { - return ChangeAbandonedEventV1 -} - func (sc ChangeAbandonedSubject) GetSubjectType() SubjectType { return "change" } diff --git a/pkg/api/zz_changecreated.go b/pkg/api/zz_changecreated.go index 779d1d8..bc0797a 100644 --- a/pkg/api/zz_changecreated.go +++ b/pkg/api/zz_changecreated.go @@ -47,10 +47,6 @@ type ChangeCreatedSubject struct { Content ChangeCreatedSubjectContent `json:"content"` } -func (sc ChangeCreatedSubject) GetEventType() CDEventType { - return ChangeCreatedEventV1 -} - func (sc ChangeCreatedSubject) GetSubjectType() SubjectType { return "change" } diff --git a/pkg/api/zz_changemerged.go b/pkg/api/zz_changemerged.go index d95ca44..afe3e9c 100644 --- a/pkg/api/zz_changemerged.go +++ b/pkg/api/zz_changemerged.go @@ -47,10 +47,6 @@ type ChangeMergedSubject struct { Content ChangeMergedSubjectContent `json:"content"` } -func (sc ChangeMergedSubject) GetEventType() CDEventType { - return ChangeMergedEventV1 -} - func (sc ChangeMergedSubject) GetSubjectType() SubjectType { return "change" } diff --git a/pkg/api/zz_changereviewed.go b/pkg/api/zz_changereviewed.go index a721831..c9a2e9f 100644 --- a/pkg/api/zz_changereviewed.go +++ b/pkg/api/zz_changereviewed.go @@ -47,10 +47,6 @@ type ChangeReviewedSubject struct { Content ChangeReviewedSubjectContent `json:"content"` } -func (sc ChangeReviewedSubject) GetEventType() CDEventType { - return ChangeReviewedEventV1 -} - func (sc ChangeReviewedSubject) GetSubjectType() SubjectType { return "change" } diff --git a/pkg/api/zz_changeupdated.go b/pkg/api/zz_changeupdated.go index 59ff955..6d7a8db 100644 --- a/pkg/api/zz_changeupdated.go +++ b/pkg/api/zz_changeupdated.go @@ -47,10 +47,6 @@ type ChangeUpdatedSubject struct { Content ChangeUpdatedSubjectContent `json:"content"` } -func (sc ChangeUpdatedSubject) GetEventType() CDEventType { - return ChangeUpdatedEventV1 -} - func (sc ChangeUpdatedSubject) GetSubjectType() SubjectType { return "change" } diff --git a/pkg/api/zz_environmentcreated.go b/pkg/api/zz_environmentcreated.go index 4e6c3fc..fc698c6 100644 --- a/pkg/api/zz_environmentcreated.go +++ b/pkg/api/zz_environmentcreated.go @@ -49,10 +49,6 @@ type EnvironmentCreatedSubject struct { Content EnvironmentCreatedSubjectContent `json:"content"` } -func (sc EnvironmentCreatedSubject) GetEventType() CDEventType { - return EnvironmentCreatedEventV1 -} - func (sc EnvironmentCreatedSubject) GetSubjectType() SubjectType { return "environment" } diff --git a/pkg/api/zz_environmentdeleted.go b/pkg/api/zz_environmentdeleted.go index 89edcc7..6234031 100644 --- a/pkg/api/zz_environmentdeleted.go +++ b/pkg/api/zz_environmentdeleted.go @@ -47,10 +47,6 @@ type EnvironmentDeletedSubject struct { Content EnvironmentDeletedSubjectContent `json:"content"` } -func (sc EnvironmentDeletedSubject) GetEventType() CDEventType { - return EnvironmentDeletedEventV1 -} - func (sc EnvironmentDeletedSubject) GetSubjectType() SubjectType { return "environment" } diff --git a/pkg/api/zz_environmentmodified.go b/pkg/api/zz_environmentmodified.go index cb14517..8ba6490 100644 --- a/pkg/api/zz_environmentmodified.go +++ b/pkg/api/zz_environmentmodified.go @@ -49,10 +49,6 @@ type EnvironmentModifiedSubject struct { Content EnvironmentModifiedSubjectContent `json:"content"` } -func (sc EnvironmentModifiedSubject) GetEventType() CDEventType { - return EnvironmentModifiedEventV1 -} - func (sc EnvironmentModifiedSubject) GetSubjectType() SubjectType { return "environment" } diff --git a/pkg/api/zz_factory_test.go b/pkg/api/zz_factory_test.go new file mode 100644 index 0000000..d91bcc8 --- /dev/null +++ b/pkg/api/zz_factory_test.go @@ -0,0 +1,637 @@ +// Code generated by tools/generator. DO NOT EDIT. + +/* +Copyright 2023 The CDEvents Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package api + +func init() { + tests = []testNewCDEventType{} + tests = append(tests, testNewCDEventType{ + name: "artifact packaged", + eventType: ArtifactPackagedEventV1.String(), + expectedEvent: &ArtifactPackagedEvent{ + Context: Context{ + Type: ArtifactPackagedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ArtifactPackagedSubject{ + SubjectBase: SubjectBase{ + Type: "artifact", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "artifact published", + eventType: ArtifactPublishedEventV1.String(), + expectedEvent: &ArtifactPublishedEvent{ + Context: Context{ + Type: ArtifactPublishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ArtifactPublishedSubject{ + SubjectBase: SubjectBase{ + Type: "artifact", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "branch created", + eventType: BranchCreatedEventV1.String(), + expectedEvent: &BranchCreatedEvent{ + Context: Context{ + Type: BranchCreatedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: BranchCreatedSubject{ + SubjectBase: SubjectBase{ + Type: "branch", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "branch deleted", + eventType: BranchDeletedEventV1.String(), + expectedEvent: &BranchDeletedEvent{ + Context: Context{ + Type: BranchDeletedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: BranchDeletedSubject{ + SubjectBase: SubjectBase{ + Type: "branch", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "build finished", + eventType: BuildFinishedEventV1.String(), + expectedEvent: &BuildFinishedEvent{ + Context: Context{ + Type: BuildFinishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: BuildFinishedSubject{ + SubjectBase: SubjectBase{ + Type: "build", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "build queued", + eventType: BuildQueuedEventV1.String(), + expectedEvent: &BuildQueuedEvent{ + Context: Context{ + Type: BuildQueuedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: BuildQueuedSubject{ + SubjectBase: SubjectBase{ + Type: "build", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "build started", + eventType: BuildStartedEventV1.String(), + expectedEvent: &BuildStartedEvent{ + Context: Context{ + Type: BuildStartedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: BuildStartedSubject{ + SubjectBase: SubjectBase{ + Type: "build", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "change abandoned", + eventType: ChangeAbandonedEventV1.String(), + expectedEvent: &ChangeAbandonedEvent{ + Context: Context{ + Type: ChangeAbandonedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ChangeAbandonedSubject{ + SubjectBase: SubjectBase{ + Type: "change", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "change created", + eventType: ChangeCreatedEventV1.String(), + expectedEvent: &ChangeCreatedEvent{ + Context: Context{ + Type: ChangeCreatedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ChangeCreatedSubject{ + SubjectBase: SubjectBase{ + Type: "change", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "change merged", + eventType: ChangeMergedEventV1.String(), + expectedEvent: &ChangeMergedEvent{ + Context: Context{ + Type: ChangeMergedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ChangeMergedSubject{ + SubjectBase: SubjectBase{ + Type: "change", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "change reviewed", + eventType: ChangeReviewedEventV1.String(), + expectedEvent: &ChangeReviewedEvent{ + Context: Context{ + Type: ChangeReviewedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ChangeReviewedSubject{ + SubjectBase: SubjectBase{ + Type: "change", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "change updated", + eventType: ChangeUpdatedEventV1.String(), + expectedEvent: &ChangeUpdatedEvent{ + Context: Context{ + Type: ChangeUpdatedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ChangeUpdatedSubject{ + SubjectBase: SubjectBase{ + Type: "change", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "environment created", + eventType: EnvironmentCreatedEventV1.String(), + expectedEvent: &EnvironmentCreatedEvent{ + Context: Context{ + Type: EnvironmentCreatedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: EnvironmentCreatedSubject{ + SubjectBase: SubjectBase{ + Type: "environment", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "environment deleted", + eventType: EnvironmentDeletedEventV1.String(), + expectedEvent: &EnvironmentDeletedEvent{ + Context: Context{ + Type: EnvironmentDeletedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: EnvironmentDeletedSubject{ + SubjectBase: SubjectBase{ + Type: "environment", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "environment modified", + eventType: EnvironmentModifiedEventV1.String(), + expectedEvent: &EnvironmentModifiedEvent{ + Context: Context{ + Type: EnvironmentModifiedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: EnvironmentModifiedSubject{ + SubjectBase: SubjectBase{ + Type: "environment", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "incident detected", + eventType: IncidentDetectedEventV1.String(), + expectedEvent: &IncidentDetectedEvent{ + Context: Context{ + Type: IncidentDetectedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: IncidentDetectedSubject{ + SubjectBase: SubjectBase{ + Type: "incident", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "incident reported", + eventType: IncidentReportedEventV1.String(), + expectedEvent: &IncidentReportedEvent{ + Context: Context{ + Type: IncidentReportedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: IncidentReportedSubject{ + SubjectBase: SubjectBase{ + Type: "incident", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "incident resolved", + eventType: IncidentResolvedEventV1.String(), + expectedEvent: &IncidentResolvedEvent{ + Context: Context{ + Type: IncidentResolvedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: IncidentResolvedSubject{ + SubjectBase: SubjectBase{ + Type: "incident", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "pipelinerun finished", + eventType: PipelineRunFinishedEventV1.String(), + expectedEvent: &PipelineRunFinishedEvent{ + Context: Context{ + Type: PipelineRunFinishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: PipelineRunFinishedSubject{ + SubjectBase: SubjectBase{ + Type: "pipelineRun", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "pipelinerun queued", + eventType: PipelineRunQueuedEventV1.String(), + expectedEvent: &PipelineRunQueuedEvent{ + Context: Context{ + Type: PipelineRunQueuedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: PipelineRunQueuedSubject{ + SubjectBase: SubjectBase{ + Type: "pipelineRun", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "pipelinerun started", + eventType: PipelineRunStartedEventV1.String(), + expectedEvent: &PipelineRunStartedEvent{ + Context: Context{ + Type: PipelineRunStartedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: PipelineRunStartedSubject{ + SubjectBase: SubjectBase{ + Type: "pipelineRun", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "repository created", + eventType: RepositoryCreatedEventV1.String(), + expectedEvent: &RepositoryCreatedEvent{ + Context: Context{ + Type: RepositoryCreatedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: RepositoryCreatedSubject{ + SubjectBase: SubjectBase{ + Type: "repository", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "repository deleted", + eventType: RepositoryDeletedEventV1.String(), + expectedEvent: &RepositoryDeletedEvent{ + Context: Context{ + Type: RepositoryDeletedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: RepositoryDeletedSubject{ + SubjectBase: SubjectBase{ + Type: "repository", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "repository modified", + eventType: RepositoryModifiedEventV1.String(), + expectedEvent: &RepositoryModifiedEvent{ + Context: Context{ + Type: RepositoryModifiedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: RepositoryModifiedSubject{ + SubjectBase: SubjectBase{ + Type: "repository", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "service deployed", + eventType: ServiceDeployedEventV1.String(), + expectedEvent: &ServiceDeployedEvent{ + Context: Context{ + Type: ServiceDeployedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ServiceDeployedSubject{ + SubjectBase: SubjectBase{ + Type: "service", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "service published", + eventType: ServicePublishedEventV1.String(), + expectedEvent: &ServicePublishedEvent{ + Context: Context{ + Type: ServicePublishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ServicePublishedSubject{ + SubjectBase: SubjectBase{ + Type: "service", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "service removed", + eventType: ServiceRemovedEventV1.String(), + expectedEvent: &ServiceRemovedEvent{ + Context: Context{ + Type: ServiceRemovedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ServiceRemovedSubject{ + SubjectBase: SubjectBase{ + Type: "service", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "service rolledback", + eventType: ServiceRolledbackEventV1.String(), + expectedEvent: &ServiceRolledbackEvent{ + Context: Context{ + Type: ServiceRolledbackEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ServiceRolledbackSubject{ + SubjectBase: SubjectBase{ + Type: "service", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "service upgraded", + eventType: ServiceUpgradedEventV1.String(), + expectedEvent: &ServiceUpgradedEvent{ + Context: Context{ + Type: ServiceUpgradedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: ServiceUpgradedSubject{ + SubjectBase: SubjectBase{ + Type: "service", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "taskrun finished", + eventType: TaskRunFinishedEventV1.String(), + expectedEvent: &TaskRunFinishedEvent{ + Context: Context{ + Type: TaskRunFinishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TaskRunFinishedSubject{ + SubjectBase: SubjectBase{ + Type: "taskRun", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "taskrun started", + eventType: TaskRunStartedEventV1.String(), + expectedEvent: &TaskRunStartedEvent{ + Context: Context{ + Type: TaskRunStartedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TaskRunStartedSubject{ + SubjectBase: SubjectBase{ + Type: "taskRun", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "testcase finished", + eventType: TestCaseFinishedEventV1.String(), + expectedEvent: &TestCaseFinishedEvent{ + Context: Context{ + Type: TestCaseFinishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TestCaseFinishedSubject{ + SubjectBase: SubjectBase{ + Type: "testCase", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "testcase queued", + eventType: TestCaseQueuedEventV1.String(), + expectedEvent: &TestCaseQueuedEvent{ + Context: Context{ + Type: TestCaseQueuedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TestCaseQueuedSubject{ + SubjectBase: SubjectBase{ + Type: "testCase", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "testcase started", + eventType: TestCaseStartedEventV1.String(), + expectedEvent: &TestCaseStartedEvent{ + Context: Context{ + Type: TestCaseStartedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TestCaseStartedSubject{ + SubjectBase: SubjectBase{ + Type: "testCase", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "testsuite finished", + eventType: TestSuiteFinishedEventV1.String(), + expectedEvent: &TestSuiteFinishedEvent{ + Context: Context{ + Type: TestSuiteFinishedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TestSuiteFinishedSubject{ + SubjectBase: SubjectBase{ + Type: "testSuite", + }, + }, + }, + }) + tests = append(tests, testNewCDEventType{ + name: "testsuite started", + eventType: TestSuiteStartedEventV1.String(), + expectedEvent: &TestSuiteStartedEvent{ + Context: Context{ + Type: TestSuiteStartedEventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: TestSuiteStartedSubject{ + SubjectBase: SubjectBase{ + Type: "testSuite", + }, + }, + }, + }) +} diff --git a/pkg/api/zz_incidentdetected.go b/pkg/api/zz_incidentdetected.go index 7c4be1e..ef71940 100644 --- a/pkg/api/zz_incidentdetected.go +++ b/pkg/api/zz_incidentdetected.go @@ -53,10 +53,6 @@ type IncidentDetectedSubject struct { Content IncidentDetectedSubjectContent `json:"content"` } -func (sc IncidentDetectedSubject) GetEventType() CDEventType { - return IncidentDetectedEventV1 -} - func (sc IncidentDetectedSubject) GetSubjectType() SubjectType { return "incident" } diff --git a/pkg/api/zz_incidentreported.go b/pkg/api/zz_incidentreported.go index ccc57a2..989fc7c 100644 --- a/pkg/api/zz_incidentreported.go +++ b/pkg/api/zz_incidentreported.go @@ -55,10 +55,6 @@ type IncidentReportedSubject struct { Content IncidentReportedSubjectContent `json:"content"` } -func (sc IncidentReportedSubject) GetEventType() CDEventType { - return IncidentReportedEventV1 -} - func (sc IncidentReportedSubject) GetSubjectType() SubjectType { return "incident" } diff --git a/pkg/api/zz_incidentresolved.go b/pkg/api/zz_incidentresolved.go index c79fef3..c0c1879 100644 --- a/pkg/api/zz_incidentresolved.go +++ b/pkg/api/zz_incidentresolved.go @@ -53,10 +53,6 @@ type IncidentResolvedSubject struct { Content IncidentResolvedSubjectContent `json:"content"` } -func (sc IncidentResolvedSubject) GetEventType() CDEventType { - return IncidentResolvedEventV1 -} - func (sc IncidentResolvedSubject) GetSubjectType() SubjectType { return "incident" } diff --git a/pkg/api/zz_pipelinerunfinished.go b/pkg/api/zz_pipelinerunfinished.go index 24739b0..bcd7b71 100644 --- a/pkg/api/zz_pipelinerunfinished.go +++ b/pkg/api/zz_pipelinerunfinished.go @@ -53,10 +53,6 @@ type PipelineRunFinishedSubject struct { Content PipelineRunFinishedSubjectContent `json:"content"` } -func (sc PipelineRunFinishedSubject) GetEventType() CDEventType { - return PipelineRunFinishedEventV1 -} - func (sc PipelineRunFinishedSubject) GetSubjectType() SubjectType { return "pipelineRun" } diff --git a/pkg/api/zz_pipelinerunqueued.go b/pkg/api/zz_pipelinerunqueued.go index 71e15e4..aedb192 100644 --- a/pkg/api/zz_pipelinerunqueued.go +++ b/pkg/api/zz_pipelinerunqueued.go @@ -49,10 +49,6 @@ type PipelineRunQueuedSubject struct { Content PipelineRunQueuedSubjectContent `json:"content"` } -func (sc PipelineRunQueuedSubject) GetEventType() CDEventType { - return PipelineRunQueuedEventV1 -} - func (sc PipelineRunQueuedSubject) GetSubjectType() SubjectType { return "pipelineRun" } diff --git a/pkg/api/zz_pipelinerunstarted.go b/pkg/api/zz_pipelinerunstarted.go index 5b3a70d..7254ccc 100644 --- a/pkg/api/zz_pipelinerunstarted.go +++ b/pkg/api/zz_pipelinerunstarted.go @@ -49,10 +49,6 @@ type PipelineRunStartedSubject struct { Content PipelineRunStartedSubjectContent `json:"content"` } -func (sc PipelineRunStartedSubject) GetEventType() CDEventType { - return PipelineRunStartedEventV1 -} - func (sc PipelineRunStartedSubject) GetSubjectType() SubjectType { return "pipelineRun" } diff --git a/pkg/api/zz_repositorycreated.go b/pkg/api/zz_repositorycreated.go index 6c05c14..423a608 100644 --- a/pkg/api/zz_repositorycreated.go +++ b/pkg/api/zz_repositorycreated.go @@ -53,10 +53,6 @@ type RepositoryCreatedSubject struct { Content RepositoryCreatedSubjectContent `json:"content"` } -func (sc RepositoryCreatedSubject) GetEventType() CDEventType { - return RepositoryCreatedEventV1 -} - func (sc RepositoryCreatedSubject) GetSubjectType() SubjectType { return "repository" } diff --git a/pkg/api/zz_repositorydeleted.go b/pkg/api/zz_repositorydeleted.go index f359d2a..9c9d3b4 100644 --- a/pkg/api/zz_repositorydeleted.go +++ b/pkg/api/zz_repositorydeleted.go @@ -53,10 +53,6 @@ type RepositoryDeletedSubject struct { Content RepositoryDeletedSubjectContent `json:"content"` } -func (sc RepositoryDeletedSubject) GetEventType() CDEventType { - return RepositoryDeletedEventV1 -} - func (sc RepositoryDeletedSubject) GetSubjectType() SubjectType { return "repository" } diff --git a/pkg/api/zz_repositorymodified.go b/pkg/api/zz_repositorymodified.go index ec0b08d..ec6da52 100644 --- a/pkg/api/zz_repositorymodified.go +++ b/pkg/api/zz_repositorymodified.go @@ -53,10 +53,6 @@ type RepositoryModifiedSubject struct { Content RepositoryModifiedSubjectContent `json:"content"` } -func (sc RepositoryModifiedSubject) GetEventType() CDEventType { - return RepositoryModifiedEventV1 -} - func (sc RepositoryModifiedSubject) GetSubjectType() SubjectType { return "repository" } diff --git a/pkg/api/zz_servicedeployed.go b/pkg/api/zz_servicedeployed.go index b66ea72..f9b04ef 100644 --- a/pkg/api/zz_servicedeployed.go +++ b/pkg/api/zz_servicedeployed.go @@ -49,10 +49,6 @@ type ServiceDeployedSubject struct { Content ServiceDeployedSubjectContent `json:"content"` } -func (sc ServiceDeployedSubject) GetEventType() CDEventType { - return ServiceDeployedEventV1 -} - func (sc ServiceDeployedSubject) GetSubjectType() SubjectType { return "service" } diff --git a/pkg/api/zz_servicepublished.go b/pkg/api/zz_servicepublished.go index 93b7160..c5e7fe3 100644 --- a/pkg/api/zz_servicepublished.go +++ b/pkg/api/zz_servicepublished.go @@ -47,10 +47,6 @@ type ServicePublishedSubject struct { Content ServicePublishedSubjectContent `json:"content"` } -func (sc ServicePublishedSubject) GetEventType() CDEventType { - return ServicePublishedEventV1 -} - func (sc ServicePublishedSubject) GetSubjectType() SubjectType { return "service" } diff --git a/pkg/api/zz_serviceremoved.go b/pkg/api/zz_serviceremoved.go index 5705713..a9e70cd 100644 --- a/pkg/api/zz_serviceremoved.go +++ b/pkg/api/zz_serviceremoved.go @@ -47,10 +47,6 @@ type ServiceRemovedSubject struct { Content ServiceRemovedSubjectContent `json:"content"` } -func (sc ServiceRemovedSubject) GetEventType() CDEventType { - return ServiceRemovedEventV1 -} - func (sc ServiceRemovedSubject) GetSubjectType() SubjectType { return "service" } diff --git a/pkg/api/zz_servicerolledback.go b/pkg/api/zz_servicerolledback.go index 7d20a42..1f87e94 100644 --- a/pkg/api/zz_servicerolledback.go +++ b/pkg/api/zz_servicerolledback.go @@ -49,10 +49,6 @@ type ServiceRolledbackSubject struct { Content ServiceRolledbackSubjectContent `json:"content"` } -func (sc ServiceRolledbackSubject) GetEventType() CDEventType { - return ServiceRolledbackEventV1 -} - func (sc ServiceRolledbackSubject) GetSubjectType() SubjectType { return "service" } diff --git a/pkg/api/zz_serviceupgraded.go b/pkg/api/zz_serviceupgraded.go index d446b0e..aa90c7a 100644 --- a/pkg/api/zz_serviceupgraded.go +++ b/pkg/api/zz_serviceupgraded.go @@ -49,10 +49,6 @@ type ServiceUpgradedSubject struct { Content ServiceUpgradedSubjectContent `json:"content"` } -func (sc ServiceUpgradedSubject) GetEventType() CDEventType { - return ServiceUpgradedEventV1 -} - func (sc ServiceUpgradedSubject) GetSubjectType() SubjectType { return "service" } diff --git a/pkg/api/zz_taskrunfinished.go b/pkg/api/zz_taskrunfinished.go index 3113990..f226f8b 100644 --- a/pkg/api/zz_taskrunfinished.go +++ b/pkg/api/zz_taskrunfinished.go @@ -55,10 +55,6 @@ type TaskRunFinishedSubject struct { Content TaskRunFinishedSubjectContent `json:"content"` } -func (sc TaskRunFinishedSubject) GetEventType() CDEventType { - return TaskRunFinishedEventV1 -} - func (sc TaskRunFinishedSubject) GetSubjectType() SubjectType { return "taskRun" } diff --git a/pkg/api/zz_taskrunstarted.go b/pkg/api/zz_taskrunstarted.go index 142020c..1d90ab1 100644 --- a/pkg/api/zz_taskrunstarted.go +++ b/pkg/api/zz_taskrunstarted.go @@ -51,10 +51,6 @@ type TaskRunStartedSubject struct { Content TaskRunStartedSubjectContent `json:"content"` } -func (sc TaskRunStartedSubject) GetEventType() CDEventType { - return TaskRunStartedEventV1 -} - func (sc TaskRunStartedSubject) GetSubjectType() SubjectType { return "taskRun" } diff --git a/pkg/api/zz_testcasefinished.go b/pkg/api/zz_testcasefinished.go index 1dab6ad..0779df7 100644 --- a/pkg/api/zz_testcasefinished.go +++ b/pkg/api/zz_testcasefinished.go @@ -46,10 +46,6 @@ type TestCaseFinishedSubject struct { Content TestCaseFinishedSubjectContent `json:"content"` } -func (sc TestCaseFinishedSubject) GetEventType() CDEventType { - return TestCaseFinishedEventV1 -} - func (sc TestCaseFinishedSubject) GetSubjectType() SubjectType { return "testCase" } diff --git a/pkg/api/zz_testcasequeued.go b/pkg/api/zz_testcasequeued.go index 72a77a6..b9cd59a 100644 --- a/pkg/api/zz_testcasequeued.go +++ b/pkg/api/zz_testcasequeued.go @@ -46,10 +46,6 @@ type TestCaseQueuedSubject struct { Content TestCaseQueuedSubjectContent `json:"content"` } -func (sc TestCaseQueuedSubject) GetEventType() CDEventType { - return TestCaseQueuedEventV1 -} - func (sc TestCaseQueuedSubject) GetSubjectType() SubjectType { return "testCase" } diff --git a/pkg/api/zz_testcasestarted.go b/pkg/api/zz_testcasestarted.go index a5d283b..b805f3f 100644 --- a/pkg/api/zz_testcasestarted.go +++ b/pkg/api/zz_testcasestarted.go @@ -46,10 +46,6 @@ type TestCaseStartedSubject struct { Content TestCaseStartedSubjectContent `json:"content"` } -func (sc TestCaseStartedSubject) GetEventType() CDEventType { - return TestCaseStartedEventV1 -} - func (sc TestCaseStartedSubject) GetSubjectType() SubjectType { return "testCase" } diff --git a/pkg/api/zz_testsuitefinished.go b/pkg/api/zz_testsuitefinished.go index da0a11a..1e0fbba 100644 --- a/pkg/api/zz_testsuitefinished.go +++ b/pkg/api/zz_testsuitefinished.go @@ -46,10 +46,6 @@ type TestSuiteFinishedSubject struct { Content TestSuiteFinishedSubjectContent `json:"content"` } -func (sc TestSuiteFinishedSubject) GetEventType() CDEventType { - return TestSuiteFinishedEventV1 -} - func (sc TestSuiteFinishedSubject) GetSubjectType() SubjectType { return "testSuite" } diff --git a/pkg/api/zz_testsuitestarted.go b/pkg/api/zz_testsuitestarted.go index ef5daf3..b2af9f6 100644 --- a/pkg/api/zz_testsuitestarted.go +++ b/pkg/api/zz_testsuitestarted.go @@ -46,10 +46,6 @@ type TestSuiteStartedSubject struct { Content TestSuiteStartedSubjectContent `json:"content"` } -func (sc TestSuiteStartedSubject) GetEventType() CDEventType { - return TestSuiteStartedEventV1 -} - func (sc TestSuiteStartedSubject) GetSubjectType() SubjectType { return "testSuite" } diff --git a/pkg/api/zz_types.go b/pkg/api/zz_types.go index 3660c7b..5a10af5 100644 --- a/pkg/api/zz_types.go +++ b/pkg/api/zz_types.go @@ -71,79 +71,79 @@ func init() { // NewCDEvent produces a CDEvent by type // This function can be used by users but it's meant mainly for testing purposes -func NewCDEvent(eventType CDEventType) (CDEvent, error) { +func NewCDEvent(eventType string) (CDEvent, error) { switch eventType { - case ArtifactPackagedEventV1: + case ArtifactPackagedEventV1.String(): return NewArtifactPackagedEvent() - case ArtifactPublishedEventV1: + case ArtifactPublishedEventV1.String(): return NewArtifactPublishedEvent() - case BranchCreatedEventV1: + case BranchCreatedEventV1.String(): return NewBranchCreatedEvent() - case BranchDeletedEventV1: + case BranchDeletedEventV1.String(): return NewBranchDeletedEvent() - case BuildFinishedEventV1: + case BuildFinishedEventV1.String(): return NewBuildFinishedEvent() - case BuildQueuedEventV1: + case BuildQueuedEventV1.String(): return NewBuildQueuedEvent() - case BuildStartedEventV1: + case BuildStartedEventV1.String(): return NewBuildStartedEvent() - case ChangeAbandonedEventV1: + case ChangeAbandonedEventV1.String(): return NewChangeAbandonedEvent() - case ChangeCreatedEventV1: + case ChangeCreatedEventV1.String(): return NewChangeCreatedEvent() - case ChangeMergedEventV1: + case ChangeMergedEventV1.String(): return NewChangeMergedEvent() - case ChangeReviewedEventV1: + case ChangeReviewedEventV1.String(): return NewChangeReviewedEvent() - case ChangeUpdatedEventV1: + case ChangeUpdatedEventV1.String(): return NewChangeUpdatedEvent() - case EnvironmentCreatedEventV1: + case EnvironmentCreatedEventV1.String(): return NewEnvironmentCreatedEvent() - case EnvironmentDeletedEventV1: + case EnvironmentDeletedEventV1.String(): return NewEnvironmentDeletedEvent() - case EnvironmentModifiedEventV1: + case EnvironmentModifiedEventV1.String(): return NewEnvironmentModifiedEvent() - case IncidentDetectedEventV1: + case IncidentDetectedEventV1.String(): return NewIncidentDetectedEvent() - case IncidentReportedEventV1: + case IncidentReportedEventV1.String(): return NewIncidentReportedEvent() - case IncidentResolvedEventV1: + case IncidentResolvedEventV1.String(): return NewIncidentResolvedEvent() - case PipelineRunFinishedEventV1: + case PipelineRunFinishedEventV1.String(): return NewPipelineRunFinishedEvent() - case PipelineRunQueuedEventV1: + case PipelineRunQueuedEventV1.String(): return NewPipelineRunQueuedEvent() - case PipelineRunStartedEventV1: + case PipelineRunStartedEventV1.String(): return NewPipelineRunStartedEvent() - case RepositoryCreatedEventV1: + case RepositoryCreatedEventV1.String(): return NewRepositoryCreatedEvent() - case RepositoryDeletedEventV1: + case RepositoryDeletedEventV1.String(): return NewRepositoryDeletedEvent() - case RepositoryModifiedEventV1: + case RepositoryModifiedEventV1.String(): return NewRepositoryModifiedEvent() - case ServiceDeployedEventV1: + case ServiceDeployedEventV1.String(): return NewServiceDeployedEvent() - case ServicePublishedEventV1: + case ServicePublishedEventV1.String(): return NewServicePublishedEvent() - case ServiceRemovedEventV1: + case ServiceRemovedEventV1.String(): return NewServiceRemovedEvent() - case ServiceRolledbackEventV1: + case ServiceRolledbackEventV1.String(): return NewServiceRolledbackEvent() - case ServiceUpgradedEventV1: + case ServiceUpgradedEventV1.String(): return NewServiceUpgradedEvent() - case TaskRunFinishedEventV1: + case TaskRunFinishedEventV1.String(): return NewTaskRunFinishedEvent() - case TaskRunStartedEventV1: + case TaskRunStartedEventV1.String(): return NewTaskRunStartedEvent() - case TestCaseFinishedEventV1: + case TestCaseFinishedEventV1.String(): return NewTestCaseFinishedEvent() - case TestCaseQueuedEventV1: + case TestCaseQueuedEventV1.String(): return NewTestCaseQueuedEvent() - case TestCaseStartedEventV1: + case TestCaseStartedEventV1.String(): return NewTestCaseStartedEvent() - case TestSuiteFinishedEventV1: + case TestSuiteFinishedEventV1.String(): return NewTestSuiteFinishedEvent() - case TestSuiteStartedEventV1: + case TestSuiteStartedEventV1.String(): return NewTestSuiteStartedEvent() default: return nil, fmt.Errorf("event %v not supported", eventType) diff --git a/tools/generator.go b/tools/generator.go index 4855d66..8fcd4e3 100644 --- a/tools/generator.go +++ b/tools/generator.go @@ -58,9 +58,12 @@ var ( } // Templates - eventTemplateFileName = "event.go.tmpl" - typesTemplateFileName = "types.go.tmpl" - examplesTestsTemplateFileName = "examples_test.go.tmpl" + eventTemplateFileName = "event.go.tmpl" + typesTemplateFileName = "types.go.tmpl" + examplesTestsTemplateFileNames = []string{ + "examples_test.go.tmpl", + "factory_test.go.tmpl", + } // Tool capitalizer cases.Caser @@ -159,10 +162,12 @@ func generate(templatesFolder, schemaFolder, genFolder, prefix string, goTypes m // Process example test files - only for real data if prefix == "" { - outputFileName := genFolder + string(os.PathSeparator) + "zz_" + prefix + strings.TrimSuffix(examplesTestsTemplateFileName, filepath.Ext(examplesTestsTemplateFileName)) - err = executeTemplate(allTemplates, examplesTestsTemplateFileName, outputFileName, allData.Slice) - if err != nil { - return err + for _, examplesTestsTemplateFileName := range examplesTestsTemplateFileNames { + outputFileName := genFolder + string(os.PathSeparator) + "zz_" + prefix + strings.TrimSuffix(examplesTestsTemplateFileName, filepath.Ext(examplesTestsTemplateFileName)) + err = executeTemplate(allTemplates, examplesTestsTemplateFileName, outputFileName, allData.Slice) + if err != nil { + return err + } } } return nil diff --git a/tools/templates/event.go.tmpl b/tools/templates/event.go.tmpl index b0ff8d0..5077b30 100644 --- a/tools/templates/event.go.tmpl +++ b/tools/templates/event.go.tmpl @@ -53,10 +53,6 @@ type {{.Subject}}{{.Predicate}}Subject struct { Content {{.Subject}}{{.Predicate}}SubjectContent `json:"content"` } -func (sc {{.Subject}}{{.Predicate}}Subject) GetEventType() CDEventType { - return {{.Subject}}{{.Predicate}}EventV1 -} - func (sc {{.Subject}}{{.Predicate}}Subject) GetSubjectType() SubjectType { return "{{.SubjectType}}" } diff --git a/tools/templates/factory_test.go.tmpl b/tools/templates/factory_test.go.tmpl new file mode 100644 index 0000000..fa669bc --- /dev/null +++ b/tools/templates/factory_test.go.tmpl @@ -0,0 +1,45 @@ +// Code generated by tools/generator. DO NOT EDIT. + +/* +Copyright 2023 The CDEvents Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package api + +func init() { + tests = []testNewCDEventType{} + +{{- range $i, $data := . }} + tests = append(tests, testNewCDEventType{ + name: "{{ .SubjectLower }} {{ .PredicateLower }}", + eventType: {{ .Subject }}{{ .Predicate }}EventV1. String(), + expectedEvent: &{{ .Subject }}{{ .Predicate }}Event{ + Context: Context{ + Type: {{ .Subject }}{{ .Predicate }}EventV1.String(), + Timestamp: timeNow(), + Id: testUUID(), + Version: CDEventsSpecVersion, + }, + Subject: {{ .Subject }}{{ .Predicate }}Subject{ + SubjectBase: SubjectBase{ + Type: "{{ .SubjectType }}", + }, + }, + }, + }) +{{- end }} +} diff --git a/tools/templates/types.go.tmpl b/tools/templates/types.go.tmpl index 9ee5464..9b3b11f 100644 --- a/tools/templates/types.go.tmpl +++ b/tools/templates/types.go.tmpl @@ -38,10 +38,10 @@ func init() { // NewCDEvent produces a CDEvent by type // This function can be used by users but it's meant mainly for testing purposes -func NewCDEvent(eventType CDEventType) (CDEvent, error) { +func NewCDEvent(eventType string) (CDEvent, error) { switch eventType { {{- range $i, $data := . }} - case {{ .Subject }}{{ .Predicate }}EventV1: + case {{ .Subject }}{{ .Predicate }}EventV1.String(): return New{{ .Subject }}{{ .Predicate }}Event() {{- end }} default: