Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat: Added Validate() method for KeptnContextExtendedCE struct (#4652)
Browse files Browse the repository at this point in the history
* 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
bacherfl authored and mowies committed Jul 22, 2021
1 parent a46301e commit 9fca604
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/api/models/keptn_context_extended_c_e.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"encoding/json"
"errors"
"time"
)

Expand Down Expand Up @@ -55,3 +56,20 @@ func (ce *KeptnContextExtendedCE) DataAs(out interface{}) error {
}
return json.Unmarshal(bytes, out)
}

// Validate checks whether the required properties 'time', 'type', 'id' and 'source' are defined and non-empty
func (ce *KeptnContextExtendedCE) Validate() error {
if ce.Time.IsZero() {
return errors.New("time must be specified")
}
if ce.Type == nil || *ce.Type == "" {
return errors.New("type must be specified")
}
if ce.ID == "" {
return errors.New("id must be specified")
}
if ce.Source == nil || *ce.Source == "" {
return errors.New("source must be specified")
}
return nil
}
96 changes: 96 additions & 0 deletions pkg/api/models/keptn_context_extended_c_e_test.go
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)
}
})
}
}

0 comments on commit 9fca604

Please sign in to comment.