Skip to content

Commit

Permalink
Fixed tests, made search work (somewhat). Updated AppVeyor build badge.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakritzator committed Mar 10, 2016
1 parent de6843d commit 17063da
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 28 deletions.
43 changes: 25 additions & 18 deletions Dapplo.Confluence.Shared/ConfluenceApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public ConfluenceApi(Uri baseUri, IHttpSettings httpSettings = null)
{
throw new ArgumentNullException(nameof(baseUri));
}
ConfluenceBaseUri = baseUri.AppendSegments("rest", "api", "2");
ConfluenceBaseUri = baseUri.AppendSegments("rest", "api");

_behaviour = new HttpBehaviour
{
HttpSettings = httpSettings,
OnHttpRequestMessageCreated = (httpMessage) =>
OnHttpRequestMessageCreated = httpMessage =>
{
httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "nocheck");
if (!string.IsNullOrEmpty(_user) && _password != null)
Expand Down Expand Up @@ -94,7 +94,7 @@ public void SetBasicAuthentication(string user, string password)
#region write
/// <summary>
/// Attach content to the specified issue
/// See: https://docs.atlassian.com/Confluence/REST/latest/#d2e3035
/// See: https://docs.atlassian.com/confluence/REST/latest/#d2e3035
/// </summary>
/// <param name="contentId">Id of the content to attach to</param>
/// <param name="content">the content can be anything what Dapplo.HttpExtensions supports</param>
Expand All @@ -121,8 +121,10 @@ public async Task<TResponse> PictureAsync<TResponse>(Attachment attachment, Canc
where TResponse : class
{
_behaviour.MakeCurrent();
var attachmentUriBuilder = new UriBuilder(ConfluenceBaseUri);
attachmentUriBuilder.Path = attachment.Links.Download;
var attachmentUriBuilder = new UriBuilder(ConfluenceBaseUri)
{
Path = attachment.Links.Download
};
return await attachmentUriBuilder.Uri.GetAsAsync<TResponse>(cancellationToken).ConfigureAwait(false);
}

Expand All @@ -137,14 +139,15 @@ public async Task<TResponse> PictureAsync<TResponse>(Picture picture, Cancellati
where TResponse : class
{
_behaviour.MakeCurrent();
var pictureUriBuilder = new UriBuilder(ConfluenceBaseUri);
pictureUriBuilder.Path = picture.Path;
var pictureUriBuilder = new UriBuilder(ConfluenceBaseUri)
{
Path = picture.Path
};
return await pictureUriBuilder.Uri.GetAsAsync<TResponse>(cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Get Space information
/// See: https://docs.atlassian.com/Confluence/REST/latest/#d2e4539
/// Get Space information see <a href="https://docs.atlassian.com/confluence/REST/latest/#d3e164">here</a>
/// </summary>
/// <param name="spaceKey">the space key</param>
/// <param name="cancellationToken">CancellationToken</param>
Expand All @@ -157,24 +160,28 @@ public async Task<Space> SpaceAsync(string spaceKey, CancellationToken cancellat
}

/// <summary>
/// Search for issues, with a CQL (e.g. from a filter)
/// See: https://docs.atlassian.com/Confluence/REST/latest/#d2e4539
/// Search for issues, with a CQL (e.g. from a filter) see <a href="https://docs.atlassian.com/confluence/REST/latest/#d2e4539">here</a>
/// </summary>
/// <param name="cql">Confluence Query Language, like SQL, for the search</param>
/// <param name="cqlContext">the execution context for CQL functions, provides current space key and content id. If this is not provided some CQL functions will not be available.</param>
/// <param name="limit">Maximum number of results returned, default is 20</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <returns>dynamic</returns>
public async Task<dynamic> SearchAsync(string cql, string cqlContext = null, int limit = 20, CancellationToken cancellationToken = default(CancellationToken))
/// <returns>result with content</returns>
public async Task<Result<Content>> SearchAsync(string cql, string cqlContext = null, int limit = 20, CancellationToken cancellationToken = default(CancellationToken))
{
_behaviour.MakeCurrent();
var searchUri = ConfluenceBaseUri.AppendSegments("content", "search").ExtendQuery("cql", cql).ExtendQuery("cqlcontext", cqlContext).ExtendQuery("limit", limit);
return await searchUri.GetAsAsync<dynamic>(cancellationToken).ConfigureAwait(false);

var searchUri = ConfluenceBaseUri.AppendSegments("content", "search").ExtendQuery("cql", cql).ExtendQuery("limit", limit);
if (cqlContext != null)
{
searchUri = searchUri.ExtendQuery("cqlcontext", cqlContext);
}
return await searchUri.GetAsAsync<Result<Content>>(cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Get content by title
/// See: https://docs.atlassian.com/Confluence/REST/latest/#d2e4539
/// See: https://docs.atlassian.com/confluence/REST/latest/#d2e4539
/// </summary>
/// <param name="spaceKey">Space key</param>
/// <param name="title">Title of the content</param>
Expand Down Expand Up @@ -209,7 +216,7 @@ public async Task<Result<Content>> ContentByTitleAsync(string spaceKey, string t

/// <summary>
/// Get currrent user information, introduced with 6.6
/// See: https://docs.atlassian.com/Confluence/REST/latest/#user-getCurrent
/// See: https://docs.atlassian.com/confluence/REST/latest/#user-getCurrent
/// </summary>
/// <param name="cancellationToken">CancellationToken</param>
/// <returns>User</returns>
Expand All @@ -222,7 +229,7 @@ public async Task<User> MyselfAsync(CancellationToken cancellationToken = defaul

/// <summary>
/// Get user information, introduced with 6.6
/// See: https://docs.atlassian.com/Confluence/REST/latest/#user-getUser
/// See: https://docs.atlassian.com/confluence/REST/latest/#user-getUser
/// </summary>
/// <param name="username"></param>
/// <param name="cancellationToken">CancellationToken</param>
Expand Down
15 changes: 7 additions & 8 deletions Dapplo.Confluence.Tests/ConfluenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

using Xunit;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading.Tasks;
using Xunit.Abstractions;
using Dapplo.LogFacade;
Expand All @@ -34,27 +32,28 @@ namespace Dapplo.Confluence.Tests
public class ConfluenceTests
{
// Test against a well known Confluence
private static readonly Uri TestConfluenceUri = new Uri("https://greenshot.atlassian.net");
private static readonly Uri TestConfluenceUri = new Uri("https://greenshot.atlassian.net/wiki");

private ConfluenceApi _confluenceApi;
private readonly ConfluenceApi _confluenceApi;

public ConfluenceTests(ITestOutputHelper testOutputHelper)
{
XUnitLogger.RegisterLogger(testOutputHelper, LogLevel.Verbose);
_confluenceApi = new ConfluenceApi(TestConfluenceUri);
_confluenceApi.SetBasicAuthentication("<username>", "<password>");
}

[Fact]
public async Task TestSearch()
{
var searchResult = await _confluenceApi.SearchAsync("text ~ \"robin\"");
var searchResult = await _confluenceApi.SearchAsync("text ~ \"greenshot\"");

Assert.NotNull(searchResult);
Assert.NotNull(searchResult.Issues.Count > 0);
Assert.True(searchResult.Results.Count > 0);

foreach (var issue in searchResult.Issues)
foreach (var content in searchResult.Results)
{
Assert.NotNull(issue.Fields.Project);
Assert.NotNull(content.Type);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Dapplo.Confluence.Tests/Dapplo.Confluence.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<None Include="JsonTestFiles\content.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="JsonTestFiles\search.json" />
<None Include="JsonTestFiles\space_content.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.Confluence.Tests/JsonParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void TestParseServerInfo()
var json = File.ReadAllText("JsonTestFiles/content.json");
var content = SimpleJson.DeserializeObject<Content>(json);
Assert.NotNull(content);
Assert.Equal("https://confluence/rest/api/content/2721", content.Links.Self.AbsoluteUri);
Assert.Equal("http://myhost:8080/confluence/rest/api/content/1234", content.Links.Self.AbsoluteUri);
}
}
}
114 changes: 114 additions & 0 deletions Dapplo.Confluence.Tests/JsonTestFiles/attachments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"results": [
{
"id": "att5678",
"type": "attachment",
"title": "myfile.txt",
"version": {
"by": {
"type": "known",
"username": "username",
"displayName": "Full Name",
"userKey": ""
},
"when": "2016-03-03T06:18:55.091Z",
"message": "change message for this edit",
"number": 2,
"minorEdit": false
},
"ancestors": [],
"operations": [],
"children": {},
"descendants": {},
"container": {
"id": "1234",
"type": "page",
"title": "Example Content title",
"space": {
"id": 11,
"key": "TST",
"name": "Example space",
"description": {
"plain": {
"value": "This is an example space",
"representation": "plain"
}
},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/space/TST"
}
},
"version": {
"by": {
"type": "known",
"username": "username",
"displayName": "Full Name",
"userKey": ""
},
"when": "2016-03-03T06:18:55.091Z",
"message": "change message for this edit",
"number": 2,
"minorEdit": false
},
"ancestors": [
{
"id": "123",
"type": "page",
"ancestors": [],
"operations": [],
"children": {},
"descendants": {},
"body": {},
"metadata": {},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/content/123"
}
}
],
"operations": [],
"children": {},
"descendants": {},
"container": {
"id": 11,
"key": "TST",
"name": "Example space",
"description": {
"plain": {
"value": "This is an example space",
"representation": "plain"
}
},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/space/TST"
}
},
"body": {
"view": {
"value": "<p><h1>Example</h1>Some example content body</p>",
"representation": "view",
"_expandable": {
"content": "/rest/api/content/1234"
}
}
},
"metadata": {},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/content/1234"
}
},
"body": {},
"metadata": {
"comment": "This is my File",
"mediaType": "text/plain"
},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/content/att5678"
}
}
],
"size": 1,
"_links": {
"base": "http://myhost:8080/confluence",
"context": "/confluence"
}
}
79 changes: 79 additions & 0 deletions Dapplo.Confluence.Tests/JsonTestFiles/content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"id": "1234",
"type": "page",
"title": "Example Content title",
"space": {
"id": 11,
"key": "TST",
"name": "Example space",
"description": {
"plain": {
"value": "This is an example space",
"representation": "plain"
}
},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/space/TST"
}
},
"version": {
"by": {
"type": "known",
"username": "username",
"displayName": "Full Name",
"userKey": ""
},
"when": "2016-03-03T06:18:54.953Z",
"message": "change message for this edit",
"number": 2,
"minorEdit": false
},
"ancestors": [
{
"id": "123",
"type": "page",
"ancestors": [],
"operations": [],
"children": {},
"descendants": {},
"body": {},
"metadata": {},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/content/123"
}
}
],
"operations": [],
"children": {},
"descendants": {},
"container": {
"id": 11,
"key": "TST",
"name": "Example space",
"description": {
"plain": {
"value": "This is an example space",
"representation": "plain"
}
},
"_links": {
"self": "http://myhost:8080/confluence/rest/api/space/TST"
}
},
"body": {
"view": {
"value": "<p><h1>Example</h1>Some example content body</p>",
"representation": "view",
"_expandable": {
"content": "/rest/api/content/1234"
}
}
},
"metadata": {},
"_links": {
"collection": "/rest/api/content",
"base": "http://myhost:8080/confluence",
"context": "/confluence",
"self": "http://myhost:8080/confluence/rest/api/content/1234"
}
}
Loading

0 comments on commit 17063da

Please sign in to comment.