Skip to content

Commit

Permalink
RHINENG-9702: store null when description is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Apr 25, 2024
1 parent b624102 commit d0b36e0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion base/mqueue/template_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TemplateResponse struct {
UUID string `json:"uuid" readonly:"true"`
Name string `json:"name"` // Name of the template
OrgID string `json:"org_id"` // Organization ID of the owner
Description string `json:"description"` // Description of the template
Description *string `json:"description"` // Description of the template
Arch string `json:"arch"` // Architecture of the template
Version string `json:"version"` // Version of the template
Date time.Time `json:"date"` // Latest date to include snapshots for
Expand Down
34 changes: 32 additions & 2 deletions listener/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func createTempateMsg(t *testing.T, eventName, orgID string, nTemplates int) mqu

templates := make([]mqueue.TemplateResponse, nTemplates)
for i := 0; i < nTemplates; i++ {
description := fmt.Sprintf("Template%d description", i)
templates[i] = mqueue.TemplateResponse{
UUID: fmt.Sprintf("77777777-0000-0000-0000-00000000000%1d", i),
Name: fmt.Sprintf("Template%d", i),
OrgID: orgID,
Description: fmt.Sprintf("Template%d description", i),
Description: &description,
Date: time.Now(),
}
}
Expand Down Expand Up @@ -87,7 +88,8 @@ func TestCreateTemplate(t *testing.T) {

updateEvent := createTempateMsg(t, "template-updated", "org_1", 2)
updateEvent.Data[0].Name = "Updated Template0"
updateEvent.Data[1].Description = "Updated Template1 description"
description := "Updated Template1 description"
updateEvent.Data[1].Description = &description
msg, err = json.Marshal(updateEvent)
assert.Nil(t, err)

Expand Down Expand Up @@ -143,3 +145,31 @@ func TestTemplateErrors(t *testing.T) {
assert.Equal(t, 1, len(after))
deleteTemplatesInDB(t, after)
}

func TestTemplateEmptyDescription(t *testing.T) {
utils.SkipWithoutDB(t)
core.SetupTestEnvironment()
configure()

event := createTempateMsg(t, "template-created", "org_1", 3)
empty := ""
spaces := " "
event.Data[0].Description = &empty
event.Data[1].Description = &spaces
event.Data[2].Description = nil
msg, err := json.Marshal(event)
assert.Nil(t, err)

// process message
err = TemplatesMessageHandler(mqueue.KafkaMessage{Value: msg})
assert.Nil(t, err)

after := testTemplatesInDB(t)
assert.Equal(t, 3, len(after))
for _, event := range after {
assert.Nil(t, event.Description)
}

// cleanup
deleteTemplatesInDB(t, after)
}
27 changes: 20 additions & 7 deletions listener/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ const (
)

func TemplatesMessageHandler(m mqueue.KafkaMessage) error {
var event mqueue.TemplateEvent
utils.LogTrace("kafka message data", string(m.Value))
if err := json.Unmarshal(m.Value, &event); err != nil {
utils.LogError("msg", string(m.Value), "message is not a valid JSON")
eType, event, err := processTemplateEvent(m.Value)
if err != nil {
utils.LogError("msg", err.Error(), "skipping template event")
// Skip invalid messages
return nil
}
eType := strings.TrimPrefix(event.Type, "com.redhat.console.repositories.")

errs := []error{}
var err error
for _, template := range event.Data {
if enableBypass {
utils.LogInfo("template", template.UUID, "Processing bypassed")
Expand Down Expand Up @@ -97,7 +94,7 @@ func TemplateUpdate(template mqueue.TemplateResponse) error {
UUID: template.UUID,
Name: template.Name,
//Config: nil,
Description: &template.Description,
Description: template.Description,
Creator: nil,
Published: &template.Date,
}
Expand All @@ -109,3 +106,19 @@ func TemplateUpdate(template mqueue.TemplateResponse) error {
}
return nil
}

func processTemplateEvent(value json.RawMessage) (eType string, event mqueue.TemplateEvent, err error) {
utils.LogTrace("kafka message data", string(value))
if err := json.Unmarshal(value, &event); err != nil {
err = errors.Wrap(err, fmt.Sprintf("value: %s", string(value)))
return "", event, errors.Wrap(err, "message is not a valid JSON")
}

for i, d := range event.Data {
if d.Description != nil && (len(*d.Description) == 0 || spacesRegex.MatchString(*d.Description)) {
d.Description = nil
event.Data[i] = d
}
}
return strings.TrimPrefix(event.Type, "com.redhat.console.repositories."), event, nil
}
6 changes: 4 additions & 2 deletions platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ var yumUpdates = `{
"metadata_time": "2022-05-30T14:00:25Z"
}`

var desc1 = "template_1_description"
var desc2 = "template_2_description"
var templates = []mqueue.TemplateResponse{
{
UUID: "10000000-0000-0000-0000-000000000001",
Name: "template_1",
OrgID: "org_1",
Description: "template_1_description",
Description: &desc1,
Arch: "x86_64",
Version: "1",
Date: time.Now(),
Expand All @@ -167,7 +169,7 @@ var templates = []mqueue.TemplateResponse{
UUID: "10000000-0000-0000-0000-000000000002",
Name: "template_2",
OrgID: "org_1",
Description: "template_2_description",
Description: &desc2,
Arch: "x86_64",
Version: "1",
Date: time.Now(),
Expand Down

0 comments on commit d0b36e0

Please sign in to comment.