overflow_service/gateway/service_test.go
2017-06-27 09:08:47 +09:00

211 lines
6.4 KiB
Go

package gateway
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"git.loafle.net/overflow/overflow_service/proxy/member"
"git.loafle.net/overflow/overflow_service/proxy/target"
)
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))
}
func TestServiceParam(t *testing.T) {
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)
}
//
//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
for i := 0; i < methodName.Type().NumIn(); i++ {
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) {
//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())
//}
func TestConvertParamModel(t *testing.T) {
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")
var NewObj reflect.Value
for i := 0; i < method.Type().NumIn(); i++ {
tt := method.Type().In(i)
t.Log(tt)
switch tt.Kind() {
case reflect.Ptr:
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())
params[0] = NewObj
result := method.Call(params)[0].String()
t.Log(result)
}
func TestListReflect(t *testing.T) {
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}}]"
t.Log(res)
ts := target.NewTargetService()
method := reflect.ValueOf(ts).MethodByName("CreateList")
var NewObj reflect.Value
for i := 0; i < method.Type().NumIn(); i++ {
tt := method.Type().In(i)
t.Log(tt)
switch tt.Kind() {
case reflect.Ptr:
NewObj = reflect.New(tt.Elem())
case reflect.Slice:
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())
params[0] = NewObj
result := method.Call(params)[0].String()
t.Log(result)
//ts.cre
}