2017-06-22 03:07:45 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-06-27 00:08:47 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-06-22 03:07:45 +00:00
|
|
|
|
2017-06-22 05:12:42 +00:00
|
|
|
"git.loafle.net/overflow/overflow_service/proxy/member"
|
|
|
|
"git.loafle.net/overflow/overflow_service/proxy/target"
|
2017-06-22 03:07:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServices(t *testing.T) {
|
|
|
|
InitServices()
|
|
|
|
|
|
|
|
meb, _ := g_services["Member"]
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["email"] = "geek@loafle.com"
|
|
|
|
m["pw"] = "qwer5795"
|
|
|
|
|
|
|
|
convertParam(meb, m)
|
|
|
|
//val := []reflect.Value{reflect.ValueOf(meb.(*proxy.MemberService))}
|
|
|
|
|
|
|
|
va := reflect.ValueOf(meb).MethodByName("Regist").Call([]reflect.Value{})
|
|
|
|
|
|
|
|
//reflect.ValueOf(meb).MethodByName("Read").Call(reflect.ValueOf(meb))
|
|
|
|
|
|
|
|
assert.Equal(t, 0, len(va))
|
|
|
|
}
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
func TestServiceParam(t *testing.T) {
|
2017-06-22 03:07:45 +00:00
|
|
|
|
|
|
|
InitServices()
|
|
|
|
|
|
|
|
meb, _ := g_services["Member"]
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["email"] = "geek@loafle.com"
|
|
|
|
m["pw"] = "qwer5795"
|
|
|
|
|
|
|
|
paramStr, err := json.Marshal(m)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatal("Json Marshal Failed : ", err.Error())
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(paramStr, meb)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatal("Json Unmarshal Failed : ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
//assert.ObjectsAreEqual(proxy.MemberService{}, meb)
|
|
|
|
}
|
2017-06-27 00:08:47 +00:00
|
|
|
|
2017-06-22 03:07:45 +00:00
|
|
|
//
|
|
|
|
//type Member struct {
|
|
|
|
// Email string `json:"email"`
|
|
|
|
// Password string `json:"password"`
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//type MemberService struct {
|
|
|
|
// ServiceName string `json:"serviceName"`
|
|
|
|
// MethodName string `json:"methodName"`
|
|
|
|
// Param *Member `json:"param"`
|
|
|
|
//}
|
|
|
|
|
|
|
|
func TestExecServices(t *testing.T) {
|
|
|
|
//res := `{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}`
|
|
|
|
res := `{"email":"geekloafle@loafle.com","pwSalt":"dssdft","company":"loafle"}`
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["model"] = res
|
|
|
|
|
|
|
|
InitServices()
|
|
|
|
|
|
|
|
meb, _ := g_services["Member"]
|
|
|
|
|
|
|
|
methodName := reflect.ValueOf(meb).MethodByName("Regist")
|
|
|
|
params := make([]reflect.Value, methodName.Type().NumIn())
|
|
|
|
var NewObj reflect.Value
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
for i := 0; i < methodName.Type().NumIn(); i++ {
|
2017-06-22 03:07:45 +00:00
|
|
|
temp := methodName.Type().In(i)
|
|
|
|
|
|
|
|
switch temp.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
NewObj = reflect.New(temp.Elem())
|
|
|
|
convertParam(NewObj.Interface(), m)
|
|
|
|
params[i] = NewObj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := methodName.Call(params)[0].String()
|
|
|
|
fmt.Println(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
//func TestReflectType(t *testing.T) {
|
2017-06-27 00:08:47 +00:00
|
|
|
//ss := &MemberService{}
|
|
|
|
////t1 := reflect.ValueOf(ss.Param).Elem()
|
|
|
|
//
|
|
|
|
//ms1 := reflect.New(reflect.TypeOf(ss.Param)).Elem().Type()
|
|
|
|
//
|
|
|
|
//fmt.Println(ms1)
|
|
|
|
////ss.Param = ms1
|
|
|
|
////ss.Param.Email = "ddd"
|
|
|
|
////fmt.Println(ss.Param.Email)
|
|
|
|
//ms := reflect.New(reflect.ValueOf(ss.Param).Type()).Elem()
|
|
|
|
//fmt.Printf("created a [%T]\n", reflect.ValueOf(ms).Type())
|
2017-06-22 03:07:45 +00:00
|
|
|
|
|
|
|
//}
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
func TestConvertParamModel(t *testing.T) {
|
2017-06-22 03:07:45 +00:00
|
|
|
res := `{"email":"geek@loafle.com","pwSalt":"dssdft","company":"loafle"}`
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
m["model"] = res
|
|
|
|
|
|
|
|
mem := &member.Member{}
|
|
|
|
convertParam(mem, m)
|
|
|
|
|
|
|
|
fmt.Println(mem.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamTypeCreate(t *testing.T) {
|
|
|
|
|
|
|
|
ms := member.NewMemberService()
|
|
|
|
|
|
|
|
res := `{"email":"geek@loafle.com","pwSalt":"dssdft","company":"loafle"}`
|
|
|
|
|
|
|
|
method := reflect.ValueOf(ms).MethodByName("Regist")
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
var NewObj reflect.Value
|
2017-06-22 03:07:45 +00:00
|
|
|
for i := 0; i < method.Type().NumIn(); i++ {
|
|
|
|
tt := method.Type().In(i)
|
|
|
|
|
|
|
|
t.Log(tt)
|
|
|
|
|
|
|
|
switch tt.Kind() {
|
2017-06-27 00:08:47 +00:00
|
|
|
case reflect.Ptr:
|
2017-06-22 03:07:45 +00:00
|
|
|
NewObj = reflect.New(tt.Elem())
|
|
|
|
}
|
|
|
|
|
|
|
|
//obj = NewObj.Elem().Interface()
|
|
|
|
|
|
|
|
//intPtr := reflect.New(tt)
|
|
|
|
//obj := intPtr.Elem().Interface()
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(res), NewObj.Interface())
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(NewObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make([]reflect.Value, method.Type().NumIn())
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
params[0] = NewObj
|
2017-06-22 03:07:45 +00:00
|
|
|
|
|
|
|
result := method.Call(params)[0].String()
|
|
|
|
|
|
|
|
t.Log(result)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListReflect(t *testing.T) {
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
res := "[{\"id\":1,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496631864025,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}},{\"id\":2,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496635285090,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}},{\"id\":3,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496642868260,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}},{\"id\":4,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496814093714,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}},{\"id\":5,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496814103052,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}},{\"id\":6,\"ip\":3232235882,\"port\":5432,\"targetType\":\"DATABASE\",\"vendorName\":\"PostgreSQL 9.5.0\",\"kinds\":\"PostgreSQL\",\"version\":\"9.5.0\",\"createDate\":1496814184478,\"member\":{\"id\":1,\"email\":\"ddddd@loafle.com\",\"pwSalt\":\"salktttt\",\"digest\":\"bbbbbbbbb\",\"name\":\"snnnnn\",\"company\":\"loafle\",\"phone\":\"000-000-0000\",\"authorizedDate\":null}}]"
|
2017-06-22 03:07:45 +00:00
|
|
|
|
|
|
|
t.Log(res)
|
|
|
|
|
|
|
|
ts := target.NewTargetService()
|
|
|
|
|
|
|
|
method := reflect.ValueOf(ts).MethodByName("CreateList")
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
var NewObj reflect.Value
|
2017-06-22 03:07:45 +00:00
|
|
|
for i := 0; i < method.Type().NumIn(); i++ {
|
|
|
|
tt := method.Type().In(i)
|
|
|
|
|
|
|
|
t.Log(tt)
|
|
|
|
|
|
|
|
switch tt.Kind() {
|
2017-06-27 00:08:47 +00:00
|
|
|
case reflect.Ptr:
|
2017-06-22 03:07:45 +00:00
|
|
|
NewObj = reflect.New(tt.Elem())
|
2017-06-27 00:08:47 +00:00
|
|
|
case reflect.Slice:
|
2017-06-22 03:07:45 +00:00
|
|
|
NewObj = reflect.MakeSlice(tt, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(res), NewObj.Interface())
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(NewObj)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make([]reflect.Value, method.Type().NumIn())
|
|
|
|
|
2017-06-27 00:08:47 +00:00
|
|
|
params[0] = NewObj
|
2017-06-22 03:07:45 +00:00
|
|
|
|
|
|
|
result := method.Call(params)[0].String()
|
|
|
|
|
|
|
|
t.Log(result)
|
|
|
|
|
|
|
|
//ts.cre
|
|
|
|
|
|
|
|
}
|