Skip to content

Commit

Permalink
feat(Additional properties): Added DynamicModel base class for models…
Browse files Browse the repository at this point in the history
… with AdditionalProperties
  • Loading branch information
mediumTaj committed Apr 24, 2019
1 parent 99b4c47 commit d2d9386
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/IBM.Cloud.SDK.Core/Model/DynamicModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System.Collections.Generic;

namespace IBM.Cloud.SDK.Core.Model
{
/// <summary>
/// This class is the base class for generated models with additional properties.
/// </summary>
/// <typeparam name="T"></typeparam>
public class DynamicModel<T>
{
/// <summary>
/// A Dictionary to keep additional properties.
/// </summary>
public Dictionary<string, T> AdditionalProperties { get; } = new Dictionary<string, T>();

/// <summary>
/// Add a property to the AdditionalProperties dictionary.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add(string key, T value)
{
AdditionalProperties.Add(key, value);
}

/// <summary>
/// Remove a property from the AdditionalProperties dictionary.
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
AdditionalProperties.Remove(key);
}

/// <summary>
/// Get a single property from the AdditionalProperties dictionary.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public T Get(string key)
{
AdditionalProperties.TryGetValue(key, out T value);
return value;
}
}
}
81 changes: 81 additions & 0 deletions test/IBM.Cloud.SDK.Core.Tests/DynamicModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using IBM.Cloud.SDK.Core.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace IBM.Cloud.SDK.Core.Tests
{
[TestClass]
public class DynamicModelTests
{
[TestMethod]
public void TestAdd()
{
DynamicModel<object> dynamicModel = new DynamicModel<object>();
object myObject = new object();
dynamicModel.Add("myObject", myObject);

Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject"));
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject));
}

[TestMethod]
public void TestRemove()
{
DynamicModel<object> dynamicModel = new DynamicModel<object>();
object myObject = new object();
dynamicModel.Add("myObject", myObject);

Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject"));
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject));

dynamicModel.Remove("myObject");
Assert.IsFalse(dynamicModel.AdditionalProperties.ContainsKey("myObject"));
Assert.IsFalse(dynamicModel.AdditionalProperties.ContainsValue(myObject));
}

[TestMethod]
public void TestGet()
{
DynamicModel<object> dynamicModel = new DynamicModel<object>();
object myObject = new object();
dynamicModel.Add("myObject", myObject);

Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject"));
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject));

object myObject2 = dynamicModel.Get("myObject");
Assert.IsTrue(myObject2 == myObject);
}

[TestMethod]
public void TestGetAdditionalProperties()
{
DynamicModel<object> dynamicModel = new DynamicModel<object>();
object myObject = new object();
dynamicModel.Add("myObject", myObject);

Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject"));
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject));

Dictionary<string, object> additionalProperties = dynamicModel.AdditionalProperties;
Assert.IsTrue(additionalProperties == dynamicModel.AdditionalProperties);
}
}
}

0 comments on commit d2d9386

Please sign in to comment.