diff options
author | Christian Trutz | 2011-04-19 21:42:42 +0000 |
---|---|---|
committer | Christian Trutz | 2011-04-19 21:42:42 +0000 |
commit | 1a19ef58647087ce594a5040c78caf72458e204e (patch) | |
tree | 8b79253ac932effa3c2a6a3d973b680521da6cd7 | |
parent | 5ee3f2cca3501446b616904d484e68d6c6d74d1b (diff) | |
download | egit-github-1a19ef58647087ce594a5040c78caf72458e204e.tar.gz egit-github-1a19ef58647087ce594a5040c78caf72458e204e.tar.xz egit-github-1a19ef58647087ce594a5040c78caf72458e204e.zip |
Unit tests for LabelService
Change-Id: I512313960dfbd4bdf6c8480c16d41ff5e8b5c511
Signed-off-by: Christian Trutz <christian.trutz@gmail.com>
4 files changed, 144 insertions, 4 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java index 9ce5d471..6b243713 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java @@ -45,6 +45,8 @@ public class LabelService { */ public List<Label> getLabels(String user, String repository) throws IOException { + Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$ + Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$ StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS); uri.append('/').append(user).append('/').append(repository); uri.append(IGitHubConstants.SEGMENT_LABELS).append( @@ -66,6 +68,9 @@ public class LabelService { */ public List<Label> setLabels(String user, String repository, String issueId, List<Label> labels) throws IOException { + Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$ + Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$ + Assert.isNotNull(issueId, "Issue id cannot be null"); //$NON-NLS-1$ StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS); uri.append('/').append(user).append('/').append(repository); uri.append(IGitHubConstants.SEGMENT_ISSUES).append('/').append(issueId); @@ -88,6 +93,9 @@ public class LabelService { */ public Label createLabel(String user, String repository, Label label) throws IOException { + Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$ + Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$ + Assert.isNotNull(label, "Label cannot be null"); //$NON-NLS-1$ StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS); uri.append('/').append(user).append('/').append(repository); uri.append(IGitHubConstants.SEGMENT_LABELS).append( diff --git a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/IssueServiceTest.java b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/IssueServiceTest.java index 6afeed1f..20e1dde2 100644 --- a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/IssueServiceTest.java +++ b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/IssueServiceTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 GitHub Inc. + * 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 diff --git a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/LabelServiceTest.java b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/LabelServiceTest.java new file mode 100644 index 00000000..04224bbb --- /dev/null +++ b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/internal/LabelServiceTest.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * 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.mylyn.github.internal; + +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.runtime.AssertionFailedException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import com.google.gson.reflect.TypeToken; + +/** + * Unit tests for {@link LabelService}. + */ +@SuppressWarnings("restriction") +@RunWith(MockitoJUnitRunner.class) +public class LabelServiceTest { + + @Mock + private GitHubClient gitHubClient; + + private LabelService labelService; + + @Before + public void before() { + labelService = new LabelService(gitHubClient); + } + + @Test(expected = AssertionFailedException.class) + public void constructor_NullArgument() { + new LabelService(null); + } + + @Test(expected = AssertionFailedException.class) + public void getLabels_NullUser() throws IOException { + labelService.getLabels(null, "not null"); + } + + @Test(expected = AssertionFailedException.class) + public void getLabels_NullRepository() throws IOException { + labelService.getLabels("not null", null); + } + + @Test + public void getLabels_OK() throws IOException { + labelService.getLabels("test_user", "test_repository"); + TypeToken<List<Label>> labelsToken = new TypeToken<List<Label>>() { + }; + verify(gitHubClient).get( + "/repos/test_user/test_repository/labels.json", + labelsToken.getType()); + } + + @Test(expected = AssertionFailedException.class) + public void setLabels_NullUser() throws IOException { + labelService.setLabels(null, "not null", "not null", + new LinkedList<Label>()); + } + + @Test(expected = AssertionFailedException.class) + public void setLabels_NullRepository() throws IOException { + labelService.setLabels("not null", null, "not null", + new LinkedList<Label>()); + } + + @Test(expected = AssertionFailedException.class) + public void setLabels_NullIssueId() throws IOException { + labelService.setLabels("not null", "not null", null, + new LinkedList<Label>()); + } + + @Test + public void setLabels_NullLabels() throws IOException { + labelService.setLabels("test_user", "test_repository", "1", null); + TypeToken<List<Label>> labelsToken = new TypeToken<List<Label>>() { + }; + verify(gitHubClient).put( + "/repos/test_user/test_repository/issues/1/labels.json", null, + labelsToken.getType()); + } + + @Test + public void setLabels_OK() throws IOException { + List<Label> labels = new LinkedList<Label>(); + labelService.setLabels("test_user", "test_repository", "1", labels); + TypeToken<List<Label>> labelsToken = new TypeToken<List<Label>>() { + }; + verify(gitHubClient).put( + "/repos/test_user/test_repository/issues/1/labels.json", + labels, labelsToken.getType()); + } + + @Test(expected = AssertionFailedException.class) + public void createLabel_NullUser() throws IOException { + labelService.createLabel(null, "not null", new Label()); + } + + @Test(expected = AssertionFailedException.class) + public void createLabel_NullRepository() throws IOException { + labelService.createLabel("not null", null, new Label()); + } + + @Test(expected = AssertionFailedException.class) + public void createLabel_NullLabel() throws IOException { + labelService.createLabel("not null", "not null", null); + } + + @Test + public void createLabel_OK() throws IOException { + Label label = new Label(); + labelService.createLabel("test_user", "test_repository", label); + verify(gitHubClient).post( + "/repos/test_user/test_repository/labels.json", label, + Label.class); + } +} diff --git a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/AllHeadlessTests.java b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/AllHeadlessTests.java index d8b8eaeb..f782f2f9 100644 --- a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/AllHeadlessTests.java +++ b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/AllHeadlessTests.java @@ -13,14 +13,14 @@ package org.eclipse.mylyn.github.tests; import org.eclipse.mylyn.github.internal.IssueServiceTest; +import org.eclipse.mylyn.github.internal.LabelServiceTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses( { // - IssueServiceTest.class - }) +@SuiteClasses({ // +IssueServiceTest.class, LabelServiceTest.class }) public class AllHeadlessTests { } |