示例#1
0
func Test_buildElasticsearchQuery(t *testing.T) {
	q := map[string]interface{}{
		"os": "ubuntu", "release": ">5",
	}
	opts, _ := types.NewQueryOptions(map[string][]string{
		"size": []string{"100"},
	})

	//jsonStr := []byte(`{"os":"ubuntu", "release":">5"}`)
	//r, _ := http.NewRequest("GET", "http://localhost:5454/v1/pool?size=100", bytes.NewBuffer(jsonStr))
	_, err := buildElasticsearchQuery("testIndex", q, &opts)
	if err != nil {
		t.Fatal(err)
	}

	opts.From = 0
	//r, _ = http.NewRequest("GET", "http://localhost:5454/v1/pool?from=0&size=100", bytes.NewBuffer(jsonStr))
	_, err = buildElasticsearchQuery("testIndex", q, &opts)
	if err != nil {
		t.Fatal(err)
	}

	//r, _ = http.NewRequest("GET", "http://localhost:5454/v1/pool?from=0&size=100", nil)
	_, err = buildElasticsearchQuery("testIndex", nil, &opts)
	if err != nil {
		t.Fatal(err)
	}
}
示例#2
0
func Test_buildElasticsearchQueryOptions_case1(t *testing.T) {
	qOpts1, _ := types.NewQueryOptions(map[string][]string{
		"sort": []string{"name:asc", "age:desc", "title"},
		"size": []string{"128"},
		"from": []string{"80"},
	})
	qopts1 := buildElasticsearchQueryOptions(qOpts1)

	//Type assertion
	if qopts1["size"].(int64) != 128 || qopts1["from"].(int64) != 80 {
		t.Fatalf("Parser returned wrong size or from")
	}
	for _, opt := range qopts1["sort"].([]map[string]string) {
		for k, v := range opt {
			switch k {
			case "name":
				if v != "asc" {
					t.Fatalf("Name should be in ascending order")
				}
			case "age":
				if v != "desc" {
					t.Fatalf("Age should be in descending order")
				}
			case "title":
				if v != "asc" {
					t.Fatalf("Title should be in ascending order")
				}
			}
		}
	}
}
示例#3
0
func Test_buildElasticsearchQueryOptions_case3(t *testing.T) {
	qOpts3, _ := types.NewQueryOptions(map[string][]string{
		"size": []string{"128"},
	})

	qopts3 := buildElasticsearchQueryOptions(qOpts3)
	if qopts3["size"].(int64) != 128 || qopts3["from"].(int64) != 0 {
		t.Fatalf("Parser returned wrong size or from")
	}
}
func Test_InventoryDatastore_aggregate_query(t *testing.T) {
	opts, _ := types.NewQueryOptions(map[string][]string{
		"aggregate": []string{"updated_by"},
	})
	rslt, err := testIds.Query(testAssetType, nil, &opts, false)
	if err != nil {
		t.Fatal(err)
	}

	aggs, ok := rslt.([]AggregatedItem)
	if !ok {
		t.Fatal("Wrong type")
	}

	t.Logf("%#v\n", aggs)
}
示例#5
0
/*
   Handle getting asset versions GET /<asset_type>/<asset>/versions
*/
func (ir *VindaluApiHandler) AssetVersionsHandler(w http.ResponseWriter, r *http.Request) {
	var (
		headers = map[string]string{}
		code    int
		data    = make([]byte, 0)

		restVars  = mux.Vars(r)
		assetType = normalizeAssetType(restVars["asset_type"])
		assetId   = restVars["asset"]
	)
	// Get search parameters
	//reqOpts, err := core.ParseGlobalParams(ir.Config().DefaultResultSize, r.URL.Query())
	reqOpts, err := types.NewQueryOptions(r.URL.Query())
	//reqOpts, err := core.BuildElasticsearchQueryOptions(ir.Config().DefaultResultSize, r.URL.Query())
	if err != nil {
		code = 400
		data = []byte(err.Error())
		headers["Content-Type"] = "text/plain"
	} else {
		// The count should come from a query param
		//versionCount, _ := reqOpts["size"].(int64)
		ir.apiLog.Debugf("Requested version count: %d\n", reqOpts.Size)

		assetVersions, err := ir.GetResourceVersions(assetType, assetId, reqOpts.Size)
		if err != nil {
			code = 404
			data = []byte(err.Error())
			headers["Content-Type"] = "text/plain"
		} else {
			if len(assetVersions) < 1 {
				ir.apiLog.Debugf("No versions found!\n")
				code = 200
				data, _ = json.Marshal(assetVersions)
				headers["Content-Type"] = "application/json"
			} else {
				ir.apiLog.Debugf("Recieved versions: %d\n", len(assetVersions))
				// Check if diff was requested.
				if _, ok := r.URL.Query()["diff"]; ok {

					diffs, err := versioning.GenerateVersionDiffs(assetVersions...)
					if err != nil {
						data = []byte(err.Error())
						code = 400
						headers["Content-Type"] = "text/plain"
					} else {
						code = 200
						data, _ = json.Marshal(diffs)
						headers["Content-Type"] = "application/json"
					}
				} else {
					code = 200
					data, _ = json.Marshal(assetVersions)
					headers["Content-Type"] = "application/json"
				}
			}
		}
	}

	w.Header().Set("Access-Control-Allow-Origin", "*")
	ir.writeAndLogResponse(w, r, code, headers, data)
}