Exemplo n.º 1
0
func testGetMultStatementOfGroup(t *testing.T, group *gabs.Container) {
	db := initDatabase(t)
	defer db.Close()
	mart := initHandler(db)

	ids := []string{
		uuid.NewV4().String(),
		uuid.NewV4().String(),
		uuid.NewV4().String(),
		uuid.NewV4().String(),
	}

	stmt, err := gabs.ParseJSON([]byte(statetmentAgentBoilerplate))
	fatalIfError(t, err)

	stmtSlice := []*gabs.Container{
		appendAccount(t, stmt),
		appendMbox(t, stmt),
		appendMboxSHA1(t, stmt),
		appendOpenID(t, stmt),
	}

	for i := 0; i < len(ids); i++ {
		putStatement(t, mart, stmtSlice[i].String(), ids[i])

		var s interface{}
		err = json.Unmarshal([]byte(stmtSlice[i].Search("actor").String()), &s)
		fatalIfError(t, err)
		err = group.ArrayAppendP(s, "actor.member")
		fatalIfError(t, err)
	}

	// construct query
	v := &url.Values{}
	//t.Log(group.Search("actor").String())
	v.Add("agent", group.Search("actor").String())

	resp := getStatement(t, mart, v)
	//t.Log(string(resp))
	respstmt, err := gabs.ParseJSON(resp)
	fatalIfError(t, err)
	cnt, err := respstmt.ArrayCount("statements")
	fatalIfError(t, err)

	if cnt != len(ids) {
		t.Fatalf("Expected %d statements in response; got %d", len(ids), cnt)
	}

	children, err := respstmt.S("statements").Children()
	fatalIfError(t, err)
	for idx, stm := range children {
		if id, ok := stm.Search("id").Data().(string); !ok || id != ids[idx] {
			t.Fatalf("Got invalid order of statement array")
		}
	}
}
Exemplo n.º 2
0
func testGetMultStatementOfActor(t *testing.T, stmt *gabs.Container) {
	db := initDatabase(t)
	defer db.Close()
	mart := initHandler(db)

	id1 := uuid.NewV4().String()
	id2 := uuid.NewV4().String()

	putStatement(t, mart, stmt.String(), id1)
	putStatement(t, mart, stmt.String(), id2)

	// construct query
	v := &url.Values{}
	v.Add("agent", stmt.Search("actor").String())

	respstmt, err := gabs.ParseJSON(getStatement(t, mart, v))
	fatalIfError(t, err)
	cnt, err := respstmt.ArrayCount("statements")
	fatalIfError(t, err)

	if cnt != 2 {
		t.Fatalf("Expected 2 statements in response; got %d", cnt)
	}

	s0, err := respstmt.ArrayElement(0, "statements")
	fatalIfError(t, err)
	if id, ok := s0.Search("id").Data().(string); !ok || id != id1 {
		t.Fatalf("Got invalid order of statement array")
	}

	s1, err := respstmt.ArrayElement(1, "statements")
	fatalIfError(t, err)
	if id, ok := s1.Search("id").Data().(string); !ok || id != id2 {
		t.Fatalf("Got invalid order of statement array")
	}
}
Exemplo n.º 3
0
func search(data *gabs.Container, filter tFilter, i int) (*gabs.Container, string) {
	var ret string //returned value
	filter.Path = filter.Path[i:]
	//fmt.Println("I:::", i, filter.Path, reflect.TypeOf(data.Data()).String())
	//fmt.Println(data)
	// fmt.Println(data.Search(filter.Key).String())
	if len(filter.Value) > 0 {
		if data.Search(filter.Key).Data() == filter.Value {
			ret = data.Search(filter.Ret).Data().(string)

			if len(filter.Match) > 0 {

				if regexp.MustCompile(filter.Match).MatchString(ret) {
					if len(filter.Replace) > 0 {
						ret = regexp.MustCompile(filter.Replace).ReplaceAllString(ret, "")
					}
					return data, ret
				} else {
					ret = ""
				}

			} else {
				if len(filter.Replace) > 0 {
					ret = regexp.MustCompile(filter.Replace).ReplaceAllString(ret, "")
				}
				return data, ret
			}
		}
	}

	for j, str := range filter.Path {

		if data.Data() != nil {
			if reflect.TypeOf(data.Data()).String() == "[]interface {}" {
				children, err := data.Children()
				if err != nil {
					fmt.Println(err.Error())
				}
				for _, child := range children {
					data, ret = search(child, filter, j)
					if len(ret) > 0 {
						return data, ret
					}
				}
			} else {

				data = data.Search(str)
			}
		}
		// fmt.Println(str, data)
	}
	return data, ret
}