diff --git a/core/base/apiModule.go b/core/base/apiModule.go index 4429027..61c6d10 100644 --- a/core/base/apiModule.go +++ b/core/base/apiModule.go @@ -345,14 +345,14 @@ func ListObjectsWithDestinationPolicyUpdatedSince(orgID string, since int64) ([] return objects, err } -// GetObjectsByFilter provides a list of objects that satisfy the given conditions -func ListObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { +// ListObjectsWithFilters provides a list of objects that satisfy the given conditions +func ListObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { apiLock.RLock() defer apiLock.RUnlock() common.HealthStatus.ClientRequestReceived() - objects, err := store.RetrieveObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, destinationType, destinationID, noData, expirationTimeBefore) + objects, err := store.RetrieveObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, objectType, objectID, destinationType, destinationID, noData, expirationTimeBefore) if trace.IsLogging(logger.DEBUG) { trace.Debug("In ListObjectsWithFilters. Get %s. Returned %d objects\n", orgID, len(objects)) diff --git a/core/base/apiServer.go b/core/base/apiServer.go index be2347a..3a91bbd 100644 --- a/core/base/apiServer.go +++ b/core/base/apiServer.go @@ -645,6 +645,16 @@ func handleObjectRequest(orgID string, objectType string, objectID string, write // description: Objects that have a Destination Policy which was updated since the specified timestamp in RFC3339 should be fetched. // required: false // type: string +// - name: objectType +// in: query +// description: Fetch the objects with given object type +// required: false +// type: string +// - name: objectID +// in: query +// description: Fetch the objects with given object id +// required: false +// type: string // - name: destinationType // in: query // description: Fetch the objects with given destination type @@ -741,6 +751,13 @@ func handleListObjectsWithFilters(orgID string, writer http.ResponseWriter, requ } } + objectType := request.URL.Query().Get("objectType") + objectID := "" + + if objectType != "" { + objectID = request.URL.Query().Get("objectID") + } + destinationType := request.URL.Query().Get("destinationType") destinationID := "" if destinationType != "" { @@ -772,10 +789,10 @@ func handleListObjectsWithFilters(orgID string, writer http.ResponseWriter, requ var err error if trace.IsLogging(logger.DEBUG) { - trace.Debug("In handleListObjectsWithFilters, get objects with %s %s %s %s %s %d %s %s %s %s\n", orgID, destinationPolicyString, dpServiceOrgID, dpServiceName, dpPropertyName, since, destinationType, destinationID, noDataString, expirationTimeBeforeString) + trace.Debug("In handleListObjectsWithFilters, get objects with %s %s %s %s %s %d %s %s %s %s %s %s\n", orgID, destinationPolicyString, dpServiceOrgID, dpServiceName, dpPropertyName, since, objectType, objectID, destinationType, destinationID, noDataString, expirationTimeBeforeString) } - if objects, err = ListObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, destinationType, destinationID, noData, expirationTimeBeforeString); err != nil { + if objects, err = ListObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, objectType, objectID, destinationType, destinationID, noData, expirationTimeBeforeString); err != nil { communications.SendErrorResponse(writer, err, "Failed to fetch the list of objects with given conditions. Error: ", 0) } else { if len(objects) == 0 { diff --git a/core/base/apiServer_test.go b/core/base/apiServer_test.go index e8e6e28..1c006fa 100644 --- a/core/base/apiServer_test.go +++ b/core/base/apiServer_test.go @@ -751,6 +751,17 @@ func testGetObjectsWithFiltersHelper(storageProvider string, t *testing.T) { t.Errorf("StoreObject failed: %s", err.Error()) } + destinations := []common.Destination{ + common.Destination{DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5a", Communication: common.MQTTProtocol}, + common.Destination{DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5a", Communication: common.MQTTProtocol}, + } + + for _, destination := range destinations { + if err := store.StoreDestination(destination); err != nil { + t.Errorf("Failed to store detination %#v. Error: %s\n", destination, err) + } + } + tests := []struct { method string orgID string @@ -758,6 +769,8 @@ func testGetObjectsWithFiltersHelper(storageProvider string, t *testing.T) { destService string destPropname string since string + objType string + objID string destType string destID string noData string @@ -767,40 +780,38 @@ func testGetObjectsWithFiltersHelper(storageProvider string, t *testing.T) { testID int }{ // Must be first test - {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "", http.StatusOK, 7, 0}, - {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "", "", http.StatusOK, 5, 1}, - {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "", http.StatusOK, 2, 2}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "", "", "", http.StatusOK, 12, 0}, + {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "", "", "", "", http.StatusOK, 6, 1}, + {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "", "", "", http.StatusOK, 6, 2}, // Must be second test - {http.MethodGet, "myorgObjFilter", "true", "plover/testerService1", "", "", "", "", "", "", http.StatusOK, 2, 3}, - {http.MethodGet, "myorgObjFilter", "true", "plover/testerService1", "b", "", "", "", "", "", http.StatusOK, 1, 4}, - {http.MethodGet, "myorgObjFilter", "", "plover/testerService1", "b", "", "", "", "", "", http.StatusOK, 7, 5}, - {http.MethodGet, "myorgObjFilter", "true", "", "b", "", "", "", "", "", http.StatusOK, 4, 6}, - {http.MethodGet, "myorgObjFilter", "true", "", "", "2000-08-14T14:00:00Z", "", "", "", "", http.StatusOK, 5, 7}, - {http.MethodGet, "myorgObjFilter", "true", "", "", "2030-08-14T14:00:00Z", "", "", "", "", http.StatusNotFound, 0, 8}, - {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "false", "", http.StatusOK, 1, 9}, - {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "true", "", http.StatusOK, 4, 10}, - {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "false", "", http.StatusOK, 1, 11}, - {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "true", "", http.StatusOK, 1, 12}, - {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "false", "", http.StatusOK, 2, 13}, - {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "true", "", http.StatusOK, 5, 14}, - {http.MethodGet, "myorgObjFilter", "", "", "", "", "myDestType5", "", "", "", http.StatusOK, 2, 15}, - {http.MethodGet, "myorgObjFilter", "", "", "", "", "myDestType5", "myDestID5a", "", "", http.StatusOK, 1, 16}, - {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "2012-08-15T14:00:00Z", http.StatusOK, 2, 17}, - {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "2012-08-15T14:00:00Z", http.StatusNotFound, 0, 18}, - {http.MethodGet, "myorgObjFilter1", "true", "", "", "", "", "", "", "", http.StatusOK, 1, 19}, - {http.MethodGet, "myorgObjFilter", "aaa", "", "", "", "", "", "", "", http.StatusBadRequest, 0, 20}, - {http.MethodPut, "myorgObjFilter", "true", "", "", "", "", "", "", "", http.StatusMethodNotAllowed, 0, 21}, - {http.MethodGet, "myorgObjFilter2", "true", "", "", "", "", "", "", "", http.StatusNotFound, 0, 22}, - } - - //sinceFormat := time.Unix(since, 0).Format(time.RFC3339) - //tests[5].since = sinceFormat - //tests[1].expectedCount = totalCount - 1 + {http.MethodGet, "myorgObjFilter", "true", "plover/testerService1", "", "", "", "", "", "", "", "", http.StatusOK, 2, 3}, + {http.MethodGet, "myorgObjFilter", "true", "plover/testerService1", "b", "", "", "", "", "", "", "", http.StatusOK, 1, 4}, + {http.MethodGet, "myorgObjFilter", "", "plover/testerService1", "b", "", "", "", "", "", "", "", http.StatusOK, 12, 5}, + {http.MethodGet, "myorgObjFilter", "true", "", "b", "", "", "", "", "", "", "", http.StatusOK, 5, 6}, + {http.MethodGet, "myorgObjFilter", "true", "", "", "2000-08-14T14:00:00Z", "", "", "", "", "", "", http.StatusOK, 6, 7}, + {http.MethodGet, "myorgObjFilter", "true", "", "", "2030-08-14T14:00:00Z", "", "", "", "", "", "", http.StatusNotFound, 0, 8}, + {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "", "", "false", "", http.StatusOK, 2, 9}, + {http.MethodGet, "myorgObjFilter", "true", "", "", "", "", "", "", "", "true", "", http.StatusOK, 4, 10}, + {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "", "false", "", http.StatusOK, 4, 11}, + {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "", "true", "", http.StatusOK, 2, 12}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "", "false", "", http.StatusOK, 6, 13}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "", "true", "", http.StatusOK, 6, 14}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "myDestType5", "", "", "", http.StatusOK, 6, 15}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "myDestType5", "myDestID5a", "", "", http.StatusOK, 4, 16}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "type2", "", "myDestType5", "", "", "", http.StatusOK, 3, 17}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "type2", "", "myDestType5", "myDestID5a", "", "", http.StatusOK, 2, 18}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "type2", "7c", "myDestType5", "myDestID5a", "", "", http.StatusOK, 1, 19}, + {http.MethodGet, "myorgObjFilter", "", "", "", "", "", "", "", "", "", "2012-08-15T14:00:00Z", http.StatusOK, 2, 20}, + {http.MethodGet, "myorgObjFilter", "false", "", "", "", "", "", "", "", "", "2012-08-15T14:00:00Z", http.StatusNotFound, 0, 21}, + {http.MethodGet, "myorgObjFilter1", "true", "", "", "", "", "", "", "", "", "", http.StatusOK, 1, 22}, + {http.MethodGet, "myorgObjFilter", "aaa", "", "", "", "", "", "", "", "", "", http.StatusBadRequest, 0, 23}, + {http.MethodPut, "myorgObjFilter", "true", "", "", "", "", "", "", "", "", "", http.StatusMethodNotAllowed, 0, 24}, + {http.MethodGet, "myorgObjFilter2", "true", "", "", "", "", "", "", "", "", "", http.StatusNotFound, 0, 25}, + } for _, test := range tests { - //urlString := fmt.Sprintf("%s?destination_policy=true&since=%d", test.orgID, test.since) - urlString := fmt.Sprintf("%s?filters=true&destinationPolicy=%s&dpPropertyName=%s&dpService=%s&since=%s&destinationType=%s&destinationID=%s&noData=%s&expirationTimeBefore=%s", - test.orgID, test.destinationPolicy, test.destPropname, test.destService, test.since, test.destType, test.destID, test.noData, test.expirationBefore) + urlString := fmt.Sprintf("%s?filters=true&destinationPolicy=%s&dpPropertyName=%s&dpService=%s&since=%s&objectType=%s&objectID=%s&destinationType=%s&destinationID=%s&noData=%s&expirationTimeBefore=%s", + test.orgID, test.destinationPolicy, test.destPropname, test.destService, test.since, test.objType, test.objID, test.destType, test.destID, test.noData, test.expirationBefore) writer := newAPIServerTestResponseWriter() request, _ := http.NewRequest(test.method, urlString, nil) request.SetBasicAuth("testerAdmin@"+test.orgID, "") @@ -826,6 +837,7 @@ func testGetObjectsWithFiltersHelper(storageProvider string, t *testing.T) { } func loadTestMetaData(nodeType string, orgID string) (int64, int, error) { + destArray := []string{"myDestType5:myDestID5a", "myDestType5:myDestID5c"} testData := []common.MetaData{ common.MetaData{ObjectID: "1", ObjectType: "type1", DestOrgID: "myorgObjFilter1", NoData: true, DestinationPolicy: &common.Policy{ @@ -913,6 +925,24 @@ func loadTestMetaData(nodeType string, orgID string) (int64, int, error) { }, common.MetaData{ObjectID: "5a", ObjectType: "type1", DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5a", NoData: true, Expiration: "2015-08-14T14:00:00Z"}, common.MetaData{ObjectID: "5b", ObjectType: "type1", DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5b", NoData: false, Expiration: "2015-08-14T14:00:00Z"}, + common.MetaData{ObjectID: "5c", ObjectType: "type1", DestOrgID: "myorgObjFilter", DestinationsList: destArray, NoData: false, Expiration: "2015-08-14T14:00:00Z"}, + common.MetaData{ObjectID: "6", ObjectType: "type2", DestOrgID: "myorgObjFilter", NoData: false, + DestinationPolicy: &common.Policy{ + Properties: []common.PolicyProperty{ + {Name: "a", Value: float64(1)}, + {Name: "b", Value: "zxcv"}, + {Name: "c", Value: true, Type: "bool"}, + }, + Constraints: []string{"Plover=34", "asdf=true"}, + Services: []common.ServiceID{ + {OrgID: "plover", Arch: "amd64", ServiceName: "testerService4", Version: "0.0.1"}, + }, + }, + Expiration: "2014-08-14T14:00:00Z", + }, + common.MetaData{ObjectID: "7a", ObjectType: "type2", DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5a", NoData: true, Expiration: "2015-08-14T14:00:00Z"}, + common.MetaData{ObjectID: "7b", ObjectType: "type2", DestOrgID: "myorgObjFilter", DestType: "myDestType5", DestID: "myDestID5b", NoData: false, Expiration: "2015-08-14T14:00:00Z"}, + common.MetaData{ObjectID: "7c", ObjectType: "type2", DestOrgID: "myorgObjFilter", DestinationsList: destArray, NoData: false, Expiration: "2015-08-14T14:00:00Z"}, } var since int64 diff --git a/core/storage/boltStorage.go b/core/storage/boltStorage.go index 31e6d3b..84218b3 100644 --- a/core/storage/boltStorage.go +++ b/core/storage/boltStorage.go @@ -481,7 +481,7 @@ func (store *BoltStorage) RetrieveObjectsWithDestinationPolicyUpdatedSince(orgID } // RetrieveObjectsWithFilters returns the list of all othe objects that meet the given conditions -func (store *BoltStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { +func (store *BoltStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { result := make([]common.MetaData, 0) function := func(object boltObject) { if orgID == object.Meta.DestOrgID { @@ -529,17 +529,52 @@ func (store *BoltStorage) RetrieveObjectsWithFilters(orgID string, destinationPo } } + // check objectType and objectID + if objectType != "" { + if objectType != object.Meta.ObjectType { + return + } + if objectID != "" { + if objectID != object.Meta.ObjectID { + return + } + } + } + // check destinationType and destinationID if destinationType != "" { - if destinationType != object.Meta.DestType { - return - } else { + if object.Meta.DestType != "" { + if destinationType != object.Meta.DestType { + return + } if destinationID != "" { if destinationID != object.Meta.DestID { return } } + + } else { // check object.Meta.DestinationsList (destinationType: destinationID) + checkedDestList := false + for _, dest := range object.Meta.DestinationsList { + if destinationID != "" { + if dest == destinationType+":"+destinationID { + checkedDestList = true + break + } + } else { + parts := strings.SplitN(dest, ":", 2) + if len(parts) == 2 && parts[0] == destinationType { + checkedDestList = true + break + } + + } + } + if !checkedDestList { + return + } } + } if noData != nil { diff --git a/core/storage/cache.go b/core/storage/cache.go index ba88d9b..ca24147 100644 --- a/core/storage/cache.go +++ b/core/storage/cache.go @@ -134,8 +134,8 @@ func (store *Cache) RetrieveObjectsWithDestinationPolicyUpdatedSince(orgID strin } // RetrieveObjectsWithFilters returns the list of all othe objects that meet the given conditions -func (store *Cache) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { - return store.Store.RetrieveObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, destinationType, destinationID, noData, expirationTimeBefore) +func (store *Cache) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { + return store.Store.RetrieveObjectsWithFilters(orgID, destinationPolicy, dpServiceOrgID, dpServiceName, dpPropertyName, since, objectType, objectID, destinationType, destinationID, noData, expirationTimeBefore) } // RetrieveAllObjects returns the list of all the objects of the specified type diff --git a/core/storage/inMemoryStorage.go b/core/storage/inMemoryStorage.go index ad203b3..01ad034 100644 --- a/core/storage/inMemoryStorage.go +++ b/core/storage/inMemoryStorage.go @@ -339,7 +339,7 @@ func (store *InMemoryStorage) RetrieveObjectsWithDestinationPolicyUpdatedSince(o } // RetrieveObjectsWithFilters returns the list of all othe objects that meet the given conditions -func (store *InMemoryStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { +func (store *InMemoryStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { return nil, nil } diff --git a/core/storage/mongoStorage.go b/core/storage/mongoStorage.go index ba2e4e3..7454553 100644 --- a/core/storage/mongoStorage.go +++ b/core/storage/mongoStorage.go @@ -688,7 +688,7 @@ func (store *MongoStorage) RetrieveObjectsWithDestinationPolicyUpdatedSince(orgI } // RetrieveObjectsWithFilters returns the list of all the objects that meet the given conditions -func (store *MongoStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { +func (store *MongoStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) { result := []object{} query := bson.M{ @@ -718,11 +718,28 @@ func (store *MongoStorage) RetrieveObjectsWithFilters(orgID string, destinationP } + if objectType != "" { + query["metadata.object-type"] = objectType + if objectID != "" { + query["metadata.object-id"] = objectID + } + } + if destinationType != "" { - query["metadata.destination-type"] = destinationType - if destinationID != "" { - query["metadata.destination-id"] = destinationID + var subquery []bson.M + if destinationID == "" { + subquery = []bson.M{ + bson.M{"metadata.destination-type": destinationType}, + bson.M{"metadata.destinations-list": bson.M{"$regex": destinationType + ":*"}}, + } + } else { + subquery = []bson.M{ + bson.M{"metadata.destination-type": destinationType, "metadata.destination-id": destinationID}, + bson.M{"metadata.destinations-list": destinationType + ":" + destinationID}, + } } + query["$or"] = subquery + } if noData != nil { diff --git a/core/storage/storage.go b/core/storage/storage.go index 18db333..b100655 100644 --- a/core/storage/storage.go +++ b/core/storage/storage.go @@ -80,7 +80,7 @@ type Storage interface { RetrieveObjectsWithDestinationPolicyUpdatedSince(orgID string, since int64) ([]common.ObjectDestinationPolicy, common.SyncServiceError) // RetrieveObjectsWithFilters returns the list of all othe objects that meet the given conditions - RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) + RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string) ([]common.MetaData, common.SyncServiceError) // RetrieveAllObjects returns the list of all the objects of the specified type RetrieveAllObjects(orgID string, objectType string) ([]common.ObjectDestinationPolicy, common.SyncServiceError) diff --git a/core/storage/storage_test.go b/core/storage/storage_test.go index d3f33de..93330d5 100644 --- a/core/storage/storage_test.go +++ b/core/storage/storage_test.go @@ -433,6 +433,13 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } defer store.Stop() + dest1 := common.Destination{DestOrgID: "myorg111", DestType: "myDestType5", DestID: "myDestID5c", Communication: common.HTTPProtocol} + destArray := []string{"myDestType5:myDestID5c"} + + if err := store.StoreDestination(dest1); err != nil { + t.Errorf("StoreDestination failed. Error: %s\n", err.Error()) + } + tests := []struct { metaData common.MetaData }{ @@ -491,6 +498,24 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { }}, {common.MetaData{ObjectID: "5a", ObjectType: "type1", DestOrgID: "myorg111", DestType: "myDestType5", DestID: "myDestID5a", NoData: true, Expiration: ""}}, {common.MetaData{ObjectID: "5b", ObjectType: "type1", DestOrgID: "myorg111", DestType: "myDestType5", DestID: "myDestID5b", NoData: false, Expiration: ""}}, + {common.MetaData{ObjectID: "5c", ObjectType: "type1", DestOrgID: "myorg111", DestinationsList: destArray, NoData: false, Expiration: ""}}, + {common.MetaData{ObjectID: "1", ObjectType: "type2", DestOrgID: "myorg111", NoData: true, + DestinationPolicy: &common.Policy{ + Properties: []common.PolicyProperty{ + {Name: "j", Value: float64(42.0)}, + {Name: "k", Value: "ghjk"}, + {Name: "l", Value: float64(613)}, + }, + Constraints: []string{"il=71", "rtyu=\"edcrfv\""}, + Services: []common.ServiceID{ + {OrgID: "plover", Arch: "amd64", ServiceName: "wompus", Version: "1.0.0"}, + }, + }, + Expiration: "2014-08-14T14:00:00Z", + }}, + {common.MetaData{ObjectID: "5a", ObjectType: "type2", DestOrgID: "myorg111", DestType: "myDestType5", DestID: "myDestID5a", NoData: true, Expiration: ""}}, + {common.MetaData{ObjectID: "5b", ObjectType: "type2", DestOrgID: "myorg111", DestType: "myDestType5", DestID: "myDestID5b", NoData: false, Expiration: ""}}, + {common.MetaData{ObjectID: "5c", ObjectType: "type2", DestOrgID: "myorg111", DestinationsList: destArray, NoData: false, Expiration: ""}}, } for _, test := range tests { @@ -500,7 +525,7 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } // insert - if _, err := store.StoreObject(test.metaData, nil, common.NotReadyToSend); err != nil { + if _, err := store.StoreObject(test.metaData, nil, common.ReadyToSend); err != nil { t.Errorf("Failed to store object (objectID = %s). Error: %s\n", test.metaData.ObjectID, err.Error()) } @@ -510,7 +535,7 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } } - objects, err := store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", nil, "") + objects, err := store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with filters. Error: %s\n", err) } @@ -519,8 +544,8 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } destinationPolicy := true - expectedResultCount := 4 - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", nil, "") + expectedResultCount := 5 + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -529,7 +554,7 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } expectedResultCount = 2 - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "plover", "xyzzy", "", 0, "", "", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "plover", "xyzzy", "", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -538,7 +563,7 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } expectedResultCount = 1 - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "plover", "xyzzy", "d", 0, "", "", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "plover", "xyzzy", "d", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -547,7 +572,16 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } expectedResultCount = 1 - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "d", 0, "", "", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "d", 0, "", "", "", "", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != expectedResultCount { + t.Errorf("Retrieved %d objects with given propertyName in destination policy. Expected %d\n", len(objects), expectedResultCount) + } + + expectedResultCount = 1 + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "type2", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -556,8 +590,8 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } destinationPolicy = false - expectedResultCount = 2 - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", nil, "") + expectedResultCount = 6 + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -565,7 +599,7 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { t.Errorf("Retrieved %d objects without destination policy. Expected %d\n", len(objects), expectedResultCount) } - objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "d", 0, "", "", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "d", 0, "", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -573,8 +607,35 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { t.Errorf("Retrieved %d objects should not check destination policy subfield if desitinationPolicy is nil. Expected %d\n", len(objects), len(tests)) } + expectedResultCount = 6 + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "myDestType5", "", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != expectedResultCount { + t.Errorf("Retrieved %d objects with given destination type. Expected %d\n", len(objects), expectedResultCount) + } + expectedResultCount = 2 - objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "myDestType5", "", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "myDestType5", "myDestID5a", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != expectedResultCount { + t.Errorf("Retrieved %d objects with given destinationType and destinationID. Expected %d\n", len(objects), expectedResultCount) + } + + expectedResultCount = 2 + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "myDestType5", "myDestID5c", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != expectedResultCount { + t.Errorf("Retrieved %d objects with given destinationType and destinationID. Expected %d\n", len(objects), expectedResultCount) + } + + expectedResultCount = 4 + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "type2", "", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -583,17 +644,34 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } expectedResultCount = 1 - objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "myDestType5", "myDestID5a", nil, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "type2", "5a", "", "", nil, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } if len(objects) != expectedResultCount { - t.Errorf("Retrieved %d objects with given destinationType and destinationID. Expected %d\n", len(objects), expectedResultCount) + t.Errorf("Retrieved %d objects with given destination type. Expected %d\n", len(objects), expectedResultCount) } - expectedResultCount = 5 + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "5a", "", "", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != len(tests) { + t.Errorf("Retrieved %d objects with given destination type. Expected %d\n", len(objects), expectedResultCount) + } + + expectedResultCount = 3 + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "type2", "", "myDestType5", "", nil, "") + if err != nil { + t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) + } + if len(objects) != expectedResultCount { + t.Errorf("Retrieved %d objects with given destination type. Expected %d\n", len(objects), expectedResultCount) + } + + expectedResultCount = 7 noData := true - objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", &noData, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "", "", &noData, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -601,9 +679,9 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { t.Errorf("Retrieved %d objects with noData set to true. Expected %d\n", len(objects), expectedResultCount) } - expectedResultCount = 1 + expectedResultCount = 2 destinationPolicy = false - objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", &noData, "") + objects, err = store.RetrieveObjectsWithFilters("myorg111", &destinationPolicy, "", "", "", 0, "", "", "", "", &noData, "") if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } @@ -612,15 +690,15 @@ func testGetObjectWithFilters(storageType string, t *testing.T) { } expectedResultCount = 2 - //noData = true expirationTimeBefore := "2013-08-15T14:00:00Z" - objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", nil, expirationTimeBefore) + objects, err = store.RetrieveObjectsWithFilters("myorg111", nil, "", "", "", 0, "", "", "", "", nil, expirationTimeBefore) if err != nil { t.Errorf("Failed to retrieve the objects with a destination policy. Error: %s\n", err) } if len(objects) != expectedResultCount { t.Errorf("Retrieved %d objects with given destination type. Expected %d\n", len(objects), expectedResultCount) } + } func testStorageObjectActivation(storageType string, t *testing.T) { diff --git a/swagger.json b/swagger.json index f120fe2..a5c8171 100644 --- a/swagger.json +++ b/swagger.json @@ -1098,6 +1098,18 @@ "name": "since", "in": "query" }, + { + "type": "string", + "description": "Fetch the objects with given object type", + "name": "objectType", + "in": "query" + }, + { + "type": "string", + "description": "Fetch the objects with given object id", + "name": "objectID", + "in": "query" + }, { "type": "string", "description": "Fetch the objects with given destination type",