Skip to content

Commit 300356e

Browse files
committed
fix namespace issue#20
1 parent dcdfb73 commit 300356e

19 files changed

+491
-257
lines changed

Fake/CommonTestData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public static string BaseDirectory
2222
public static string NorthWindV4 => GetFullPath("northwindV4.xml");
2323
public static string NorthWindV3 => GetFullPath("northwindV3.xml");
2424
public static string TripPin4Flag => GetFullPath("trippinV4Flags.xml"); // @"..\..\..\..\fake\trippinV4Flags.xml";
25-
public static string UrlNorthWindV4 = "http://services.odata.org/V4/Northwind/Northwind.svc";
25+
public const string UrlNorthWindV4 = "http://services.odata.org/V4/Northwind/Northwind.svc";
2626
public const string UrlNorthWindV3 = "http://services.odata.org/V3/Northwind/Northwind.svc";
27+
public const string UrlNorthWindV2 = "http://services.odata.org/V2/Northwind/Northwind.svc";
2728
public const string UrlTripPinService = "http://services.odata.org/V4/TripPinServiceRW";
2829
//parameter file
2930
public static string Param1 => GetFullPath("param1.txt");

Fake/all_types_v3.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
2+
<edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
3+
<Schema Namespace="ODataDemo" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
4+
<EntityType Name="AllTypesEntity">
5+
<Key>
6+
<PropertyRef Name="ID" />
7+
</Key>
8+
<Property Name="ID" Type="Edm.Int32" Nullable="false" />
9+
<Property Name="Edm_Int32" Type="Edm.Int32" />
10+
<Property Name="Edm_String" Type="Edm.String" />
11+
<Property Name="Edm_Binary" Type="Edm.Binary" />
12+
<Property Name="Edm_Decimal" Type="Edm.Decimal" />
13+
<Property Name="Edm_Int16" Type="Edm.Int16" />
14+
<Property Name="Edm_Single" Type="Edm.Single" />
15+
<Property Name="Edm_Boolean" Type="Edm.Boolean" />
16+
<Property Name="Edm_Double" Type="Edm.Double" />
17+
<Property Name="Edm_Guid" Type="Edm.Guid" />
18+
<Property Name="Edm_Byte" Type="Edm.Byte" />
19+
<Property Name="Edm_Int64" Type="Edm.Int64" />
20+
<Property Name="Edm_SByte" Type="Edm.SByte" />
21+
<Property Name="Edm_Stream" Type="Edm.Stream" />
22+
<Property Name="Edm_Geography" Type="Edm.Geography" />
23+
<Property Name="Edm_GeographyPoint" Type="Edm.GeographyPoint" />
24+
<Property Name="Edm_GeographyLineString" Type="Edm.GeographyLineString" />
25+
<Property Name="Edm_GeographyPolygon" Type="Edm.GeographyPolygon" />
26+
<Property Name="Edm_GeographyCollection" Type="Edm.GeographyCollection" />
27+
<Property Name="Edm_GeographyMultiPolygon" Type="Edm.GeographyMultiPolygon" />
28+
<Property Name="Edm_GeographyMultiLineString" Type="Edm.GeographyMultiLineString" />
29+
<Property Name="Edm_GeographyMultiPoint" Type="Edm.GeographyMultiPoint" />
30+
<Property Name="Edm_Geometry" Type="Edm.Geometry" />
31+
<Property Name="Edm_GeometryPoint" Type="Edm.GeometryPoint" />
32+
<Property Name="Edm_GeometryLineString" Type="Edm.GeometryLineString" />
33+
<Property Name="Edm_GeometryPolygon" Type="Edm.GeometryPolygon" />
34+
<Property Name="Edm_GeometryCollection" Type="Edm.GeometryCollection" />
35+
<Property Name="Edm_GeometryMultiPolygon" Type="Edm.GeometryMultiPolygon" />
36+
<Property Name="Edm_GeometryMultiLineString" Type="Edm.GeometryMultiLineString" />
37+
<Property Name="Edm_GeometryMultiPoint" Type="Edm.GeometryMultiPoint" />
38+
<Property Name="Edm_DateTimeOffset" Type="Edm.DateTimeOffset" />
39+
<Property Name="Edm_DateTime" Type="Edm.DateTime" />
40+
<Property Name="Edm_Time" Type="Edm.Time" />
41+
</EntityType>
42+
<EntityContainer Name="DemoService" m:IsDefaultEntityContainer="true">
43+
<EntitySet Name="AllTypesEntity" EntityType="ODataDemo.AllTypesEntity" />
44+
</EntityContainer>
45+
</Schema>
46+
</edmx:DataServices>
47+
</edmx:Edmx>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
using OData2Poco.Extensions;
6+
using OData2Poco.Http;
7+
8+
namespace OData2Poco.Tests
9+
{
10+
class CustomeHttpClientTest
11+
{
12+
[Test]
13+
[TestCase(TestSample.UrlNorthWindV4)]
14+
[TestCase(TestSample.UrlNorthWindV3)]
15+
public async Task No_auth_ReadMetaDataTest(string url)
16+
{
17+
var connection = new OdataConnectionString
18+
{
19+
ServiceUrl = url,
20+
};
21+
var cc = new CustomeHttpClient(connection);
22+
var metadata = await cc.ReadMetaDataAsync();
23+
Assert.That(metadata.Length, Is.GreaterThan(0));
24+
}
25+
26+
[Test]
27+
public async Task Token_Auth_CheckHttpRequestMessage_HttpGet()
28+
{
29+
var connection = new OdataConnectionString
30+
{
31+
32+
ServiceUrl = "http://localhost/odata2/api/northwind",
33+
Password = "accessToken",
34+
Authenticate = AuthenticationType.Token
35+
36+
37+
};
38+
var client = new CustomeHttpClient(connection, new RouteHandler(r =>
39+
{
40+
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost/odata2/api/northwind/$metadata");
41+
42+
Assert.IsNotNull(r.Headers.UserAgent);
43+
Assert.AreEqual(r.Headers.UserAgent.ToString(), "OData2Poco");
44+
45+
Assert.IsNotNull(r.Headers.Authorization);
46+
Assert.AreEqual(r.Headers.Authorization.ToString(),
47+
"Bearer accessToken");
48+
}));
49+
var metadata = await client.ReadMetaDataAsync();
50+
51+
}
52+
53+
[Test]
54+
public async Task Basic_Auth_CheckHttpRequestMessage_HttpGet()
55+
{
56+
var connection = new OdataConnectionString
57+
{
58+
ServiceUrl = "http://localhost/odata2/api/northwind",
59+
UserName = "user1",
60+
Password = "secret",
61+
Authenticate = AuthenticationType.Basic
62+
63+
64+
};
65+
var client = new CustomeHttpClient(connection, new RouteHandler(r =>
66+
{
67+
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost/odata2/api/northwind/$metadata");
68+
69+
Assert.IsNotNull(r.Headers.UserAgent);
70+
Assert.AreEqual(r.Headers.UserAgent.ToString(), "OData2Poco");
71+
72+
Assert.IsNotNull(r.Headers.Authorization);
73+
Assert.AreEqual(r.Headers.Authorization.ToString(),
74+
"Basic dXNlcjE6c2VjcmV0");
75+
}));
76+
var metadata = await client.ReadMetaDataAsync();
77+
}
78+
}
79+
}

OData2Poco.Tests/O2PTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Threading.Tasks;
44
using NUnit.Framework;
55
using OData2Poco.Api;
6-
using OData2Poco.OAuth2;
76

87
namespace OData2Poco.Tests
98
{

OData2Poco.Tests/OData2Poco.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<ItemGroup>
2525
<ProjectReference Include="..\OData2PocoLib\OData2Poco.csproj" />
2626
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Reference Include="System.Net.Http" />
30+
</ItemGroup>
2731

2832
<ItemGroup>
2933
<None Update="Data\category.json">

OData2Poco.Tests/RouteHandler.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Newtonsoft.Json;
9+
10+
namespace OData2Poco.Tests
11+
{
12+
13+
public class RouteHandler : DelegatingHandler
14+
{
15+
16+
Action<HttpRequestMessage> _testingAction;
17+
18+
//public SampleRouteHandler(HttpMessageHandler innerHandler,
19+
// Action<HttpRequestMessage> testingAction) : base(innerHandler)
20+
//{
21+
// _testingAction = testingAction;
22+
23+
//}
24+
25+
public RouteHandler(Action<HttpRequestMessage> testingAction)
26+
{
27+
_testingAction = testingAction;
28+
}
29+
30+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
31+
{
32+
_testingAction(request);
33+
var resp = new HttpResponseMessage(HttpStatusCode.OK)
34+
{
35+
36+
Content = new JsonContent(new
37+
{
38+
Success = true,
39+
Message = "Success"
40+
})
41+
};
42+
43+
var tsc = new TaskCompletionSource<HttpResponseMessage>();
44+
tsc.SetResult(resp);
45+
return tsc.Task;
46+
47+
//var response = await base.SendAsync(request, ct);
48+
//Console.WriteLine(response.Dump());
49+
//if (!response.IsSuccessStatusCode)
50+
//{
51+
// Console.WriteLine("{0}\t{1}\t{2}", request.RequestUri,
52+
// (int)response.StatusCode, response.Headers.Date);
53+
//}
54+
//return response;
55+
}
56+
}
57+
58+
internal class JsonContent : HttpContent
59+
{
60+
private readonly MemoryStream _stream = new MemoryStream();
61+
public JsonContent(object value)
62+
{
63+
64+
Headers.ContentType = new MediaTypeHeaderValue("application/json");
65+
var jw = new JsonTextWriter(new StreamWriter(_stream)) { Formatting = Formatting.Indented };
66+
var serializer = new JsonSerializer();
67+
serializer.Serialize(jw, value);
68+
jw.Flush();
69+
_stream.Position = 0;
70+
71+
}
72+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
73+
{
74+
return _stream.CopyToAsync(stream);
75+
}
76+
77+
protected override bool TryComputeLength(out long length)
78+
{
79+
length = _stream.Length;
80+
return true;
81+
}
82+
}
83+
}

OData2PocoLib/ClassTemplate.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace OData2Poco
77
/// <summary>
88
/// Define the propertis of the class
99
/// </summary>
10-
public partial class ClassTemplate
10+
public class ClassTemplate
1111
{
1212
public string Name { get; set; }
1313
public string BaseType { get; set; }
@@ -22,16 +22,18 @@ public partial class ClassTemplate
2222
public bool IsFlags { get; set; } //v3, Add [FlagsAttribute] to enum
2323
public List<string> EnumElements { get; set; }
2424
public string OriginalName { get; set; }
25+
26+
//v1.4.0
27+
public string EntitySetName { get; set; }
28+
public string NameSpace { get; set; }
29+
public bool IsComplex { get; set; }
2530
public ClassTemplate()
2631
{
2732
Properties = new List<PropertyTemplate>();
2833
Keys = new List<string>();
2934
EnumElements = new List<string>();
3035
Navigation = new List<string>();
3136
}
32-
//v1.4.0
33-
public string EntitySetName { get; set; }
34-
public bool IsComplex { get; set; }
3537

3638
private readonly AttributeFactory _attributeFactory = AttributeFactory.Default;
3739

OData2PocoLib/ClassTemplate2.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

OData2PocoLib/CustAttributes/NamedAtributes/OriginalNameAttribute.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,27 @@ public class OriginalNameAttribute : INamedAttribute
1515
{
1616
public string Name { get; } = "original";
1717

18-
public List<string> GetAttributes(PropertyTemplate property) =>
19-
new List<string> {
20-
property.OriginalName !=property.PropName
21-
// ? $"//[OriginalName({property.OriginalName})]"
22-
?$"[JsonProperty(\"{property.OriginalName}\")]"
23-
:string.Empty,
24-
};
18+
public List<string> GetAttributes(PropertyTemplate property)
19+
{
20+
return new List<string>
21+
{
22+
property.OriginalName != property.PropName
23+
// ? $"//[OriginalName({property.OriginalName})]"
24+
? $"[JsonProperty(\"{property.OriginalName}\")]"
25+
: string.Empty,
26+
};
27+
}
2528

26-
public List<string> GetAttributes(ClassTemplate classTemplate) =>
27-
new List<string>
29+
public List<string> GetAttributes(ClassTemplate classTemplate)
30+
{
31+
return new List<string>
2832
{
29-
classTemplate.OriginalName !=classTemplate.Name
30-
//? $"//[OriginalName({classTemplate.OriginalName})]"
31-
?$"[JsonProperty(\"{classTemplate.OriginalName}\")]"
32-
:string.Empty,
33-
};
33+
classTemplate.OriginalName != classTemplate.Name
34+
//? $"//[OriginalName({classTemplate.OriginalName})]"
35+
? $"[JsonProperty(\"{classTemplate.OriginalName}\")]"
36+
: string.Empty,
37+
};
38+
}
3439
}
3540
}
3641

OData2PocoLib/Extensions/StringExtensions.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,20 @@ public static string ToSnakeCase(this string str)
169169
text = text.Replace("__", "_");
170170
return text;
171171
}
172-
173-
public static string Dump(this object o)
172+
173+
public static string Dump<T>(this T obj,int level=1)
174174
{
175-
return ToJson(o);
175+
return JsonConvert.SerializeObject(obj, Formatting.Indented,
176+
new JsonSerializerSettings
177+
{
178+
NullValueHandling = NullValueHandling.Ignore,
179+
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
180+
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
181+
MaxDepth = level,
182+
});
183+
176184
}
177185

178-
179186
public static string RemoveEmptyLines(this string input)
180187
{
181188
return Regex.Replace(input, @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline);

0 commit comments

Comments
 (0)