Skip to content

Commit 4c5abfd

Browse files
ShiranuitAschen
authored andcommitted
Add Admin controller and unit tests (#22)
Add an Admin Controller wit the following methods from the API reference: dump loadFixtures loadMappings loadSecurities resetCache resetDatabase resetKuzzleData resetSecurity shutdown
1 parent 9c1340a commit 4c5abfd

File tree

5 files changed

+381
-0
lines changed

5 files changed

+381
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
using Xunit;
2+
using KuzzleSdk.API.Controllers;
3+
using Newtonsoft.Json.Linq;
4+
using KuzzleSdk.Utils;
5+
6+
namespace Kuzzle.Tests.API.Controllers {
7+
public class AdminControllerTest {
8+
private readonly AdminController _adminController;
9+
private readonly KuzzleApiMock _api;
10+
11+
public AdminControllerTest() {
12+
_api = new KuzzleApiMock();
13+
_adminController = new AdminController(_api.MockedObject);
14+
}
15+
16+
17+
[Fact]
18+
public async void DumpAsyncTestSuccess() {
19+
20+
_api.SetResult(@"{result: {acknowledge: true}}");
21+
22+
await _adminController.DumpAsync();
23+
24+
_api.Verify(new JObject {
25+
{"controller", "admin"},
26+
{"action", "dump"}
27+
});
28+
29+
}
30+
31+
[Theory]
32+
[InlineData(false)]
33+
[InlineData(true)]
34+
public async void LoadFixturesAsyncTestSuccess(bool waitForRefresh) {
35+
36+
_api.SetResult(@"{result: {acknowledge: true}}");
37+
38+
await _adminController.LoadFixturesAsync(
39+
JObject.Parse(@"{foo: [{create: {_id: 'bar'}}, {field: 'value', field2: true}]}"),
40+
waitForRefresh);
41+
42+
JObject verifyQuery = new JObject {
43+
{"controller", "admin"},
44+
{"action", "loadFixtures"},
45+
{"body", new JObject {
46+
{"index-name", new JObject{
47+
{"foo", new JArray {
48+
new JObject{{"create", new JObject {{ "_id", "bar" }} } },
49+
new JObject{{"field", "value"}, {"field2", true} }
50+
}}
51+
}}
52+
}}
53+
};
54+
55+
QueryUtils.HandleRefreshOption(verifyQuery, waitForRefresh);
56+
57+
_api.Verify(verifyQuery);
58+
59+
}
60+
61+
[Theory]
62+
[InlineData(false)]
63+
[InlineData(true)]
64+
public async void LoadMappingsAsyncTestSuccess(bool waitForRefresh) {
65+
66+
_api.SetResult(@"{result: {acknowledge: true}}");
67+
68+
await _adminController.LoadMappingsAsync(
69+
JObject.Parse(@"{foo: {properties: {field1: {}, field2: {}}}}"),
70+
waitForRefresh);
71+
72+
JObject verifyQuery = new JObject {
73+
{"controller", "admin"},
74+
{"action", "loadMappings"},
75+
{"body", new JObject {
76+
{"index-name", new JObject{
77+
{"foo", new JObject {
78+
{"properties", new JObject{
79+
{"field1", new JObject{ }},
80+
{"field2", new JObject{ }}
81+
}}
82+
}}
83+
}}
84+
}}
85+
};
86+
87+
QueryUtils.HandleRefreshOption(verifyQuery, waitForRefresh);
88+
89+
_api.Verify(verifyQuery);
90+
91+
}
92+
93+
[Theory]
94+
[InlineData(false)]
95+
[InlineData(true)]
96+
public async void LoadSecuritiesAsyncTestSuccess(bool waitForRefresh) {
97+
98+
_api.SetResult(@"{result: {acknowledge: true}}");
99+
100+
await _adminController.LoadSecuritiesAsync(
101+
JObject.Parse(@"{roles: {foobar: {foo: 'bar', bar: 'foo'}}}"),
102+
waitForRefresh);
103+
104+
JObject verifyQuery = new JObject {
105+
{"controller", "admin"},
106+
{"action", "loadSecurities"},
107+
{"body", new JObject {
108+
{"roles", new JObject{
109+
{"foobar", new JObject {
110+
{"foo", "bar"},
111+
{"bar", "foo"}
112+
}}
113+
}}
114+
}}
115+
};
116+
117+
QueryUtils.HandleRefreshOption(verifyQuery, waitForRefresh);
118+
119+
_api.Verify(verifyQuery);
120+
121+
}
122+
123+
[Fact]
124+
public async void ResetCacheAsyncTestSuccess() {
125+
126+
_api.SetResult(@"{result: {acknowledge: true}}");
127+
128+
await _adminController.ResetCacheAsync("foobar");
129+
130+
JObject verifyQuery = new JObject {
131+
{"controller", "admin"},
132+
{"action", "resetCache"},
133+
{"database", "foobar"}
134+
};
135+
136+
_api.Verify(verifyQuery);
137+
138+
}
139+
140+
[Fact]
141+
public async void ResetDatabaseAsyncTestSuccess() {
142+
143+
_api.SetResult(@"{result: {acknowledge: true}}");
144+
145+
await _adminController.ResetDatabaseAsync();
146+
147+
JObject verifyQuery = new JObject {
148+
{"controller", "admin"},
149+
{"action", "resetDatabase"},
150+
};
151+
152+
_api.Verify(verifyQuery);
153+
154+
}
155+
156+
[Fact]
157+
public async void ResetKuzzleDataAsyncTestSuccess() {
158+
159+
_api.SetResult(@"{result: {acknowledge: true}}");
160+
161+
await _adminController.ResetKuzzleDataAsync();
162+
163+
JObject verifyQuery = new JObject {
164+
{"controller", "admin"},
165+
{"action", "resetKuzzleData"},
166+
};
167+
168+
_api.Verify(verifyQuery);
169+
170+
}
171+
172+
[Fact]
173+
public async void ResetSecurityAsyncTestSuccess() {
174+
175+
_api.SetResult(@"{result: {acknowledge: true}}");
176+
177+
await _adminController.ResetSecurityAsync();
178+
179+
JObject verifyQuery = new JObject {
180+
{"controller", "admin"},
181+
{"action", "resetSecurity"},
182+
};
183+
184+
_api.Verify(verifyQuery);
185+
186+
}
187+
188+
[Fact]
189+
public async void ShutdownAsyncTestSuccess() {
190+
191+
_api.SetResult(@"{result: {acknowledge: true}}");
192+
193+
await _adminController.ShutdownAsync();
194+
195+
JObject verifyQuery = new JObject {
196+
{"controller", "admin"},
197+
{"action", "shutdown"},
198+
};
199+
200+
_api.Verify(verifyQuery);
201+
202+
}
203+
204+
}
205+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KuzzleSdk.Utils;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace KuzzleSdk.API.Controllers {
7+
public class AdminController : BaseController {
8+
internal AdminController(IKuzzleApi k) : base(k) { }
9+
10+
11+
/// <summary>
12+
/// Asynchronously create a snapshot of Kuzzle's state.
13+
/// Depending on the configuration of Kuzzle.
14+
/// </summary>
15+
public async Task DumpAsync() {
16+
17+
await api.QueryAsync(new JObject {
18+
{"controller", "admin"},
19+
{"action", "dump"}
20+
});
21+
22+
}
23+
24+
/// <summary>
25+
/// Load fixtures into the storage layer.
26+
/// </summary>
27+
public async Task LoadFixturesAsync(JObject indexName, bool waitForRefresh = false) {
28+
29+
JObject query = new JObject {
30+
{"controller", "admin"},
31+
{"action", "loadFixtures"},
32+
{"body", new JObject {
33+
{"index-name", indexName}
34+
}
35+
}
36+
};
37+
38+
QueryUtils.HandleRefreshOption(query, waitForRefresh);
39+
40+
await api.QueryAsync(query);
41+
}
42+
43+
/// <summary>
44+
/// Apply mappings to the storage layer.
45+
/// </summary>
46+
public async Task LoadMappingsAsync(JObject indexName, bool waitForRefresh = false) {
47+
48+
JObject query = new JObject {
49+
{"controller", "admin"},
50+
{"action", "loadMappings"},
51+
{"body", new JObject {
52+
{"index-name", indexName}
53+
}
54+
}
55+
};
56+
57+
QueryUtils.HandleRefreshOption(query, waitForRefresh);
58+
59+
await api.QueryAsync(query);
60+
}
61+
62+
/// <summary>
63+
/// Load roles, profiles and users into the storage layer.
64+
/// </summary>
65+
public async Task LoadSecuritiesAsync(JObject body, bool waitForRefresh = false) {
66+
67+
JObject query = new JObject {
68+
{"controller", "admin"},
69+
{"action", "loadSecurities"},
70+
{"body", body}
71+
};
72+
73+
QueryUtils.HandleRefreshOption(query, waitForRefresh);
74+
75+
await api.QueryAsync(query);
76+
}
77+
78+
/// <summary>
79+
/// Asynchronously clears the cache database.
80+
/// </summary>
81+
public async Task ResetCacheAsync(string database) {
82+
83+
await api.QueryAsync(new JObject {
84+
{"controller", "admin"},
85+
{"action", "resetCache"},
86+
{"database", database}
87+
});
88+
}
89+
90+
/// <summary>
91+
/// Asynchronously deletes all indexes created by users.
92+
/// Neither Kuzzle internal indexes nor Plugin indexes are deleted.
93+
/// </summary>
94+
public async Task ResetDatabaseAsync() {
95+
96+
await api.QueryAsync(new JObject {
97+
{"controller", "admin"},
98+
{"action", "resetDatabase"},
99+
});
100+
101+
}
102+
103+
/// <summary>
104+
/// Asynchronously starts the following sequence, in this order:
105+
/// - Invalidates and deletes all users along with their associated credentials
106+
/// - Deletes all user-defined roles and profiles
107+
/// - Resets the default roles and profiles to their default values
108+
/// Deletes all document validation specifications
109+
/// This action has no impact on Plugin and Document storages.
110+
/// </summary>
111+
public async Task ResetKuzzleDataAsync() {
112+
113+
await api.QueryAsync(new JObject {
114+
{"controller", "admin"},
115+
{"action", "resetKuzzleData"},
116+
});
117+
118+
}
119+
120+
/// <summary>
121+
/// Asynchronously deletes all users, profiles and roles.
122+
/// Then resets anonymous, default and admin profiles and roles
123+
/// to default values, specified in Kuzzle configuration files.
124+
/// </summary>
125+
public async Task ResetSecurityAsync() {
126+
127+
await api.QueryAsync(new JObject {
128+
{"controller", "admin"},
129+
{"action", "resetSecurity"},
130+
});
131+
132+
}
133+
134+
/// <summary>
135+
/// Safely stops a Kuzzle instance after all remaining requests are processed.
136+
///
137+
/// In a cluster environment, the shutdown action will be propagated across all nodes.
138+
/// </summary>
139+
public async Task ShutdownAsync() {
140+
141+
await api.QueryAsync(new JObject {
142+
{"controller", "admin"},
143+
{"action", "shutdown"},
144+
});
145+
}
146+
147+
}
148+
}

Kuzzle/Kuzzle.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public void DispatchTokenExpired() {
125125
/// </summary>
126126
public ServerController Server { get; private set; }
127127

128+
/// <summary>
129+
/// Exposes actions from the "admin" Kuzzle API controller
130+
/// </summary>
131+
public AdminController Admin { get; private set; }
132+
128133
/// <summary>
129134
/// Authentication token
130135
/// </summary>
@@ -215,6 +220,7 @@ public Kuzzle(AbstractProtocol networkProtocol) {
215220
Index = new IndexController(this);
216221
Realtime = new RealtimeController(this);
217222
Server = new ServerController(this);
223+
Admin = new AdminController(this);
218224

219225
// Initializes instance unique properties
220226
Version = typeof(Kuzzle)

Kuzzle/Kuzzle.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<ItemGroup>
3333
<Compile Remove="API\Options\DocumentOptions.cs" />
3434
</ItemGroup>
35+
<ItemGroup>
36+
<Folder Include="Utils\" />
37+
</ItemGroup>
3538
<ProjectExtensions>
3639
<MonoDevelop>
3740
<Properties>

0 commit comments

Comments
 (0)