Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get job error details method to JobEntity #383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/com/auth0/client/mgmt/JobsEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import com.auth0.client.mgmt.filter.UsersImportOptions;
import com.auth0.json.mgmt.EmailVerificationIdentity;
import com.auth0.json.mgmt.jobs.Job;
import com.auth0.json.mgmt.jobs.JobErrorDetails;
import com.auth0.net.CustomRequest;
import com.auth0.net.MultipartRequest;
import com.auth0.net.Request;
import com.auth0.utils.Asserts;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import okhttp3.ResponseBody;

/**
* Class that provides an implementation of the Jobs methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Jobs
Expand Down Expand Up @@ -53,6 +58,39 @@ public Request<Job> get(String jobId) {
return request;
}

/**
* Get error details of a failed job. A token with scope create:users is needed.
jasonfagerberg-toast marked this conversation as resolved.
Show resolved Hide resolved
* See https://auth0.com/docs/api/management/v2#!/Jobs/get_errors.
*
* @param jobId the id of the job to retrieve.
* @return a Request to execute.
*/
public Request<List<JobErrorDetails>> getErrorDetails(String jobId) {
Asserts.assertNotNull(jobId, "job id");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/jobs")
.addPathSegment(jobId)
.addPathSegment("errors")
.build()
.toString();

TypeReference<List<JobErrorDetails>> jobErrorDetailsListType = new TypeReference<List<JobErrorDetails>>() {
};
CustomRequest<List<JobErrorDetails>> request = new CustomRequest<List<JobErrorDetails>>(client, url, "GET", jobErrorDetailsListType) {
@Override
protected List<JobErrorDetails> readResponseBody(ResponseBody body) throws IOException {
if (body.contentLength() == 0) {
return Collections.emptyList();
}
return super.readResponseBody(body);
}
};
request.addHeader("Authorization", "Bearer " + apiToken);
jasonfagerberg-toast marked this conversation as resolved.
Show resolved Hide resolved
return request;
}

/**
* Sends an Email Verification. A token with scope update:users is needed.
* See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/auth0/json/mgmt/jobs/JobError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.auth0.json.mgmt.jobs;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class JobError {

@JsonProperty("code")
private String code;
@JsonProperty("message")
private String message;
@JsonProperty("path")
private String path;

@JsonCreator
public JobError(@JsonProperty("code") String code, @JsonProperty("message") String message, @JsonProperty("path") String path) {
this.code = code;
this.message = message;
this.path = path;
}

@JsonProperty("code")
public String getCode() {
return code;
}

@JsonProperty("message")
public String getMessage() {
return message;
}

@JsonProperty("path")
public String getPath() {
return path;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/auth0/json/mgmt/jobs/JobErrorDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.auth0.json.mgmt.jobs;

import com.auth0.json.mgmt.users.User;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
* Class that represents the error details for an Auth0 Job object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JobErrorDetails {

@JsonProperty("user")
private final User user;

@JsonProperty("errors")
private final List<JobError> errors;

@JsonCreator
public JobErrorDetails(@JsonProperty("user") User user, @JsonProperty("errors") List<JobError> errors) {
this.user = user;
this.errors = errors;
}

@JsonProperty("user")
public User getUser() {
return user;
}

@JsonProperty("errors")
public List<JobError> getErrors() {
return errors;
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class MockServer {
public static final String MGMT_JOB_POST_USERS_EXPORTS = "src/test/resources/mgmt/job_post_users_exports.json";
public static final String MGMT_JOB_POST_USERS_IMPORTS = "src/test/resources/mgmt/job_post_users_imports.json";
public static final String MGMT_JOB_POST_USERS_IMPORTS_INPUT = "src/test/resources/mgmt/job_post_users_imports_input.json";
public static final String MGMT_JOB_ERROR_DETAILS = "src/test/resources/mgmt/job_error_details.json";
public static final String MULTIPART_SAMPLE = "src/test/resources/mgmt/multipart_sample.json";
public static final String PASSWORDLESS_EMAIL_RESPONSE = "src/test/resources/auth/passwordless_email.json";
public static final String PASSWORDLESS_SMS_RESPONSE = "src/test/resources/auth/passwordless_sms.json";
Expand Down Expand Up @@ -153,6 +154,14 @@ public void jsonResponse(String path, int statusCode) throws IOException {
server.enqueue(response);
}

public void noContentResponse() {
MockResponse response = new MockResponse()
.setResponseCode(204)
.addHeader("Content-Type", "application/json")
.setBody("");
server.enqueue(response);
}

public void rateLimitReachedResponse(long limit, long remaining, long reset) {
MockResponse response = new MockResponse().setResponseCode(429);
if (limit != -1) {
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/auth0/client/mgmt/JobsEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.auth0.client.mgmt.filter.UsersImportOptions;
import com.auth0.json.mgmt.EmailVerificationIdentity;
import com.auth0.json.mgmt.jobs.Job;
import com.auth0.json.mgmt.jobs.JobErrorDetails;
import com.auth0.json.mgmt.jobs.UsersExportField;
import com.auth0.net.Request;
import com.auth0.net.multipart.FilePart;
Expand Down Expand Up @@ -55,6 +56,48 @@ public void shouldGetJob() throws Exception {
assertThat(response, is(notNullValue()));
}

@Test
public void shouldThrowOnGetJobErrorDetailsWithNullId() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'job id' cannot be null!");
api.jobs().getErrorDetails(null);
}

@Test
public void shouldGetJobErrorDetails() throws Exception {
Request<List<JobErrorDetails>> request = api.jobs().getErrorDetails("1");
assertThat(request, is(notNullValue()));

server.jsonResponse(MGMT_JOB_ERROR_DETAILS, 200);
List<JobErrorDetails> response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/jobs/1/errors"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));

assertThat(response, is(notNullValue()));
assertThat(response, hasSize(1));
assertThat(response.get(0).getErrors(), hasSize(1));
}

@Test
public void shouldGetJobErrorDetails_noErrors() throws Exception {
Request<List<JobErrorDetails>> request = api.jobs().getErrorDetails("1");
assertThat(request, is(notNullValue()));

server.noContentResponse();
List<JobErrorDetails> response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/jobs/1/errors"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));

assertThat(response, is(notNullValue()));
assertThat(response, is(empty()));
}

@Test
public void shouldThrowOnRequestUsersExportWithNullConnectionId() {
exception.expect(IllegalArgumentException.class);
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/auth0/json/mgmt/jobs/JobErrorDetailsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.auth0.json.mgmt.jobs;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import com.auth0.json.JsonMatcher;
import com.auth0.json.JsonTest;
import com.auth0.json.mgmt.users.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.Collections;
import org.junit.Test;

public class JobErrorDetailsTest extends JsonTest<JobErrorDetails> {

String json = "{\"user\":{\"email\":\"john.doe@gmail.com\"},\"errors\":[{\"code\":\"code\",\"message\":\"message\",\"path\":\"path\"}]}";

@Test
public void shouldDeserialize() throws Exception {
JobErrorDetails errorDetails = fromJSON(json, JobErrorDetails.class);

assertThat(errorDetails, is(notNullValue()));
assertThat(errorDetails.getUser(), is(notNullValue()));
assertThat(errorDetails.getUser().getEmail(), is(equalTo("john.doe@gmail.com")));
assertThat(errorDetails.getErrors(), hasSize(1));

assertThat(errorDetails.getErrors().get(0).getCode(), is(equalTo("code")));
assertThat(errorDetails.getErrors().get(0).getMessage(), is(equalTo("message")));
assertThat(errorDetails.getErrors().get(0).getPath(), is(equalTo("path")));
}

@Test
public void shouldSerialize() throws JsonProcessingException {
User user = new User();
user.setEmail("john.doe@gmail.com");
JobError error = new JobError("code", "message", "path");
JobErrorDetails errorDetails = new JobErrorDetails(user, Collections.singletonList(error));
String serialized = toJSON(errorDetails);

assertThat(serialized, is(equalTo(json)));
}
}
59 changes: 59 additions & 0 deletions src/test/resources/mgmt/job_error_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
{
"user": {
"email": "john.doe@gmail.com",
"email_verified": false,
"username": "johndoe",
"phone_number": "+199999999999999",
"phone_verified": false,
"user_id": "usr_5457edea1b8f33391a000004",
"created_at": "",
"updated_at": "",
"last_password_reset": "2019-08-14T19:35:00Z",
"identities": [
{
"provider": "facebook",
"user_id": "5457edea1b8f2289",
"connection": "facebook",
"isSocial": true
},
{
"profileData": {
"name": "Auth0️",
"picture": "https://pbs.twimg.com/profile_images/auth0/5457ed_normal.jpg",
"created_at": "Fri May 20 17:13:23 +0000 2011",
"description": "My twitter bio",
"lang": "es",
"location": "Palermo, Buenos Aires.",
"screen_name": "auth0",
"time_zone": "Buenos Aires",
"utc_offset": -10800
},
"provider": "twitter",
"user_id": "5457ed",
"connection": "twitter",
"isSocial": true
}
],
"app_metadata": {},
"user_metadata": {},
"picture": "",
"name": "",
"nickname": "",
"multifactor": [],
"last_ip": "",
"last_login": "",
"logins_count": 0,
"blocked": false,
"given_name": "",
"family_name": ""
},
"errors": [
{
"code": "code",
"message": "message",
"path": "path"
}
]
}
]