PageRenderTime 22ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/redistributable/openstack.net/src/testing/unit/Providers/Rackspace/ProviderBaseTests.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 310 lines | 308 code | 2 blank | 0 comment | 0 complexity | 5849d49ce32633d356f181acba07ca68 MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using JSIStudios.SimpleRESTServices.Client;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Moq.Protected;
  9. using net.openstack.Core;
  10. using net.openstack.Core.Domain;
  11. using net.openstack.Core.Providers;
  12. using net.openstack.Providers.Rackspace;
  13. using net.openstack.Providers.Rackspace.Objects;
  14. using Newtonsoft.Json;
  15. namespace OpenStackNet.Testing.Unit.Providers.Rackspace
  16. {
  17. [TestClass]
  18. public class ProviderBaseTests
  19. {
  20. private const string _testService = "test";
  21. [TestMethod]
  22. [TestCategory(TestCategories.Unit)]
  23. public void Should_Return_Correct_Endpoint_When_Identity_Is_Explicitly_Set_And_Region_Is_Explicitly_Declared()
  24. {
  25. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  26. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  27. var identityProviderMock = new Mock<IIdentityProvider>();
  28. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  29. var provider = new MockProvider(null, identityProviderMock.Object, null);
  30. var endpoint = provider.GetEndpoint(_testService, "DFW", new CloudIdentity());
  31. Assert.IsNotNull(endpoint);
  32. Assert.AreEqual("DFW", endpoint.Region);
  33. }
  34. [TestMethod]
  35. [TestCategory(TestCategories.Unit)]
  36. public void Should_Return_Correct_Endpoint_When_Identity_Is_Explicitly_Set_And_Region_Is_Different_Than_Default_Region()
  37. {
  38. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  39. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  40. var identityProviderMock = new Mock<IIdentityProvider>();
  41. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  42. var provider = new MockProvider(null, identityProviderMock.Object, null);
  43. var endpoint = provider.GetEndpoint(_testService, "ORD", new CloudIdentity());
  44. Assert.IsNotNull(endpoint);
  45. Assert.AreEqual("ORD", endpoint.Region);
  46. }
  47. [TestMethod]
  48. [TestCategory(TestCategories.Unit)]
  49. public void Should_Return_Correct_Endpoint_When_Identity_Set_On_Provider_And_Region_Is_Different_Than_Default_Region()
  50. {
  51. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  52. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  53. var identityProviderMock = new Mock<IIdentityProvider>();
  54. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  55. var provider = new MockProvider(new CloudIdentity(), identityProviderMock.Object, null);
  56. var endpoint = provider.GetEndpoint(_testService, "ORD", null);
  57. Assert.IsNotNull(endpoint);
  58. Assert.AreEqual("ORD", endpoint.Region);
  59. }
  60. [TestMethod]
  61. [TestCategory(TestCategories.Unit)]
  62. public void Should_Return_Correct_Endpoint_When_Identity_Is_Explicitly_Set_And_Region_Is_NOT_Explicitly_Declared()
  63. {
  64. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  65. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  66. var identityProviderMock = new Mock<IIdentityProvider>();
  67. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  68. var provider = new MockProvider(null, identityProviderMock.Object, null);
  69. var endpoint = provider.GetEndpoint(_testService, null, new CloudIdentity());
  70. Assert.IsNotNull(endpoint);
  71. Assert.AreEqual("DFW", endpoint.Region);
  72. }
  73. [TestMethod]
  74. [TestCategory(TestCategories.Unit)]
  75. public void Should_Return_Correct_Endpoint_When_Identity_Is_Set_On_Provider_And_Region_Is_Explicitly_Declared()
  76. {
  77. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  78. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  79. var identityProviderMock = new Mock<IIdentityProvider>();
  80. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  81. var provider = new MockProvider(new CloudIdentity(), identityProviderMock.Object, null);
  82. var endpoint = provider.GetEndpoint(_testService, "DFW", null);
  83. Assert.IsNotNull(endpoint);
  84. Assert.AreEqual("DFW", endpoint.Region);
  85. }
  86. [TestMethod]
  87. [TestCategory(TestCategories.Unit)]
  88. public void Should_Return_Correct_Endpoint_When_Identity_Is_Set_On_Provider_And_Region_Is_NOT_Explicitly_Declared()
  89. {
  90. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  91. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""DFW""}, {region:""ORD""}]}], user:{""RAX-AUTH:defaultRegion"":""DFW""} }");
  92. var identityProviderMock = new Mock<IIdentityProvider>();
  93. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  94. var provider = new MockProvider(new CloudIdentity(), identityProviderMock.Object, null);
  95. var endpoint = provider.GetEndpoint(_testService, null, null);
  96. Assert.IsNotNull(endpoint);
  97. Assert.AreEqual("DFW", endpoint.Region);
  98. }
  99. [TestMethod]
  100. [TestCategory(TestCategories.Unit)]
  101. public void Should_Return_Correct_LON_Endpoint_When_Identity_Is_Explicitly_Set_And_Region_Is_Explicitly_Declared()
  102. {
  103. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  104. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""LON""}, {region:""LON2""}]}], user:{""RAX-AUTH:defaultRegion"":""LON""} }");
  105. var identityProviderMock = new Mock<IIdentityProvider>();
  106. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  107. var provider = new MockProvider(null, identityProviderMock.Object, null);
  108. var endpoint = provider.GetEndpoint(_testService, "LON", new RackspaceCloudIdentity());
  109. Assert.IsNotNull(endpoint);
  110. Assert.AreEqual("LON", endpoint.Region);
  111. }
  112. [TestMethod]
  113. [TestCategory(TestCategories.Unit)]
  114. public void Should_Return_Correct_LON_Endpoint_When_Identity_Is_Explicitly_Set_And_Region_Is_NOT_Explicitly_Declared()
  115. {
  116. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  117. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""LON""}, {region:""LON2""}]}], user:{""RAX-AUTH:defaultRegion"":""LON""} }");
  118. var identityProviderMock = new Mock<IIdentityProvider>();
  119. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  120. var provider = new MockProvider(null, identityProviderMock.Object, null);
  121. var endpoint = provider.GetEndpoint(_testService, null, new RackspaceCloudIdentity());
  122. Assert.IsNotNull(endpoint);
  123. Assert.AreEqual("LON", endpoint.Region);
  124. }
  125. [TestMethod]
  126. [TestCategory(TestCategories.Unit)]
  127. public void Should_Return_Correct_LON_Endpoint_When_Identity_Is_Set_On_Provider_And_Region_Is_Explicitly_Declared()
  128. {
  129. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  130. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""LON""}, {region:""LON2""}]}], user:{""RAX-AUTH:defaultRegion"":""LON""} }");
  131. var identityProviderMock = new Mock<IIdentityProvider>();
  132. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  133. var provider = new MockProvider(new RackspaceCloudIdentity(), identityProviderMock.Object, null);
  134. var endpoint = provider.GetEndpoint(_testService, "LON", null);
  135. Assert.IsNotNull(endpoint);
  136. Assert.AreEqual("LON", endpoint.Region);
  137. }
  138. [TestMethod]
  139. [TestCategory(TestCategories.Unit)]
  140. public void Should_Return_Correct_LON_Endpoint_When_Identity_Is_Set_On_Provider_And_Region_Is_NOT_Explicitly_Declared()
  141. {
  142. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  143. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""LON""}, {region:""LON2""}]}], user:{""RAX-AUTH:defaultRegion"":""LON""} }");
  144. var identityProviderMock = new Mock<IIdentityProvider>();
  145. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  146. var provider = new MockProvider(new RackspaceCloudIdentity(), identityProviderMock.Object, null);
  147. var endpoint = provider.GetEndpoint(_testService, null, null);
  148. Assert.IsNotNull(endpoint);
  149. Assert.AreEqual("LON", endpoint.Region);
  150. }
  151. [TestMethod]
  152. [TestCategory(TestCategories.Unit)]
  153. public void Should_Return_Correct_LON_Endpoint_When_Identity_Is_Explicitly_And_Region_Is_Always_Empty()
  154. {
  155. UserAccess userAccess = JsonConvert.DeserializeObject<UserAccess>(
  156. @"{ serviceCatalog:[{ type : """ + _testService + @""", endpoints : [{region:""LON""}, {region:""LON2""}]}], user:{""RAX-AUTH:defaultRegion"":""LON""} }");
  157. var identityProviderMock = new Mock<IIdentityProvider>();
  158. identityProviderMock.Setup(m => m.GetUserAccess(It.IsAny<CloudIdentity>(), It.IsAny<bool>())).Returns(userAccess);
  159. var provider = new MockProvider(null, identityProviderMock.Object, null);
  160. var endpoint = provider.GetEndpoint(_testService, null, new RackspaceCloudIdentity());
  161. Assert.IsNotNull(endpoint);
  162. Assert.AreEqual("LON", endpoint.Region);
  163. }
  164. [TestMethod]
  165. [TestCategory(TestCategories.Unit)]
  166. public void Should_Return_Null_While_Building_Optional_Parameter_List_When_A_Null_Value_Is_Passed()
  167. {
  168. var providerBase = new MockProvider(null, null, null);
  169. var paramList = providerBase.BuildOptionalParameterList(null);
  170. Assert.IsNull(paramList);
  171. }
  172. [TestMethod]
  173. [TestCategory(TestCategories.Unit)]
  174. public void Should_Return_Null_While_Building_Optional_Parameter_List_When_An_Empty_Value_Is_Passed()
  175. {
  176. var providerBase = new MockProvider(null, null, null);
  177. var paramList = providerBase.BuildOptionalParameterList(new Dictionary<string, string>());
  178. Assert.IsNull(paramList);
  179. }
  180. [TestMethod]
  181. [TestCategory(TestCategories.Unit)]
  182. public void Should_Return_Null_While_Building_Optional_Parameter_List_When_All_Values_In_List_Are_InValid()
  183. {
  184. var providerBase = new MockProvider(null, null, null);
  185. var paramList = providerBase.BuildOptionalParameterList(new Dictionary<string, string>
  186. {
  187. {"key1", ""},
  188. {"key2", null},
  189. {"key3", ""},
  190. {"key4", null},
  191. });
  192. Assert.IsNull(paramList);
  193. }
  194. [TestMethod]
  195. [TestCategory(TestCategories.Unit)]
  196. public void Should_Return_All_Parameters_While_Building_Optional_Parameter_List_When_All_Values_In_List_Are_Valid()
  197. {
  198. var providerBase = new MockProvider(null, null, null);
  199. var paramList = providerBase.BuildOptionalParameterList(new Dictionary<string, string>
  200. {
  201. {"key1", "val1"},
  202. {"key2", "val2"},
  203. {"key3", "val3"},
  204. {"key4", "val4"},
  205. });
  206. Assert.AreEqual(4, paramList.Count);
  207. Assert.IsTrue(paramList.Any(p => p.Key == "key1" && p.Value == "val1"));
  208. Assert.IsTrue(paramList.Any(p => p.Key == "key2" && p.Value == "val2"));
  209. Assert.IsTrue(paramList.Any(p => p.Key == "key3" && p.Value == "val3"));
  210. Assert.IsTrue(paramList.Any(p => p.Key == "key4" && p.Value == "val4"));
  211. }
  212. [TestMethod]
  213. [TestCategory(TestCategories.Unit)]
  214. public void Should_Return_Only_Valid_Parameters_While_Building_Optional_Parameter_List_When_Some_Values_In_List_Are_Valid()
  215. {
  216. var providerBase = new MockProvider(null, null, null);
  217. var paramList = providerBase.BuildOptionalParameterList(new Dictionary<string, string>
  218. {
  219. {"key1", "val1"},
  220. {"key2", ""},
  221. {"key3", "val3"},
  222. {"key4", null},
  223. });
  224. Assert.AreEqual(2, paramList.Count);
  225. Assert.IsTrue(paramList.Any(p => p.Key == "key1" && p.Value == "val1"));
  226. Assert.IsFalse(paramList.Any(p => p.Key == "key2" && p.Value == "val2"));
  227. Assert.IsTrue(paramList.Any(p => p.Key == "key3" && p.Value == "val3"));
  228. Assert.IsFalse(paramList.Any(p => p.Key == "key4" && p.Value == "val4"));
  229. }
  230. public class MockProvider : ProviderBase<IIdentityProvider>
  231. {
  232. internal MockProvider(CloudIdentity defaultIdentity, IIdentityProvider identityProvider, IRestService restService) : base(defaultIdentity, null, identityProvider, restService)
  233. {
  234. }
  235. public Endpoint GetEndpoint(string serviceType, string region, CloudIdentity identity)
  236. {
  237. return base.GetServiceEndpoint(identity, serviceType, null, region);
  238. }
  239. public new Dictionary<string, string> BuildOptionalParameterList(Dictionary<string, string> optionalParameters)
  240. {
  241. return base.BuildOptionalParameterList(optionalParameters);
  242. }
  243. }
  244. }
  245. }