diff --git a/.eclipse_iplog b/.eclipse_iplog new file mode 100644 index 000000000..7fedb456b --- /dev/null +++ b/.eclipse_iplog @@ -0,0 +1,12 @@ +[project "technology.egit"] + name = EGit + license = Eclipse Public License v1.0 + +[review] + url = http://egit.eclipse.org/r/r/ + +[consumes "technology.jgit"] + name = JGit + version = 1.3.0 + license = Eclipse Distribution License v1.0 + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..57989b5e2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,44 @@ +Contributing +============ + +Please refer to the contributor guide for all the details: + +https://wiki.eclipse.org/EGit/GitHub/ContributorGuide + + +Reporting bugs +-------------- + +For anything other than small changes, it's a good idea to open a bug +report for it (in case one doesn't already exist). This gives others the +chance to give input and is useful for tracking. Create one here: + +https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EGit&component=GitHub + + +Submitting changes +------------------ + +We use Gerrit to review all changes by committers or contributors before +they are merged: + +https://git.eclipse.org/r/ + +Make sure you have an account and have set up the `commit-msg` hook +before committing. + +When committing your changes, see the contributor guide or other commits +on what your commit message should include. + +Run the following to push your change for review (with `username` +replaced by your Gerrit username): + + git push ssh://username@git.eclipse.org:29418/egit/egit-github.git HEAD:refs/for/master + +Add the link to the review as a comment on the bug report, so that +people coming from the bug report can find it. + +Then wait for someone to review your change. If there is something to be +corrected, amend your commit and push it again. + +Have fun :). diff --git a/README.md b/README.md new file mode 100644 index 000000000..28bfd9a01 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +Eclipse EGit Github Connector +============================= + +EGit Github Connector is an Eclipse plugin for working with Git repositories +hosted on Github. It is based on the JGit library, which is a Git implementation +in pure Java and integrates with EGit which is the Eclipse Git Team Provider. + +This package is licensed under the EPL. Please refer to the LICENSE file +for the complete license. + +Compatibility +------------- + +- In general, EGit supports at least the latest two Eclipse releases. + For details, please see https://wiki.eclipse.org/EGit/FAQ + +- Newer version of EGit may implement new functionality, remove + existing functions and change others without other notice than what + is written in the commit log and source files themselves. + +Support +------- + +Post question or comments to the egit-dev@eclipse.org mailing list. +You need to be subscribed to post, see here: + +https://dev.eclipse.org/mailman/listinfo/egit-dev + +Bugs are tracked in [Bugzilla](https://bugs.eclipse.org/bugs/buglist.cgi?bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&classification=Technology&component=GitHub&list_id=11227256&product=EGit&query_format=advanced) + +Create new bugs [here](https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EGit;component=GitHub) + + +Contributing +------------ + +**This project does not use pull requests.** +Pull requests to this GitHub repository will be ignored. + +Push patches to Gerrit at https://git.eclipse.org/r/egit/egit-github as explained +in the Contributor Guide: + +https://wiki.eclipse.org/EGit/GitHub/ContributorGuide + + +About Git +--------- + +More information about Git, its repository format, and the canonical +C based implementation can be obtained from the Git websites: + +http://git-scm.com/ diff --git a/org.eclipse.egit.github.core.tests/.classpath b/org.eclipse.egit.github.core.tests/.classpath new file mode 100644 index 000000000..b1dabee38 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.eclipse.egit.github.core.tests/.gitignore b/org.eclipse.egit.github.core.tests/.gitignore new file mode 100644 index 000000000..d567ba01e --- /dev/null +++ b/org.eclipse.egit.github.core.tests/.gitignore @@ -0,0 +1,2 @@ +bin +target diff --git a/org.eclipse.egit.github.core.tests/.project b/org.eclipse.egit.github.core.tests/.project new file mode 100644 index 000000000..707737606 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/.project @@ -0,0 +1,28 @@ + + + org.eclipse.egit.github.core.tests + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..24ed0404b --- /dev/null +++ b/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Wed May 18 07:04:44 CEST 2011 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.ui.prefs b/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 000000000..490a3e932 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,55 @@ +#Wed Sep 07 17:28:01 PDT 2011 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=false +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=false +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=false +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.make_local_variable_final=false +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=false +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_trailing_whitespaces=true +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=false +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/org.eclipse.egit.github.core.tests/META-INF/MANIFEST.MF b/org.eclipse.egit.github.core.tests/META-INF/MANIFEST.MF new file mode 100644 index 000000000..5b065f882 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/META-INF/MANIFEST.MF @@ -0,0 +1,21 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: %pluginName +Bundle-SymbolicName: org.eclipse.egit.github.core.tests +Bundle-Version: 4.1.0.qualifier +Bundle-Localization: plugin +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Import-Package: com.google.gson;version="[1.6.0,3.0.0]", + com.google.gson.reflect;version="[1.6.0,3.0.0)", + org.eclipse.egit.github.core;version="[4.1.0,4.2.0)", + org.eclipse.egit.github.core.client;version="[4.1.0,4.2.0)", + org.eclipse.egit.github.core.event;version="[4.1.0,4.2.0)", + org.eclipse.egit.github.core.service;version="[4.1.0,4.2.0)", + org.eclipse.egit.github.core.util;version="[4.1.0,4.2.0)", + org.mockito;version="[1.8.4,2.0.0)", + org.mockito.runners;version="[1.8.4,2.0.0)", + org.mockito.stubbing;version="[1.8.4,2.0.0)" +Require-Bundle: org.junit;bundle-version="[4.8.1,5.0.0)", + org.hamcrest;bundle-version="[1.1.0,2.0.0)", + org.objenesis;bundle-version="[1.0.0,2.0.0)" +Bundle-Vendor: %providerName diff --git a/org.eclipse.egit.github.core.tests/build.properties b/org.eclipse.egit.github.core.tests/build.properties new file mode 100644 index 000000000..aa1a00826 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.properties diff --git a/org.eclipse.egit.github.core.tests/plugin.properties b/org.eclipse.egit.github.core.tests/plugin.properties new file mode 100644 index 000000000..76c9f6f08 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/plugin.properties @@ -0,0 +1,2 @@ +pluginName=EGit GitHub API Tests +providerName=Eclipse EGit \ No newline at end of file diff --git a/org.eclipse.egit.github.core.tests/pom.xml b/org.eclipse.egit.github.core.tests/pom.xml new file mode 100644 index 000000000..11a5d6f8b --- /dev/null +++ b/org.eclipse.egit.github.core.tests/pom.xml @@ -0,0 +1,46 @@ + + + + + 4.0.0 + + + org.eclipse.mylyn.github + github-parent + 4.1.0-SNAPSHOT + + + org.eclipse.egit.github.core.tests + eclipse-test-plugin + + Eclipse EGit GitHub API Test Plug-in + + + + + org.eclipse.tycho + tycho-surefire-plugin + ${tycho-version} + + + + **/Test*.class + + false + false + org.eclipse.egit.github.core.tests + org.eclipse.egit.github.core.tests.AllHeadlessTests + + + + + diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java new file mode 100644 index 000000000..d6a2c49fa --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * Copyright (c) 2011 Red Hat and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * David Green - initial contribution + * Christian Trutz - initial contribution + * Chris Aniszczyk - initial contribution + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * All headless unit tests of GitHub Java API + */ +@RunWith(Suite.class) +@SuiteClasses({ ApplicationTest.class, // + AuthorizationTest.class, // + BlobTest.class, // + CollaboratorServiceTest.class, // + CommentTest.class, // + CommitCommentTest.class, // + CommitFileTest.class, // + CommitServiceTest.class, // + CommitStatsTest.class, // + CommitStatusTest.class, // + CommitTest.class, // + CommitUserTest.class, // + ContentsServiceTest.class, // + ContributorTest.class, // + DataServiceTest.class, // + DateFormatterTest.class, // + DateUtilTests.class, // + DeployKeyServiceTest.class, // + DownloadServiceTest.class, // + DownloadTest.class, // + DownloadResourceTest.class, // + EncodingUtilsTest.class, // + EventFormatterTest.class, // + EventPayloadTest.class, // + EventRepositoryTest.class, // + EventServiceTest.class, // + EventTest.class, // + FieldErrorTest.class, // + GistChangeStatusTest.class, // + GistFileTest.class, // + GistRevisionTest.class, // + GistServiceTest.class, // + GistTest.class, // + GitHubClientTest.class, // + GollumPageTest.class, // + GsonUtilsTest.class, // + IdTest.class, // + IssueEventTest.class, // + IssueServiceTest.class, // + IssueTest.class, // + KeyTest.class, // + LabelComparatorTest.class, // + LabelServiceTest.class, // + LabelTest.class, // + LanguageTest.class, // + MarkdownServiceTest.class, // + MergeStatusTest.class, // + MilestoneComparatorTest.class, // + MilestoneServiceTest.class, // + MilestoneTest.class, // + OAuthServiceTest.class, // + OrganizationServiceTest.class, // + PagedRequestTest.class, // + PullRequestMarkerTest.class, // + PullRequestServiceTest.class, // + PullRequestTest.class, // + ReferenceTest.class, // + RepositoryBranchTest.class, // + RepositoryCommitCompareTest.class, // + RepositoryCommitTest.class, // + RepositoryContentsTest.class, // + RepositoryHookResponseTest.class, // + RepositoryHookTest.class, // + RepositoryIdTest.class, // + RepositoryIssueTest.class, // + RepositoryServiceTest.class, // + RepositoryTagTest.class, // + RepositoryTest.class, // + RequestErrorTest.class, // + RequestExceptionTest.class, // + SearchIssueTest.class, // + SearchRepositoryTest.class, // + ShaResourceTest.class, // + TagTest.class, // + TeamServiceTest.class, // + TeamTest.class, // + TreeEntryTest.class, // + TreeTest.class, // + TypedResourceTest.class, // + UrlUtilsTest.class, // + UserPlanTest.class, // + UserServiceTest.class, // + UserTest.class, // + WatcherServiceTest.class // +}) +public class AllHeadlessTests { + +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java new file mode 100644 index 000000000..a8e62ec66 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java @@ -0,0 +1,43 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Application; +import org.junit.Test; + +/** + * Unit tests of {@link Application} + */ +public class ApplicationTest { + + /** + * Test default state of application + */ + @Test + public void defaultState() { + Application app = new Application(); + assertNull(app.getName()); + assertNull(app.getUrl()); + } + + /** + * Test updating application fields + */ + @Test + public void updateFields() { + Application app = new Application(); + assertEquals("name", app.setName("name").getName()); + assertEquals("url", app.setUrl("url").getUrl()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java new file mode 100644 index 000000000..1d79fc890 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java @@ -0,0 +1,65 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Collections; +import java.util.Date; + +import org.eclipse.egit.github.core.Application; +import org.eclipse.egit.github.core.Authorization; +import org.junit.Test; + +/** + * Unit tests of {@link Authorization} + */ +public class AuthorizationTest { + + /** + * Test default state of authorization + */ + @Test + public void defaultState() { + Authorization auth = new Authorization(); + assertNull(auth.getApp()); + assertNull(auth.getCreatedAt()); + assertEquals(0, auth.getId()); + assertNull(auth.getNote()); + assertNull(auth.getNoteUrl()); + assertNull(auth.getScopes()); + assertNull(auth.getToken()); + assertNull(auth.getUpdatedAt()); + assertNull(auth.getUrl()); + } + + /** + * Test updating application fields + */ + @Test + public void updateFields() { + Authorization auth = new Authorization(); + Application app = new Application(); + assertEquals(app, auth.setApp(app).getApp()); + assertEquals(new Date(2500), auth.setCreatedAt(new Date(2500)) + .getCreatedAt()); + assertEquals(123, auth.setId(123).getId()); + assertEquals("note", auth.setNote("note").getNote()); + assertEquals("noteUrl", auth.setNoteUrl("noteUrl").getNoteUrl()); + assertEquals(Collections.singletonList("repo"), + auth.setScopes(Collections.singletonList("repo")).getScopes()); + assertEquals("token", auth.setToken("token").getToken()); + assertEquals(new Date(8000), auth.setUpdatedAt(new Date(8000)) + .getUpdatedAt()); + assertEquals("url", auth.setUrl("url").getUrl()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/BlobTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/BlobTest.java new file mode 100644 index 000000000..8a9cbb142 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/BlobTest.java @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Blob; +import org.junit.Test; + +/** + * Unit tests of {@link Blob} class + */ +public class BlobTest { + + /** + * Test default state of blob + */ + @Test + public void defaultState() { + Blob blob = new Blob(); + assertNull(blob.getContent()); + assertNull(blob.getEncoding()); + } + + /** + * Test updating blob fields + */ + @Test + public void updateFields() { + Blob blob = new Blob(); + assertEquals("content123", blob.setContent("content123").getContent()); + assertEquals(Blob.ENCODING_UTF8, blob.setEncoding(Blob.ENCODING_UTF8) + .getEncoding()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CollaboratorServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CollaboratorServiceTest.java new file mode 100644 index 000000000..333b21d6e --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CollaboratorServiceTest.java @@ -0,0 +1,181 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; + +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.service.CollaboratorService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link CollaboratorService} + */ +@RunWith(MockitoJUnitRunner.class) +public class CollaboratorServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private CollaboratorService service; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(client).get(any(GitHubRequest.class)); + service = new CollaboratorService(client); + } + + /** + * Create service using default constructor + */ + @Test + public void constructor() { + assertNotNull(new CollaboratorService().getClient()); + } + + /** + * Get all collaborators for repository + * + * @throws IOException + */ + @Test + public void getCollaborators() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.getCollaborators(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/u/p/collaborators")); + verify(client).get(request); + } + + /** + * Check collaborator with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void isCollaboratorNullUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.isCollaborator(repo, null); + } + + /** + * Check collaborator with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void isCollaboratorEmptyUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.isCollaborator(repo, ""); + } + + /** + * Check collaborator + * + * @throws IOException + */ + @Test + public void isCollaborator() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.isCollaborator(repo, "collab"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/u/p/collaborators/collab"); + verify(client).get(request); + } + + /** + * Add collaborator + * + * @throws IOException + */ + @Test + public void addCollaborator() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.addCollaborator(repo, "collab"); + verify(client).put("/repos/u/p/collaborators/collab"); + } + + /** + * Add collaborator with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void addCollaboratorNullUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.addCollaborator(repo, null); + } + + /** + * Add collaborator with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void addCollaboratorEmptyUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.addCollaborator(repo, ""); + } + + /** + * Remove collaborator + * + * @throws IOException + */ + @Test + public void delete() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.removeCollaborator(repo, "collab"); + verify(client).delete("/repos/u/p/collaborators/collab"); + } + + /** + * Remove collaborator with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void removeCollaboratorNullUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.removeCollaborator(repo, null); + } + + /** + * Remove collaborator with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void removeCollaboratorEmptyUser() throws IOException { + RepositoryId repo = new RepositoryId("u", "p"); + service.removeCollaborator(repo, ""); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommentTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommentTest.java new file mode 100644 index 000000000..5ff0cfa74 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommentTest.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; + +import org.eclipse.egit.github.core.Comment; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link Comment} + */ +public class CommentTest { + + /** + * Test default state of comment + */ + @Test + public void defaultState() { + Comment comment = new Comment(); + assertNull(comment.getBody()); + assertNull(comment.getBodyHtml()); + assertNull(comment.getBodyText()); + assertNull(comment.getCreatedAt()); + assertEquals(0, comment.getId()); + assertNull(comment.getUpdatedAt()); + assertNull(comment.getUrl()); + assertNull(comment.getUser()); + } + + /** + * Test updating comment fields + */ + @Test + public void updateFields() { + Comment comment = new Comment(); + assertEquals("body", comment.setBody("body").getBody()); + assertEquals("", comment.setBodyHtml("").getBodyHtml()); + assertEquals("text", comment.setBodyText("text").getBodyText()); + assertEquals(new Date(1234), comment.setCreatedAt(new Date(1234)) + .getCreatedAt()); + assertEquals(100, comment.setId(100).getId()); + assertEquals(new Date(2345), comment.setUpdatedAt(new Date(2345)) + .getUpdatedAt()); + assertEquals("http", comment.setUrl("http").getUrl()); + User user = new User().setLogin("auser"); + assertEquals(user, comment.setUser(user).getUser()); + } + + /** + * Test non-mutable created at date + */ + @Test + public void getCreatedAtReferenceMutableObject() { + Comment comment = new Comment(); + comment.setCreatedAt(new Date(12345)); + comment.getCreatedAt().setTime(0); + assertTrue(comment.getCreatedAt().getTime() != 0); + } + + /** + * Test non-mutable updated at date + */ + @Test + public void getUpdatedAtReferenceMutableObject() { + Comment comment = new Comment(); + comment.setUpdatedAt(new Date(54321)); + comment.getUpdatedAt().setTime(0); + assertTrue(comment.getUpdatedAt().getTime() != 0); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentPayloadTest.java new file mode 100644 index 000000000..7dd56e823 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentPayloadTest.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.CommitComment; +import org.eclipse.egit.github.core.event.CommitCommentPayload; +import org.junit.Test; + +/** + * Unit tests of {@link CommitCommentPayload} + */ +public class CommitCommentPayloadTest { + + /** + * Test default state of CommitCommentPayload + */ + @Test + public void defaultState() { + CommitCommentPayload payload = new CommitCommentPayload(); + assertNull(payload.getComment()); + } + + /** + * Test updating CommitCommentPayload fields + */ + @Test + public void updateFields() { + CommitCommentPayload payload = new CommitCommentPayload(); + CommitComment comment = new CommitComment(); + comment.setBody("comment"); + assertEquals(comment, payload.setComment(comment).getComment()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentTest.java new file mode 100644 index 000000000..e61708c3e --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitCommentTest.java @@ -0,0 +1,65 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Date; + +import org.eclipse.egit.github.core.CommitComment; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link CommitComment} + */ +public class CommitCommentTest { + + /** + * Test default state of commit comment + */ + @Test + public void defaultState() { + CommitComment comment = new CommitComment(); + assertNull(comment.getBody()); + assertNull(comment.getCommitId()); + assertNull(comment.getCreatedAt()); + assertEquals(0, comment.getId()); + assertEquals(0, comment.getLine()); + assertNull(comment.getPath()); + assertEquals(0, comment.getPosition()); + assertNull(comment.getUpdatedAt()); + assertNull(comment.getUrl()); + assertNull(comment.getUser()); + } + + /** + * Test updating commit comment fields + */ + @Test + public void updateFields() { + CommitComment comment = new CommitComment(); + assertEquals("a body", comment.setBody("a body").getBody()); + assertEquals("123abc", comment.setCommitId("123abc").getCommitId()); + assertEquals(new Date(8000), comment.setCreatedAt(new Date(8000)) + .getCreatedAt()); + assertEquals(20, comment.setId(20).getId()); + assertEquals(12, comment.setLine(12).getLine()); + assertEquals("/a/path", comment.setPath("/a/path").getPath()); + assertEquals(4, comment.setPosition(4).getPosition()); + assertEquals(new Date(10000), comment.setUpdatedAt(new Date(10000)) + .getUpdatedAt()); + assertEquals("http://url", comment.setUrl("http://url").getUrl()); + User user = new User().setLogin("theuser"); + assertEquals(user, comment.setUser(user).getUser()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitFileTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitFileTest.java new file mode 100644 index 000000000..f3d653d58 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitFileTest.java @@ -0,0 +1,57 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.CommitFile; +import org.junit.Test; + +/** + * Unit tests of {@link CommitFile} + */ +public class CommitFileTest { + + /** + * Test default state of commit file + */ + @Test + public void defaultState() { + CommitFile file = new CommitFile(); + assertEquals(0, file.getAdditions()); + assertEquals(0, file.getChanges()); + assertEquals(0, file.getDeletions()); + assertNull(file.getBlobUrl()); + assertNull(file.getFilename()); + assertNull(file.getPatch()); + assertNull(file.getRawUrl()); + assertNull(file.getSha()); + assertNull(file.getStatus()); + } + + /** + * Test updating commit file fields + */ + @Test + public void updateFields() { + CommitFile file = new CommitFile(); + assertEquals(123, file.setAdditions(123).getAdditions()); + assertEquals(456, file.setChanges(456).getChanges()); + assertEquals(789, file.setDeletions(789).getDeletions()); + assertEquals("blob url", file.setBlobUrl("blob url").getBlobUrl()); + assertEquals("file.txt", file.setFilename("file.txt").getFilename()); + assertEquals("file.patch", file.setPatch("file.patch").getPatch()); + assertEquals("raw url", file.setRawUrl("raw url").getRawUrl()); + assertEquals("aaaaa", file.setSha("aaaaa").getSha()); + assertEquals("add", file.setStatus("add").getStatus()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitServiceTest.java new file mode 100644 index 000000000..1a378ebb3 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitServiceTest.java @@ -0,0 +1,470 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.egit.github.core.CommitComment; +import org.eclipse.egit.github.core.CommitStatus; +import org.eclipse.egit.github.core.RepositoryCommit; +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.client.PageIterator; +import org.eclipse.egit.github.core.service.CommitService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link CommitService} + */ +@RunWith(MockitoJUnitRunner.class) +public class CommitServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private CommitService service; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(client).get(any(GitHubRequest.class)); + service = new CommitService(client); + } + + /** + * Create service using default constructor + */ + @Test + public void constructor() { + assertNotNull(new CommitService().getClient()); + } + + /** + * Get commits + * + * @throws IOException + */ + @Test + public void getCommits() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getCommits(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/commits")); + verify(client).get(request); + } + + /** + * Page commits + */ + @Test + public void pageCommits() { + RepositoryId repo = new RepositoryId("o", "n"); + PageIterator iterator = service.pageCommits(repo); + assertNotNull(iterator); + assertEquals(Utils.page("/repos/o/n/commits"), iterator.getRequest() + .generateUri()); + assertTrue(iterator.hasNext()); + } + + /** + * Get commit with null sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommitNullSha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getCommit(repo, null); + } + + /** + * Get commit with empty sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommitEmptySha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getCommit(repo, ""); + } + + /** + * Get commit + * + * @throws IOException + */ + @Test + public void getCommit() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getCommit(repo, "abc"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/commits/abc"); + verify(client).get(request); + } + + /** + * Get comments with null sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullSha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getComments(repo, null); + } + + /** + * Get comments with empty sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsEmptySha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getComments(repo, ""); + } + + /** + * Get commit comments + * + * @throws IOException + */ + @Test + public void getCommitComments() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getComments(repo, "abc"); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/commits/abc/comments")); + verify(client).get(request); + } + + /** + * Get all commit comments + * + * @throws IOException + */ + @Test + public void getAllComments() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getComments(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/comments")); + verify(client).get(request); + } + + /** + * Get comment comment + * + * @throws IOException + */ + @Test + public void getComment() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getComment(repo, 33); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/comments/33"); + verify(client).get(request); + } + + /** + * Add comment with null sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void addCommentNullSha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitComment comment = new CommitComment(); + service.addComment(repo, null, comment); + } + + /** + * Add comment with empty sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void addCommentEmptySha() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitComment comment = new CommitComment(); + service.addComment(repo, "", comment); + } + + /** + * Add commit comment + * + * @throws IOException + */ + @Test + public void addComment() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitComment comment = new CommitComment(); + service.addComment(repo, "abcd", comment); + verify(client).post("/repos/o/n/commits/abcd/comments", comment, + CommitComment.class); + } + + /** + * Edit with null comment + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editCommentNullComment() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.editComment(repo, null); + } + + /** + * Edit commit comment + * + * @throws IOException + */ + @Test + public void editComment() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitComment comment = new CommitComment(); + comment.setId(56); + service.editComment(repo, comment); + verify(client).post("/repos/o/n/comments/56", comment, + CommitComment.class); + } + + /** + * Delete commit comment + * + * @throws IOException + */ + @Test + public void deleteComment() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.deleteComment(repo, 75); + verify(client).delete("/repos/o/n/comments/75"); + } + + /** + * Compare commits with null base + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void compareNullBase() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.compare(repo, null, "HEAD"); + } + + /** + * Compare commits with empty base + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void compareEmptyBase() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.compare(repo, "", "HEAD"); + } + + /** + * Compare commits with null head + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void compareNullHead() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.compare(repo, "HEAD~1", null); + } + + /** + * Compare commits empty head + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void compareEmptyHead() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.compare(repo, "HEAD~1", ""); + } + + /** + * Compare commits + * + * @throws IOException + */ + @Test + public void compare() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.compare(repo, "v1", "HEAD"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/compare/v1...HEAD"); + verify(client).get(request); + } + + /** + * Get statuses + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getStatusesNullRepository() throws IOException { + service.getStatuses(null, "123"); + } + + /** + * Get statuses + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getStatusesNullSha() throws IOException { + service.getStatuses(new RepositoryId("o", "n"), null); + } + + /** + * Get statuses + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getStatusesEmptySha() throws IOException { + service.getStatuses(new RepositoryId("o", "n"), ""); + } + + /** + * Get statuses + * + * @throws IOException + */ + @Test + public void getStatuses() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + service.getStatuses(repo, "123"); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/statuses/123")); + verify(client).get(request); + } + + /** + * Create status + * + * @throws IOException + */ + @Test + public void createStatus() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitStatus status = new CommitStatus(); + status.setDescription("description"); + status.setTargetUrl("http://target/url"); + status.setState(CommitStatus.STATE_SUCCESS); + status.setContext("context"); + service.createStatus(repo, "123", status); + Map params = new HashMap(); + params.put("description", status.getDescription()); + params.put("target_url", status.getTargetUrl()); + params.put("state", status.getState()); + params.put("context", status.getContext()); + verify(client).post("/repos/o/n/statuses/123", params, + CommitStatus.class); + } + + /** + * Create status + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createStatusNullRepository() throws IOException { + CommitStatus status = new CommitStatus(); + status.setDescription("description"); + status.setTargetUrl("http://target/url"); + status.setState(CommitStatus.STATE_SUCCESS); + status.setContext("context"); + service.createStatus(null, "123", status); + } + + /** + * Create status + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createStatusNullSha() throws IOException { + CommitStatus status = new CommitStatus(); + status.setDescription("description"); + status.setTargetUrl("http://target/url"); + status.setState(CommitStatus.STATE_SUCCESS); + status.setContext("context"); + service.createStatus(new RepositoryId("o", "n"), null, status); + } + + /** + * Create status + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createStatusEmptySha() throws IOException { + CommitStatus status = new CommitStatus(); + status.setDescription("description"); + status.setTargetUrl("http://target/url"); + status.setState(CommitStatus.STATE_SUCCESS); + status.setContext("context"); + service.createStatus(new RepositoryId("o", "n"), "", status); + } + + /** + * Create status + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createStatusNullStatus() throws IOException { + service.createStatus(new RepositoryId("o", "n"), "123", null); + } + + /** + * Create status + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createStatusInvalidState() throws IOException { + RepositoryId repo = new RepositoryId("o", "n"); + CommitStatus status = new CommitStatus(); + status.setState("non-existing-state"); + status.setDescription("description"); + status.setTargetUrl("http://target/url"); + status.setContext("context"); + service.createStatus(repo, "123", status); + } +} \ No newline at end of file diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatsTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatsTest.java new file mode 100644 index 000000000..f01ac5178 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatsTest.java @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.CommitStats; +import org.junit.Test; + +/** + * + */ +public class CommitStatsTest { + + /** + * Test default state of commit stats + */ + @Test + public void defaultState() { + CommitStats stats = new CommitStats(); + assertEquals(0, stats.getAdditions()); + assertEquals(0, stats.getDeletions()); + assertEquals(0, stats.getTotal()); + } + + /** + * Test updating commit stats fields + */ + @Test + public void updateFields() { + CommitStats stats = new CommitStats(); + assertEquals(10, stats.setAdditions(10).getAdditions()); + assertEquals(36, stats.setDeletions(36).getDeletions()); + assertEquals(123, stats.setTotal(123).getTotal()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatusTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatusTest.java new file mode 100644 index 000000000..37e393749 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitStatusTest.java @@ -0,0 +1,66 @@ +/****************************************************************************** + * Copyright (c) 2012, 2015 GitHub Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Date; + +import org.eclipse.egit.github.core.CommitStatus; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link CommitStatus} + */ +public class CommitStatusTest { + + /** + * Test default state of commit status + */ + @Test + public void defaultState() { + CommitStatus status = new CommitStatus(); + assertNull(status.getCreatedAt()); + assertNull(status.getCreator()); + assertNull(status.getContext()); + assertNull(status.getDescription()); + assertEquals(0, status.getId()); + assertNull(status.getState()); + assertNull(status.getTargetUrl()); + assertNull(status.getUpdatedAt()); + assertNull(status.getUrl()); + assertNull(status.getContext()); + } + + /** + * Test updating commit status fields + */ + @Test + public void updateFields() { + CommitStatus status = new CommitStatus(); + assertEquals(new Date(1234), status.setCreatedAt(new Date(1234)) + .getCreatedAt()); + User creator = new User().setId(1); + assertEquals(creator, status.setCreator(creator).getCreator()); + assertEquals("con/text", status.setContext("con/text").getContext()); + assertEquals("desc", status.setDescription("desc").getDescription()); + assertEquals(40, status.setId(40).getId()); + assertEquals("success", status.setState("success").getState()); + assertEquals("targetUrl", status.setTargetUrl("targetUrl") + .getTargetUrl()); + assertEquals(new Date(5678), status.setUpdatedAt(new Date(5678)) + .getUpdatedAt()); + assertEquals("url", status.setUrl("url").getUrl()); + assertEquals("context", status.setContext("context").getContext()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitTest.java new file mode 100644 index 000000000..22d7a34e0 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitTest.java @@ -0,0 +1,65 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; + +import org.eclipse.egit.github.core.Commit; +import org.eclipse.egit.github.core.CommitUser; +import org.eclipse.egit.github.core.Tree; +import org.junit.Test; + +/** + * Unit tests of {@link Commit} + */ +public class CommitTest { + + /** + * Test default state of commit + */ + @Test + public void defaultState() { + Commit commit = new Commit(); + assertNull(commit.getAuthor()); + assertNull(commit.getCommitter()); + assertNull(commit.getMessage()); + assertNull(commit.getParents()); + assertNull(commit.getSha()); + assertNull(commit.getTree()); + assertNull(commit.getUrl()); + assertEquals(0, commit.getCommentCount()); + } + + /** + * Test updating commit fields + */ + @Test + public void updateFields() { + Commit commit = new Commit(); + CommitUser author = new CommitUser().setName("Art Thor"); + assertEquals(author, commit.setAuthor(author).getAuthor()); + CommitUser committer = new CommitUser().setName("Comb Mitter"); + assertEquals(committer, commit.setCommitter(committer).getCommitter()); + assertEquals("commit message", commit.setMessage("commit message") + .getMessage()); + assertEquals(new ArrayList(), + commit.setParents(new ArrayList()).getParents()); + assertEquals("abcdef", commit.setSha("abcdef").getSha()); + Tree tree = new Tree(); + tree.setSha("12345"); + assertEquals(tree, commit.setTree(tree).getTree()); + assertEquals("url", commit.setUrl("url").getUrl()); + assertEquals(32, commit.setCommentCount(32).getCommentCount()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitUserTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitUserTest.java new file mode 100644 index 000000000..a84f8e691 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CommitUserTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Date; + +import org.eclipse.egit.github.core.CommitUser; +import org.junit.Test; + +/** + * Unit tests of {@link CommitUser} + */ +public class CommitUserTest { + + /** + * Test default state of commit user + */ + @Test + public void defaultState() { + CommitUser user = new CommitUser(); + assertNull(user.getDate()); + assertNull(user.getEmail()); + assertNull(user.getName()); + } + + /** + * Test updating commit user fields + */ + @Test + public void updateFields() { + CommitUser user = new CommitUser(); + assertEquals(new Date(123456), user.setDate(new Date(123456)).getDate()); + assertEquals("a@b.com", user.setEmail("a@b.com").getEmail()); + assertEquals("a user", user.setName("a user").getName()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContentsServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContentsServiceTest.java new file mode 100644 index 000000000..a17714468 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContentsServiceTest.java @@ -0,0 +1,116 @@ +/****************************************************************************** + * Copyright (c) 2012 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; + +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.service.ContentsService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link ContentsService} + */ +@RunWith(MockitoJUnitRunner.class) +public class ContentsServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private ContentsService service; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(client).get(any(GitHubRequest.class)); + service = new ContentsService(client); + } + + /** + * Get readme for null repository + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void getReadmeNullRepository() throws Exception { + service.getReadme(null); + } + + /** + * Get readme for repository + * + * @throws Exception + */ + @Test + public void getReadme() throws Exception { + RepositoryId repo = new RepositoryId("o", "n"); + service.getReadme(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/readme"); + verify(client).get(request); + } + + /** + * Get contents for null repository + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void getContentsNullRepository() throws Exception { + service.getContents(null); + } + + /** + * Get contents at root in repository + * + * @throws Exception + */ + @Test + public void getRootContents() throws Exception { + RepositoryId repo = new RepositoryId("o", "n"); + service.getContents(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/contents"); + verify(client).get(request); + } + + /** + * Get contents at path in repository + * + * @throws Exception + */ + @Test + public void getContents() throws Exception { + RepositoryId repo = new RepositoryId("o", "n"); + service.getContents(repo, "a/b"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/contents/a/b"); + verify(client).get(request); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContributorTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContributorTest.java new file mode 100644 index 000000000..c4492ff37 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ContributorTest.java @@ -0,0 +1,55 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Contributor; +import org.junit.Test; + +/** + * Unit tests of {@link Contributor} + */ +public class ContributorTest { + + /** + * Test default state of contributor + */ + @Test + public void defaultState() { + Contributor contributor = new Contributor(); + assertNull(contributor.getAvatarUrl()); + assertEquals(0, contributor.getContributions()); + assertEquals(0, contributor.getId()); + assertNull(contributor.getLogin()); + assertNull(contributor.getName()); + assertNull(contributor.getType()); + assertNull(contributor.getUrl()); + } + + /** + * Test updating contributor fields + */ + @Test + public void updateFields() { + Contributor contributor = new Contributor(); + assertEquals("aUrl", contributor.setAvatarUrl("aUrl").getAvatarUrl()); + assertEquals(10, contributor.setContributions(10).getContributions()); + assertEquals(4321, contributor.setId(4321).getId()); + assertEquals("user", contributor.setLogin("user").getLogin()); + assertEquals("U ser", contributor.setName("U ser").getName()); + assertEquals(Contributor.TYPE_ANONYMOUS, + contributor.setType(Contributor.TYPE_ANONYMOUS).getType()); + assertEquals("url", contributor.setUrl("url").getUrl()); + } + +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CreatePayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CreatePayloadTest.java new file mode 100644 index 000000000..e58c32a17 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/CreatePayloadTest.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.event.CreatePayload; +import org.junit.Test; + +/** + * Unit tests of {@link CreatePayload} + */ +public class CreatePayloadTest { + + /** + * Test default state of CreatePayload + */ + @Test + public void defaultState() { + CreatePayload payload = new CreatePayload(); + assertNull(payload.getRefType()); + assertNull(payload.getRef()); + assertNull(payload.getMasterBranch()); + assertNull(payload.getDescription()); + } + + /** + * Test updating CreatePayload fields + */ + @Test + public void updateFields() { + CreatePayload payload = new CreatePayload(); + assertEquals("branch", payload.setRefType("branch").getRefType()); + assertEquals("ref", payload.setRef("ref").getRef()); + assertEquals("master", payload.setMasterBranch("master").getMasterBranch()); + assertEquals("description", + payload.setDescription("description").getDescription()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DataServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DataServiceTest.java new file mode 100644 index 000000000..31bdc59ef --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DataServiceTest.java @@ -0,0 +1,432 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; + +import org.eclipse.egit.github.core.Blob; +import org.eclipse.egit.github.core.Commit; +import org.eclipse.egit.github.core.Reference; +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.ShaResource; +import org.eclipse.egit.github.core.Tag; +import org.eclipse.egit.github.core.Tree; +import org.eclipse.egit.github.core.TypedResource; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.service.DataService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link DataService} + */ +@RunWith(MockitoJUnitRunner.class) +public class DataServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private DataService service; + + private RepositoryId repo; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(client).get(any(GitHubRequest.class)); + service = new DataService(client); + repo = new RepositoryId("o", "n"); + } + + /** + * Create service using default constructor + */ + @Test + public void constructor() { + assertNotNull(new DataService().getClient()); + } + + /** + * Get blob with null sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getBlobNullSha() throws IOException { + service.getBlob(repo, null); + } + + /** + * Get blob with empty sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getBlobEmptySha() throws IOException { + service.getBlob(repo, ""); + } + + /** + * Get blob + * + * @throws IOException + */ + @Test + public void getBlob() throws IOException { + service.getBlob(repo, "aaa"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/git/blobs/aaa"); + verify(client).get(request); + } + + /** + * Create blob with null blob + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createBlobNullBlob() throws IOException { + service.createBlob(repo, null); + } + + /** + * Create blob + * + * @throws IOException + */ + @Test + public void createBlob() throws IOException { + Blob blob = new Blob().setContent("a"); + service.createBlob(repo, blob); + verify(client).post("/repos/o/n/git/blobs", blob, ShaResource.class); + } + + /** + * Get tree with null sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getTreeNullSha() throws IOException { + service.getTree(repo, null); + } + + /** + * Get tree with empty sha + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getTreeEmptySha() throws IOException { + service.getTree(repo, ""); + } + + /** + * Get tree + * + * @throws IOException + */ + @Test + public void getTree() throws IOException { + service.getTree(repo, "abc"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/git/trees/abc"); + verify(client).get(request); + } + + /** + * Create tree + * + * @throws IOException + */ + @Test + public void createTree() throws IOException { + service.createTree(repo, null); + verify(client).post("/repos/o/n/git/trees", + new HashMap(), Tree.class); + } + + /** + * Get reference with null name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getReferenceNullName() throws IOException { + service.getReference(repo, null); + } + + /** + * Get reference for empty name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getReferenceEmptyName() throws IOException { + service.getReference(repo, ""); + } + + /** + * Get reference + * + * @throws IOException + */ + @Test + public void getReference() throws IOException { + service.getReference(repo, "refs/heads/master"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/git/refs/heads/master"); + verify(client).get(request); + } + + /** + * Get references + * + * @throws IOException + */ + @Test + public void getReferences() throws IOException { + service.getReferences(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/git/refs")); + verify(client).get(request); + } + + /** + * Create reference with null reference + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createReferenceNullReference() throws IOException { + service.createReference(repo, null); + } + + /** + * Create reference with null object + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createReferenceNullObject() throws IOException { + service.createReference(repo, new Reference()); + } + + /** + * Create reference + * + * @throws IOException + */ + @Test + public void createReference() throws IOException { + Reference ref = new Reference(); + ref.setRef("refs/heads/master"); + TypedResource object = new TypedResource(); + object.setSha("abcdef"); + ref.setObject(object); + service.createReference(repo, ref); + verify(client).post(eq("/repos/o/n/git/refs"), any(), + eq(Reference.class)); + } + + /** + * Edit reference with null reference + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editReferenceNullReference() throws IOException { + service.editReference(repo, null); + } + + /** + * Edit reference with null object + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editReferenceNullObject() throws IOException { + service.editReference(repo, new Reference().setRef("a")); + } + + /** + * Edit reference with null name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editReferenceNullName() throws IOException { + service.editReference(repo, + new Reference().setObject(new TypedResource())); + } + + /** + * Edit reference with empty name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editReferenceEmptyName() throws IOException { + service.editReference(repo, + new Reference().setObject(new TypedResource()).setRef("")); + } + + /** + * Edit reference + * + * @throws IOException + */ + @Test + public void editReference() throws IOException { + Reference ref = new Reference(); + ref.setRef("refs/heads/master"); + TypedResource object = new TypedResource(); + object.setSha("00aa"); + ref.setObject(object); + service.editReference(repo, ref); + verify(client).post(eq("/repos/o/n/git/refs/heads/master"), any(), + eq(Reference.class)); + } + + /** + * Get commit with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommitNullId() throws IOException { + service.getCommit(repo, null); + } + + /** + * Get commit with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommitEmptyId() throws IOException { + service.getCommit(repo, ""); + } + + /** + * Get commit + * + * @throws IOException + */ + @Test + public void getCommit() throws IOException { + service.getCommit(repo, "ccc"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/git/commits/ccc"); + verify(client).get(request); + } + + /** + * Create commit with null commit + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommitNullCommit() throws IOException { + service.createCommit(repo, null); + } + + /** + * Create commit + * + * @throws IOException + */ + @Test + public void createCommit() throws IOException { + Commit commit = new Commit(); + commit.setParents(Collections.singletonList(new Commit().setSha("abcd"))); + commit.setTree(new Tree().setSha("aaa")); + service.createCommit(repo, commit); + verify(client).post(eq("/repos/o/n/git/commits"), any(), + eq(Commit.class)); + } + + /** + * Get tag with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getTagNullId() throws IOException { + service.getTag(repo, null); + } + + /** + * Get tag with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getTagEmptyId() throws IOException { + service.getTag(repo, ""); + } + + /** + * Get tag + * + * @throws IOException + */ + @Test + public void getTag() throws IOException { + service.getTag(repo, "abcdef"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/git/tags/abcdef"); + verify(client).get(request); + } + + /** + * Create tag with null tag + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createTagNullTag() throws IOException { + service.createTag(repo, null); + } + + /** + * Create tag + * + * @throws IOException + */ + @Test + public void createTag() throws IOException { + Tag tag = new Tag(); + tag.setObject(new TypedResource()); + service.createTag(repo, tag); + verify(client).post(eq("/repos/o/n/git/tags"), any(), eq(Tag.class)); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateFormatterTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateFormatterTest.java new file mode 100644 index 000000000..fa6fefae0 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateFormatterTest.java @@ -0,0 +1,55 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; + +import java.util.Date; + +import org.eclipse.egit.github.core.client.DateFormatter; +import org.junit.Test; + +/** + * Unit tests of {@link DateFormatter} + */ +public class DateFormatterTest { + + /** + * Verify serialized date returns value deserilized + */ + @Test + public void serializeDeserialize() { + DateFormatter formatter = new DateFormatter(); + Date date = new Date(10000); + JsonElement element = formatter.serialize(date, null, null); + assertNotNull(element); + String value = element.getAsString(); + assertNotNull(value); + assertTrue(value.length() > 0); + Date out = formatter.deserialize(element, null, null); + assertNotNull(out); + assertEquals(date.getTime(), out.getTime()); + } + + /** + * Deserialize empty string + */ + @Test(expected = JsonParseException.class) + public void emptyInput() { + new DateFormatter().deserialize(new JsonPrimitive(""), null, null); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateUtilTests.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateUtilTests.java new file mode 100644 index 000000000..9830a8078 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DateUtilTests.java @@ -0,0 +1,48 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; + +import java.util.Date; + +import org.eclipse.egit.github.core.util.DateUtils; +import org.junit.Test; + +/** + * Unit tests of {@link DateUtils} + */ +public class DateUtilTests { + + /** + * Test default constructor through anonymous sub-class + */ + @Test + public void constructor() { + assertNotNull(new DateUtils() { + }); + } + + /** + * Test cloning date + */ + @Test + public void cloneDate() { + assertNull(DateUtils.clone(null)); + assertEquals(new Date(1000), DateUtils.clone(new Date(1000))); + Date date = new Date(25000); + assertNotSame(date, DateUtils.clone(date)); + } + +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeletePayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeletePayloadTest.java new file mode 100644 index 000000000..ffed2d322 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeletePayloadTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.event.DeletePayload; +import org.junit.Test; + +/** + * Unit tests of {@link DeletePayload} + */ +public class DeletePayloadTest { + + /** + * Test default state of DeletePayload + */ + @Test + public void defaultState() { + DeletePayload payload = new DeletePayload(); + assertNull(payload.getRefType()); + assertNull(payload.getRef()); + } + + /** + * Test updating DeletePayload fields + */ + @Test + public void updateFields() { + DeletePayload payload = new DeletePayload(); + assertEquals("branch", payload.setRefType("branch").getRefType()); + assertEquals("ref", payload.setRef("ref").getRef()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeployKeyServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeployKeyServiceTest.java new file mode 100644 index 000000000..641f65c50 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DeployKeyServiceTest.java @@ -0,0 +1,138 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; + +import org.eclipse.egit.github.core.Key; +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.service.DeployKeyService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link DeployKeyService} + */ +@RunWith(MockitoJUnitRunner.class) +public class DeployKeyServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private DeployKeyService service; + + private RepositoryId repo; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(client).get(any(GitHubRequest.class)); + service = new DeployKeyService(client); + repo = new RepositoryId("o", "n"); + } + + /** + * Create service using default constructor + */ + @Test + public void constructor() { + assertNotNull(new DeployKeyService().getClient()); + } + + /** + * Get keys + * + * @throws IOException + */ + @Test + public void getKeys() throws IOException { + service.getKeys(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/keys")); + verify(client).get(request); + } + + /** + * Get key + * + * @throws IOException + */ + @Test + public void getKey() throws IOException { + service.getKey(repo, 40); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/keys/40"); + verify(client).get(request); + } + + /** + * Create key + * + * @throws IOException + */ + @Test + public void createKey() throws IOException { + Key key = new Key(); + service.createKey(repo, key); + verify(client).post("/repos/o/n/keys", key, Key.class); + } + + /** + * Edit key with null key + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editKeyNullKey() throws IOException { + service.editKey(repo, null); + } + + /** + * Edit key + * + * @throws IOException + */ + @Test + public void editKey() throws IOException { + Key key = new Key().setId(8); + service.editKey(repo, key); + verify(client).post("/repos/o/n/keys/8", key, Key.class); + } + + /** + * Delete key + * + * @throws IOException + */ + @Test + public void deleteKey() throws IOException { + service.deleteKey(repo, 88); + verify(client).delete("/repos/o/n/keys/88"); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadPayloadTest.java new file mode 100644 index 000000000..97f517b6f --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadPayloadTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Download; +import org.eclipse.egit.github.core.event.DownloadPayload; +import org.junit.Test; + +/** + * Unit tests of {@link DownloadPayload} + */ +public class DownloadPayloadTest { + + /** + * Test default state of DownloadPayload + */ + @Test + public void defaultState() { + DownloadPayload payload = new DownloadPayload(); + assertNull(payload.getDownload()); + } + + /** + * Test updating DownloadPayload fields + */ + @Test + public void updateFields() { + DownloadPayload payload = new DownloadPayload(); + Download download = new Download().setName("download"); + assertEquals(download, payload.setDownload(download).getDownload()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadResourceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadResourceTest.java new file mode 100644 index 000000000..3296bf6b0 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadResourceTest.java @@ -0,0 +1,81 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; + +import org.eclipse.egit.github.core.DownloadResource; +import org.junit.Test; + +/** + * Unit tests of {@link DownloadResource} + */ +public class DownloadResourceTest { + + /** + * Test default state of download resource + */ + @Test + public void defaultState() { + DownloadResource resource = new DownloadResource(); + assertFalse(resource.isRedirect()); + assertNull(resource.getAccesskeyid()); + assertNull(resource.getAcl()); + assertNull(resource.getDescription()); + assertEquals(0, resource.getDownloadCount()); + assertNull(resource.getExpirationdate()); + assertNull(resource.getHtmlUrl()); + assertEquals(0, resource.getId()); + assertNull(resource.getMimeType()); + assertNull(resource.getName()); + assertNull(resource.getPath()); + assertNull(resource.getPolicy()); + assertNull(resource.getPrefix()); + assertNull(resource.getS3Url()); + assertNull(resource.getSignature()); + assertEquals(0, resource.getSize()); + assertNull(resource.getUrl()); + } + + /** + * Test updating download resource fields + */ + @Test + public void updateFields() { + DownloadResource resource = new DownloadResource(); + assertEquals("abc", resource.setAccesskeyid("abc").getAccesskeyid()); + assertEquals("group", resource.setAcl("group").getAcl()); + assertEquals("a download", resource.setDescription("a download") + .getDescription()); + assertEquals(5, resource.setDownloadCount(5).getDownloadCount()); + assertEquals(new Date(2500), resource.setExpirationdate(new Date(2500)) + .getExpirationdate()); + assertEquals("download.html", resource.setHtmlUrl("download.html") + .getHtmlUrl()); + assertEquals(58, resource.setId(58).getId()); + assertEquals("text/plain", resource.setMimeType("text/plain") + .getMimeType()); + assertEquals("download.jar", resource.setName("download.jar").getName()); + assertEquals("/a/b", resource.setPath("/a/b").getPath()); + assertEquals("ro", resource.setPolicy("ro").getPolicy()); + assertEquals("s3", resource.setPrefix("s3").getPrefix()); + assertEquals("/s/3", resource.setS3Url("/s/3").getS3Url()); + assertEquals("1a2b", resource.setSignature("1a2b").getSignature()); + assertEquals(12345, resource.setSize(12345).getSize()); + assertEquals("/path/dl", resource.setUrl("/path/dl").getUrl()); + assertTrue(resource.setRedirect(true).isRedirect()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadServiceTest.java new file mode 100644 index 000000000..57e1790d3 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadServiceTest.java @@ -0,0 +1,139 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.eclipse.egit.github.core.Download; +import org.eclipse.egit.github.core.DownloadResource; +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.service.DownloadService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link DownloadService} + */ +@RunWith(MockitoJUnitRunner.class) +public class DownloadServiceTest { + + @Mock + private GitHubClient client; + + @Mock + private GitHubResponse response; + + private RepositoryId repo; + + private DownloadService service; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + service = new DownloadService(client); + doReturn(response).when(client).get(any(GitHubRequest.class)); + repo = new RepositoryId("o", "n"); + } + + /** + * Create service using default constructor + */ + @Test + public void constructor() { + assertNotNull(new DownloadService().getClient()); + } + + /** + * Get download + * + * @throws IOException + */ + @Test + public void getDownload() throws IOException { + service.getDownload(repo, 3); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/o/n/downloads/3"); + verify(client).get(request); + } + + /** + * Get downloads + * + * @throws IOException + */ + @Test + public void getDownloads() throws IOException { + service.getDownloads(repo); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/o/n/downloads")); + verify(client).get(request); + } + + /** + * Create download resource + * + * @throws IOException + */ + @Test + public void createResource() throws IOException { + Download download = new Download().setName("dl.txt"); + service.createResource(repo, download); + verify(client).post("/repos/o/n/downloads", download, + DownloadResource.class); + } + + /** + * Delete downloads + * + * @throws IOException + */ + @Test + public void deleteDownload() throws IOException { + service.deleteDownload(repo, 49); + verify(client).delete("/repos/o/n/downloads/49"); + } + + /** + * Upload resource with null resource + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void uploadResourceNullResource() throws IOException { + service.uploadResource(null, new ByteArrayInputStream(new byte[0]), 1); + } + + /** + * Upload resource with null stream + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void uploadResourceNullStream() throws IOException { + service.uploadResource(new DownloadResource(), null, 1); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadTest.java new file mode 100644 index 000000000..5399f87ca --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/DownloadTest.java @@ -0,0 +1,58 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Download; +import org.junit.Test; + +/** + * Unit tests of {@link Download} + */ +public class DownloadTest { + + /** + * Test default state of download resource + */ + @Test + public void defaultState() { + Download dl = new Download(); + assertNull(dl.getDescription()); + assertEquals(0, dl.getDownloadCount()); + assertNull(dl.getHtmlUrl()); + assertEquals(0, dl.getId()); + assertNull(dl.getName()); + assertEquals(0, dl.getSize()); + assertNull(dl.getUrl()); + assertNull(dl.getContentType()); + } + + /** + * Test updating download resource fields + */ + @Test + public void updateFields() { + Download dl = new Download(); + assertEquals("a download", dl.setDescription("a download") + .getDescription()); + assertEquals(5, dl.setDownloadCount(5).getDownloadCount()); + assertEquals("download.html", dl.setHtmlUrl("download.html") + .getHtmlUrl()); + assertEquals(58, dl.setId(58).getId()); + assertEquals("download.jar", dl.setName("download.jar").getName()); + assertEquals(12345, dl.setSize(12345).getSize()); + assertEquals("/path/dl", dl.setUrl("/path/dl").getUrl()); + assertEquals("text/plain", dl.setContentType("text/plain").getContentType()); + } + +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EncodingUtilsTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EncodingUtilsTest.java new file mode 100644 index 000000000..5b0b6da4b --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EncodingUtilsTest.java @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import org.eclipse.egit.github.core.util.EncodingUtils; +import org.junit.Test; + +/** + * Unit tests of {@link EncodingUtils} + */ +public class EncodingUtilsTest { + + /** + * Test default constructor through anonymous sub-class + */ + @Test + public void constructor() { + assertNotNull(new EncodingUtils() { + }); + } + + /** + * Encode and decode content + */ + @Test + public void encodeDecode() { + String test = "content"; + String encoded = EncodingUtils.toBase64(test.getBytes()); + assertNotNull(encoded); + assertFalse(encoded.length() == 0); + assertFalse(test.equals(encoded)); + byte[] decoded = EncodingUtils.fromBase64(new String(encoded)); + assertNotNull(decoded); + assertFalse(decoded.length == 0); + assertEquals(test, new String(decoded)); + } + +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java new file mode 100644 index 000000000..0777f58d4 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventFormatterTest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.eclipse.egit.github.core.event.Event.TYPE_FOLLOW; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.client.EventFormatter; +import org.eclipse.egit.github.core.client.GsonUtils; +import org.eclipse.egit.github.core.event.Event; +import org.eclipse.egit.github.core.event.EventPayload; +import org.eclipse.egit.github.core.event.FollowPayload; +import org.junit.Test; + +/** + * Unit tests of {@link EventFormatter} + */ +public class EventFormatterTest { + + /** + * Follow event payload returned as {@link FollowPayload} + */ + @Test + public void followPayload() { + Event event = GsonUtils.fromJson("{\"type\":\"" + TYPE_FOLLOW + + "\",\"payload\":{}}", Event.class); + assertNotNull(event); + assertNotNull(event.getPayload()); + assertEquals(FollowPayload.class, event.getPayload().getClass()); + } + + /** + * Unknown event payload returned as {@link EventPayload} + */ + @Test + public void unknownPayload() { + Event event = GsonUtils.fromJson( + "{\"type\":\"NotAnEventType\",\"payload\":{}}", Event.class); + assertNotNull(event); + assertNotNull(event.getPayload()); + assertEquals(EventPayload.class, event.getPayload().getClass()); + } + + /** + * Event with missing type has payload returned as {@link EventPayload} + */ + @Test + public void missingType() { + Event event = GsonUtils.fromJson("{\"payload\":{}}", Event.class); + assertNotNull(event); + assertNotNull(event.getPayload()); + assertEquals(EventPayload.class, event.getPayload().getClass()); + } + + /** + * Missing payload + */ + @Test + public void missingPayload() { + Event event = GsonUtils.fromJson("{}", Event.class); + assertNotNull(event); + assertNull(event.getPayload()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventPayloadTest.java new file mode 100644 index 000000000..09eb9149c --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventPayloadTest.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * Tests for all subclasses of EventPayload + */ +@RunWith(Suite.class) +@SuiteClasses({ CommitCommentPayloadTest.class, // + CreatePayloadTest.class, // + DeletePayloadTest.class, // + DownloadPayloadTest.class, // + FollowPayloadTest.class, // + ForkApplyPayloadTest.class, // + ForkPayloadTest.class, // + GistPayloadTest.class, // + GollumPayloadTest.class, // + IssueCommentPayloadTest.class, // + IssuesPayloadTest.class, // + MemberPayloadTest.class, // + PullRequestPayloadTest.class, // + PullRequestReviewCommentPayloadTest.class, // + PushPayloadTest.class, // + TeamAddPayloadTest.class, // + WatchPayloadTest.class }) +public class EventPayloadTest { + // EventPayload is an empty class, no tests to run. +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventRepositoryTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventRepositoryTest.java new file mode 100644 index 000000000..847e0e45e --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventRepositoryTest.java @@ -0,0 +1,45 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.event.EventRepository; +import org.junit.Test; + +/** + * Unit tests of {@link EventRepository} + */ +public class EventRepositoryTest { + + /** + * Test default state of event repository + */ + @Test + public void defaultState() { + EventRepository repo = new EventRepository(); + assertEquals(0, repo.getId()); + assertNull(repo.getName()); + assertNull(repo.getUrl()); + } + + /** + * Test updating event repository fields + */ + @Test + public void updateFields() { + EventRepository repo = new EventRepository(); + assertEquals(4, repo.setId(4).getId()); + assertEquals("repo1", repo.setName("repo1").getName()); + assertEquals("url", repo.setUrl("url").getUrl()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventServiceTest.java new file mode 100644 index 000000000..c0269c909 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventServiceTest.java @@ -0,0 +1,202 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; + +import java.io.IOException; + +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.client.PageIterator; +import org.eclipse.egit.github.core.event.Event; +import org.eclipse.egit.github.core.service.EventService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link EventService} + */ +@RunWith(MockitoJUnitRunner.class) +public class EventServiceTest { + + @Mock + private GitHubClient gitHubClient; + + @Mock + private GitHubResponse response; + + private EventService eventService; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(gitHubClient).get(any(GitHubRequest.class)); + eventService = new EventService(gitHubClient); + } + + /** + * Create service with null client + */ + @Test(expected = IllegalArgumentException.class) + public void constructorNullArgument() { + new EventService(null); + } + + /** + * Create default service + */ + @Test + public void defaultConstructor() { + assertNotNull(new EventService().getClient()); + } + + /** + * Page public events + * + * @throws IOException + */ + @Test + public void pagePublicEvents() throws IOException { + PageIterator iterator = eventService.pagePublicEvents(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/events"), iterator + .getRequest().generateUri()); + } + + /** + * Page events for repository + * + * @throws IOException + */ + @Test + public void pageRepsitoryEvents() throws IOException { + RepositoryId repo = new RepositoryId("user", "repo"); + PageIterator iterator = eventService.pageEvents(repo); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/repos/user/repo/events"), iterator + .getRequest().generateUri()); + } + + /** + * Page events for network of repositories + * + * @throws IOException + */ + @Test + public void pageNetworkEvents() throws IOException { + RepositoryId repo = new RepositoryId("user", "repo"); + PageIterator iterator = eventService.pageNetworkEvents(repo); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/networks/user/repo/events"), iterator + .getRequest().generateUri()); + } + + /** + * Page events for org + * + * @throws IOException + */ + @Test + public void pageOrgEvents() throws IOException { + PageIterator iterator = eventService.pageOrgEvents("org"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/orgs/org/events"), iterator + .getRequest().generateUri()); + } + + /** + * Page received events for user + * + * @throws IOException + */ + @Test + public void pageUserReceivedEvents() throws IOException { + PageIterator iterator = eventService.pageUserReceivedEvents("user"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/received_events"), iterator + .getRequest().generateUri()); + } + + /** + * Page public received events for user + * + * @throws IOException + */ + @Test + public void pagePublicUserReceivedEvents() throws IOException { + PageIterator iterator = eventService.pageUserReceivedEvents("user", true); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/received_events/public"), iterator + .getRequest().generateUri()); + } + + /** + * Page events for user + * + * @throws IOException + */ + @Test + public void pageUserEvents() throws IOException { + PageIterator iterator = eventService.pageUserEvents("user"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/events"), iterator + .getRequest().generateUri()); + } + + /** + * Page public events for user + * + * @throws IOException + */ + @Test + public void pagePublicUserEvents() throws IOException { + PageIterator iterator = eventService.pageUserEvents("user", true); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/events/public"), iterator + .getRequest().generateUri()); + } + + /** + * Page org events for user + * + * @throws IOException + */ + @Test + public void pageUserOrgEvents() throws IOException { + PageIterator iterator = eventService.pageUserOrgEvents("user", "org"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/events/orgs/org"), iterator + .getRequest().generateUri()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventTest.java new file mode 100644 index 000000000..c1a6ab7ec --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/EventTest.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; + +import org.eclipse.egit.github.core.User; +import org.eclipse.egit.github.core.event.Event; +import org.eclipse.egit.github.core.event.EventPayload; +import org.eclipse.egit.github.core.event.EventRepository; +import org.junit.Test; + +/** + * Unit tests of {@link Event} + */ +public class EventTest { + + /** + * Test default state of event + */ + @Test + public void defaultState() { + Event event = new Event(); + assertNull(event.getType()); + assertNull(event.getPayload()); + assertNull(event.getRepo()); + assertNull(event.getActor()); + assertNull(event.getOrg()); + assertNull(event.getCreatedAt()); + assertFalse(event.isPublic()); + assertNull(event.getId()); + } + + /** + * Test updating event fields + */ + @Test + public void updateFields() { + Event event = new Event(); + assertEquals("PushEvent", event.setType("PushEvent").getType()); + EventPayload payload = new EventPayload(); + assertEquals(payload, event.setPayload(payload).getPayload()); + EventRepository repo = new EventRepository().setName("repo"); + assertEquals(repo, event.setRepo(repo).getRepo()); + User actor = new User().setLogin("actor"); + assertEquals(actor, event.setActor(actor).getActor()); + User org = new User().setLogin("org"); + assertEquals(org, event.setOrg(org).getOrg()); + assertEquals(new Date(5000), event.setCreatedAt(new Date(5000)) + .getCreatedAt()); + assertTrue(event.setPublic(true).isPublic()); + assertEquals("123", event.setId("123").getId()); + } + + /** + * Test non-mutable created at date + */ + @Test + public void getCreatedAtReferenceMutableObject() { + Event event = new Event(); + event.setCreatedAt(new Date(11111)); + event.getCreatedAt().setTime(0); + assertTrue(event.getCreatedAt().getTime() != 0); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FieldErrorTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FieldErrorTest.java new file mode 100644 index 000000000..4458c521d --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FieldErrorTest.java @@ -0,0 +1,49 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.FieldError; +import org.junit.Test; + +/** + * Unit tests of {@link FieldError} + */ +public class FieldErrorTest { + + /** + * Test default state of field error + */ + @Test + public void defaultState() { + FieldError error = new FieldError(); + assertNull(error.getCode()); + assertNull(error.getField()); + assertNull(error.getMessage()); + assertNull(error.getResource()); + assertNull(error.getValue()); + } + + /** + * Test updating field error fields + */ + @Test + public void updateFields() { + FieldError error = new FieldError(); + assertEquals("invalid", error.setCode("invalid").getCode()); + assertEquals("name", error.setField("name").getField()); + assertEquals("commit", error.setResource("commit").getResource()); + assertEquals("message", error.setMessage("message").getMessage()); + assertEquals("-1", error.setValue("-1").getValue()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FollowPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FollowPayloadTest.java new file mode 100644 index 000000000..567f5de7b --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/FollowPayloadTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.User; +import org.eclipse.egit.github.core.event.FollowPayload; +import org.junit.Test; + +/** + * Unit tests of {@link FollowPayload} + */ +public class FollowPayloadTest { + + /** + * Test default state of FollowPayload + */ + @Test + public void defaultState() { + FollowPayload payload = new FollowPayload(); + assertNull(payload.getTarget()); + } + + /** + * Test updating FollowPayload fields + */ + @Test + public void updateFields() { + FollowPayload payload = new FollowPayload(); + User target = new User().setName("target"); + assertEquals(target, payload.setTarget(target).getTarget()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkApplyPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkApplyPayloadTest.java new file mode 100644 index 000000000..bfc4c913b --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkApplyPayloadTest.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.event.ForkApplyPayload; +import org.junit.Test; + +/** + * Unit tests of {@link ForkApplyPayload} + */ +public class ForkApplyPayloadTest { + + /** + * Test default state of ForkApplyPayload + */ + @Test + public void defaultState() { + ForkApplyPayload payload = new ForkApplyPayload(); + assertNull(payload.getHead()); + assertNull(payload.getBefore()); + assertNull(payload.getAfter()); + } + + /** + * Test updating ForkApplyPayload fields + */ + @Test + public void updateFields() { + ForkApplyPayload payload = new ForkApplyPayload(); + assertEquals("head", payload.setHead("head").getHead()); + assertEquals("000", payload.setBefore("000").getBefore()); + assertEquals("001", payload.setAfter("001").getAfter()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkPayloadTest.java new file mode 100644 index 000000000..9e68251f1 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ForkPayloadTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Repository; +import org.eclipse.egit.github.core.event.ForkPayload; +import org.junit.Test; + +/** + * Unit tests of {@link ForkPayload} + */ +public class ForkPayloadTest { + + /** + * Test default state of ForkPayload + */ + @Test + public void defaultState() { + ForkPayload payload = new ForkPayload(); + assertNull(payload.getForkee()); + } + + /** + * Test updating ForkPayload fields + */ + @Test + public void updateFields() { + ForkPayload payload = new ForkPayload(); + Repository forkee = new Repository().setDescription("forkee"); + assertEquals(forkee, payload.setForkee(forkee).getForkee()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistChangeStatusTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistChangeStatusTest.java new file mode 100644 index 000000000..43acc81c6 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistChangeStatusTest.java @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.GistChangeStatus; +import org.junit.Test; + +/** + * Unit tests of {@link GistChangeStatus} + */ +public class GistChangeStatusTest { + + /** + * Test default state of gist change status + */ + @Test + public void defaultState() { + GistChangeStatus change = new GistChangeStatus(); + assertEquals(0, change.getAdditions()); + assertEquals(0, change.getDeletions()); + assertEquals(0, change.getTotal()); + } + + /** + * Test updating gist change status fields + */ + @Test + public void updateFields() { + GistChangeStatus change = new GistChangeStatus(); + assertEquals(50, change.setAdditions(50).getAdditions()); + assertEquals(200, change.setDeletions(200).getDeletions()); + assertEquals(123, change.setTotal(123).getTotal()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistFileTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistFileTest.java new file mode 100644 index 000000000..d14f8edeb --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistFileTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.GistFile; +import org.junit.Test; + +/** + * Unit tests of {@link GistFile} + */ +public class GistFileTest { + + /** + * Test default state of gist file + */ + @Test + public void defaultState() { + GistFile file = new GistFile(); + assertEquals(0, file.getSize()); + assertNull(file.getContent()); + assertNull(file.getRawUrl()); + assertNull(file.getFilename()); + } + + /** + * Test updating gist file fields + */ + @Test + public void updateFields() { + GistFile file = new GistFile(); + assertEquals(100, file.setSize(100).getSize()); + assertEquals("content", file.setContent("content").getContent()); + assertEquals("rawUrl", file.setRawUrl("rawUrl").getRawUrl()); + assertEquals("name", file.setFilename("name").getFilename()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistPayloadTest.java new file mode 100644 index 000000000..eb5e13752 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistPayloadTest.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.Gist; +import org.eclipse.egit.github.core.event.GistPayload; +import org.junit.Test; + +/** + * Unit tests of {@link GistPayload} + */ +public class GistPayloadTest { + + /** + * Test default state of GistPayload + */ + @Test + public void defaultState() { + GistPayload payload = new GistPayload(); + assertNull(payload.getAction()); + assertNull(payload.getGist()); + } + + /** + * Test updating GistPayload fields + */ + @Test + public void updateFields() { + GistPayload payload = new GistPayload(); + Gist gist = new Gist().setId("id"); + assertEquals("create", payload.setAction("create").getAction()); + assertEquals(gist, payload.setGist(gist).getGist()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistRevisionTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistRevisionTest.java new file mode 100644 index 000000000..a7c0feec4 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistRevisionTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; + +import org.eclipse.egit.github.core.GistChangeStatus; +import org.eclipse.egit.github.core.GistRevision; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link GistRevision} + */ +public class GistRevisionTest { + + /** + * Test default state of gist revision + */ + @Test + public void defaultState() { + GistRevision revision = new GistRevision(); + assertNull(revision.getChangeStatus()); + assertNull(revision.getCommittedAt()); + assertNull(revision.getUrl()); + assertNull(revision.getUser()); + assertNull(revision.getVersion()); + } + + /** + * Test updating fields of a gist revision + */ + @Test + public void updateFields() { + GistRevision revision = new GistRevision(); + GistChangeStatus status = new GistChangeStatus(); + assertEquals(status, revision.setChangeStatus(status).getChangeStatus()); + assertEquals(new Date(5000), revision.setCommittedAt(new Date(5000)) + .getCommittedAt()); + assertEquals("url", revision.setUrl("url").getUrl()); + User user = new User().setLogin("testuser"); + assertEquals(user, revision.setUser(user).getUser()); + assertEquals("abc", revision.setVersion("abc").getVersion()); + } + + /** + * Test non-mutable committed at date + */ + @Test + public void getCreatedAReferenceMutableObject() { + GistRevision gistRevision = new GistRevision(); + Date date = new Date(10000); + gistRevision.setCommittedAt(date); + gistRevision.getCommittedAt().setTime(0); + assertTrue(gistRevision.getCommittedAt().getTime() != 0); + date.setTime(1000); + assertEquals(10000, gistRevision.getCommittedAt().getTime()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistServiceTest.java new file mode 100644 index 000000000..b88f31c98 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistServiceTest.java @@ -0,0 +1,536 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.egit.github.core.Comment; +import org.eclipse.egit.github.core.Gist; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.client.PageIterator; +import org.eclipse.egit.github.core.service.GistService; +import org.eclipse.egit.github.core.service.IssueService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link GistService} + */ +@RunWith(MockitoJUnitRunner.class) +public class GistServiceTest { + + @Mock + private GitHubClient gitHubClient; + + @Mock + private GitHubResponse response; + + private GistService gistService; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(gitHubClient).get(any(GitHubRequest.class)); + gistService = new GistService(gitHubClient); + } + + /** + * Create service with null client + */ + @Test(expected = IllegalArgumentException.class) + public void constructorNullArgument() { + new GistService(null); + } + + /** + * Create default service + */ + @Test + public void defaultContructor() { + assertNotNull(new GistService().getClient()); + } + + /** + * Get gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getGistNullId() throws IOException { + gistService.getGist(null); + } + + /** + * Get gist with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getGistEmptyId() throws IOException { + gistService.getGist(""); + } + + /** + * Get gist with valid id + * + * @throws IOException + */ + @Test + public void getGist() throws IOException { + gistService.getGist("1"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/gists/1"); + verify(gitHubClient).get(request); + } + + /** + * Delete gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteGistNullId() throws IOException { + gistService.deleteGist(null); + } + + /** + * Delete gist with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteGistEmptyId() throws IOException { + gistService.deleteGist(""); + } + + /** + * Delete gist with valid id + * + * @throws IOException + */ + @Test + public void deleteGist() throws IOException { + gistService.deleteGist("1"); + verify(gitHubClient).delete("/gists/1"); + } + + /** + * Star gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void starGistNullId() throws IOException { + gistService.starGist(null); + } + + /** + * Star gist with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void starGistEmptyId() throws IOException { + gistService.starGist(""); + } + + /** + * Star gist with valid id + * + * @throws IOException + */ + @Test + public void starGist() throws IOException { + gistService.starGist("1"); + verify(gitHubClient).put("/gists/1/star"); + } + + /** + * Unstar gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void unstarGistNullId() throws IOException { + gistService.unstarGist(null); + } + + /** + * Unstar gist with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void unstarGistEmptyId() throws IOException { + gistService.unstarGist(""); + } + + /** + * Unstar gist with valid id + * + * @throws IOException + */ + @Test + public void unstarGist() throws IOException { + gistService.unstarGist("1"); + verify(gitHubClient).delete("/gists/1/star"); + } + + /** + * Is gist starred with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void isStarredGistNullId() throws IOException { + gistService.isStarred(null); + } + + /** + * Is gist starred with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void isStarredGistEmptyId() throws IOException { + gistService.isStarred(""); + } + + /** + * Is gist starred with valid id + * + * @throws IOException + */ + @Test + public void isStarredGist() throws IOException { + gistService.isStarred("1"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/gists/1/star"); + verify(gitHubClient).get(request); + } + + /** + * Fork gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void forkGistNullId() throws IOException { + gistService.forkGist(null); + } + + /** + * Fork gist with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void forkGistEmptyId() throws IOException { + gistService.forkGist(""); + } + + /** + * Fork gist with valid id + * + * @throws IOException + */ + @Test + public void forkGist() throws IOException { + gistService.forkGist("1"); + verify(gitHubClient).post("/gists/1/fork", null, Gist.class); + } + + /** + * Edit comment with null comment + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editGistCommentNullComment() throws IOException { + gistService.editComment(null); + } + + /** + * Edit comment + * + * @throws IOException + */ + @Test + public void editGistComment() throws IOException { + Comment comment = new Comment(); + comment.setId(48).setBody("new body"); + gistService.editComment(comment); + verify(gitHubClient).post("/gists/comments/48", comment, Comment.class); + } + + /** + * Get gist comment + * + * @throws IOException + */ + @Test + public void getGistComment() throws IOException { + gistService.getComment(59); + GitHubRequest request = new GitHubRequest(); + request.setUri("/gists/comments/59"); + verify(gitHubClient).get(request); + } + + /** + * Delete gist comment + * + * @throws IOException + */ + @Test + public void deleteGistComment() throws IOException { + gistService.deleteComment(1234); + verify(gitHubClient).delete("/gists/comments/1234"); + } + + /** + * Get starred gists + * + * @throws IOException + */ + @Test + public void getStarredGists() throws IOException { + List starred = gistService.getStarredGists(); + assertNotNull(starred); + assertEquals(0, starred.size()); + } + + /** + * Get iterator for starred gists + * + * @throws IOException + */ + @Test + public void pageStarredGists() throws IOException { + PageIterator iterator = gistService.pageStarredGists(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/gists/starred"), iterator.getRequest() + .generateUri()); + } + + /** + * Get gists for null login name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getGistsNullUser() throws IOException { + gistService.getGists(null); + } + + /** + * Get gists for empty login name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getGistsEmptyUser() throws IOException { + gistService.getGists(""); + } + + /** + * Get gists for valid login name + * + * @throws IOException + */ + @Test + public void getGistsOK() throws IOException { + gistService.getGists("test_user"); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/users/test_user/gists")); + verify(gitHubClient).get(request); + } + + /** + * Create gist with null model + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createGistNullGist() throws IOException { + gistService.createGist(null); + } + + /** + * Get gist with null user + * + * @throws IOException + */ + @Test + public void createGistNullUser() throws IOException { + Gist gist = new Gist(); + gist.setUser(null); + gistService.createGist(gist); + verify(gitHubClient).post("/gists", gist, Gist.class); + } + + /** + * Update null gist + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void updateGistNullGist() throws IOException { + gistService.updateGist(null); + } + + /** + * Update gist with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void updateGistNullId() throws IOException { + Gist gist = new Gist(); + gist.setId(null); + gistService.updateGist(gist); + } + + /** + * Update valid gist + * + * @throws IOException + */ + @Test + public void updateGistOK() throws IOException { + Gist gist = new Gist(); + gist.setId("123"); + gistService.updateGist(gist); + verify(gitHubClient).post("/gists/123", gist, Gist.class); + } + + /** + * Create comment for with null gist id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentNullGistId() throws IOException { + gistService.createComment(null, "not null"); + } + + /** + * Create null comment + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentNullComment() throws IOException { + gistService.createComment("not null", null); + } + + /** + * Create valid comment + * + * @throws IOException + */ + @Test + public void createCommentOK() throws IOException { + gistService.createComment("1", "test_comment"); + + Map params = new HashMap(1, 1); + params.put(IssueService.FIELD_BODY, "test_comment"); + verify(gitHubClient).post("/gists/1/comments", params, Comment.class); + } + + /** + * Get comments for null gist id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullGistId() throws IOException { + gistService.getComments(null); + } + + /** + * Get comment with valid gist id + * + * @throws IOException + */ + @Test + public void getCommentsOK() throws IOException { + gistService.getComments("1"); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/gists/1/comments")); + verify(gitHubClient).get(request); + } + + /** + * Page gists with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void pageUserGistsNullId() throws IOException { + gistService.pageGists(null); + } + + /** + * Page gists with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void pageUserGistsEmptyId() throws IOException { + gistService.pageGists(""); + } + + /** + * Page gists with valid user + * + * @throws IOException + */ + @Test + public void pageUserGists() throws IOException { + PageIterator iterator = gistService.pageGists("user"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/users/user/gists"), iterator.getRequest() + .generateUri()); + } + + /** + * Page public gists + * + * @throws IOException + */ + @Test + public void pagePublicGists() throws IOException { + PageIterator iterator = gistService.pagePublicGists(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistTest.java new file mode 100644 index 000000000..e03248c15 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistTest.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.Date; + +import org.eclipse.egit.github.core.Gist; +import org.eclipse.egit.github.core.GistFile; +import org.eclipse.egit.github.core.GistRevision; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link Gist} + */ +public class GistTest { + + /** + * Test default state of gist + */ + @Test + public void defaultState() { + Gist gist = new Gist(); + assertEquals(0, gist.getComments()); + assertNull(gist.getCreatedAt()); + assertNull(gist.getDescription()); + assertNull(gist.getFiles()); + assertNull(gist.getGitPullUrl()); + assertNull(gist.getGitPushUrl()); + assertNull(gist.getHistory()); + assertNull(gist.getHtmlUrl()); + assertNull(gist.getId()); + assertNull(gist.getUpdatedAt()); + assertNull(gist.getUrl()); + assertNull(gist.getUser()); + assertNull(gist.getOwner()); + assertFalse(gist.isPublic()); + } + + /** + * Test updating gist fields + */ + @Test + public void updateFields() { + Gist gist = new Gist(); + assertEquals(3, gist.setComments(3).getComments()); + assertEquals(new Date(5000), gist.setCreatedAt(new Date(5000)) + .getCreatedAt()); + assertEquals("desc", gist.setDescription("desc").getDescription()); + assertEquals(Collections.emptyMap(), + gist.setFiles(Collections. emptyMap()) + .getFiles()); + assertEquals("pull", gist.setGitPullUrl("pull").getGitPullUrl()); + assertEquals("push", gist.setGitPushUrl("push").getGitPushUrl()); + assertEquals(Collections.emptyList(), + gist.setHistory(Collections. emptyList()) + .getHistory()); + assertEquals("html", gist.setHtmlUrl("html").getHtmlUrl()); + assertEquals("id", gist.setId("id").getId()); + assertEquals(new Date(1000), gist.setUpdatedAt(new Date(1000)) + .getUpdatedAt()); + assertEquals("url", gist.setUrl("url").getUrl()); + User user = new User().setLogin("use"); + assertEquals(user, gist.setUser(user).getUser()); + assertTrue(gist.setPublic(true).isPublic()); + } + + /** + * Test non-mutable created at date + */ + @Test + public void getCreatedAtReferenceMutableObject() { + Gist gist = new Gist(); + gist.setCreatedAt(new Date(11111)); + gist.getCreatedAt().setTime(0); + assertTrue(gist.getCreatedAt().getTime() != 0); + } + + /** + * Test non-mutable updated at date + */ + @Test + public void getUpdatedAtReferenceMutableObject() { + Gist gist = new Gist(); + gist.setUpdatedAt(new Date(22222)); + gist.getUpdatedAt().setTime(0); + assertTrue(gist.getUpdatedAt().getTime() != 0); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GitHubClientTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GitHubClientTest.java new file mode 100644 index 000000000..f0a2e3795 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GitHubClientTest.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.IGitHubConstants; +import org.junit.Test; + +/** + * Unit tests of {@link GitHubClient} + */ +public class GitHubClientTest { + + private static class PrefixClient extends GitHubClient { + + public PrefixClient(String host) { + super(host); + } + + String uri(String uri) { + return super.configureUri(uri); + } + } + + /** + * Verify prefix with API v3 host + */ + @Test + public void prefixHostApiV3() { + PrefixClient client = new PrefixClient(IGitHubConstants.HOST_API); + assertEquals("/api/v3/repos/o/n", client.uri("/api/v3/repos/o/n")); + assertEquals("/repos/o/n", client.uri("/repos/o/n")); + assertEquals("/api/v2/json/repos/search/test", + client.uri("/api/v2/json/repos/search/test")); + } + + /** + * Verify prefix with localhost + */ + @Test + public void prefixLocalhost() { + PrefixClient client = new PrefixClient("localhost"); + assertEquals("/api/v3/repos/o/n", client.uri("/repos/o/n")); + assertEquals("/api/v3/repos/o/n", client.uri("/api/v3/repos/o/n")); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPageTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPageTest.java new file mode 100644 index 000000000..cc291c93a --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPageTest.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +import org.eclipse.egit.github.core.GollumPage; +import org.junit.Test; + +/** + * Unit tests of {@link GollumPage} + */ +public class GollumPageTest { + + /** + * Test default state of GollumPage + */ + @Test + public void defaultState() { + GollumPage GollumPage = new GollumPage(); + assertNull(GollumPage.getAction()); + assertNull(GollumPage.getHtmlUrl()); + assertNull(GollumPage.getPageName()); + assertNull(GollumPage.getSha()); + assertNull(GollumPage.getTitle()); + } + + /** + * Test updating GollumPage fields + */ + @Test + public void updateFields() { + GollumPage GollumPage = new GollumPage(); + assertEquals("create", GollumPage.setAction("create").getAction()); + assertEquals("url://a", GollumPage.setHtmlUrl("url://a").getHtmlUrl()); + assertEquals("page", GollumPage.setPageName("page").getPageName()); + assertEquals("000", GollumPage.setSha("000").getSha()); + assertEquals("title", GollumPage.setTitle("title").getTitle()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPayloadTest.java new file mode 100644 index 000000000..a8648a861 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GollumPayloadTest.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.egit.github.core.GollumPage; +import org.eclipse.egit.github.core.event.GollumPayload; +import org.junit.Test; + +/** + * Unit tests of {@link GollumPayload} + */ +public class GollumPayloadTest { + + /** + * Test default state of GollumPayload + */ + @Test + public void defaultState() { + GollumPayload payload = new GollumPayload(); + assertNull(payload.getPages()); + } + + /** + * Test updating GollumPayload fields + */ + @Test + public void updateFields() { + GollumPayload payload = new GollumPayload(); + List pages = new ArrayList(); + pages.add(new GollumPage().setPageName("page")); + assertEquals(pages, payload.setPages(pages).getPages()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GsonUtilsTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GsonUtilsTest.java new file mode 100644 index 000000000..f5e36d4db --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GsonUtilsTest.java @@ -0,0 +1,71 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; + +import org.eclipse.egit.github.core.Blob; +import org.eclipse.egit.github.core.client.GsonUtils; +import org.junit.Test; + +/** + * Unit tests of {@link GsonUtils} + */ +public class GsonUtilsTest { + + /** + * Get Gson instances + */ + @Test + public void getGson() { + assertNotNull(GsonUtils.getGson()); + assertNotNull(GsonUtils.getGson(true)); + assertNotNull(GsonUtils.getGson(false)); + assertNotSame(GsonUtils.getGson(true), GsonUtils.getGson(false)); + } + + /** + * Create Gson instances + */ + @Test + public void createGson() { + assertNotNull(GsonUtils.createGson()); + assertNotNull(GsonUtils.createGson(true)); + assertNotNull(GsonUtils.createGson(false)); + assertNotSame(GsonUtils.createGson(true), GsonUtils.createGson(false)); + } + + /** + * Serialize objects with all null fields + */ + @Test + public void noSeriazlizeNulls() { + Blob blob = new Blob(); + String json = GsonUtils.toJson(blob, false); + assertEquals("{}", json); + } + + /** + * Serialize objects with all null fields + */ + @Test + public void seriazlizeNulls() { + Blob blob = new Blob(); + String json = GsonUtils.toJson(blob, true); + assertNotNull(json); + assertTrue(json.length() > 2); + assertFalse("{}".equals(json)); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IdTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IdTest.java new file mode 100644 index 000000000..e1998f730 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IdTest.java @@ -0,0 +1,41 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Id; +import org.junit.Test; + +/** + * Unit tests of {@link Id} + */ +public class IdTest { + + /** + * Test default state of id + */ + @Test + public void defaultState() { + Id id = new Id(); + assertNull(id.getId()); + } + + /** + * Test updating field of id + */ + @Test + public void updateField() { + Id id = new Id(); + assertEquals("12345", id.setId("12345").getId()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueCommentPayloadTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueCommentPayloadTest.java new file mode 100644 index 000000000..230b462c0 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueCommentPayloadTest.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Jason Tsay (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.egit.github.core.Comment; +import org.eclipse.egit.github.core.Issue; +import org.eclipse.egit.github.core.event.IssueCommentPayload; +import org.junit.Test; + +/** + * Unit tests of {@link IssueCommentPayload} + */ +public class IssueCommentPayloadTest { + + /** + * Test default state of IssueCommentPayload + */ + @Test + public void defaultState() { + IssueCommentPayload payload = new IssueCommentPayload(); + assertNull(payload.getAction()); + assertNull(payload.getIssue()); + assertNull(payload.getComment()); + } + + /** + * Test updating IssueCommentPayload fields + */ + @Test + public void updateFields() { + IssueCommentPayload payload = new IssueCommentPayload(); + Issue issue = new Issue().setTitle("issue"); + Comment comment = new Comment().setBody("comment"); + assertEquals("create", payload.setAction("create").getAction()); + assertEquals(issue, payload.setIssue(issue).getIssue()); + assertEquals(comment, payload.setComment(comment).getComment()); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueEventTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueEventTest.java new file mode 100644 index 000000000..e9fdafb8f --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueEventTest.java @@ -0,0 +1,88 @@ +/****************************************************************************** + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import java.util.Date; + +import org.eclipse.egit.github.core.Issue; +import org.eclipse.egit.github.core.IssueEvent; +import org.eclipse.egit.github.core.Label; +import org.eclipse.egit.github.core.Milestone; +import org.eclipse.egit.github.core.Rename; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link IssueEvent} + */ +public class IssueEventTest { + + /** + * Test default state of issue event + */ + @Test + public void defaultState() { + IssueEvent event = new IssueEvent(); + assertEquals(0, event.getId()); + assertNull(event.getUrl()); + assertNull(event.getActor()); + assertNull(event.getCommitId()); + assertNull(event.getEvent()); + assertNull(event.getCreatedAt()); + assertNull(event.getLabel()); + assertNull(event.getAssignee()); + assertNull(event.getMilestone()); + assertNull(event.getRename()); + assertNull(event.getIssue()); + } + + /** + * Test updating issue event fields + */ + @Test + public void updateFields() { + IssueEvent event = new IssueEvent(); + assertEquals(4356, event.setId(4356).getId()); + assertEquals("url://a", event.setUrl("url://a").getUrl()); + User actor = new User().setName("Act Tor"); + assertEquals(actor, event.setActor(actor).getActor()); + assertEquals("a12b", event.setCommitId("a12b").getCommitId()); + assertEquals(IssueEvent.TYPE_CLOSED, event.setEvent("closed").getEvent()); + assertEquals(new Date(60000), event.setCreatedAt(new Date(60000)) + .getCreatedAt()); + Label label = new Label().setName("Lab El").setColor("563d7c"); + assertEquals(label, event.setLabel(label).getLabel()); + User assignee = new User().setName("Assig Nee"); + assertEquals(assignee, event.setAssignee(assignee).getAssignee()); + Milestone milestone = new Milestone().setTitle("Milestone"); + assertEquals(milestone, event.setMilestone(milestone).getMilestone()); + Rename rename = new Rename().setFrom("from").setTo("to"); + assertEquals(rename, event.setRename(rename).getRename()); + Issue issue = new Issue().setNumber(30); + assertEquals(issue, event.setIssue(issue).getIssue()); + } + + /** + * Test non-mutable created at date + */ + @Test + public void nonMutableCreatedAt() { + IssueEvent event = new IssueEvent(); + Date date = new Date(1000); + event.setCreatedAt(date); + date.setTime(0); + assertFalse(date.equals(event.getCreatedAt())); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueServiceTest.java new file mode 100644 index 000000000..17b9f816f --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueServiceTest.java @@ -0,0 +1,917 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.egit.github.core.Comment; +import org.eclipse.egit.github.core.Issue; +import org.eclipse.egit.github.core.IssueEvent; +import org.eclipse.egit.github.core.RepositoryId; +import org.eclipse.egit.github.core.RepositoryIssue; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; +import org.eclipse.egit.github.core.client.GitHubResponse; +import org.eclipse.egit.github.core.client.PageIterator; +import org.eclipse.egit.github.core.service.IssueService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * Unit tests of {@link IssueService} + */ +@RunWith(MockitoJUnitRunner.class) +public class IssueServiceTest { + + @Mock + private GitHubClient gitHubClient; + + @Mock + private GitHubResponse response; + + private IssueService issueService; + + /** + * Test case set up + * + * @throws IOException + */ + @Before + public void before() throws IOException { + doReturn(response).when(gitHubClient).get(any(GitHubRequest.class)); + issueService = new IssueService(gitHubClient); + } + + /** + * Create issue service with null client + */ + @Test(expected = IllegalArgumentException.class) + public void constructorNullArgument() { + new IssueService(null); + } + + /** + * Create issue service default constructor + */ + @Test + public void defaultConstructor() { + assertNotNull(new IssueService().getClient()); + } + + /** + * Get issue with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueNullUser() throws IOException { + issueService.getIssue(null, "not null", "not null"); + } + + /** + * Get issue with null repository id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueNullRepositoryId() throws IOException { + issueService.getIssue(null, 1); + } + + /** + * Get issue with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueEmptyUser() throws IOException { + issueService.getIssue("", "repo", "not null"); + } + + /** + * Get issue with null name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueNullRepositoryName() throws IOException { + issueService.getIssue("not null", null, "not null"); + } + + /** + * Get issue with empty name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueEmptyRepositoryName() throws IOException { + issueService.getIssue("user", "", "not null"); + } + + /** + * Get issue with null id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueNullId() throws IOException { + issueService.getIssue("not null", "not null", null); + } + + /** + * Get issue with empty id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssueEmptyId() throws IOException { + issueService.getIssue("not null", "not null", ""); + } + + /** + * Get issue with valid parameters + * + * @throws IOException + */ + @Test + public void getIssue() throws IOException { + issueService.getIssue("tu", "tr", 3); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/tu/tr/issues/3"); + verify(gitHubClient).get(request); + } + + /** + * Get issue with valid parameters + * + * @throws IOException + */ + @Test + public void getIssueWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.getIssue(id, 3); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/tu/tr/issues/3"); + verify(gitHubClient).get(request); + } + + /** + * Get issue comments with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullUser() throws IOException { + issueService.getComments(null, "not null", 1); + } + + /** + * Get issue comments with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsEmptyUser() throws IOException { + issueService.getComments("", "not null", 2); + } + + /** + * Get issue comments with null name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullRepositoryName() throws IOException { + issueService.getComments("not null", null, 3); + } + + /** + * Get issue comments with null repository id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullRepositoryId() throws IOException { + issueService.getComments(null, 3); + } + + /** + * Get issue comments with empty name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsEmptyRepositoryName() throws IOException { + issueService.getComments("not null", "", 3); + } + + /** + * Get issue comments with null issue id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsNullId() throws IOException { + issueService.getComments("not null", "not null", null); + } + + /** + * Get issue comments with empty issue id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getCommentsEmptyId() throws IOException { + issueService.getComments("not null", "not null", ""); + } + + /** + * Get issue comments with valid parameters + * + * @throws IOException + */ + @Test + public void getComments() throws IOException { + issueService.getComments("tu", "tr", 4); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/tu/tr/issues/4/comments")); + verify(gitHubClient).get(request); + } + + /** + * Get issue comments with valid parameters + * + * @throws IOException + */ + @Test + public void getCommentsWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.getComments(id, 4); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/tu/tr/issues/4/comments")); + verify(gitHubClient).get(request); + } + + /** + * Get issues with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssuesNullUser() throws IOException { + issueService.getIssues(null, "not null", null); + } + + /** + * Get issues with null repository id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssuesNullRepositoryId() throws IOException { + issueService.getIssues(null, null); + } + + /** + * Get issues with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssuesEmptyUser() throws IOException { + issueService.getIssues("", "not null", null); + } + + /** + * Get issues with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssuesNullRepositoryName() throws IOException { + issueService.getIssues("not null", null, null); + } + + /** + * Get issues with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void getIssuesEmptyRepositoryName() throws IOException { + issueService.getIssues("not null", "", null); + } + + /** + * Get issues with valid parameters + * + * @throws IOException + */ + @Test + public void getIssues() throws IOException { + issueService.getIssues("tu", "tr", null); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/tu/tr/issues")); + verify(gitHubClient).get(request); + } + + /** + * Get issues with valid parameters + * + * @throws IOException + */ + @Test + public void getIssuesRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.getIssues(id, null); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils.page("/repos/tu/tr/issues")); + verify(gitHubClient).get(request); + } + + /** + * Create issue with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createIssueNullUser() throws IOException { + issueService.createIssue(null, "not null", new Issue()); + } + + /** + * Create issue with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createIssueEmptyUser() throws IOException { + issueService.createIssue("", "not null", new Issue()); + } + + /** + * Create issue with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createIssueNullRepositoryName() throws IOException { + issueService.createIssue("not null", null, null); + } + + /** + * Create issue with empty repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createIssueEmptyRepositoryName() throws IOException { + issueService.createIssue("not null", "", new Issue()); + } + + /** + * Create issue with null issue + * + * @throws IOException + */ + @Test + public void createIssueNullIssue() throws IOException { + issueService.createIssue("test_user", "test_repository", null); + verify(gitHubClient).post("/repos/test_user/test_repository/issues", + new HashMap(), Issue.class); + } + + /** + * Create issue with null issue + * + * @throws IOException + */ + @Test + public void createIssueNullIssueWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.createIssue(id, null); + verify(gitHubClient).post("/repos/tu/tr/issues", + new HashMap(), Issue.class); + } + + /** + * Edit issue with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueNullUser() throws IOException { + issueService.editIssue(null, "not null", new Issue()); + } + + /** + * Edit issue with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueEmptyUser() throws IOException { + issueService.editIssue("", "not null", new Issue()); + } + + /** + * Edit issue with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueNullRepositoryName() throws IOException { + issueService.editIssue("not null", null, new Issue()); + } + + /** + * Edit issue with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueEmptyRepositoryName() throws IOException { + issueService.editIssue("not null", "", new Issue()); + } + + /** + * Edit issue with null issue + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueNullIssue() throws IOException { + issueService.editIssue("not null", "not null", null); + } + + /** + * Edit issue with valid parameters + * + * @throws IOException + */ + @Test + public void editIssue() throws IOException { + Issue issue = new Issue(); + issue.setNumber(1); + issue.setTitle("test_title"); + issue.setBody("test_body"); + issue.setState("test_state"); + issueService.editIssue("test_user", "test_repository", issue); + + Map params = new HashMap(); + params.put(IssueService.FIELD_TITLE, "test_title"); + params.put(IssueService.FIELD_BODY, "test_body"); + params.put(IssueService.FILTER_STATE, "test_state"); + verify(gitHubClient).post("/repos/test_user/test_repository/issues/1", + params, Issue.class); + } + + /** + * Edit issue with valid parameters + * + * @throws IOException + */ + @Test + public void editIssueWithRepositoryId() throws IOException { + Issue issue = new Issue(); + issue.setNumber(1); + issue.setTitle("test_title"); + issue.setBody("test_body"); + issue.setState("test_state"); + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.editIssue(id, issue); + + Map params = new HashMap(); + params.put(IssueService.FIELD_TITLE, "test_title"); + params.put(IssueService.FIELD_BODY, "test_body"); + params.put(IssueService.FILTER_STATE, "test_state"); + verify(gitHubClient).post("/repos/tu/tr/issues/1", params, Issue.class); + } + + /** + * Create issue comment with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentNullUser() throws IOException { + issueService.createComment(null, "not null", 1, "not null"); + } + + /** + * Create issue comment with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentEmptyUser() throws IOException { + issueService.createComment("", "not null", 2, "not null"); + } + + /** + * Create issue comment with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentNullRepositoryName() throws IOException { + issueService.createComment("not null", null, 3, "not null"); + } + + /** + * Create issue comment with empty repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentEmptyRepositoryName() throws IOException { + issueService.createComment("not null", "", 4, "not null"); + } + + /** + * Create issue comment with null issue id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentNullIssueId() throws IOException { + issueService.createComment("not null", "not null", null, "not null"); + } + + /** + * Create issue comment with empty issue id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void createCommentEmptyIssueId() throws IOException { + issueService.createComment("not null", "not null", "", "not null"); + } + + /** + * Create issue comment with valid parameters + * + * @throws IOException + */ + @Test + public void createComment() throws IOException { + issueService.createComment("test_user", "test_repository", 1, + "test_comment"); + + Map params = new HashMap(); + params.put(IssueService.FIELD_BODY, "test_comment"); + verify(gitHubClient).post( + "/repos/test_user/test_repository/issues/1/comments", params, + Comment.class); + } + + /** + * Create issue comment with valid parameters + * + * @throws IOException + */ + @Test + public void createCommentWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("tu", "tr"); + issueService.createComment(id, 1, "test_comment"); + + Map params = new HashMap(); + params.put(IssueService.FIELD_BODY, "test_comment"); + verify(gitHubClient).post("/repos/tu/tr/issues/1/comments", params, + Comment.class); + } + + /** + * Delete comment with null user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentNullUser() throws IOException { + issueService.deleteComment(null, "repo", 1); + } + + /** + * Delete comment with empty user + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentEmptyUser() throws IOException { + issueService.deleteComment("", "repo", 1); + } + + /** + * Delete comment with null repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentNullRepositoryName() throws IOException { + issueService.deleteComment("user", null, 1); + } + + /** + * Delete comment with empty repository name + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentEmptyRepositoryName() throws IOException { + issueService.deleteComment("user", "", 1); + } + + /** + * Delete comment with null comment id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentNullId() throws IOException { + issueService.deleteComment("user", "repo", null); + } + + /** + * Delete comment with empty comment id + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void deleteCommentEmptyId() throws IOException { + issueService.deleteComment("user", "repo", ""); + } + + /** + * Delete issue comment + * + * @throws IOException + */ + @Test + public void deleteComment() throws IOException { + issueService.deleteComment("user", "repo", 1); + verify(gitHubClient).delete("/repos/user/repo/issues/comments/1"); + } + + /** + * Delete issue comment + * + * @throws IOException + */ + @Test + public void deleteCommentWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.deleteComment(id, 1); + verify(gitHubClient).delete("/repos/user/repo/issues/comments/1"); + } + + /** + * Page issues for current user + * + * @throws IOException + */ + @Test + public void pageIssues() throws IOException { + PageIterator iterator = issueService.pageIssues(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/issues"), iterator.getRequest().generateUri()); + } + + /** + * Page issues for repository + * + * @throws IOException + */ + @Test + public void pageRepsitoryIssues() throws IOException { + PageIterator iterator = issueService.pageIssues("user", "repo"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/repos/user/repo/issues"), iterator + .getRequest().generateUri()); + } + + /** + * Page issues for repository + * + * @throws IOException + */ + @Test + public void pageRepsitoryIssuesWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + PageIterator iterator = issueService.pageIssues(id); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + assertEquals(Utils.page("/repos/user/repo/issues"), iterator + .getRequest().generateUri()); + } + + /** + * Get issues for current user + * + * @throws IOException + */ + @Test + public void getCurrentUserIssues() throws IOException { + List issues = issueService.getIssues(); + assertNotNull(issues); + assertTrue(issues.isEmpty()); + } + + /** + * Page all issue events for repository + * + * @throws IOException + */ + @Test + public void pageEvents() throws IOException { + PageIterator iter = issueService.pageEvents("user", "repo"); + assertNotNull(iter); + assertTrue(iter.hasNext()); + assertEquals(Utils.page("/repos/user/repo/issues/events"), iter + .getRequest().generateUri()); + } + + /** + * Page issue events for repository + * + * @throws IOException + */ + @Test + public void pageIssueEvents() throws IOException { + PageIterator iter = issueService.pageIssueEvents("user", + "repo", 16); + assertNotNull(iter); + assertTrue(iter.hasNext()); + assertEquals(Utils.page("/repos/user/repo/issues/16/events"), iter + .getRequest().generateUri()); + } + + /** + * Get issue event + * + * @throws IOException + */ + @Test + public void getIssueEvent() throws IOException { + issueService.getIssueEvent("user", "repo", 4399); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/user/repo/issues/events/4399"); + verify(gitHubClient).get(request); + } + + /** + * Get issue comment + * + * @throws IOException + */ + @Test + public void getIssueComment() throws IOException { + issueService.getComment("user", "repo", 38); + GitHubRequest request = new GitHubRequest(); + request.setUri("/repos/user/repo/issues/comments/38"); + verify(gitHubClient).get(request); + } + + /** + * Edit issue comment with null comment + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void editIssueCommentNullComment() throws IOException { + issueService.editComment("user", "repo", null); + } + + /** + * Edit issue comment + * + * @throws IOException + */ + @Test + public void editIssueComment() throws IOException { + Comment comment = new Comment().setId(29).setBody("new body"); + issueService.editComment("user", "repo", comment); + verify(gitHubClient).post("/repos/user/repo/issues/comments/29", + comment, Comment.class); + } + + /** + * Edit issue comment + * + * @throws IOException + */ + @Test + public void editIssueCommentWithRepositoryId() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + Comment comment = new Comment().setId(44).setBody("new body"); + issueService.editComment(id, comment); + verify(gitHubClient).post("/repos/user/repo/issues/comments/44", + comment, Comment.class); + } + + /** + * Search issues with null repository + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void searchIssuesNullRepository() throws IOException { + issueService.searchIssues(null, "open", "test"); + } + + /** + * Search issues with null query + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void searchIssueNullQuery() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, "open", null); + } + + /** + * Search issues with empty query + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void searchIssueEmptyQuery() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, "open", ""); + } + + /** + * Search issues with null state + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void searchIssueNullState() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, null, "test"); + } + + /** + * Search issues with empty state + * + * @throws IOException + */ + @Test(expected = IllegalArgumentException.class) + public void searchIssueEmptyState() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, "", "test"); + } + + /** + * Search issues + * + * @throws IOException + */ + @Test + public void searchIssues() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, "closed", "test"); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils + .page("/legacy/issues/search/user/repo/closed/test")); + verify(gitHubClient).get(request); + } + + /** + * Search issues with query that needs escaping + * + * @throws IOException + */ + @Test + public void searchEscaped() throws IOException { + RepositoryId id = new RepositoryId("user", "repo"); + issueService.searchIssues(id, "open", "a and a."); + GitHubRequest request = new GitHubRequest(); + request.setUri(Utils + .page("/legacy/issues/search/user/repo/open/a%20and%20a%2E")); + verify(gitHubClient).get(request); + } +} diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueTest.java new file mode 100644 index 000000000..56cf30157 --- /dev/null +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/IssueTest.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (c) 2011 Christian Trutz + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian Trutz - initial API and implementation + *******************************************************************************/ +package org.eclipse.egit.github.core.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Date; + +import org.eclipse.egit.github.core.Issue; +import org.eclipse.egit.github.core.Label; +import org.eclipse.egit.github.core.Milestone; +import org.eclipse.egit.github.core.PullRequest; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +/** + * Unit tests of {@link Issue} + */ +public class IssueTest { + + /** + * Test default state of issue + */ + @Test + public void defaultState() { + Issue issue = new Issue(); + assertNull(issue.getAssignee()); + assertNull(issue.getBody()); + assertNull(issue.getBodyHtml()); + assertNull(issue.getBodyText()); + assertNull(issue.getClosedAt()); + assertEquals(0, issue.getComments()); + assertNull(issue.getCreatedAt()); + assertNull(issue.getHtmlUrl()); + assertNull(issue.getLabels()); + assertNull(issue.getMilestone()); + assertEquals(0, issue.getNumber()); + assertNull(issue.getPullRequest()); + assertNull(issue.getState()); + assertNull(issue.getTitle()); + assertNull(issue.getUpdatedAt()); + assertNull(issue.getUrl()); + assertNull(issue.getUser()); + assertNotNull(issue.toString()); + assertEquals(0, issue.getId()); + assertNull(issue.getClosedBy()); + } + + /** + * Test updating issue fields + */ + @Test + public void updateFields() { + Issue issue = new Issue(); + User assignee = new User().setLogin("assignee"); + assertEquals(assignee, issue.setAssignee(assignee).getAssignee()); + assertEquals("body", issue.setBody("body").getBody()); + assertEquals("", issue.setBodyHtml("").getBodyHtml()); + assertEquals("text", issue.setBodyText("text").getBodyText()); + assertEquals(new Date(1000), issue.setClosedAt(new Date(1000)) + .getClosedAt()); + assertEquals(5, issue.setComments(5).getComments()); + assertEquals(new Date(2000), issue.setCreatedAt(new Date(2000)) + .getCreatedAt()); + assertEquals("html", issue.setHtmlUrl("html").getHtmlUrl()); + assertEquals(new ArrayList