|
44 | 44 | import org.elasticsearch.client.security.DeleteRoleMappingResponse;
|
45 | 45 | import org.elasticsearch.client.security.DeleteRoleRequest;
|
46 | 46 | import org.elasticsearch.client.security.DeleteRoleResponse;
|
| 47 | +import org.elasticsearch.client.security.DeleteUserRequest; |
| 48 | +import org.elasticsearch.client.security.DeleteUserResponse; |
47 | 49 | import org.elasticsearch.client.security.DisableUserRequest;
|
48 | 50 | import org.elasticsearch.client.security.EmptyResponse;
|
49 | 51 | import org.elasticsearch.client.security.EnableUserRequest;
|
@@ -150,6 +152,67 @@ public void onFailure(Exception e) {
|
150 | 152 | }
|
151 | 153 | }
|
152 | 154 |
|
| 155 | + public void testDeleteUser() throws Exception { |
| 156 | + RestHighLevelClient client = highLevelClient(); |
| 157 | + addUser(client, "testUser", "testPassword"); |
| 158 | + |
| 159 | + { |
| 160 | + // tag::delete-user-request |
| 161 | + DeleteUserRequest deleteUserRequest = new DeleteUserRequest( |
| 162 | + "testUser"); // <1> |
| 163 | + // end::delete-user-request |
| 164 | + |
| 165 | + // tag::delete-user-execute |
| 166 | + DeleteUserResponse deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); |
| 167 | + // end::delete-user-execute |
| 168 | + |
| 169 | + // tag::delete-user-response |
| 170 | + boolean found = deleteUserResponse.isAcknowledged(); // <1> |
| 171 | + // end::delete-user-response |
| 172 | + assertTrue(found); |
| 173 | + |
| 174 | + // check if deleting the already deleted user again will give us a different response |
| 175 | + deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); |
| 176 | + assertFalse(deleteUserResponse.isAcknowledged()); |
| 177 | + } |
| 178 | + |
| 179 | + { |
| 180 | + DeleteUserRequest deleteUserRequest = new DeleteUserRequest("testUser", RefreshPolicy.IMMEDIATE); |
| 181 | + |
| 182 | + ActionListener<DeleteUserResponse> listener; |
| 183 | + //tag::delete-user-execute-listener |
| 184 | + listener = new ActionListener<DeleteUserResponse>() { |
| 185 | + @Override |
| 186 | + public void onResponse(DeleteUserResponse deleteUserResponse) { |
| 187 | + // <1> |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void onFailure(Exception e) { |
| 192 | + // <2> |
| 193 | + } |
| 194 | + }; |
| 195 | + //end::delete-user-execute-listener |
| 196 | + |
| 197 | + // Replace the empty listener by a blocking listener in test |
| 198 | + final CountDownLatch latch = new CountDownLatch(1); |
| 199 | + listener = new LatchedActionListener<>(listener, latch); |
| 200 | + |
| 201 | + //tag::delete-user-execute-async |
| 202 | + client.security().deleteUserAsync(deleteUserRequest, RequestOptions.DEFAULT, listener); // <1> |
| 203 | + //end::delete-user-execute-async |
| 204 | + |
| 205 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private void addUser(RestHighLevelClient client, String userName, String password) throws IOException { |
| 210 | + User user = new User(userName, Collections.singletonList(userName)); |
| 211 | + PutUserRequest request = new PutUserRequest(user, password.toCharArray(), true, RefreshPolicy.NONE); |
| 212 | + PutUserResponse response = client.security().putUser(request, RequestOptions.DEFAULT); |
| 213 | + assertTrue(response.isCreated()); |
| 214 | + } |
| 215 | + |
153 | 216 | public void testPutRoleMapping() throws Exception {
|
154 | 217 | final RestHighLevelClient client = highLevelClient();
|
155 | 218 |
|
|
0 commit comments