Beispiel #1
0
func main() {
	cfg, err := parseConfig()
	if err != nil {
		panic(err)
	}

	s := server.NewServer(cfg.Host, cfg.Port)

	s.UseStore(store.NewMongoStore(cfg.MongoDatabase.Host, cfg.MongoDatabase.Database))
	s.Start()
}
func TestCrudSensingProfile(t *testing.T) {

	var w *MockResponseWriter
	var ret map[string]interface{}
	var req *http.Request
	var l int
	c := web.C{}
	var entityTypes []string = []string{
		"/FeaturesOfInterest",
		"/Locations",
		"/Sensors",
		"/Observations",
		"/Datastreams",
		"/Things",
		"/ObservedProperties",
	}

	s := &server.GossamerServer{}
	s.UseStore(store.NewMongoStore("localhost", "sensorthings"))
	DropCollection()

	// ####### BASIC INSERT #######
	//	Create Location
	req, w = NewMockHttp("POST", "/Locations", NewDefaultLocation())
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/Locations", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))

	//	Create Sensor
	req, w = NewMockHttp("POST", "/Sensors", NewDefaultSensor())
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/Sensors", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))
	sensorId := GetMapProperty(0, "@iot.id", ret)

	//	Create ObservedProperty
	req, w = NewMockHttp("POST", "/ObservedProperties", NewDefaultObservedProperty())
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/ObservedProperties", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))
	observedPropertyId := GetMapProperty(0, "@iot.id", ret)

	//	Create FeatureOfInterest
	req, w = NewMockHttp("POST", "/FeaturesOfInterest", NewDefaultFeaturesOfInterest())
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/FeaturesOfInterest", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))
	featureOfInterestId := GetMapProperty(0, "@iot.id", ret)

	//	Create Thing
	req, w = NewMockHttp("POST", "/Things", NewDefaultThing())
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/Things", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))
	thingId := GetMapProperty(0, "@iot.id", ret)

	// Create Datastream
	var ds *gossamer.DatastreamEntity
	ds = NewDefaultDatastream()
	ds.Thing = &gossamer.ThingEntity{}
	ds.Thing.Id = thingId
	ds.Sensor = &gossamer.SensorEntity{}
	ds.Sensor.Id = sensorId
	ds.ObservedProperty = &gossamer.ObservedPropertyEntity{}
	ds.ObservedProperty.Id = observedPropertyId

	req, w = NewMockHttp("POST", "/Datastreams", ds)
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/Datastreams", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))
	datastreamId := GetMapProperty(0, "@iot.id", ret)

	// Create Observation
	var obs *gossamer.ObservationEntity
	obs = NewDefaultObservation()
	ds = &gossamer.DatastreamEntity{}
	ds.Id = datastreamId
	obs.Datastream = ds
	foi := &gossamer.FeatureOfInterestEntity{}
	foi.Id = featureOfInterestId
	obs.FeatureOfInterest = foi
	req, w = NewMockHttp("POST", "/Observations", obs)
	s.HandlePost(c, w, req)
	req, w = NewMockHttp("GET", "/Observations", "")
	s.HandleGet(c, w, req)
	ret = w.GetJSON()
	assert.Equal(t, 1, len(ret["value"].([]interface{})))

	// ####### ASSOCIATIVE INSERTS #######

	// ####### UPDATE #######

	// ####### UPDATE (PATCH) #######

	// ####### ADVANCED QUERIES #######
	for _, v := range entityTypes {
		u := v + "?$top=1"
		req, w = NewMockHttp("GET", u, "")
		s.HandleGet(c, w, req)
		ret = w.GetJSON()
		assert.NotNil(t, ret)
		assert.Equal(t, 1, len(ret["value"].([]interface{})))
		id := ret["value"].([]interface{})[0].(map[string]interface{})["@iot.id"]

		u = v + "(" + id.(string) + ")"
		req, w = NewMockHttp("GET", u, "")
		s.HandleGet(c, w, req)
		ret = w.GetJSON()
		assert.NotNil(t, ret)
	}

	// ####### DELETE #######
	for _, v := range entityTypes {
		req, w = NewMockHttp("GET", v, "")
		s.HandleGet(c, w, req)
		ret = w.GetJSON()
		l = len(ret["value"].([]interface{}))

		i := 0
		for i < l {
			id := GetMapProperty(i, "@iot.id", ret)
			req, w = NewMockHttp("DELETE", v+"("+id+")", "")
			s.HandleDelete(c, w, req)
			i++
		}
	}

	// ####### CHECK ZERO-ED COLLECTIONS #######
	for _, v := range entityTypes {
		req, w = NewMockHttp("GET", v, "")
		s.HandleGet(c, w, req)
		ret = w.GetJSON()
		l = len(ret["value"].([]interface{}))
		assert.Equal(t, 0, l)
	}
}