Skip to content

Commit e7acd29

Browse files
committed
Add Unit test for the Storage ServiceProperties parse functions.
1 parent 6f0d828 commit e7acd29

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<Compile Include="Common\ResourceNotFoundExceptionTest.cs" />
212212
<Compile Include="Common\AccessPolicyHelperTest.cs" />
213213
<Compile Include="Common\SasTokenHelperTest.cs" />
214+
<Compile Include="Common\PSServicePropertiesTest.cs" />
214215
<Compile Include="Common\StorageCloudCmdletBaseTest.cs" />
215216
<Compile Include="Common\StorageExceptionUtilTest.cs" />
216217
<Compile Include="File\Cmdlet\GetAzureStorageFileContentTest.cs" />
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright 2012 Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Microsoft.WindowsAzure.Commands.Storage.Common;
19+
using Microsoft.WindowsAzure.Commands.Storage.Model.ResourceModel;
20+
using Microsoft.WindowsAzure.Storage.Blob;
21+
using Microsoft.WindowsAzure.Storage.Table;
22+
using Microsoft.WindowsAzure.Storage.File;
23+
using Microsoft.WindowsAzure.Storage.Queue;
24+
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
25+
26+
namespace Microsoft.WindowsAzure.Commands.Storage.Test.Common
27+
{
28+
/// <summary>
29+
/// Unit test for Access Policy helper
30+
/// </summary>
31+
[TestClass]
32+
public class PSServicePropertiesTest : StorageTestBase
33+
{
34+
/// <summary>
35+
/// unit test for ParseCorsRules() functional
36+
/// </summary>
37+
[TestMethod]
38+
public void ValidateParseCorsRulesTest()
39+
{
40+
CorsProperties coresproperties = PrepareCoresproperties();
41+
PSCorsRule[] pscores = PSCorsRule.ParseCorsRules(coresproperties);
42+
CompareCors(pscores, coresproperties);
43+
44+
coresproperties = new CorsProperties();
45+
pscores = PSCorsRule.ParseCorsRules(coresproperties);
46+
CompareCors(pscores, coresproperties);
47+
48+
coresproperties = null;
49+
pscores = PSCorsRule.ParseCorsRules(coresproperties);
50+
CompareCors(pscores, coresproperties);
51+
}
52+
53+
/// <summary>
54+
/// unit test for new PSSeriviceProperties() functional
55+
/// </summary>
56+
[TestMethod]
57+
public void ValidateParseServicePropertiesTest()
58+
{
59+
ServiceProperties serviceProperties = new ServiceProperties();
60+
serviceProperties.Cors = PrepareCoresproperties();
61+
serviceProperties.HourMetrics = new MetricsProperties("1.0");
62+
serviceProperties.HourMetrics.MetricsLevel = MetricsLevel.ServiceAndApi;
63+
serviceProperties.HourMetrics.RetentionDays = 1;
64+
serviceProperties.MinuteMetrics = new MetricsProperties("1.0");
65+
serviceProperties.MinuteMetrics.MetricsLevel = MetricsLevel.Service;
66+
serviceProperties.MinuteMetrics.RetentionDays = 3;
67+
serviceProperties.Logging = new LoggingProperties("1.0");
68+
serviceProperties.Logging.LoggingOperations = LoggingOperations.All;
69+
serviceProperties.Logging.RetentionDays = 5;
70+
serviceProperties.DefaultServiceVersion = "2017-04-17";
71+
PSSeriviceProperties pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
72+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
73+
74+
serviceProperties = new ServiceProperties();
75+
pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
76+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
77+
78+
serviceProperties = null;
79+
pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
80+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
81+
}
82+
83+
/// <summary>
84+
/// Comapare PSServiceProperties and ServiceProperties, to make sure they are same content
85+
/// </summary>
86+
static private void CompareServiceProperties(PSSeriviceProperties pSSeriviceProperties, ServiceProperties serviceProperties)
87+
{
88+
if ((pSSeriviceProperties != null && pSSeriviceProperties.HourMetrics != null) || (serviceProperties != null && serviceProperties.HourMetrics != null))
89+
{
90+
Assert.AreEqual(serviceProperties.HourMetrics.Version, pSSeriviceProperties.HourMetrics.Version);
91+
Assert.AreEqual(serviceProperties.HourMetrics.MetricsLevel, pSSeriviceProperties.HourMetrics.MetricsLevel);
92+
Assert.AreEqual(serviceProperties.HourMetrics.RetentionDays, pSSeriviceProperties.HourMetrics.RetentionDays);
93+
}
94+
if ((pSSeriviceProperties != null && pSSeriviceProperties.MinuteMetrics != null) || (serviceProperties != null && serviceProperties.MinuteMetrics != null))
95+
{
96+
Assert.AreEqual(serviceProperties.MinuteMetrics.Version, pSSeriviceProperties.MinuteMetrics.Version);
97+
Assert.AreEqual(serviceProperties.MinuteMetrics.MetricsLevel, pSSeriviceProperties.MinuteMetrics.MetricsLevel);
98+
Assert.AreEqual(serviceProperties.MinuteMetrics.RetentionDays, pSSeriviceProperties.MinuteMetrics.RetentionDays);
99+
}
100+
if ((pSSeriviceProperties != null && pSSeriviceProperties.Logging != null) || (serviceProperties != null && serviceProperties.Logging != null))
101+
{
102+
Assert.AreEqual(serviceProperties.Logging.Version, pSSeriviceProperties.Logging.Version);
103+
Assert.AreEqual(serviceProperties.Logging.LoggingOperations, pSSeriviceProperties.Logging.LoggingOperations);
104+
Assert.AreEqual(serviceProperties.Logging.RetentionDays, pSSeriviceProperties.Logging.RetentionDays);
105+
}
106+
if ((pSSeriviceProperties != null && pSSeriviceProperties.Cors != null) || (serviceProperties != null && serviceProperties.Cors != null))
107+
{
108+
CompareCors(pSSeriviceProperties.Cors, serviceProperties.Cors);
109+
}
110+
if ((pSSeriviceProperties != null && pSSeriviceProperties.DefaultServiceVersion != null) || (serviceProperties != null && serviceProperties.DefaultServiceVersion != null))
111+
{
112+
Assert.AreEqual(serviceProperties.DefaultServiceVersion, pSSeriviceProperties.DefaultServiceVersion);
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Comapare PSCorsRule and CorsProperties, to make sure they are same content
118+
/// </summary>
119+
static private void CompareCors(PSCorsRule[] psCorsRules, CorsProperties corsProperties)
120+
{
121+
if ((psCorsRules == null || psCorsRules.Length == 0)
122+
&& (corsProperties == null || corsProperties.CorsRules == null || corsProperties.CorsRules.Count == 0))
123+
{
124+
return;
125+
}
126+
Assert.AreEqual(psCorsRules.Length, corsProperties.CorsRules.Count);
127+
int i = 0;
128+
foreach (CorsRule CorsRule in corsProperties.CorsRules)
129+
{
130+
PSCorsRule psCorsRule = psCorsRules[i];
131+
i++;
132+
CompareStrings(psCorsRule.AllowedHeaders, CorsRule.AllowedHeaders);
133+
CompareStrings(psCorsRule.ExposedHeaders, CorsRule.ExposedHeaders);
134+
CompareStrings(psCorsRule.AllowedOrigins, CorsRule.AllowedOrigins);
135+
Assert.AreEqual(psCorsRule.MaxAgeInSeconds, CorsRule.MaxAgeInSeconds);
136+
137+
CorsHttpMethods psAllowedMethods = CorsHttpMethods.None;
138+
foreach (string method in psCorsRule.AllowedMethods)
139+
{
140+
CorsHttpMethods allowedCorsMethod = CorsHttpMethods.None;
141+
if (Enum.TryParse<CorsHttpMethods>(method, true, out allowedCorsMethod))
142+
{
143+
psAllowedMethods |= allowedCorsMethod;
144+
}
145+
else
146+
{
147+
throw new InvalidOperationException(string.Format("Can't parse {0} to CorsHttpMethods.", method));
148+
}
149+
}
150+
151+
Assert.AreEqual(psAllowedMethods, CorsRule.AllowedMethods);
152+
}
153+
}
154+
155+
/// <summary>
156+
/// Comapare String Array and String List, to make sure they are same content
157+
/// </summary>
158+
static private void CompareStrings(string[] stringArray, IList<String> stringList)
159+
{
160+
if ((stringArray == null || stringArray.Length == 0) && (stringList == null || stringList.Count == 0))
161+
{
162+
return;
163+
}
164+
string[] stringArray2 = new string[stringList.Count];
165+
stringList.CopyTo(stringArray2, 0);
166+
Assert.AreEqual(stringArray.Length, stringArray2.Length);
167+
168+
for(int i=0; i< stringArray.Length; i++)
169+
{
170+
Assert.AreEqual(stringArray[i], stringArray2[i]);
171+
}
172+
}
173+
174+
/// <summary>
175+
/// Create a set of CorsRule that containers different parameters combination
176+
/// </summary>
177+
static private CorsProperties PrepareCoresproperties()
178+
{
179+
CorsProperties coresproperties = new CorsProperties();
180+
coresproperties.CorsRules.Add(
181+
new CorsRule()
182+
{
183+
AllowedHeaders = new List<string>
184+
{
185+
"x-ms-meta-data*",
186+
"x -ms-meta-target*",
187+
"x -ms-meta-abc"
188+
},
189+
AllowedMethods = CorsHttpMethods.Connect | CorsHttpMethods.Delete | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Merge,
190+
AllowedOrigins = new List<string>
191+
{
192+
"http://www.contoso.com",
193+
"http://www.fabrikam.com"
194+
},
195+
ExposedHeaders = new List<string>
196+
{
197+
"x-ms-meta-*"
198+
},
199+
MaxAgeInSeconds = 100
200+
});
201+
coresproperties.CorsRules.Add(
202+
new CorsRule()
203+
{
204+
AllowedHeaders = new List<string>
205+
{
206+
"x -ms-meta-12345675754564*"
207+
},
208+
AllowedMethods = CorsHttpMethods.None,
209+
AllowedOrigins = new List<string>
210+
{
211+
"http://www.abc23.com",
212+
"https://www.fabrikam.com/*"
213+
},
214+
ExposedHeaders = new List<string>
215+
{
216+
"x-ms-meta-data*",
217+
"x -ms-meta-target*",
218+
"x -ms-meta-abc"
219+
},
220+
MaxAgeInSeconds = 2000
221+
});
222+
coresproperties.CorsRules.Add(
223+
new CorsRule()
224+
{
225+
AllowedHeaders = new List<string>
226+
{
227+
"*"
228+
},
229+
AllowedMethods = CorsHttpMethods.Options | CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Trace,
230+
AllowedOrigins = new List<string>
231+
{
232+
"*"
233+
},
234+
ExposedHeaders = new List<string>
235+
{
236+
"*",
237+
},
238+
MaxAgeInSeconds = 0
239+
});
240+
return coresproperties;
241+
}
242+
}
243+
}

0 commit comments

Comments
 (0)