-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Axway/dev
Merge dev to master
- Loading branch information
Showing
7 changed files
with
349 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,130 @@ | ||
package linker | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/Axway/ace-golang-sdk/util/logging" | ||
"go.uber.org/zap" | ||
|
||
"github.com/Axway/ace-golang-sdk/messaging" | ||
"github.com/Axway/ace-golang-sdk/rpc" | ||
) | ||
|
||
const ( | ||
stringType = "string" | ||
intType = "int" | ||
boolType = "boolean" | ||
) | ||
|
||
// messageContext - Represents the current execution context that holds the message and related properties | ||
type messageContext struct { | ||
ctx context.Context | ||
businessMessages []*messaging.BusinessMessage | ||
configMap map[string]*rpc.ConfigParameter | ||
msgProducer MsgProducer | ||
} | ||
|
||
// ExecutionContext - Interface exposed to clients to access the current message context | ||
type ExecutionContext interface { | ||
GetSpanContext() context.Context | ||
GetBusinessMessages() []*messaging.BusinessMessage | ||
GetMsgProducer() MsgProducer | ||
GetStringConfigValue(string) string | ||
GetIntConfigValue(string) int | ||
GetBooleanConfigValue(string) bool | ||
} | ||
|
||
// GetSpanContext - Returns the current context | ||
func (msgCtx *messageContext) GetSpanContext() context.Context { | ||
return msgCtx.ctx | ||
} | ||
|
||
// GetBusinessMessages - Returns business messages | ||
func (msgCtx *messageContext) GetBusinessMessages() []*messaging.BusinessMessage { | ||
return msgCtx.businessMessages | ||
} | ||
|
||
// GetMsgProducer - Returns the interfacee for producing messages | ||
func (msgCtx *messageContext) GetMsgProducer() MsgProducer { | ||
return msgCtx.msgProducer | ||
} | ||
|
||
// GetStringConfigValue - Returns the string confign value | ||
func (msgCtx *messageContext) GetStringConfigValue(name string) string { | ||
cfgParam, ok := msgCtx.configMap[name] | ||
if !ok { | ||
log.Warn("The requested configuration did not exist, returning default for type string", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamTypeRequested, stringType)) | ||
return "" | ||
} | ||
|
||
if cfgParam.GetType() != "string" { | ||
log.Warn("The requested configuration is not correct type, returning default for type string", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamType, cfgParam.GetType()), | ||
zap.String(logging.LogConfigParamTypeRequested, stringType)) | ||
return "" | ||
} | ||
|
||
return cfgParam.GetValue() | ||
} | ||
|
||
// GetIntConfigValue - Returns the int config value | ||
func (msgCtx *messageContext) GetIntConfigValue(name string) int { | ||
cfgParam, ok := msgCtx.configMap[name] | ||
if !ok { | ||
log.Warn("The requested configuration did not exist, returning default for type int", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamTypeRequested, boolType)) | ||
return 0 | ||
} | ||
|
||
if cfgParam.GetType() != "int" { | ||
log.Warn("The requested configuration is not correct type, returning default for type int", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamType, cfgParam.GetType()), | ||
zap.String(logging.LogConfigParamTypeRequested, intType)) | ||
return 0 | ||
} | ||
|
||
intVal, err := strconv.Atoi(cfgParam.GetValue()) | ||
if err != nil { | ||
log.Warn("Could not parse the config value to an int, returning default for type int", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamType, cfgParam.GetType()), | ||
zap.String(logging.LogConfigParamTypeRequested, intType)) | ||
return 0 | ||
} | ||
return intVal | ||
} | ||
|
||
// GetBooleanConfigValue - Returns the boolean config value | ||
func (msgCtx *messageContext) GetBooleanConfigValue(name string) bool { | ||
cfgParam, ok := msgCtx.configMap[name] | ||
if !ok { | ||
log.Warn("The requested configuration did not exist, returning default for type boolean", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamTypeRequested, boolType)) | ||
return false | ||
} | ||
|
||
if cfgParam.GetType() != "boolean" { | ||
log.Warn("The requested configuration is not correct type, returning default for type boolean", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamType, cfgParam.GetType()), | ||
zap.String(logging.LogConfigParamTypeRequested, boolType)) | ||
return false | ||
} | ||
|
||
boolVal, err := strconv.ParseBool(cfgParam.GetValue()) | ||
if err != nil { | ||
log.Warn("Could not parse the config value to a boolean, returning default for type boolean", | ||
zap.String(logging.LogConfigParamName, name), | ||
zap.String(logging.LogConfigParamType, cfgParam.GetType()), | ||
zap.String(logging.LogConfigParamTypeRequested, boolType)) | ||
return false | ||
} | ||
return boolVal | ||
} |
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,115 @@ | ||
package linker | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/Axway/ace-golang-sdk/messaging" | ||
"github.com/Axway/ace-golang-sdk/rpc" | ||
) | ||
|
||
// var testProcBusMsg *messaging.BusinessMessage | ||
|
||
var testExecutionContext ExecutionContext | ||
|
||
func testBusinessProcess(ec ExecutionContext) error { | ||
testExecutionContext = ec | ||
return nil | ||
} | ||
|
||
type testMsgProducer struct{} | ||
|
||
// Send - | ||
func (p testMsgProducer) Send(bm *messaging.BusinessMessage) error { | ||
return nil | ||
} | ||
|
||
func TestExecutionContext(t *testing.T) { | ||
ctx := context.Background() | ||
msgProducer := testMsgProducer{} | ||
configMap := make(map[string]*rpc.ConfigParameter) | ||
|
||
// Add all the config map values we will need | ||
configMap["string-param"] = &rpc.ConfigParameter{ | ||
Name: "string-param", | ||
Type: "string", | ||
Value: "string-value", | ||
} | ||
configMap["boolean-param"] = &rpc.ConfigParameter{ | ||
Name: "boolean-param", | ||
Type: "boolean", | ||
Value: "true", | ||
} | ||
configMap["int-param"] = &rpc.ConfigParameter{ | ||
Name: "int-param", | ||
Type: "int", | ||
Value: "123", | ||
} | ||
configMap["int-param-misconfig"] = &rpc.ConfigParameter{ | ||
Name: "int-param", | ||
Type: "int", | ||
Value: "abc", | ||
} | ||
|
||
msgContext := messageContext{ | ||
ctx: ctx, | ||
msgProducer: msgProducer, | ||
configMap: configMap, | ||
} | ||
|
||
testBusinessProcess(&msgContext) | ||
|
||
if ctx != testExecutionContext.GetSpanContext() { | ||
t.Error("incorrect Context received from GetSpanContext") | ||
} | ||
|
||
// Test GetStringConfigValue | ||
if testExecutionContext.GetStringConfigValue("string-param") != configMap["string-param"].Value { | ||
t.Errorf("The GetStringConfigValue method returned %s but we expected %s", testExecutionContext.GetStringConfigValue("string-param"), configMap["string-param"]) | ||
} | ||
|
||
if testExecutionContext.GetStringConfigValue("int-param") != "" { | ||
t.Error("The GetStringConfigValue method returned a value for an int type but we expected \"\"") | ||
} | ||
|
||
if testExecutionContext.GetStringConfigValue("param-does-not-exist") != "" { | ||
t.Error("The GetStringConfigValue method returned a value for a non existent params but we expected \"\"") | ||
} | ||
|
||
// Test GetIntConfigValue | ||
intVal, _ := strconv.Atoi(configMap["int-param"].Value) | ||
if testExecutionContext.GetIntConfigValue("int-param") != intVal { | ||
t.Errorf("The GetIntConfigValue method returned %d but we expected %s", testExecutionContext.GetIntConfigValue("int-param"), configMap["int-param"]) | ||
} | ||
|
||
if testExecutionContext.GetIntConfigValue("string-param") != 0 { | ||
t.Error("The GetIntConfigValue method returned a value for a string type but we expected 0") | ||
} | ||
|
||
if testExecutionContext.GetIntConfigValue("param-does-not-exist") != 0 { | ||
t.Error("The GetIntConfigValue method returned a value for a non existent params but we expected 0") | ||
} | ||
|
||
if testExecutionContext.GetIntConfigValue("int-param-misconfig") != 0 { | ||
t.Error("The GetIntConfigValue method returned a value for a non parsable value but we expected 0") | ||
} | ||
|
||
// Test GetBooleanConfigValue | ||
boolVal, _ := strconv.ParseBool(configMap["boolean-param"].Value) | ||
if testExecutionContext.GetBooleanConfigValue("boolean-param") != boolVal { | ||
t.Errorf("The GetBooleanConfigValue method returned %t but we expected %s", testExecutionContext.GetBooleanConfigValue("boolean-param"), configMap["boolean-param"]) | ||
} | ||
|
||
if testExecutionContext.GetBooleanConfigValue("string-param") != false { | ||
t.Error("The GetBooleanConfigValue method returned a value for a string type but we expected false") | ||
} | ||
|
||
if testExecutionContext.GetBooleanConfigValue("param-does-not-exist") != false { | ||
t.Error("The GetBooleanConfigValue method returned a value for a non existent params but we expected false") | ||
} | ||
|
||
if testExecutionContext.GetBooleanConfigValue("boolean-param-misconfig") != false { | ||
t.Error("The GetBooleanConfigValue method returned a value for a non parsable value but we expected false") | ||
} | ||
} |
Oops, something went wrong.