Permalink
Browse files

NCBC-179: Add expiry and CAS to StoreJson extension methods

Change-Id: I81265938cf3487be0321fc0d6ca3e27f48aaa4aa
Reviewed-on: http://review.couchbase.org/23759
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Reviewed-by: Saakshi Manocha <saakshi.manocha@globallogic.com>
Tested-by: Saakshi Manocha <saakshi.manocha@globallogic.com>
  • Loading branch information...
johnzablocki authored and saakshimanocha committed Jan 7, 2013
1 parent b105fa6 commit 1f27152199a5a642bfec3535341cae9d5ecf90bf
@@ -6,13 +6,15 @@
using Newtonsoft.Json;
using Couchbase.Extensions;
using Enyim.Caching.Memcached;
+using System.Threading;
namespace Couchbase.Tests
{
[TestFixture]
public class CouchbaseJsonExtensionTests : CouchbaseClientTestsBase
{
- /// <summary>
+ #region Store tests
+ /// <summary>
/// @test: create a city object, store it in form of json against
/// a unique key, then get the value, it should be saved as type string
/// @pre: Default configuration to initialize client in app.config
@@ -29,12 +31,112 @@ public void When_Saving_City_With_JsonStore_City_Is_Stored_As_String()
Assert.That(savedCity, Is.InstanceOf<string>());
}
- /// <summary>
+ /// <summary>
+ /// @test: create a city object, store it in form of json against
+ /// a unique key, then get the value, it should be saved as type string
+ /// After expiry, item should not be valid
+ /// @pre: Default configuration to initialize client in app.config
+ /// @post: Test passes if result is of type string
+ /// </summary>
+ [Test]
+ public void When_Saving_City_With_JsonStore_And_DateTime_Expiry_Item_Expires()
+ {
+ var city = new City { Name = "Hartford", State = "CT", Type = "city" };
+ var result = _Client.ExecuteStoreJson(StoreMode.Set, "city_Hartford_CT_Exp", city, DateTime.Now.AddSeconds(2));
+ Assert.That(result.Success, Is.True, result.Message);
+ Thread.Sleep(3000);
+ var savedCity = _Client.Get("city_Hartford_CT_Exp");
+ Assert.That(savedCity, Is.Null);
+ }
+
+ /// <summary>
+ /// @test: create a city object, store it in form of json against
+ /// a unique key, then get the value, it should be saved as type string
+ /// When passing valid Cas, operation is successful
+ /// After expiry, item should not be valid
+ /// @pre: Default configuration to initialize client in app.config
+ /// @post: Test passes if result is of type string
+ /// </summary>
+ [Test]
+ public void When_Saving_City_With_JsonStore_City_And_TimeSpan_Expiry_Item_Expires()
+ {
+ var city = new City { Name = "Hartford", State = "CT", Type = "city" };
+ var result = _Client.ExecuteStoreJson(StoreMode.Set, "city_Hartford_CT_Exp", city, TimeSpan.FromSeconds(2));
+ Assert.That(result.Success, Is.True);
+ Thread.Sleep(3000);
+ var savedCity = _Client.Get("city_Hartford_CT_Exp");
+ Assert.That(savedCity, Is.Null);
+ }
+
+ /// <summary>
+ /// @test: create a city object, store it in form of json against
+ /// a unique key, then get the value, it should be saved as type string
+ /// After expiry, item should not be valid
+ /// @pre: Default configuration to initialize client in app.config
+ /// @post: Test passes if result is of type string
+ /// </summary>
+ [Test]
+ public void When_Saving_City_With_JsonStore_And_Valid_Cas_Store_Is_Successful()
+ {
+ var city = new City { Name = "Hartford", State = "CT", Type = "city" };
+ var result = _Client.ExecuteStoreJson(StoreMode.Set, "city_Hartford_CT", city, TimeSpan.FromSeconds(2));
+ Assert.That(result.Success, Is.True);
+
+ city.Name = "New Haven";
+ var casResult = _Client.ExecuteCasJson(StoreMode.Set, "city_Hartford_CT", city, result.Cas);
+ Assert.That(casResult.Success, Is.True);
+ }
+
+ /// <summary>
+ /// @test: create a city object, store it in form of json against
+ /// a unique key, then get the value, it should be saved as type string
+ /// After expiry, item should not be valid
+ /// @pre: Default configuration to initialize client in app.config
+ /// @post: Test passes if result is of type string
+ /// </summary>
+ [Test]
+ public void When_Saving_City_With_JsonStore_And_Invalid_Cas_Store_Is_Successful()
+ {
+ var city = new City { Name = "Hartford", State = "CT", Type = "city" };
+ var result = _Client.ExecuteStoreJson(StoreMode.Set, "city_Hartford_CT", city, TimeSpan.FromSeconds(2));
+ Assert.That(result.Success, Is.True);
+
+ city.Name = "New Haven";
+ var casResult = _Client.ExecuteCasJson(StoreMode.Set, "city_Hartford_CT", city, result.Cas-1);
+ Assert.That(casResult.Success, Is.False);
+
+ }
+
+ #endregion
+
+ #region Get extension tests
+ /// <summary>
/// @test: create a city object, store it in form of json against
- /// a unique key, then get the Json object as return value
+ /// a unique key, then get the Json object as return IGetOperationResult
/// @pre: Default configuration to initialize client in app.config
/// @post: Test passes if result is of type json object
/// </summary>
+ [Test]
+ public void When_Execute_Getting_City_With_Json_Get_Result_Value_Is_Returned_As_City()
+ {
+ var city = new City { Name = "Boston", State = "MA", Type = "city" };
+ var result = _Client.ExecuteStoreJson(StoreMode.Set, "city_Boston_MA", city);
+ Assert.That(result.Success, Is.True);
+
+ var savedCity = _Client.ExecuteGetJson<City>("city_Boston_MA").Value;
+
+ Assert.That(savedCity, Is.InstanceOf<City>());
+ Assert.That(savedCity.Name, Is.StringMatching("Boston"));
+ Assert.That(savedCity.Type, Is.StringMatching("city"));
+ Assert.That(savedCity.State, Is.StringMatching("MA"));
+ }
+
+ /// <summary>
+ /// @test: create a city object, store it in form of json against
+ /// a unique key, then get the Json object as return value
+ /// @pre: Default configuration to initialize client in app.config
+ /// @post: Test passes if result is of type json object
+ /// </summary>
[Test]
public void When_Getting_City_With_JsonGet_City_Is_Returned_As_City()
{
@@ -48,6 +150,8 @@ public void When_Getting_City_With_JsonGet_City_Is_Returned_As_City()
Assert.That(savedCity.Type, Is.StringMatching("city"));
Assert.That(savedCity.State, Is.StringMatching("MA"));
}
+ #endregion
+
private class City
{
@@ -59,6 +163,9 @@ private class City
[JsonProperty("type")]
public string Type { get; set; }
+
+ [JsonProperty("lastUpdated")]
+ public DateTime LastUpdated { get; set; }
}
}
}
@@ -6,27 +6,123 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Couchbase.Extensions
{
public static class CouchbaseClientExtensions
{
- public static bool StoreJson(this ICouchbaseClient client, StoreMode storeMode, string key, object value)
+ #region No expiry
+ public static IStoreOperationResult ExecuteStoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value)
{
- var json = JsonConvert.SerializeObject(value,
- Formatting.None,
- new JsonSerializerSettings
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- return client.Store(storeMode, key, json);
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json);
+ }
+
+ public static bool StoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json).Success;
+ }
+
+ public static IStoreOperationResult ExecuteCasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, ulong cas)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, cas);
+ }
+
+ public static bool CasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, ulong cas)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, cas).Success;
+ }
+ #endregion
+
+ #region DateTime expiry
+ public static IStoreOperationResult ExecuteStoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value, DateTime expiresAt)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json, expiresAt);
+ }
+
+ public static bool StoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value, DateTime expiresAt)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json, expiresAt).Success;
+ }
+
+ public static IStoreOperationResult ExecuteCasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, DateTime expiresAt, ulong cas)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, expiresAt, cas);
+ }
+
+ public static bool CasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, ulong cas, DateTime expiresAt)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, expiresAt, cas).Success;
}
+ #endregion
+
+ #region TimeSpan expiry
+ public static IStoreOperationResult ExecuteStoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value, TimeSpan validFor)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json, validFor);
+ }
+
+ public static bool StoreJson(this ICouchbaseClient client, StoreMode mode, string key, object value, TimeSpan validFor)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteStore(mode, key, json, validFor).Success;
+ }
+
+ public static IStoreOperationResult ExecuteCasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, TimeSpan validFor, ulong cas)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, validFor, cas);
+ }
+
+ public static bool CasJson(this ICouchbaseClient client, StoreMode mode, string key, object value, ulong cas, TimeSpan validFor)
+ {
+ var json = serializeObject(value);
+ return client.ExecuteCas(mode, key, json, validFor, cas).Success;
+ }
+ #endregion
public static T GetJson<T>(this ICouchbaseClient client, string key) where T : class
{
var json = client.Get<string>(key);
return json == null ? null : JsonConvert.DeserializeObject<T>(json);
}
+
+ public static IGetOperationResult<T> ExecuteGetJson<T>(this ICouchbaseClient client, string key) where T : class
+ {
+ var result = client.ExecuteGet<string>(key);
+ var retVal = new GetOperationResult<T>();
+ result.Combine(retVal);
+
+ if (! result.Success)
+ {
+ return retVal;
+ }
+
+ var obj = JsonConvert.DeserializeObject<T>(result.Value);
+ retVal.Value = obj;
+ return retVal;
+ }
+
+ private static string serializeObject(object value)
+ {
+ var json = JsonConvert.SerializeObject(value,
+ Formatting.None,
+ new JsonSerializerSettings
+ {
+ ContractResolver = new CamelCasePropertyNamesContractResolver()
+ });
+ return json;
+ }
}
}

0 comments on commit 1f27152

Please sign in to comment.