-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfluenceSearchClientTests.cs
More file actions
186 lines (168 loc) · 6.4 KB
/
Copy pathConfluenceSearchClientTests.cs
File metadata and controls
186 lines (168 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Linq;
using System.Net;
using System.Text.Json;
using Flow.ConfluenceSearch.ConfluenceClient;
using Shouldly;
namespace Flow.ConfluenceSearch.Test;
public class ConfluenceSearchClientTests : IDisposable
{
private readonly TestHttpMessageHandler _httpMessageHandler;
private readonly HttpClient _httpClient;
private readonly ConfluenceSearchClient _searchClient;
public ConfluenceSearchClientTests()
{
_httpMessageHandler = new TestHttpMessageHandler();
_httpClient = new HttpClient(_httpMessageHandler)
{
BaseAddress = new Uri("https://test.atlassian.net/"),
};
var httpFactory = () => _httpClient;
_searchClient = new ConfluenceSearchClient(httpFactory);
}
[Fact]
public async Task SearchCqlAsync_WithValidCql_ReturnsConfluenceResponse()
{
// Arrange
const string cql = "project = TEST";
const int maxResults = 50;
var cancellationToken = CancellationToken.None;
var expectedResponse = new ContentSearchResponse
{
Results = new List<ConfluenceSearchItemDto>
{
new()
{
Excerpt = "TEST-1",
Content = new ConfluenceContentDto
{
Id = "Test Issue Summary",
History = new ConfluenceHistoryDto
{
LastUpdated = new ConfluenceLastUpdatedDto
{
By = new ConfluenceUserDto
{
DisplayName = "John Doe",
ProfilePicture = new ConfluenceProfilePictureDto
{
Path = "High",
},
},
},
},
},
Title = "TEST",
Url = "Bug",
ResultGlobalContainer = new ResultGlobalContainerDto
{
DisplayUrl = "/spaces/John Doe/pages",
},
},
},
};
_httpMessageHandler.SetResponse(
HttpStatusCode.OK,
JsonSerializer.Serialize(expectedResponse)
);
// Act
var result = await _searchClient.SearchCqlAsync(cql, maxResults, cancellationToken);
// Assert
result.ShouldNotBeNull();
result.Results.ShouldNotBeEmpty();
result.Results.Count.ShouldBe(1);
// Test issue details
var issue = result.Results[0];
issue.Excerpt.ShouldBe("TEST-1");
issue.Content.Id.ShouldBe("Test Issue Summary");
issue.LastUpdatedByAvatarPath.ShouldBe("High");
issue.SpaceKey.ShouldBe("John Doe");
issue.Title.ShouldBe("TEST");
issue.Url.ShouldBe("Bug");
}
[Fact]
public async Task SearchCqlAsync_WhenApiReturnsError_ThrowsApplicationException()
{
// Arrange
var errorContent = "server error details";
_httpMessageHandler.SetResponse(HttpStatusCode.InternalServerError, errorContent);
// Act & Assert
var ex = await Should.ThrowAsync<ApplicationException>(async () =>
await _searchClient.SearchCqlAsync("invalid cql", 10, CancellationToken.None)
);
ex.Message.ShouldContain(errorContent);
}
[Fact]
public async Task SearchCqlAsync_WithEmptyResults_ReturnsEmptyList()
{
// Arrange
var expected = new ContentSearchResponse { Results = new List<ConfluenceSearchItemDto>() };
_httpMessageHandler.SetResponse(HttpStatusCode.OK, JsonSerializer.Serialize(expected));
// Act
var result = await _searchClient.SearchCqlAsync("no results", 10, CancellationToken.None);
// Assert
result.ShouldNotBeNull();
result.Results.ShouldBeEmpty();
}
[Fact]
public async Task SearchCqlAsync_WithSpecialCharacters_PreservesFieldsAndBuildsBrowseUrl()
{
// Arrange
var special = "\\*+-!():^[]{}~?|&/\\\"'";
var expected = new ContentSearchResponse
{
Results = new List<ConfluenceSearchItemDto>
{
new()
{
Excerpt = special,
Title = special,
Url = "/pages/viewpage.action?pageId=123",
Content = new ConfluenceContentDto
{
Id = special,
History = new ConfluenceHistoryDto
{
LastUpdated = new ConfluenceLastUpdatedDto
{
By = new ConfluenceUserDto
{
DisplayName = special,
ProfilePicture = new ConfluenceProfilePictureDto
{
Path = "/avatars/1",
},
},
},
},
},
ResultGlobalContainer = new ResultGlobalContainerDto
{
DisplayUrl = "/spaces/KEY/pages",
},
},
},
};
_httpMessageHandler.SetResponse(HttpStatusCode.OK, JsonSerializer.Serialize(expected));
// Act
var result = await _searchClient.SearchCqlAsync("special", 10, CancellationToken.None);
// Assert
result.ShouldNotBeNull();
result.Results.Count.ShouldBe(1);
var item = result.Results.Single();
item.Title.ShouldBe(special);
item.Excerpt.ShouldBe(special);
item.Content.Id.ShouldBe(special);
item.LastUpdatedByName.ShouldBe(special);
item.LastUpdatedByAvatarPath.ShouldBe("/avatars/1");
// BrowseUrl should contain base + /wiki + url
var browse = item.BrowseUrl(_httpClient.BaseAddress!.ToString());
browse.ShouldContain("/wiki");
browse.ShouldContain("viewpage.action");
}
public void Dispose()
{
_httpClient?.Dispose();
_httpMessageHandler?.Dispose();
GC.SuppressFinalize(this);
}
}