xmidt-org / wrp-go

go implementation of the Web Routing Protocol
Apache License 2.0
4 stars 7 forks source link

FR: Add simple message types validators for the WRP messages #85

Closed denopink closed 2 years ago

denopink commented 2 years ago

Overview

related to #25, #78, xmidt-org/scytale#88, xmidt-org/talaria#153 and builds on top of #80 and #84 .

tl;dr

This pr introduces our wrp simple message types validators built with our validation framework introduced in #80 and leverages basic spec validators introduced in #84 . Clients can leverage these prebuilt validators to validate their messages.

Explanation * Validates requirements described at https://xmidt.io/docs/wrp/simple-messages/ * Includes basic spec validation described at xmidt.io/docs/wrp/basics * Validates all string fields are UTF8 Clients can leverage our prebuilt validators to validate their messages, such as `SimpleEventValidators` and `SimpleResponseRequestValidators`: ```go msgv, err := NewTypeValidator( // Validates found msg types map[MessageType]Validator{ SimpleEventMessageType: SimpleEventValidators(), SimpleRequestResponseMessageType: SimpleResponseRequestValidators(), }, // Validates unfound msg types AlwaysInvalid()) if err != nil { return } var ( expectedStatus int64 = 3471 expectedRequestDeliveryResponse int64 = 34 expectedIncludeSpans bool = true ) foundErrFailure := msgv.Validate(Message{ Type: SimpleRequestResponseMessageType, // Missing scheme Source: "external.com", // Invalid Mac Destination: "MAC:+++BB-44-55", TransactionUUID: "DEADBEEF", ContentType: "ContentType", Accept: "Accept", Status: &expectedStatus, RequestDeliveryResponse: &expectedRequestDeliveryResponse, Headers: []string{"Header1", "Header2"}, Metadata: map[string]string{"name": "value"}, Spans: [][]string{ // // Invalid length {}, // Invalid length {"3"}, // Invalid 'start time', 'duration' and 'status' components {"parent", "name", "not start time", "not duration", "not status"}, // Invalid 'parent' and 'name' components {"1234", "1234", "1234", "1234", "1234"}, }, IncludeSpans: &expectedIncludeSpans, Path: "/some/where/over/the/rainbow", Payload: []byte{1, 2, 3, 4, 0xff, 0xce}, ServiceName: "ServiceName", // Not UFT8 URL string URL: "someURL\xed\xbf\xbf.com", PartnerIDs: []string{"foo"}, SessionID: "sessionID123", }) // Found error foundErrSuccess1 := msgv.Validate(Message{ Type: SimpleRequestResponseMessageType, Source: "MAC:11:22:33:44:55:66", Destination: "MAC:11:22:33:44:55:61", }) // Found success foundErrSuccess2 := msgv.Validate(Message{ Type: SimpleEventMessageType, Source: "MAC:11:22:33:44:55:66", // Invalid Destination Destination: "invalid:a-BB-44-55", }) // Found error unfoundErrFailure := msgv.Validate(Message{Type: CreateMessageType}) // Unfound error fmt.Println(foundErrFailure == nil, foundErrSuccess1 == nil, foundErrSuccess2 == nil, unfoundErrFailure == nil) // Output: false true false false ```
Type of Change(s) - Non-breaking Enhancement - All new and existing tests passed.
codecov[bot] commented 2 years ago

Codecov Report

Merging #85 (f0c26e2) into main (1bdb169) will increase coverage by 0.80%. The diff coverage is 47.53%.

@@            Coverage Diff             @@
##             main      #85      +/-   ##
==========================================
+ Coverage   50.16%   50.97%   +0.80%     
==========================================
  Files          21       24       +3     
  Lines        3827     3949     +122     
==========================================
+ Hits         1920     2013      +93     
- Misses       1736     1760      +24     
- Partials      171      176       +5     
Flag Coverage Δ
unittests 50.97% <47.53%> (+0.80%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
messages.go 96.77% <ø> (ø)
messages_codec.go 34.48% <20.17%> (+0.03%) :arrow_up:
qoslevel_string.go 40.00% <40.00%> (ø)
qos.go 100.00% <100.00%> (ø)
simpleMessageTypes_validator.go 100.00% <100.00%> (ø)
spec_validator.go 100.00% <100.00%> (ø)
validator.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 373d6d6...f0c26e2. Read the comment docs.