Example #1
0
func testUpdateCloud(t *testing.T, s mapper.Store, ea common.EndnodeAddress, ca common.CloudAddress) common.MapItem {
	m, err := s.UpdateCloud(ea, ca)
	if err != nil {
		t.Fatal("UpdateCloud failed:", err)
	}
	if m.EndnodeAddress == nil || m.EndnodeAddress.IDString() != ea.IDString() {
		t.Fatalf("UpdateCloud modified EndnodeAddress: %#v", m.EndnodeAddress)
	}
	if m.CloudAddress == nil || m.CloudAddress.IDString() != ca.IDString() {
		t.Fatalf("UpdateCloud returned unexpected CloudAddress: %#v",
			m.CloudAddress)
	}
	return m
}
Example #2
0
func testUpsertEndnodeProperties(t *testing.T, s mapper.Store, ea common.EndnodeAddress, d []byte) common.MapItem {
	m, err := s.UpsertEndnodeProperties(ea, d)
	if err != nil {
		t.Fatal("UpsertEndnodeProperties failed:", err)
	}
	if m.EndnodeAddress == nil || m.EndnodeAddress.IDString() != ea.IDString() {
		t.Fatalf("UpsertEndnode returns unexpected MapItem: %#v",
			m.EndnodeAddress)
	}
	if !bytes.Equal(m.ThingProperties, d) {
		t.Fatalf("UpsertEndnode store wrong data: %#v (expecetd:%#v)",
			m.States, d)
	}
	return m
}
Example #3
0
func testMapperStoreGateway(t *testing.T, s mapper.Store) {
	// Error callback returns should be propagated as return value of InsertGateway.
	_, err := s.InsertGateway("testappid", func(appID string) (common.CloudAddress, error) {
		return common.CloudAddress{}, common.ErrNotFound
	})
	if err != common.ErrNotFound {
		t.Errorf("unexpected error on InsertGateway failure: %v", err)
	}

	expectedCAddr := common.CloudAddress{
		AppID:          "testApp",
		ServerLocation: "jp",
		ThingID:        "dummyID",
	}
	_, err = s.InsertGateway("testappid", func(appID string) (common.CloudAddress, error) {
		return expectedCAddr, nil
	})

	if err != nil {
		t.Errorf("should not fail: %v", err)
	}

	gc, err := s.FindGatewayByAppID(expectedCAddr.AppID)

	if err != nil {
		t.Errorf("should not fail to find Gateway: %v", err)
	}
	if gc.AppID != expectedCAddr.AppID ||
		gc.ServerLocation != expectedCAddr.ServerLocation ||
		gc.ThingID != expectedCAddr.ThingID {
		t.Error("cloud address of inserted gateway is not equal to expected one: ", gc)
	}

	err = s.DeleteGateway(expectedCAddr.AppID)

	if err != nil {
		t.Error("should not fail to delete gateway: ", err)
	}

	_, err = s.FindGatewayByAppID(expectedCAddr.AppID)

	if err != common.ErrUnknownGateway {
		t.Errorf("should fail with ErrUnknownGateway: %v", err)
	}

	// delete a not existing gateway should return error
	err = s.DeleteGateway(expectedCAddr.AppID)

	if err != common.ErrUnknownGateway {
		t.Errorf("should fail with ErrUnknownGateway: %v", err)
	}

}
Example #4
0
func testMapperStore(t *testing.T, s mapper.Store) {
	// TODO: test FindGatewayByAppID
	// insert endnodes.
	m := testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		VendorThingID:  "ABCDEF-001",
	}, []byte("state1-1-1"))
	if m.CloudAddress != nil {
		t.Fatalf("UpsertEndnode (insret) failed with CloudAddress: %#v",
			m.CloudAddress)
	}
	testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		VendorThingID:  "ABCDEF-002",
	}, []byte("state1-2-1"))
	testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		VendorThingID:  "ABCDEF-003",
	}, []byte("state1-3-1"))
	testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-001",
	}, []byte("state2-1-1"))
	testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-002",
	}, []byte("state2-2-1"))
	testUpsertEndnode(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-003",
	}, []byte("state2-3-1"))

	// test update endnode properties
	testUpsertEndnodeProperties(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-003",
	}, []byte(`{"_thingType":"LED"}`))

	// update cloud address
	testUpdateCloud(t, s, common.EndnodeAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		VendorThingID:  "ABCDEF-003",
	}, common.CloudAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		ThingID:        "foo-333",
	})
	testUpdateCloud(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-001",
	}, common.CloudAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		ThingID:        "bar-111",
	})
	testUpdateCloud(t, s, common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-002",
	}, common.CloudAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		ThingID:        "bar-222",
	})

	// find endnodes which without CloudAddress
	items, err := s.FindNilCloud("us", "01234567")
	if err != nil {
		t.Fatal("FindNilCloud(us) failed:", err)
	}
	if len(items) != 2 {
		t.Fatalf("FindNilCloud(us) returns wrong num of items: %d", len(items))
	}
	a, b := items[0], items[1]
	if !((a.VendorThingID == "ABCDEF-001" && b.VendorThingID == "ABCDEF-002") || (a.VendorThingID == "ABCDEF-002" && b.VendorThingID == "ABCDEF-001")) {
		t.Fatalf("FindNilCloud(us) returns wrong items: %s, %s",
			a.VendorThingID, b.VendorThingID)
	}
	items, err = s.FindNilCloud("jp", "76543210")
	if err != nil {
		t.Fatal("FindNilCloud(jp) failed:", err)
	}
	if len(items) != 1 {
		t.Fatalf("FindNilCloud(jp) returns wrong num of items: %d", len(items))
	}
	if items[0].VendorThingID != "FEDCBA-003" {
		t.Fatalf("FindNilCloud(jp) returns wrong item: %s",
			items[0].VendorThingID)
	}

	// find endnodes whichs have CloudAddress
	items, err = s.FindNonNilCloud("jp", "76543210")
	if err != nil {
		t.Fatal("FindNonNilCloud failed:", err)
	}
	if len(items) != 2 {
		t.Fatalf("FindNonNilCloud returns wrong num of items: %d", len(items))
	}
	a, b = items[0], items[1]
	if !((a.VendorThingID == "FEDCBA-001" && b.VendorThingID == "FEDCBA-002") || (a.VendorThingID == "FEDCBA-002" && b.VendorThingID == "FEDCBA-001")) {
		t.Fatalf("FindNoNullCloud(us) returns wrong items: %s, %s",
			a.VendorThingID, b.VendorThingID)
	}
	if !((a.ThingID == "bar-111" && b.ThingID == "bar-222") || (a.ThingID == "bar-222" && b.ThingID == "bar-111")) {
		t.Fatalf("FindNoNullCloud(us) returns wrong items: %s, %s",
			a.ThingID, b.ThingID)
	}

	// test FindByCloudAddress
	item, err := s.FindByCloudAddress(common.CloudAddress{
		ServerLocation: "us",
		AppID:          "01234567",
		ThingID:        "foo-333",
	})
	if err != nil {
		t.Fatal("FindByCloudAddress(us-01234567-foo-333) failed:", err)
	}
	if item.VendorThingID != "ABCDEF-003" {
		t.Fatalf("FindByCloudAddress(us-01234567-foo-333) returns wrong item: %s", item.VendorThingID)
	}
	item, err = s.FindByCloudAddress(common.CloudAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		ThingID:        "bar-333",
	})
	if err != common.ErrUnknownEndnode {
		t.Fatalf("FindByCloudAddress(jp-76543210-bar-333) should return ErrUnknownEndnode: %s", err)
	}

	// test FindByEndnodeAddress
	item, err = s.FindByEndnodeAddress(common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-001",
	})
	if err != nil {
		t.Fatalf("FindByEndnodeAddress(%s) failed %s",
			"jp-76543210-FEDCBA-001", err)
	}
	item, err = s.FindByEndnodeAddress(common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "FEDCBA-004",
	})
	if err != common.ErrUnknownEndnode {
		t.Fatalf("FindByEndnodeAddress(%s) should return ErrUnknownEndnode: %s",
			"jp-76543210-FEDCBA-004", err)
	}

	// test UpdateEndnode
	item, err = s.UpdateEndnode(common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "newVendorThingID",
	}, common.CloudAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		ThingID:        "bar-222",
	})
	if err != nil {
		t.Fatalf("fail to update endnode address: %s", err)
	}

	item, err = s.FindByEndnodeAddress(common.EndnodeAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		VendorThingID:  "newVendorThingID",
	})
	if err != nil {
		t.Fatalf("FindByEndnodeAddress(jp-76543210-newVendorThingID) fail: %s", err)
	}
	item, err = s.FindByCloudAddress(common.CloudAddress{
		ServerLocation: "jp",
		AppID:          "76543210",
		ThingID:        "bar-222",
	})
	if err != nil {
		t.Fatalf("FindByCloudAddress(jp-76543210-bar-222) fail: %s", err)
	}

	if item.EndnodeAddress.IDString() != "jp-76543210-newVendorThingID" {
		t.Fatalf("endnode address should be updated to jp-76543210-newVendorThingID: %s", item.EndnodeAddress.IDString())
	}
}