Skip to content

Commit ba8b455

Browse files
committed
Extract interface for driver object, added AssociateOfAsync and ChildOfAsync helper methods
1 parent d9071a7 commit ba8b455

File tree

10 files changed

+79
-17
lines changed

10 files changed

+79
-17
lines changed

cloudcms-csharp-driver.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RootNamespace>CloudCMS</RootNamespace>
77

88
<PackageId>cloudcms</PackageId>
9-
<PackageVersion>1.1.3</PackageVersion>
9+
<PackageVersion>1.1.4</PackageVersion>
1010
<Title>CloudCMS Driver</Title>
1111
<Description>C# .NET driver for CloudCMS</Description>
1212
<Summary>C# .NET driver for CloudCMS</Summary>

src/CloudCMSDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace CloudCMS
1414
{
15-
public class CloudCMSDriver
15+
public class CloudCMSDriver : ICloudCMSDriver
1616
{
1717
// Static connection methods
1818

src/ICloudCMSDriver.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace CloudCMS
8+
{
9+
public interface ICloudCMSDriver
10+
{
11+
Task<JObject> RequestAsync(string uri, HttpMethod method, IDictionary<string, string> queryParams = null, HttpContent body = null);
12+
Task<JObject> GetAsync(string uri, IDictionary<string, string> queryParams = null);
13+
Task<JObject> PostAsync(string uri, IDictionary<string, string> queryParams = null, HttpContent body = null);
14+
Task<JObject> PutAsync(string uri, IDictionary<string, string> queryParams = null, HttpContent body = null);
15+
Task<JObject> DeleteAsync(string uri, IDictionary<string, string> queryParams = null);
16+
Task<Stream> DownloadAsync(string uri);
17+
Task<byte[]> DownloadBytesAsync(string uri);
18+
Task UploadAsync(string uri, byte[] bytes, string mimetype, IDictionary<string, string> paramMap = null);
19+
Task UploadAsync(string uri, Stream stream, string mimetype, IDictionary<string, string> paramMap = null);
20+
Task UploadAsync(string uri, IDictionary<string, string> paramMap, IDictionary<string, AttachmentContent> payloads);
21+
22+
}
23+
}

src/documents/AbstractDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public abstract class AbstractDocument : IDocument
1212

1313
public abstract string URI { get; }
1414

15-
public CloudCMSDriver Driver { get; }
15+
public ICloudCMSDriver Driver { get; }
1616

17-
protected AbstractDocument(CloudCMSDriver driver, JObject obj)
17+
protected AbstractDocument(ICloudCMSDriver driver, JObject obj)
1818
{
1919
this.Driver = driver;
2020
this.Id = obj.GetValue("_doc").ToString();

src/documents/IDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IDocument : ITypedID, IReferenceable
99

1010
JObject Data { get; set; }
1111

12-
CloudCMSDriver Driver { get; }
12+
ICloudCMSDriver Driver { get; }
1313

1414
string URI { get; }
1515

src/nodes/INode.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ public interface INode : IBaseNode
1515
Task<List<IAssociation>> AssociationsAsync(QName associationTypeQName);
1616
Task<List<IAssociation>> AssociationsAsync(QName associationTypeQName, Direction direction, JObject pagination);
1717

18-
Task<Association> AssociateAsync(INode targetNode, QName associationTypeQName);
19-
Task<Association> AssociateAsync(INode targetNode, QName associationTypeQName, JObject data);
20-
Task<Association> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality);
21-
Task<Association> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality, JObject data);
18+
Task<IAssociation> AssociateAsync(INode targetNode, QName associationTypeQName);
19+
Task<IAssociation> AssociateAsync(INode targetNode, QName associationTypeQName, JObject data);
20+
Task<IAssociation> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality);
21+
Task<IAssociation> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality, JObject data);
2222

2323
Task UnassociateAsync(INode targetNode, QName associationTypeQName);
2424
Task UnassociateAsync(INode targetNode, QName associationTypeQName, Directionality directionality);
25+
26+
Task<IAssociation> AssociateOfAsync(INode sourceNode, QName associationTypeQName, JObject data=null);
27+
Task<IAssociation> ChildOfAsync(INode sourceNode);
2528

29+
2630
// File Folder
2731
Task<JObject> FileFolderTreeAsync();
2832
Task<JObject> FileFolderTreeAsync(string basePath);

src/nodes/Node.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@ public async Task<List<IAssociation>> AssociationsAsync(QName associationTypeQNa
6161
return associations;
6262
}
6363

64-
public async Task<Association> AssociateAsync(INode targetNode, QName associationTypeQName)
64+
public async Task<IAssociation> AssociateAsync(INode targetNode, QName associationTypeQName)
6565
{
6666
return await AssociateAsync(targetNode, associationTypeQName, Directionality.DIRECTED, null);
6767
}
6868

69-
public async Task<Association> AssociateAsync(INode targetNode, QName associationTypeQName, JObject data)
69+
public async Task<IAssociation> AssociateAsync(INode targetNode, QName associationTypeQName, JObject data)
7070
{
7171
return await AssociateAsync(targetNode, associationTypeQName, Directionality.DIRECTED, data);
7272
}
7373

74-
public async Task<Association> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality)
74+
public async Task<IAssociation> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality)
7575
{
7676
return await AssociateAsync(otherNode, associationTypeQName, directionality, null);
7777
}
7878

79-
public async Task<Association> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality, JObject data)
79+
public async Task<IAssociation> AssociateAsync(INode otherNode, QName associationTypeQName, Directionality directionality, JObject data)
8080
{
8181
if (data == null)
8282
{
@@ -123,6 +123,16 @@ public Task UnassociateAsync(INode targetNode, QName associationTypeQName, Direc
123123
return Driver.PostAsync(uri, queryParams);
124124
}
125125

126+
public Task<IAssociation> AssociateOfAsync(INode sourceNode, QName associationTypeQName, JObject data = null)
127+
{
128+
return sourceNode.AssociateAsync(this, associationTypeQName, Directionality.DIRECTED, data);
129+
}
130+
131+
public Task<IAssociation> ChildOfAsync(INode sourceNode)
132+
{
133+
return AssociateOfAsync(sourceNode, QName.create("a:child"));
134+
}
135+
126136
public Task<JObject> FileFolderTreeAsync()
127137
{
128138
return FileFolderTreeAsync(null);

src/platforms/Platform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CloudCMS
99
class Platform : AbstractDocument,
1010
IPlatform
1111
{
12-
public Platform(CloudCMSDriver driver, JObject obj) : base(driver, obj)
12+
public Platform(ICloudCMSDriver driver, JObject obj) : base(driver, obj)
1313
{
1414

1515
}

src/repositories/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Repository : AbstractDocument,
1010
{
1111
public string PlatformId { get; }
1212

13-
public Repository(CloudCMSDriver driver, JObject obj) : base(driver, obj)
13+
public Repository(ICloudCMSDriver driver, JObject obj) : base(driver, obj)
1414
{
1515
this.PlatformId = obj.SelectToken("platformId").ToString();
1616
}

tests/AssociationTest.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public async void TestAssociateUnassociate()
2222
INode node1 = (INode) await master.CreateNodeAsync(nodeObj1);
2323

2424
JObject nodeObj2 = new JObject(
25-
new JProperty("title", "Node1")
25+
new JProperty("title", "Node2")
2626
);
2727
INode node2 = (INode) await master.CreateNodeAsync(nodeObj2);
2828

2929
JObject nodeObj3 = new JObject(
30-
new JProperty("title", "Node1")
30+
new JProperty("title", "Node3")
3131
);
3232
INode node3 = (INode) await master.CreateNodeAsync(nodeObj3);
3333

@@ -76,5 +76,30 @@ public async void TestAssociateUnassociate()
7676
allAssociations = await node1.AssociationsAsync();
7777
Assert.Single(allAssociations); // will include a has_role association
7878
}
79+
80+
[Fact]
81+
public async void TestChildOf()
82+
{
83+
IBranch master = await Fixture.Repository.MasterAsync();
84+
85+
JObject nodeObj1 = new JObject(
86+
new JProperty("title", "Node1")
87+
);
88+
INode node1 = (INode) await master.CreateNodeAsync(nodeObj1);
89+
90+
JObject nodeObj2 = new JObject(
91+
new JProperty("title", "Node2")
92+
);
93+
INode node2 = (INode) await master.CreateNodeAsync(nodeObj2);
94+
95+
IAssociation association = await node1.ChildOfAsync(node2);
96+
Assert.NotNull(association);
97+
Assert.Equal(Directionality.DIRECTED, association.Directionality);
98+
99+
INode source = await association.ReadSourceNodeAsync();
100+
Assert.Equal(node2.Id, source.Id);
101+
INode target = await association.ReadTargetNodeAsync();
102+
Assert.Equal(node1.Id, target.Id);
103+
}
79104
}
80105
}

0 commit comments

Comments
 (0)