Commit d9c722ae authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support and test Discussions API (#279).

parent 4bd96b69
Showing with 241 additions and 210 deletions
+241 -210
......@@ -554,6 +554,15 @@ public abstract class AbstractApi implements Constants {
return (new GitLabApiException(thrown));
}
/**
* Creates a MultivaluedMap instance containing the "per_page" param.
*
* @param perPage the number of projects per page
* @return a MultivaluedMap instance containing the "per_page" param
*/
protected MultivaluedMap<String, String> getPerPageQueryParam(int perPage) {
return (new GitLabApiForm().withParam(PER_PAGE_PARAM, perPage).asMap());
}
/**
* Creates a MultivaluedMap instance containing "page" and "per_page" params.
......
......@@ -10,7 +10,6 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Discussion;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestFilter;
......@@ -280,44 +279,6 @@ public class MergeRequestApi extends AbstractApi {
"projects", projectId, "merge_requests", mergeRequestIid, "commits"));
}
/**
* Get a list of merge request discussions.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
*
* @param projectId the project ID for the merge request
* @param mergeRequestIid the internal ID of the merge request
* @param page the page to get
* @param perPage the number of commits per page
* @return a list containing the discussions for the specified merge request
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<Discussion> getDiscussions(int projectId, int mergeRequestIid, int page, int perPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("owned", false).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid, "discussions");
return (response.readEntity(new GenericType<List<Discussion>>() {}));
}
/**
* Get a Pager of merge request discussions.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
*
* @param projectId the project ID for the merge request
* @param mergeRequestIid the internal ID of the merge request
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager containing the discussions for the specified merge request
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<Discussion> getDiscussions(int projectId, int mergeRequestIid, int itemsPerPage) throws GitLabApiException {
return (new Pager<Discussion>(this, Discussion.class, itemsPerPage, null,
"projects", projectId, "merge_requests", mergeRequestIid, "discussions"));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
*
......@@ -361,7 +322,6 @@ public class MergeRequestApi extends AbstractApi {
return (response.readEntity(MergeRequest.class));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
*
......
......@@ -3,7 +3,6 @@ package org.gitlab4j.api;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
......@@ -87,7 +86,7 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
try {
currentItems = mapper.readValue((InputStream) response.getEntity(), javaType);
} catch (IOException e) {
} catch (Exception e) {
throw new GitLabApiException(e);
}
......@@ -310,8 +309,8 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
// regardless of what page the instance is currently on.
currentPage = 0;
// Create a Stream.Builder to ontain all the items. This is more efficient than
// getting a List with all() and streaming that List
// Create a Stream.Builder to contain all the items. This is more efficient than
// getting a List with all() and streaming that List
Stream.Builder<T> streamBuilder = Stream.builder();
// Iterate through the pages and append each page of items to the stream builder
......
......@@ -3,8 +3,6 @@ package org.gitlab4j.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
......
......@@ -12,37 +12,36 @@ import org.gitlab4j.api.utils.JacksonJson;
@XmlAccessorType(XmlAccessType.FIELD)
public class Discussion {
private String id;
private Boolean individualNote;
private List<Note> notes;
public String getId() {
return id;
}
public Boolean getIndividualNote() {
return individualNote;
}
public List<Note> getNotes() {
return notes;
}
public void setId(String id) {
this.id = id;
}
public void setIndividualNote(Boolean individualNote) {
this.individualNote = individualNote;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
private String id;
private Boolean individualNote;
private List<Note> notes;
public String getId() {
return id;
}
public Boolean getIndividualNote() {
return individualNote;
}
public List<Note> getNotes() {
return notes;
}
public void setId(String id) {
this.id = id;
}
public void setIndividualNote(Boolean individualNote) {
this.individualNote = individualNote;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
......@@ -40,7 +40,7 @@ public class Note {
public static enum NoteableType {
COMMIT, ISSUE, MERGE_REQUEST, SNIPPET;
COMMIT, EPIC, ISSUE, MERGE_REQUEST, SNIPPET;
private static JacksonJsonEnumHelper<NoteableType> enumHelper = new JacksonJsonEnumHelper<>(NoteableType.class, true, true);
@JsonCreator
......@@ -93,7 +93,7 @@ public class Note {
private Integer noteableIid;
private Boolean system;
private String title;
private String updatedAt;
private Date updatedAt;
private Boolean upvote;
private Boolean resolved;
private Boolean resolvable;
......@@ -204,11 +204,11 @@ public class Note {
this.title = title;
}
public String getUpdatedAt() {
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
......
......@@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.ws.rs.Produces;
......@@ -23,6 +24,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
......@@ -87,12 +89,12 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
/**
* Unmarshal the JSON data on the specified Reader instance to an instance of the provided class.
*
*
* @param <T> the generics type for the return value
* @param returnType an instance of this type class will be returned
* @param reader the Reader instance that contains the JSON data
* @return an instance of the provided class containing the parsed data from the Reader
* @throws JsonParseException when an error occurs paresing the provided JSON
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
......@@ -103,12 +105,12 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
/**
* Unmarshal the JSON data contained by the string and populate an instance of the provided returnType class.
*
*
* @param <T> the generics type for the return value
* @param returnType an instance of this type class will be returned
* @param postData a String holding the POST data
* @return an instance of the provided class containing the parsed data from the string
* @throws JsonParseException when an error occurs paresing the provided JSON
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
......@@ -117,6 +119,70 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
return (objectMapper.readValue(postData, returnType));
}
/**
* Unmarshal the JSON data on the specified Reader instance and populate a List of instances of the provided returnType class.
*
* @param <T> the generics type for the List
* @param returnType an instance of this type class will be contained in the returned List
* @param reader the Reader instance that contains the JSON data
* @return a List of the provided class containing the parsed data from the Reader
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> List<T> unmarshalList(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(reader, new TypeReference<List<T>>() {}));
}
/**
* Unmarshal the JSON data contained by the string and populate a List of instances of the provided returnType class.
*
* @param <T> the generics type for the List
* @param returnType an instance of this type class will be contained in the returned List
* @param postData a String holding the POST data
* @return a List of the provided class containing the parsed data from the string
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> List<T> unmarshalList(Class<T> returnType, String postData) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return objectMapper.readValue(postData, new TypeReference<List<T>>() {});
}
/**
* Unmarshal the JSON data on the specified Reader instance and populate a Map of String keys and values of the provided returnType class.
*
* @param <T> the generics type for the Map value
* @param returnType an instance of this type class will be contained the values of the Map
* @param reader the Reader instance that contains the JSON data
* @return a Map containing the parsed data from the Reader
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> Map<String, T> unmarshalMap(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(reader, new TypeReference<Map<String, T>>() {}));
}
/**
* Unmarshal the JSON data and populate a Map of String keys and values of the provided returnType class.
*
* @param <T> the generics type for the Map value
* @param returnType an instance of this type class will be contained the values of the Map
* @param jsonData the String containing the JSON data
* @return a Map containing the parsed data from the String
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> Map<String, T> unmarshalMap(Class<T> returnType, String jsonData) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(jsonData, new TypeReference<Map<String, T>>() {}));
}
/**
* Marshals the supplied object out as a formatted JSON string.
*
......
......@@ -26,7 +26,6 @@ package org.gitlab4j.api;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
......@@ -40,6 +39,7 @@ import org.gitlab4j.api.models.CommitStatus;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.DeployKey;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Discussion;
import org.gitlab4j.api.models.Epic;
import org.gitlab4j.api.models.EpicIssue;
import org.gitlab4j.api.models.Event;
......@@ -78,37 +78,31 @@ import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
import org.gitlab4j.api.systemhooks.PushSystemHookEvent;
import org.gitlab4j.api.systemhooks.SystemHookEvent;
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
import org.gitlab4j.api.utils.JacksonJson;
import org.junit.BeforeClass;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestGitLabApiBeans {
private static JacksonJson jacksonJson;
private <T> T unmarshal(Class<T> returnType, String file) throws JsonParseException, JsonMappingException, IOException {
return (JsonUtils.unmarshal(returnType, file + ".json"));
}
public TestGitLabApiBeans() {
super();
private <T> List<T> unmarshalList(Class<T> returnType, String file) throws JsonParseException, JsonMappingException, IOException {
return (JsonUtils.unmarshalList(returnType, file + ".json"));
}
@BeforeClass
public static void setup() {
jacksonJson = new JacksonJson();
jacksonJson.getObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
private <T> boolean compareJson(T apiObject, String file) throws IOException {
return (JsonUtils.compareJson(apiObject, file + ".json"));
}
@Test
public void testAwardEmoji() {
try {
AwardEmoji awardEmoji = makeFakeApiCall(AwardEmoji.class, "award-emoji");
AwardEmoji awardEmoji = unmarshal(AwardEmoji.class, "award-emoji");
assertTrue(compareJson(awardEmoji, "award-emoji"));
} catch (Exception e) {
e.printStackTrace();
......@@ -119,10 +113,10 @@ public class TestGitLabApiBeans {
public void testBranch() {
try {
Branch branch = makeFakeApiCall(Branch.class, "branch");
Branch branch = unmarshal(Branch.class, "branch");
assertTrue(compareJson(branch, "branch"));
branch = makeFakeApiCall(Branch.class, "bad-branch");
branch = unmarshal(Branch.class, "bad-branch");
assertTrue(!Branch.isValid(branch));
} catch (Exception e) {
e.printStackTrace();
......@@ -133,7 +127,7 @@ public class TestGitLabApiBeans {
public void testCommit() {
try {
Commit commit = makeFakeApiCall(Commit.class, "commit");
Commit commit = unmarshal(Commit.class, "commit");
assertTrue(compareJson(commit, "commit"));
} catch (Exception e) {
e.printStackTrace();
......@@ -144,7 +138,7 @@ public class TestGitLabApiBeans {
public void testCommitPayload() {
try {
CommitPayload commitPayload = makeFakeApiCall(CommitPayload.class, "commit-payload");
CommitPayload commitPayload = unmarshal(CommitPayload.class, "commit-payload");
assertTrue(compareJson(commitPayload, "commit-payload"));
} catch (Exception e) {
e.printStackTrace();
......@@ -155,7 +149,7 @@ public class TestGitLabApiBeans {
public void testCommitStatus() {
try {
CommitStatus commitStatus = makeFakeApiCall(CommitStatus.class, "commit-status");
CommitStatus commitStatus = unmarshal(CommitStatus.class, "commit-status");
assertTrue(compareJson(commitStatus, "commit-status"));
} catch (Exception e) {
e.printStackTrace();
......@@ -166,7 +160,7 @@ public class TestGitLabApiBeans {
public void testCompareResults() {
try {
CompareResults compareResults = makeFakeApiCall(CompareResults.class, "compare-results");
CompareResults compareResults = unmarshal(CompareResults.class, "compare-results");
assertTrue(compareJson(compareResults, "compare-results"));
} catch (Exception e) {
e.printStackTrace();
......@@ -177,7 +171,7 @@ public class TestGitLabApiBeans {
public void testDiff() {
try {
Diff diff = makeFakeApiCall(Diff.class, "diff");
Diff diff = unmarshal(Diff.class, "diff");
assertTrue(compareJson(diff, "diff"));
} catch (Exception e) {
e.printStackTrace();
......@@ -188,7 +182,7 @@ public class TestGitLabApiBeans {
public void testComment() {
try {
Comment comment = makeFakeApiCall(Comment.class, "comment");
Comment comment = unmarshal(Comment.class, "comment");
assertTrue(compareJson(comment, "comment"));
} catch (Exception e) {
e.printStackTrace();
......@@ -199,7 +193,7 @@ public class TestGitLabApiBeans {
public void testEpic() {
try {
Epic epic = makeFakeApiCall(Epic.class, "epic");
Epic epic = unmarshal(Epic.class, "epic");
assertTrue(compareJson(epic, "epic"));
} catch (Exception e) {
e.printStackTrace();
......@@ -210,7 +204,7 @@ public class TestGitLabApiBeans {
public void testEpicIssue() {
try {
EpicIssue epicIssue = makeFakeApiCall(EpicIssue.class, "epic-issue");
EpicIssue epicIssue = unmarshal(EpicIssue.class, "epic-issue");
assertTrue(compareJson(epicIssue, "epic-issue"));
} catch (Exception e) {
e.printStackTrace();
......@@ -221,7 +215,7 @@ public class TestGitLabApiBeans {
public void testEvent() {
try {
Event event = makeFakeApiCall(Event.class, "event");
Event event = unmarshal(Event.class, "event");
assertTrue(compareJson(event, "event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -232,7 +226,7 @@ public class TestGitLabApiBeans {
public void testFileUpload() {
try {
FileUpload fileUpload = makeFakeApiCall(FileUpload.class, "file-upload");
FileUpload fileUpload = unmarshal(FileUpload.class, "file-upload");
assertTrue(compareJson(fileUpload, "file-upload"));
} catch (Exception e) {
e.printStackTrace();
......@@ -243,7 +237,7 @@ public class TestGitLabApiBeans {
public void testGroup() {
try {
Group group = makeFakeApiCall(Group.class, "group");
Group group = unmarshal(Group.class, "group");
assertTrue(compareJson(group, "group"));
} catch (Exception e) {
e.printStackTrace();
......@@ -254,7 +248,7 @@ public class TestGitLabApiBeans {
public void testHealthCheckInfo() {
try {
HealthCheckInfo healthCheck = makeFakeApiCall(HealthCheckInfo.class, "health-check");
HealthCheckInfo healthCheck = unmarshal(HealthCheckInfo.class, "health-check");
assertTrue(compareJson(healthCheck, "health-check"));
} catch (Exception e) {
e.printStackTrace();
......@@ -265,18 +259,74 @@ public class TestGitLabApiBeans {
public void testIssue() {
try {
Issue issue = makeFakeApiCall(Issue.class, "issue");
Issue issue = unmarshal(Issue.class, "issue");
assertTrue(compareJson(issue, "issue"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testCommitDiscussions() {
try {
List<Discussion> discussions = unmarshalList(Discussion.class, "commit-discussions");
assertTrue(compareJson(discussions, "commit-discussions"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testEpicDiscussions() {
try {
List<Discussion> discussions = unmarshalList(Discussion.class, "epic-discussions");
assertTrue(compareJson(discussions, "epic-discussions"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testIssueDiscussions() {
try {
List<Discussion> discussions = unmarshalList(Discussion.class, "issue-discussions");
assertTrue(compareJson(discussions, "issue-discussions"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testMergeRequestDiscussions() {
try {
List<Discussion> discussions = unmarshalList(Discussion.class, "merge-request-discussions");
assertTrue(compareJson(discussions, "merge-request-discussions"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSnippetDiscussions() {
try {
List<Discussion> discussions = unmarshalList(Discussion.class, "snippet-discussions");
assertTrue(compareJson(discussions, "snippet-discussions"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testPipeline() {
try {
Pipeline pipeline = makeFakeApiCall(Pipeline.class, "pipeline");
Pipeline pipeline = unmarshal(Pipeline.class, "pipeline");
assertTrue(compareJson(pipeline, "pipeline"));
} catch (Exception e) {
e.printStackTrace();
......@@ -287,7 +337,7 @@ public class TestGitLabApiBeans {
public void testJob() {
try {
Job job = makeFakeApiCall(Job.class, "job");
Job job = unmarshal(Job.class, "job");
assertTrue(compareJson(job, "job"));
} catch (Exception e) {
e.printStackTrace();
......@@ -298,12 +348,8 @@ public class TestGitLabApiBeans {
public void testDeployKeys() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("deploy-keys.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<DeployKey> deployKeys = objectMapper.readValue(reader, new TypeReference<List<DeployKey>>() {});
List<DeployKey> deployKeys = unmarshalList(DeployKey.class, "deploy-keys");
assertTrue(compareJson(deployKeys, "deploy-keys"));
} catch (Exception e) {
e.printStackTrace();
}
......@@ -313,7 +359,7 @@ public class TestGitLabApiBeans {
public void testArtifactsFile() {
try {
ArtifactsFile artifactFile = makeFakeApiCall(ArtifactsFile.class, "artifacts-file");
ArtifactsFile artifactFile = unmarshal(ArtifactsFile.class, "artifacts-file");
assertTrue(compareJson(artifactFile, "artifacts-file"));
} catch (Exception e) {
e.printStackTrace();
......@@ -324,21 +370,18 @@ public class TestGitLabApiBeans {
public void testProjectLanguages() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("project-languages.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
Map<String, Float> projectLanguages = objectMapper.readValue(reader, new TypeReference<Map<String, Float>>() {});
Map<String, Float> projectLanguages = JsonUtils.unmarshalMap(Float.class, "project-languages.json");
assertTrue(compareJson(projectLanguages, "project-languages"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testProjectUsers() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("project-users.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<ProjectUser> projectUsers = objectMapper.readValue(reader, new TypeReference<List<ProjectUser>>() {});
List<ProjectUser> projectUsers = unmarshalList(ProjectUser.class, "project-users");
assertTrue(compareJson(projectUsers, "project-users"));
} catch (Exception e) {
e.printStackTrace();
......@@ -349,7 +392,7 @@ public class TestGitLabApiBeans {
public void testProjectHook() {
try {
ProjectHook hook = makeFakeApiCall(ProjectHook.class, "hook");
ProjectHook hook = unmarshal(ProjectHook.class, "hook");
assertTrue(compareJson(hook, "hook"));
} catch (Exception e) {
e.printStackTrace();
......@@ -360,12 +403,8 @@ public class TestGitLabApiBeans {
public void testProjectEvents() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("project-events.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<Event> events = objectMapper.readValue(reader, new TypeReference<List<Event>>() {});
List<Event> events = unmarshalList(Event.class, "project-events");
assertTrue(compareJson(events, "project-events"));
} catch (Exception e) {
e.printStackTrace();
}
......@@ -375,7 +414,7 @@ public class TestGitLabApiBeans {
public void testProtectedBranch() {
try {
ProtectedBranch protectedBranch = makeFakeApiCall(ProtectedBranch.class, "protected-branch");
ProtectedBranch protectedBranch = unmarshal(ProtectedBranch.class, "protected-branch");
assertTrue(compareJson(protectedBranch, "protected-branch"));
} catch (Exception e) {
e.printStackTrace();
......@@ -386,7 +425,7 @@ public class TestGitLabApiBeans {
public void testPushRule() {
try {
PushRules pushRule = makeFakeApiCall(PushRules.class, "push-rule");
PushRules pushRule = unmarshal(PushRules.class, "push-rule");
assertTrue(compareJson(pushRule, "push-rule"));
} catch (Exception e) {
e.printStackTrace();
......@@ -397,7 +436,7 @@ public class TestGitLabApiBeans {
public void testRunnerDetail() {
try {
RunnerDetail runnerDetail = makeFakeApiCall(RunnerDetail.class, "runner-detail");
RunnerDetail runnerDetail = unmarshal(RunnerDetail.class, "runner-detail");
assertTrue(compareJson(runnerDetail, "runner-detail"));
} catch (Exception e) {
e.printStackTrace();
......@@ -408,10 +447,7 @@ public class TestGitLabApiBeans {
public void testAllRunners() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("all-runners.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<Runner> allRunners = objectMapper.readValue(reader, new TypeReference<List<Runner>>() {});
List<Runner> allRunners = unmarshalList(Runner.class, "all-runners");
assertTrue(compareJson(allRunners, "all-runners"));
} catch (Exception e) {
......@@ -423,7 +459,7 @@ public class TestGitLabApiBeans {
public void testJiraService() {
try {
JiraService jira = makeFakeApiCall(JiraService.class, "jira");
JiraService jira = unmarshal(JiraService.class, "jira");
assertTrue(compareJson(jira, "jira"));
} catch (Exception e) {
e.printStackTrace();
......@@ -434,7 +470,7 @@ public class TestGitLabApiBeans {
public void testKey() {
try {
Key key = makeFakeApiCall(Key.class, "key");
Key key = unmarshal(Key.class, "key");
assertTrue(compareJson(key, "key"));
} catch (Exception e) {
e.printStackTrace();
......@@ -445,7 +481,7 @@ public class TestGitLabApiBeans {
public void testMember() {
try {
Member member = makeFakeApiCall(Member.class, "member");
Member member = unmarshal(Member.class, "member");
assertTrue(compareJson(member, "member"));
} catch (Exception e) {
e.printStackTrace();
......@@ -456,7 +492,7 @@ public class TestGitLabApiBeans {
public void testMergeRequestApprovals() {
try {
MergeRequest mergeRequestApprovals = makeFakeApiCall(MergeRequest.class, "approvals");
MergeRequest mergeRequestApprovals = unmarshal(MergeRequest.class, "approvals");
assertTrue(compareJson(mergeRequestApprovals, "approvals"));
} catch (Exception e) {
e.printStackTrace();
......@@ -467,7 +503,7 @@ public class TestGitLabApiBeans {
public void testMergeRequest() {
try {
MergeRequest mergeRequest = makeFakeApiCall(MergeRequest.class, "merge-request");
MergeRequest mergeRequest = unmarshal(MergeRequest.class, "merge-request");
assertTrue(compareJson(mergeRequest, "merge-request"));
} catch (Exception e) {
e.printStackTrace();
......@@ -478,7 +514,7 @@ public class TestGitLabApiBeans {
public void testMilestone() {
try {
Milestone milestone = makeFakeApiCall(Milestone.class, "milestone");
Milestone milestone = unmarshal(Milestone.class, "milestone");
assertTrue(compareJson(milestone, "milestone"));
} catch (Exception e) {
e.printStackTrace();
......@@ -489,7 +525,7 @@ public class TestGitLabApiBeans {
public void testNote() {
try {
Note note = makeFakeApiCall(Note.class, "note");
Note note = unmarshal(Note.class, "note");
assertTrue(compareJson(note, "note"));
} catch (Exception e) {
e.printStackTrace();
......@@ -500,7 +536,7 @@ public class TestGitLabApiBeans {
public void testMergeRequestNote() {
try {
Note note = makeFakeApiCall(Note.class, "merge-request-note");
Note note = unmarshal(Note.class, "merge-request-note");
assertTrue(compareJson(note, "merge-request-note"));
} catch (Exception e) {
e.printStackTrace();
......@@ -511,7 +547,7 @@ public class TestGitLabApiBeans {
public void testNotificationSettings() {
try {
NotificationSettings settings = makeFakeApiCall(NotificationSettings.class, "notification-settings");
NotificationSettings settings = unmarshal(NotificationSettings.class, "notification-settings");
assertTrue(compareJson(settings, "notification-settings"));
} catch (Exception e) {
e.printStackTrace();
......@@ -522,7 +558,7 @@ public class TestGitLabApiBeans {
public void testProject() {
try {
Project project = makeFakeApiCall(Project.class, "project");
Project project = unmarshal(Project.class, "project");
assertTrue(compareJson(project, "project"));
} catch (Exception e) {
e.printStackTrace();
......@@ -533,7 +569,7 @@ public class TestGitLabApiBeans {
public void testProjectSnippet() {
try {
Snippet snippet = makeFakeApiCall(Snippet.class, "snippet");
Snippet snippet = unmarshal(Snippet.class, "snippet");
assertTrue(compareJson(snippet, "snippet"));
} catch (Exception e) {
e.printStackTrace();
......@@ -544,7 +580,7 @@ public class TestGitLabApiBeans {
public void testSession() {
try {
Session session = makeFakeApiCall(Session.class, "session");
Session session = unmarshal(Session.class, "session");
assertTrue(compareJson(session, "session"));
} catch (Exception e) {
e.printStackTrace();
......@@ -555,7 +591,7 @@ public class TestGitLabApiBeans {
public void testSlackService() {
try {
SlackService slackNotifications = makeFakeApiCall(SlackService.class, "slack-notifications");
SlackService slackNotifications = unmarshal(SlackService.class, "slack-notifications");
assertTrue(compareJson(slackNotifications, "slack-notifications"));
} catch (Exception e) {
e.printStackTrace();
......@@ -566,7 +602,7 @@ public class TestGitLabApiBeans {
public void testSystemHook() {
try {
SystemHook systemHook = makeFakeApiCall(SystemHook.class, "system-hook");
SystemHook systemHook = unmarshal(SystemHook.class, "system-hook");
assertTrue(compareJson(systemHook, "system-hook"));
} catch (Exception e) {
e.printStackTrace();
......@@ -577,7 +613,7 @@ public class TestGitLabApiBeans {
public void testTag() {
try {
Tag tag = makeFakeApiCall(Tag.class, "tag");
Tag tag = unmarshal(Tag.class, "tag");
assertTrue(compareJson(tag, "tag"));
} catch (Exception e) {
e.printStackTrace();
......@@ -588,7 +624,7 @@ public class TestGitLabApiBeans {
public void testSshKey() {
try {
SshKey sshKey = makeFakeApiCall(SshKey.class, "sshkey");
SshKey sshKey = unmarshal(SshKey.class, "sshkey");
assertTrue(compareJson(sshKey, "sshkey"));
} catch (Exception e) {
e.printStackTrace();
......@@ -599,12 +635,8 @@ public class TestGitLabApiBeans {
public void testTree() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("tree.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<TreeItem> tree = objectMapper.readValue(reader, new TypeReference<List<TreeItem>>() {});
List<TreeItem> tree = unmarshalList(TreeItem.class, "tree");
assertTrue(compareJson(tree, "tree"));
} catch (Exception e) {
e.printStackTrace();
}
......@@ -614,7 +646,7 @@ public class TestGitLabApiBeans {
public void testUser() {
try {
User user = makeFakeApiCall(User.class, "user");
User user = unmarshal(User.class, "user");
assertTrue(compareJson(user, "user"));
} catch (Exception e) {
e.printStackTrace();
......@@ -625,7 +657,7 @@ public class TestGitLabApiBeans {
public void testImpersonationToken() {
try {
ImpersonationToken token = makeFakeApiCall(ImpersonationToken.class, "impersonation-token");
ImpersonationToken token = unmarshal(ImpersonationToken.class, "impersonation-token");
assertTrue(compareJson(token, "impersonation-token"));
} catch (Exception e) {
e.printStackTrace();
......@@ -636,7 +668,7 @@ public class TestGitLabApiBeans {
public void testOauthToken() {
try {
OauthTokenResponse token = makeFakeApiCall(OauthTokenResponse.class, "oauth-token");
OauthTokenResponse token = unmarshal(OauthTokenResponse.class, "oauth-token");
assertTrue(compareJson(token, "oauth-token"));
} catch (Exception e) {
e.printStackTrace();
......@@ -647,7 +679,7 @@ public class TestGitLabApiBeans {
public void testProjectSystemHookEvent() {
try {
ProjectSystemHookEvent event = makeFakeApiCall(ProjectSystemHookEvent.class, "project-system-hook-event");
ProjectSystemHookEvent event = unmarshal(ProjectSystemHookEvent.class, "project-system-hook-event");
assertTrue(compareJson(event, "project-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -658,7 +690,7 @@ public class TestGitLabApiBeans {
public void testTeamMemberSystemHookEvent() {
try {
TeamMemberSystemHookEvent event = makeFakeApiCall(TeamMemberSystemHookEvent.class, "team-member-system-hook-event");
TeamMemberSystemHookEvent event = unmarshal(TeamMemberSystemHookEvent.class, "team-member-system-hook-event");
assertTrue(compareJson(event, "team-member-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -669,7 +701,7 @@ public class TestGitLabApiBeans {
public void testPushSystemHookEvent() {
try {
PushSystemHookEvent event = makeFakeApiCall(PushSystemHookEvent.class, "push-system-hook-event");
PushSystemHookEvent event = unmarshal(PushSystemHookEvent.class, "push-system-hook-event");
assertTrue(compareJson(event, "push-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -680,7 +712,7 @@ public class TestGitLabApiBeans {
public void testUserSystemHookEvent() {
try {
SystemHookEvent event = makeFakeApiCall(SystemHookEvent.class, "user-system-hook-event");
SystemHookEvent event = unmarshal(SystemHookEvent.class, "user-system-hook-event");
assertTrue(compareJson(event, "user-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -691,7 +723,7 @@ public class TestGitLabApiBeans {
public void testGroupSystemHookEvent() {
try {
SystemHookEvent event = makeFakeApiCall(SystemHookEvent.class, "group-system-hook-event");
SystemHookEvent event = unmarshal(SystemHookEvent.class, "group-system-hook-event");
assertTrue(compareJson(event, "group-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -702,7 +734,7 @@ public class TestGitLabApiBeans {
public void testGroupMemberSystemHookEvent() {
try {
SystemHookEvent event = makeFakeApiCall(SystemHookEvent.class, "group-member-system-hook-event");
SystemHookEvent event = unmarshal(SystemHookEvent.class, "group-member-system-hook-event");
assertTrue(compareJson(event, "group-member-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -713,7 +745,7 @@ public class TestGitLabApiBeans {
public void testTagPushSystemHookEvent() {
try {
SystemHookEvent event = makeFakeApiCall(SystemHookEvent.class, "tag-push-system-hook-event");
SystemHookEvent event = unmarshal(SystemHookEvent.class, "tag-push-system-hook-event");
assertTrue(compareJson(event, "tag-push-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -724,7 +756,7 @@ public class TestGitLabApiBeans {
public void testRepositorySystemHookEvent() {
try {
SystemHookEvent event = makeFakeApiCall(SystemHookEvent.class, "repository-system-hook-event");
SystemHookEvent event = unmarshal(SystemHookEvent.class, "repository-system-hook-event");
assertTrue(compareJson(event, "repository-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
......@@ -735,42 +767,10 @@ public class TestGitLabApiBeans {
public void testLabels() {
try {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream("labels.json"));
ObjectMapper objectMapper = jacksonJson.getContext(null);
List<Label> labels = objectMapper.readValue(reader, new TypeReference<List<Label>>() {});
List<Label> labels = unmarshalList(Label.class, "labels");
assertTrue(compareJson(labels, "labels"));
} catch (Exception e) {
e.printStackTrace();
}
}
private <T> T makeFakeApiCall(Class<T> returnType, String file) throws JsonParseException, JsonMappingException, IOException {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(file + ".json"));
ObjectMapper objectMapper = jacksonJson.getContext(returnType);
return (objectMapper.readValue(reader, returnType));
}
private <T> boolean compareJson(T apiObject, String file) throws IOException {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(file + ".json"));
String objectJson = jacksonJson.marshal(apiObject);
JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes());
JsonNode tree2 = jacksonJson.getObjectMapper().readTree(reader);
boolean sameJson = tree1.equals(tree2);
if (!sameJson) {
System.out.println("JSON did not match:");
sortedDump(tree1);
sortedDump(tree2);
}
return (sameJson);
}
private void sortedDump(final JsonNode node) throws JsonProcessingException {
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
System.out.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment