func Test_StateMachineWithParameterToJson(t *testing.T) { var m sm.Machine err := json.Unmarshal([]byte(`{ "name" : "test", "machine" : "default", "states" : [ "ok", "fail" ], "properties" : { "failure_rate" : { "type" : "ITrigger" } }, "transitions" : { "ok" : { "fail" : [ { "type" : "property", "property" : "failure_rate" } ] }, "fail" : { "ok" : [ { "type" : "deterministic", "parameter" : 2 } ] } } }`), &m) assert.Nil(t, err) duration2h, _ := tools.ParseDuration("2h") property, _ := m.Property("failure_rate") property.SetValue(triggers.NewDeterministicTrigger(duration2h)) jsonBin, _ := json.MarshalIndent(&m, "", " ") jsonStr := string(jsonBin) expected := strings.Join([]string{ "{", " \"name\": \"test\",", " \"machine\": \"default\",", " \"states\": {", " \"ok\": {", " \"name\": \"ok\",", " \"transitions\": {", " \"fail\": {", " \"to\": \"fail\"", " }", " }", " },", " \"fail\": {", " \"name\": \"fail\",", " \"transitions\": {", " \"ok\": {", " \"to\": \"ok\"", " }", " }", " }", " },", " \"state\": \"ok\",", " \"properties\": {", " \"failure_rate\": {", " \"name\": \"failure_rate\",", " \"value\": {", " \"type\": \"deterministic\",", " \"parameter\": 0.00022831050228310502", " },", " \"required\": true", " }", " }", "}", }, "\n") if jsonStr != expected { fmt.Println(jsonStr) t.Errorf("Unexpected JSON serialisation") } }
func Test_ParseDuration(t *testing.T) { if v, err := tools.ParseDuration("1"); err != nil || v != 1.0 { t.Errorf("Parsed '1' to %v, expect 1.0", v) } if v, err := tools.ParseDuration("1.5"); err != nil || v != 1.5 { t.Errorf("Parsed '1.5' to %v, expect 1.5", v) } if v, err := tools.ParseDuration("1year"); err != nil || v != 1 { t.Errorf("Parsed '1 year' to %v, expect 1.0", v) } if v, err := tools.ParseDuration("52 weeks"); err != nil || v != 1 { t.Errorf("Parsed '52 weeks' to %v, expect 1.0", v) } if v, err := tools.ParseDuration("10 min"); err != nil || v != 1.9025875190258754e-05 { t.Errorf("Parsed '10 min' to %v, expect 1.9...e-5", v) } }
func (f *Factory) loadTriggerFromJSON(b []byte) (sm.ITrigger, error) { var t sm.ITrigger var err error data := make(map[string]interface{}) err = json.Unmarshal(b, &data) if err != nil { return nil, err } jm := tools.JsonMap(data) if transitionType, ok := jm.GetString("type"); ok { comment, _ := jm.GetString("comment") switch transitionType { case "idle": t = triggers.NewIdleTrigger() case "deterministic": if parameter, ok := jm.GetString("parameter"); ok { if parameter == "never" { fmt.Println("Deprecated: trigger deterministic/never trigger should be replaced with idle trigger type.") t = triggers.NewIdleTrigger() } else if value, err := tools.ParseDuration(parameter); err == nil { fmt.Println("Deprecated: trigger parameter should be numeric.") t = triggers.NewDeterministicTrigger(value) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition's deterministic parameter.") } } else if parameter, ok := jm.GetFloat("parameter"); ok { t = triggers.NewDeterministicTrigger(parameter) } else if parameter, ok := jm.GetInt("parameter"); ok { t = triggers.NewDeterministicTrigger(float64(parameter)) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition's deterministic parameter.") } case "probabilistic": if distribution, ok := jm.GetString("distribution"); ok { switch distribution { case "exponential": if param, ok := jm.GetString("parameter"); ok { if value, err := tools.ParseDuration(param); err == nil { fmt.Println("Deprecated: trigger parameter should be numeric.") t = triggers.NewProbabilisticTrigger(value) t.SetComment(comment) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition's exponential distribution parameter.") } } else { if value, ok := jm.GetFloat("parameter"); ok { t = triggers.NewProbabilisticTrigger(value) } else if value, ok := jm.GetInt("parameter"); ok { t = triggers.NewProbabilisticTrigger(float64(value)) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition's exponential distribution parameter.") } } default: t = triggers.NewIdleTrigger() err = fmt.Errorf("This distribution is not supported.") } } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition's distribution type.") } case "property": if property, ok := jm.GetString("property"); ok { t = triggers.NewPropertyTrigger(property) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition.") } } t.SetComment(comment) } else { t = triggers.NewIdleTrigger() err = fmt.Errorf("Cannot parse transition type.") } return t, err }