public async Task PATCHClient(string objId, Client toUpdate) { if (toUpdate !=null) { //getting the client object try { var collection = this.Database.GetCollection<Client>(Properties.Settings.Default.clientsCollection); var filter = Builders<Client>.Filter.Eq(x => x.id , new BsonObjectId(objId)); var replaceTask = collection.FindOneAndReplaceAsync(filter, toUpdate); await replaceTask; var result = replaceTask.Result; if (result != null) { return; } else { throw new IOException("the replacement operation failed"); } } catch (TimeoutException exception) { throw new IOException(Properties.Settings.Default.timeoutException); } catch (MongoConnectionException exception) { //failed on database connection throw new IOException(Properties.Settings.Default.mongoConnException, exception); } } else { throw new ArgumentException("the client object to uopdate cannot be null ");}; }
public async Task POSTClient(Client toAdd) { if (toAdd != null && !string.IsNullOrEmpty(toAdd.alias)) { var collection = this.Database.GetCollection<Client>(Properties.Settings.Default.clientsCollection); var task = collection.InsertOneAsync(toAdd); await task; return; } else { throw new ArgumentException(String.Format("client to be added cannot be null or atleast has to have a valid alias")); } }
public void TEST_Client() { //setting up multiple clients Client[] clients = new Client[] { new Client(){pan="dfgfdgdg", nationality="indian", gender="male", adhaar="dfgdfgdfgfdg", age=31, alias="tintumon", contact="3453-665-657657", addresses = new Address[]{ new Address(){city="pune", house="house#420", nation="india", state="maharashtra", street="khau galli"}, new Address(){city="pune", house="house#422", nation="india", state="maharashtra", street="khau galli"} }}, new Client(){pan="dfgfdgdg", nationality="indian", gender="male", adhaar="dfgdfgdfgfdg", age=31, alias="bhosadkulla", contact="3453-665-657657", addresses = new Address[]{ new Address(){city="pune", house="house#420", nation="india", state="maharashtra", street="khau galli"}, new Address(){city="pune", house="house#422", nation="india", state="maharashtra", street="khau galli"} }} }; //test for insert Trace.WriteLine("we are now ready to test the insert client API"); foreach (var item in clients) { try { var task =this.Store.POSTClient(item); task.Wait(); } catch (ArgumentException exception) { Assert.IsNotNull(exception, String.Format("argument exception - {0}", item.alias)); } catch (IOException exception) { Assert.IsNotNull(exception, String.Format("i/o exception - {0} for employee{1}",exception.Message, item.alias)); } } //retrieve tes for the client //also need to test if the id of the client is saved from the insert we did in the last operation Array.ForEach<Client>(clients, cl => { Trace.WriteLine("Trying to get the id to know if the insert operation was able to update the poco object with Id"); Trace.WriteLine(String.Format("BsonId of the object is {0}", cl.BsonObjectIdAsString)); var getTask = this.Store.GETClientOfName(cl.alias); getTask.Wait(); var inDatabase = getTask.Result; Assert.IsNotNull(inDatabase, String.Format("failed to get {0} from the database", cl.alias)); Trace.WriteLine("trying to see if the id exists for the object in the database"); Trace.WriteLine(String.Format("BsonId of the object in the database is {0}", inDatabase.BsonObjectIdAsString)); }); ////patch test here .. //Trace.WriteLine("we are now trying to get the patch test complete"); //Array.ForEach<Client>(clients, cl => { // cl.alias = cl.alias + "_changed"; // var patchTask = Store.PATCHClient(cl.BsonObjectIdAsString, cl); // patchTask.Wait(); // var getTask = Store.GETClientOfId(cl.BsonObjectIdAsString); // getTask.Wait(); // Assert.IsTrue(cl.alias == getTask.Result.alias, "the alias has not been updated"); //}); //Trace.WriteLine("we are now attempting to test the delete client api "); //Array.ForEach<Client>(clients, cl => { // var delTask = Store.DELClient(cl.BsonObjectIdAsString); // delTask.Wait(); // //then try and test if the object with the same id is still in the database // var getTask = Store.GETClientOfId(cl.BsonObjectIdAsString); // getTask.Wait(); // Assert.IsNull(getTask.Result, String.Format("{0} client has not been deleted from the database", cl.alias)); //}); }