This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Validate() method for KeptnContextExtendedCE struct (#4652)
* feat: Added Validate() method for KeptnContextExtendedCE struct (keptn/keptn/#4652) Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> * added unit test for event validation Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestKeptnContextExtendedCE_Validate(t *testing.T) { | ||
source := "my-source" | ||
eventType := "my-type" | ||
type fields struct { | ||
Contenttype string | ||
Data interface{} | ||
Extensions interface{} | ||
ID string | ||
Shkeptncontext string | ||
Shkeptnspecversion string | ||
Source *string | ||
Specversion string | ||
Time time.Time | ||
Triggeredid string | ||
Type *string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "validation passes", | ||
fields: fields{ | ||
ID: "my-id", | ||
Source: &source, | ||
Time: time.Now(), | ||
Type: &eventType, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing type", | ||
fields: fields{ | ||
ID: "my-id", | ||
Source: &source, | ||
Time: time.Now(), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing id", | ||
fields: fields{ | ||
Source: &source, | ||
Time: time.Now(), | ||
Type: &eventType, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing time", | ||
fields: fields{ | ||
ID: "my-id", | ||
Source: &source, | ||
Type: &eventType, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing source", | ||
fields: fields{ | ||
ID: "my-id", | ||
Time: time.Now(), | ||
Type: &eventType, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ce := &KeptnContextExtendedCE{ | ||
Contenttype: tt.fields.Contenttype, | ||
Data: tt.fields.Data, | ||
Extensions: tt.fields.Extensions, | ||
ID: tt.fields.ID, | ||
Shkeptncontext: tt.fields.Shkeptncontext, | ||
Shkeptnspecversion: tt.fields.Shkeptnspecversion, | ||
Source: tt.fields.Source, | ||
Specversion: tt.fields.Specversion, | ||
Time: tt.fields.Time, | ||
Triggeredid: tt.fields.Triggeredid, | ||
Type: tt.fields.Type, | ||
} | ||
if err := ce.Validate(); (err != nil) != tt.wantErr { | ||
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |