Commit 43904f94 authored by Ajit Jagtap's avatar Ajit Jagtap
Browse files

Service and Powerbuilder added.

This is for testing before we fix approch.
parent ff4884c2
Branches
Tags
1 merge request!23Devbranch to Master
Showing with 336 additions and 1 deletion
......@@ -62,7 +62,7 @@
[[projects]]
name = "github.com/op/go-logging"
packages = ["."]
revision = "b2cb9fa56473e98db8caba80237377e83fe44db5"
revision = "970db520ece77730c7e4724c61121037378659d9"
version = "v1"
[[projects]]
......
......@@ -12,6 +12,9 @@ import (
// IsTestingNegetiveCaseOn mark this on if you want system cases to fail
var IsTestingNegetiveCaseOnCheckBool bool
var IsTestingNegetiveCaseOnCheckBool1 bool
var IsTestingNegetiveCaseOnCheckBool2 bool
var IsTestingNegetiveCaseOnCheckBool3 bool
// IsTestingNegetiveCaseOn mark this on if you want system cases to fail
var IsTestingNegetiveCaseOnCheckInt bool
......@@ -41,6 +44,26 @@ func CheckBool(orginalInput bool) bool {
return orginalInput
}
func CheckBool1(orginalInput bool) bool {
if IsTestingNegetiveCaseOnCheckBool1 {
return !orginalInput
}
return orginalInput
}
func CheckBool2(orginalInput bool) bool {
if IsTestingNegetiveCaseOnCheckBool2 {
return !orginalInput
}
return orginalInput
}
func CheckBool3(orginalInput bool) bool {
if IsTestingNegetiveCaseOnCheckBool3 {
return !orginalInput
}
return orginalInput
}
// CheckInt will help checking int condition for real as well as test cases. It can fail based on IsTestingNegetiveCaseOn flag
func CheckInt(len int) int {
if IsTestingNegetiveCaseOnCheckInt {
......
......@@ -11,6 +11,8 @@ import (
"fmt"
"os"
"github.com/tidwall/gjson"
"github.com/TylerBrock/colorjson"
logging "github.com/op/go-logging"
goon "github.com/shurcooL/go-goon"
......@@ -95,6 +97,47 @@ func LogJSONString(str string) {
fmt.Println(string(s))
}
// LogJSONString Format string
func LogGJSONString(obj gjson.Result) {
// var objnew map[string]interface{}
// json.Unmarshal([]byte(str), &objnew)
f := colorjson.NewFormatter()
f.Indent = 2
s, _ := f.Marshal(obj.Raw)
fmt.Println(string(s))
}
func LogGJSONString2(obj *gjson.Result) {
// var objn ew map[string]interface{}
var objnew1 map[string]interface{}
// st := obj.Value
// fmt.Println(obj)
test, _ := json.Marshal(obj)
// LogVars()
json.Unmarshal(test, &objnew1)
// fmt.Println(objnew1["Raw"])
somevar := objnew1["Raw"]
mystr := somevar.(string)
// fmt.Println("<<<<", mystr)
a := `{"Age": 1,"Name": "sometext"}`
LogJSONString(mystr)
LogJSONString(a)
// test2, _ := json.Marshal(somevar)
// err1 := json.Unmarshal([]byte(somevar), &objnew)
// fmt.Println(err1, objnew)
// f := colorjson.NewFormatter()
// f.Indent = 2
// s, _ := f.Marshal(objnew)
// fmt.Println(string(s))
}
// LogHRStart can end line with <<<
func LogHRStart() {
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>")
......
package powerbuildermdl
package servicebuildermdl
import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/tidwall/gjson"
"github.com/caibirdme/yql"
)
// LoadData is a method sign for loader methods
type LoadData = func(ab *AbstractBusinessLogicHolder)
// FinalStepProcessOutput is a method sign for loader methods
type FinalStepProcessOutput = func(ab *AbstractBusinessLogicHolder) *interface{}
// AbstractBusinessLogicHolder use this type to inheritance
type AbstractBusinessLogicHolder struct {
ServiceData map[string]interface{}
}
// GetDataString will give you string
func (ab *AbstractBusinessLogicHolder) GetDataString(key string) (string, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return "", false
}
// cast it
value, ok := temp.(string)
if errormdl.CheckBool1(!ok) {
return "", false
}
return value, true
}
// GetDataInt will give you int
func (ab *AbstractBusinessLogicHolder) GetDataInt(key string) (int, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return 0, false
}
// cast it
value, ok := temp.(int)
if errormdl.CheckBool1(!ok) {
return 0, false
}
return value, true
}
// GetBool will give you int
func (ab *AbstractBusinessLogicHolder) GetResult(key string) (*gjson.Result, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
loggermdl.LogWarn("Key not found -", key)
return &gjson.Result{}, false
}
// cast it
value, ok := temp.(*gjson.Result)
if errormdl.CheckBool1(!ok) {
return &gjson.Result{}, false
}
return value, true
}
// GetBool will give you int
func (ab *AbstractBusinessLogicHolder) GetBool(key string) (bool, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return false, false
}
// cast it
value, ok := temp.(bool)
if errormdl.CheckBool1(!ok) {
return false, false
}
return value, true
}
// GetInterface will give you string
func (ab *AbstractBusinessLogicHolder) GetInterface(key string) (interface{}, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return 0, false
}
// cast it
return temp, true
}
// Build will create memory for your data
func (ab *AbstractBusinessLogicHolder) Build() {
ab.ServiceData = make(map[string]interface{})
}
// GetFinalData will return map data with finaldata key
func (ab *AbstractBusinessLogicHolder) GetFinalData() *interface{} {
a := ab.ServiceData["finaldata"]
return &a
}
// GetFinalData will return map data with finaldata key
func (ab *AbstractBusinessLogicHolder) SetFinalData(data interface{}) {
ab.ServiceData["finaldata"] = data
}
// EchoBL sample EchoBL logic handler
func (ab *AbstractBusinessLogicHolder) EchoBL() map[string]interface{} {
loggermdl.LogWarn("EchoBL called")
return map[string]interface{}{
"ok": int64(1),
}
}
// Step help to maintain steps
type Step struct {
Stepname string
RawYQL string
processDataFunc LoadData
RunFunc func() map[string]interface{}
ErrorFunc func() map[string]interface{}
}
// ServiceBuilder will help you to run steps
type ServiceBuilder struct {
ServiceName string
steps []Step
businessLogicHolder *AbstractBusinessLogicHolder
}
// GetSB Gives you service builder from where you can run steps
func GetSB(name string, ab *AbstractBusinessLogicHolder) *ServiceBuilder {
newsb := ServiceBuilder{}
newsb.ServiceName = name
newsb.businessLogicHolder = ab
return &newsb
}
// AddStep will add func step with rule
// Stepname : Give Name to step. It will appear in log.
// Rule : Give Ybl rule
// blfunc : Give Business Logic function pointer
// errorfunc : Give Error function pointer
func (sb *ServiceBuilder) AddStep(stepname, rule string, ld LoadData, blfunc, errorfunc func() map[string]interface{}) *ServiceBuilder {
step := Step{}
step.RawYQL = rule
step.RunFunc = blfunc
step.ErrorFunc = errorfunc
step.Stepname = stepname
step.processDataFunc = ld
sb.steps = append(sb.steps, step)
return sb
}
// Run all Steps one by one
func (sb *ServiceBuilder) Run() *ServiceBuilder {
for _, step := range sb.steps {
//Load Data
if step.processDataFunc != nil {
step.processDataFunc(sb.businessLogicHolder)
}
//Run step func
tmp := step.RunFunc()
result, _ := yql.Match(step.RawYQL, tmp)
if !result {
loggermdl.LogWarn(step.Stepname, "Failed", result)
step.ErrorFunc()
break
}
}
return sb
}
// FinalOutput Step
func (sb *ServiceBuilder) FinalOutput(fn FinalStepProcessOutput) *interface{} {
if fn == nil {
return sb.businessLogicHolder.GetFinalData()
}
return fn(sb.businessLogicHolder)
}
package servicebuildermdl
import (
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
dalmdl "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/dalmdl/dao"
"github.com/caibirdme/yql"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)
//Create Type with abstract Type
type MyBLLayer struct {
AbstractBusinessLogicHolder
}
// attach methods to it
func (m *MyBLLayer) FindAndVerifyMobileNumber() map[string]interface{} {
loginID := "noone"
result1, ok := m.GetResult("daodata1")
if !ok {
loggermdl.LogError("error in convert")
}
loginID = result1.Get("#[mobileNo==\"7875383220\"].loginId").String()
m.SetFinalData(loginID)
return map[string]interface{}{
"owner": loginID,
}
}
//Now we will write DAO
func GetUsersInfo() *gjson.Result {
return dalmdl.GetDAO().FilePath("../testingdata/users.json").IsCacheable(true).Query("*").Run()
}
func TestService(t *testing.T) {
mb := MyBLLayer{}
mb.Build()
loaddata := func(ab *AbstractBusinessLogicHolder) {
mb.ServiceData["daodata1"] = GetUsersInfo()
}
// finalStep := func(ab *AbstractBusinessLogicHolder) *interface{} {
// s :=
// return &s
// }
sb := GetSB("mysrv", &mb.AbstractBusinessLogicHolder).
AddStep("Check Mobile", "owner in ('SystemIntegrator')", loaddata, mb.FindAndVerifyMobileNumber, mb.EchoBL).
AddStep("Test STEP 2", "owner = 'SystemIntegrator'", nil, mb.FindAndVerifyMobileNumber, mb.EchoBL).
Run().FinalOutput(nil)
assert.Equal(t, "SystemIntegrator", *sb, "this should be same")
}
func TestYQL(t *testing.T) {
a := map[string]interface{}{
"owner": "a",
}
result, _ := yql.Match("owner ='a')", a)
assert.Equal(t, true, result, "Yql should work")
}
func TestPanic(t *testing.T) {
var a interface{}
a = nil
// a= nil
_, ok := a.(string)
assert.False(t, ok, "should panic")
// assert.Equal(t, "something", s, "should be same")
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment