diff options
| author | Christian Trutz | 2011-04-26 19:29:47 +0000 |
|---|---|---|
| committer | Christian Trutz | 2011-04-26 19:29:47 +0000 |
| commit | 9e2b2c9b75ddec007dedbc5c5885be3628e3dd6b (patch) | |
| tree | e2705947ed41d31392cc8a5c6dab53d5eb586fe6 | |
| parent | 6fead739c1c3ba4fba54aba78050ddeaf7f4fa4d (diff) | |
| download | egit-github-9e2b2c9b75ddec007dedbc5c5885be3628e3dd6b.tar.gz egit-github-9e2b2c9b75ddec007dedbc5c5885be3628e3dd6b.tar.xz egit-github-9e2b2c9b75ddec007dedbc5c5885be3628e3dd6b.zip | |
Unit tests for GistService
Change-Id: I74702f0208baed0dd7903bd6bbfbffb28a54283d
Signed-off-by: Christian Trutz <christian.trutz@gmail.com>
| -rw-r--r-- | org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java | 6 | ||||
| -rw-r--r-- | org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/GistServiceTest.java | 49 |
2 files changed, 54 insertions, 1 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java index 4153d2a6..0a2fa4cf 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java @@ -58,6 +58,7 @@ public class GistService { * @throws IOException */ public List<Gist> getGists(String user) throws IOException { + Assert.isNotNull(user, "User cannot be null"); StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_USERS); uri.append('/').append(user); uri.append(IGitHubConstants.SEGMENT_GISTS).append( @@ -75,11 +76,14 @@ public class GistService { * @throws IOException */ public Gist createGist(Gist gist) throws IOException { + Assert.isNotNull(gist, "Gist cannot be null"); StringBuilder uri = new StringBuilder(); User user = gist.getUser(); if (user != null) { + String login = user.getLogin(); + Assert.isNotNull(login, "User login name cannot be null"); uri.append(IGitHubConstants.SEGMENT_USERS); - uri.append('/').append(user.getLogin()); + uri.append('/').append(login); } uri.append(IGitHubConstants.SEGMENT_GISTS).append( IGitHubConstants.SUFFIX_JSON); diff --git a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/GistServiceTest.java b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/GistServiceTest.java index 6f05d4b8..ed2cc7d7 100644 --- a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/GistServiceTest.java +++ b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/GistServiceTest.java @@ -13,6 +13,7 @@ package org.eclipse.mylyn.github.internal; import static org.mockito.Mockito.verify; import java.io.IOException; +import java.util.List; import org.eclipse.core.runtime.AssertionFailedException; import org.junit.Before; @@ -21,6 +22,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import com.google.gson.reflect.TypeToken; + /** * Unit tests for {@link GistService} */ @@ -54,4 +57,50 @@ public class GistServiceTest { verify(gitHubClient).get("/gists/1.json", Gist.class); } + @Test(expected = AssertionFailedException.class) + public void getGists_NullUser() throws IOException { + gistService.getGists(null); + } + + @Test + public void getGists_OK() throws IOException { + gistService.getGists("test_user"); + TypeToken<List<Gist>> gistsToken = new TypeToken<List<Gist>>() { + }; + verify(gitHubClient).get("/users/test_user/gists.json", + gistsToken.getType()); + } + + @Test(expected = AssertionFailedException.class) + public void createGist_NullGist() throws IOException { + gistService.createGist(null); + } + + @Test + public void createGist_NullUser() throws IOException { + Gist gist = new Gist(); + gist.setUser(null); + gistService.createGist(gist); + verify(gitHubClient).post("/gists.json", gist, Gist.class); + } + + @Test + public void createGist_NonNullUser() throws IOException { + Gist gist = new Gist(); + User user = new User(); + user.setLogin("test_user"); + gist.setUser(user); + gistService.createGist(gist); + verify(gitHubClient).post("/users/test_user/gists.json", gist, + Gist.class); + } + + @Test(expected = AssertionFailedException.class) + public void createGist_NonNullUser_NullLogin() throws IOException { + Gist gist = new Gist(); + User user = new User(); + user.setLogin(null); + gist.setUser(user); + gistService.createGist(gist); + } } |
