diff options
author | Kevin Sawicki | 2011-09-05 00:09:48 +0000 |
---|---|---|
committer | Kevin Sawicki | 2011-09-05 17:49:30 +0000 |
commit | 5a35fcc03d05ba8493f9e413ac1b7a43ce4236f6 (patch) | |
tree | 1b70c3cd33e3f22498638fde8c36abb6aa246213 | |
parent | 85e0f99da89118e311a531327af82875dc636e34 (diff) | |
download | egit-github-5a35fcc03d05ba8493f9e413ac1b7a43ce4236f6.tar.gz egit-github-5a35fcc03d05ba8493f9e413ac1b7a43ce4236f6.tar.xz egit-github-5a35fcc03d05ba8493f9e413ac1b7a43ce4236f6.zip |
Add more unit tests of Gist service and model classes.
Change-Id: I38ae72b057a84d20e5fbd6c1fb2113c09fd41cb3
Signed-off-by: Kevin Sawicki <kevin@github.com>
7 files changed, 478 insertions, 9 deletions
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 index 9e3e1d82..a661c08c 100644 --- 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 @@ -10,11 +10,14 @@ *******************************************************************************/ 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; /** @@ -23,6 +26,37 @@ import org.junit.Test; public class CommentTest { /** + * Test default state of comment + */ + @Test + public void defaultState() { + Comment comment = new Comment(); + assertNull(comment.getBody()); + 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(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 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 00000000..43acc81c --- /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 00000000..d14f8ede --- /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/GistRevisionTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/GistRevisionTest.java index 90ff17ae..a7c0feec 100644 --- 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 @@ -10,29 +10,62 @@ *******************************************************************************/ 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; -import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnitRunner; /** * Unit tests of {@link GistRevision} */ -@RunWith(MockitoJUnitRunner.class) 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(); - gistRevision.setCommittedAt(new Date(10000)); + 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 index 8e3f5497..66dc4594 100644 --- 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 @@ -10,12 +10,16 @@ *******************************************************************************/ 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; @@ -23,6 +27,7 @@ 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; @@ -65,6 +70,14 @@ public class GistServiceTest { } /** + * Create default service + */ + @Test + public void defaultContructor() { + assertNotNull(new GistService().getClient()); + } + + /** * Get gist with null id * * @throws IOException @@ -75,17 +88,205 @@ public class GistServiceTest { } /** + * 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 getGistOK() throws IOException { + public void getGist() throws IOException { gistService.getGist("1"); verify(gitHubClient).get(any(GitHubRequest.class)); } /** + * 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"); + verify(gitHubClient).get(any(GitHubRequest.class)); + } + + /** + * 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); + } + + /** + * 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<Gist> starred = gistService.getStarredGists(); + assertNotNull(starred); + assertEquals(0, starred.size()); + } + + /** * Get gists for null login name * * @throws IOException @@ -96,6 +297,16 @@ public class GistServiceTest { } /** + * 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 @@ -218,4 +429,48 @@ public class GistServiceTest { gistService.getComments("1"); verify(gitHubClient).get(any(GitHubRequest.class)); } + + /** + * 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<Gist> iterator = gistService.pageGists("user"); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + } + + /** + * Page public gists + * + * @throws IOException + */ + @Test + public void pagePublicGists() throws IOException { + PageIterator<Gist> 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 index f4557182..22d27733 100644 --- 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 @@ -10,11 +10,18 @@ *******************************************************************************/ 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; /** @@ -23,6 +30,55 @@ import org.junit.Test; public class GistTest { /** + * Test default state of state + */ + @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()); + 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.<String, GistFile> emptyMap()) + .getFiles()); + assertEquals("pull", gist.setGitPullUrl("pull").getGitPullUrl()); + assertEquals("push", gist.setGitPushUrl("push").getGitPushUrl()); + assertEquals(Collections.emptyList(), + gist.setHistory(Collections.<GistRevision> 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 diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/GistService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/GistService.java index dc8d1330..275e190c 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/GistService.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/GistService.java @@ -115,6 +115,8 @@ public class GistService extends GitHubService { int size) { if (user == null) throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$ + if (user.length() == 0) + throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$ StringBuilder uri = new StringBuilder(SEGMENT_USERS); uri.append('/').append(user); @@ -297,9 +299,7 @@ public class GistService extends GitHubService { * @throws IOException */ public void deleteGist(String gistId) throws IOException { - if (gistId == null) - throw new IllegalArgumentException("Gist id cannot be null"); //$NON-NLS-1$ - + checkGistId(gistId); StringBuilder uri = new StringBuilder(SEGMENT_GISTS); uri.append('/').append(gistId); client.delete(uri.toString()); @@ -328,7 +328,7 @@ public class GistService extends GitHubService { StringBuilder uri = new StringBuilder(SEGMENT_GISTS); uri.append('/').append(gistId); uri.append(SEGMENT_STAR); - client.put(uri.toString(), null, null); + client.put(uri.toString()); } /** |