Skip to content

Commit 9c1340a

Browse files
alexandrebouthinonAschen
authored andcommitted
Add Index Controller (#23)
Add Index controller to the SDK C#:
1 parent c30b01e commit 9c1340a

File tree

5 files changed

+350
-13
lines changed

5 files changed

+350
-13
lines changed

.travis.yml

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
21
jobs:
32
include:
4-
- name: Unit Tests
3+
- stage: "Tests"
4+
name: "Unit Tests"
5+
if: type = pull_request OR type = push AND branch =~ /^master|[0-9]+-dev$/ OR type = cron
56
language: csharp
67
solution: sdk-csharp.sln
78
sudo: required
@@ -12,22 +13,38 @@ jobs:
1213
- dotnet restore
1314

1415
script:
15-
# incompatibilities with msbuild, so building with .net core instead
16-
- dotnet build $TRAVIS_BUILD_DIR/Kuzzle/Kuzzle.csproj -c Release
17-
- dotnet build $TRAVIS_BUILD_DIR/Kuzzle.Tests/Kuzzle.Tests.csproj
1816
- dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=$TRAVIS_BUILD_DIR/lcov.info
1917
- curl -s https://codecov.io/bash > codecov && bash codecov
2018

21-
deploy:
22-
skip_cleanup: true
23-
email: support@kuzzle.io
24-
provider: script
25-
script: bash deploy.sh
26-
27-
- name: Documentation tests
19+
- stage: "Tests"
20+
name: "Documentation"
21+
if: type = pull_request OR type = push AND branch =~ /^master|[0-9]+-dev$/ OR type = cron
2822
stage: Tests
2923
language: node_js
3024
node_js: 8
3125

3226
script:
3327
- docker-compose -f .ci/doc/docker-compose.yml run doc-tests node index
28+
29+
- stage: "Deployments"
30+
name: "NuGet"
31+
if: tag IS present AND branch = master
32+
language: csharp
33+
solution: sdk-csharp.sln
34+
sudo: required
35+
dist: xenial
36+
dotnet: 2.2
37+
mono: latest
38+
install:
39+
- dotnet restore
40+
41+
script:
42+
# incompatibilities with msbuild, so building with .net core instead
43+
- dotnet build $TRAVIS_BUILD_DIR/Kuzzle/Kuzzle.csproj -c Release
44+
- dotnet build $TRAVIS_BUILD_DIR/Kuzzle.Tests/Kuzzle.Tests.csproj
45+
46+
deploy:
47+
skip_cleanup: true
48+
email: support@kuzzle.io
49+
provider: script
50+
script: bash deploy.sh
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
using KuzzleSdk.API.Controllers;
2+
using Newtonsoft.Json.Linq;
3+
using Xunit;
4+
5+
namespace Kuzzle.Tests.API.Controllers {
6+
public class IndexControllerTest {
7+
private readonly IndexController _indexController;
8+
private readonly KuzzleApiMock _api;
9+
10+
public IndexControllerTest() {
11+
_api = new KuzzleApiMock();
12+
_indexController = new IndexController(_api.MockedObject);
13+
}
14+
15+
[Fact]
16+
public async void CreateAsyncTest() {
17+
_api.SetResult(@"
18+
{
19+
result: {
20+
acknowledged: true,
21+
shards_acknowledged: true
22+
}
23+
}
24+
");
25+
26+
await _indexController.CreateAsync("foo");
27+
28+
_api.Verify(new JObject {
29+
{ "controller", "index" },
30+
{ "action", "create" },
31+
{ "index", "foo" }
32+
});
33+
}
34+
35+
[Fact]
36+
public async void DeleteAsyncTest() {
37+
_api.SetResult(@"
38+
{
39+
result: {
40+
acknowledged: true
41+
}
42+
}
43+
");
44+
45+
await _indexController.DeleteAsync("foo");
46+
47+
_api.Verify(new JObject {
48+
{ "controller", "index" },
49+
{ "action", "delete" },
50+
{ "index", "foo" }
51+
});
52+
}
53+
54+
[Theory]
55+
[InlineData(true)]
56+
[InlineData(false)]
57+
public async void ExistsAsyncTest(bool result) {
58+
_api.SetResult(new JObject { { "result" , result } } );
59+
60+
Assert.Equal(
61+
result,
62+
await _indexController.ExistsAsync("foo")
63+
);
64+
65+
_api.Verify(new JObject {
66+
{ "controller", "index" },
67+
{ "action", "exists" },
68+
{ "index", "foo" }
69+
});
70+
}
71+
72+
[Theory]
73+
[InlineData(true)]
74+
[InlineData(false)]
75+
public async void GetAutoRefreshAsyncTest(bool result) {
76+
_api.SetResult(new JObject { { "result" , result } } );
77+
78+
Assert.Equal(
79+
result,
80+
await _indexController.GetAutoRefreshAsync("foo")
81+
);
82+
83+
_api.Verify(new JObject {
84+
{ "controller", "index" },
85+
{ "action", "getAutoRefresh" },
86+
{ "index", "foo" }
87+
});
88+
}
89+
90+
[Fact]
91+
public async void ListAsyncTest() {
92+
var indexes = new JArray { "foo", "bar", "zoo" };
93+
_api.SetResult(new JObject {
94+
{ "result" , new JObject { { "indexes", indexes } } }
95+
});
96+
97+
Assert.Equal(
98+
indexes,
99+
await _indexController.ListAsync(),
100+
new JTokenEqualityComparer()
101+
);
102+
103+
_api.Verify(new JObject {
104+
{ "controller", "index" },
105+
{ "action", "list" },
106+
});
107+
}
108+
109+
[Fact]
110+
public async void MDeleteAsyncTest() {
111+
var indexes = new JArray { "foo", "bar", "zoo" };
112+
_api.SetResult(new JObject {
113+
{ "result" , new JObject { { "indexes", indexes } } }
114+
});
115+
116+
Assert.Equal(
117+
indexes,
118+
await _indexController.MDeleteAsync(indexes),
119+
new JTokenEqualityComparer()
120+
);
121+
122+
_api.Verify(new JObject {
123+
{ "controller", "index" },
124+
{ "action", "mDelete" },
125+
{ "body", new JObject { { "indexes", indexes } } }
126+
});
127+
}
128+
129+
[Fact]
130+
public async void RefreshAsyncTest() {
131+
_api.SetResult(@"
132+
{
133+
result: {
134+
_shards: {
135+
failed: 0,
136+
successful: 5,
137+
total: 10
138+
}
139+
}
140+
}
141+
");
142+
143+
Assert.Equal(new JObject {
144+
{ "failed", 0 },
145+
{ "successful", 5 },
146+
{ "total", 10 }
147+
}, await _indexController.RefreshAsync("foo"));
148+
149+
_api.Verify(new JObject {
150+
{ "controller", "index" },
151+
{ "action", "refresh" },
152+
{ "index", "foo" }
153+
});
154+
}
155+
156+
[Fact]
157+
public async void RefreshInternalAsyncTest() {
158+
_api.SetResult(@"
159+
{
160+
result: {
161+
acknowledged: true
162+
}
163+
}
164+
");
165+
166+
await _indexController.RefreshInternalAsync();
167+
168+
_api.Verify(new JObject {
169+
{ "controller", "index" },
170+
{ "action", "refreshInternal" },
171+
});
172+
}
173+
174+
[Theory]
175+
[InlineData(true)]
176+
[InlineData(false)]
177+
public async void SetAutoRefreshAsyncTest(bool autoRefresh) {
178+
_api.SetResult(new JObject {
179+
{ "result" , new JObject { { "response", autoRefresh } } }
180+
});
181+
182+
await _indexController.SetAutoRefreshAsync("foo", autoRefresh);
183+
184+
_api.Verify(new JObject {
185+
{ "controller", "index" },
186+
{ "action", "setAutoRefresh" },
187+
{ "index", "foo" },
188+
{ "body", new JObject { { "autoRefresh", autoRefresh } } }
189+
});
190+
}
191+
}
192+
}

Kuzzle.Tests/KuzzleTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void KuzzleConstructorTest() {
4040
Assert.IsType<AuthController>(_kuzzle.Auth);
4141
Assert.IsType<CollectionController>(_kuzzle.Collection);
4242
Assert.IsType<DocumentController>(_kuzzle.Document);
43+
Assert.IsType<IndexController>(_kuzzle.Index);
4344
Assert.IsType<RealtimeController>(_kuzzle.Realtime);
4445
Assert.IsType<ServerController>(_kuzzle.Server);
4546
Assert.NotEqual(_kuzzle.InstanceId, kuzzle2.InstanceId);
@@ -204,4 +205,4 @@ await Assert.ThrowsAsync<ConnectionLostException>(async () => {
204205
Assert.Empty(_kuzzle.requests.Keys);
205206
}
206207
}
207-
}
208+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System.Threading.Tasks;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace KuzzleSdk.API.Controllers {
5+
/// <summary>
6+
/// Implements the "index" Kuzzle API controller
7+
/// </summary>
8+
public class IndexController: BaseController {
9+
internal IndexController(IKuzzleApi api) : base(api) {}
10+
11+
/// <summary>
12+
/// Creates a new index in Kuzzle via the persistence engine.
13+
/// </summary>
14+
public async Task CreateAsync(string index) {
15+
await api.QueryAsync(new JObject {
16+
{ "controller", "index" },
17+
{ "action", "create" },
18+
{ "index", index },
19+
});
20+
}
21+
22+
/// <summary>
23+
/// Deletes an index from the Kuzzle persistence engine.
24+
/// </summary>
25+
public async Task DeleteAsync(string index) {
26+
await api.QueryAsync(new JObject {
27+
{ "controller", "index" },
28+
{ "action", "delete" },
29+
{ "index", index },
30+
});
31+
}
32+
33+
/// <summary>
34+
/// Checks if an index exists in the Kuzzle persistence engine.
35+
/// </summary>
36+
public async Task<bool> ExistsAsync(string index) {
37+
Response response = await api.QueryAsync(new JObject {
38+
{ "controller", "index" },
39+
{ "action", "exists" },
40+
{ "index", index },
41+
});
42+
43+
return (bool)response.Result;
44+
}
45+
46+
/// <summary>
47+
/// Gets the autoRefresh flag value for the given index.
48+
/// </summary>
49+
public async Task<bool> GetAutoRefreshAsync(string index) {
50+
Response response = await api.QueryAsync(new JObject {
51+
{ "controller", "index" },
52+
{ "action", "getAutoRefresh" },
53+
{ "index", index },
54+
});
55+
56+
return (bool)response.Result;
57+
}
58+
59+
/// <summary>
60+
/// Lists indexes from the Kuzzle persistence engine.
61+
/// </summary>
62+
public async Task<JArray> ListAsync() {
63+
Response response = await api.QueryAsync(new JObject {
64+
{ "controller", "index" },
65+
{ "action", "list" },
66+
});
67+
68+
return (JArray)response.Result["indexes"];
69+
}
70+
71+
/// <summary>
72+
/// Deletes multiple indexes from the Kuzzle persistence engine.
73+
/// </summary>
74+
public async Task<JArray> MDeleteAsync(JArray indexes) {
75+
var request = new JObject {
76+
{ "controller", "index" },
77+
{ "action", "mDelete" },
78+
{ "body", new JObject { { "indexes", indexes } } }
79+
};
80+
81+
Response response = await api.QueryAsync(request);
82+
83+
return (JArray)response.Result["indexes"];
84+
}
85+
86+
/// <summary>
87+
/// Forces an immediate reindexation of the provided index.
88+
/// </summary>
89+
public async Task<JObject> RefreshAsync(string index) {
90+
Response response = await api.QueryAsync(new JObject {
91+
{ "controller", "index" },
92+
{ "action", "refresh" },
93+
{ "index", index },
94+
});
95+
96+
return (JObject)response.Result["_shards"];
97+
}
98+
99+
/// <summary>
100+
/// Forces an immediate reindexation of Kuzzle internal storage.
101+
/// </summary>
102+
public async Task RefreshInternalAsync() {
103+
await api.QueryAsync(new JObject {
104+
{ "controller", "index" },
105+
{ "action", "refreshInternal" },
106+
});
107+
}
108+
109+
/// <summary>
110+
/// Changes the autoRefresh configuration of the given index.
111+
/// </summary>
112+
public async Task SetAutoRefreshAsync(string index, bool autoRefresh) {
113+
await api.QueryAsync(new JObject {
114+
{ "controller", "index" },
115+
{ "action", "setAutoRefresh" },
116+
{ "index", index },
117+
{ "body", new JObject { { "autoRefresh", autoRefresh } } }
118+
});
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)