Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-12-28 17:01:22 -0500
committerMichael Keppler2019-12-29 12:42:43 -0500
commitc53b9b34fb56f3e80a0e0bfb17625fb09fa0e49d (patch)
tree5da2f260125cbab9a466cc5ddc98baf34b36a12f /org.eclipse.egit.github.core.tests/src
parent56560c75b1658df51a09f6f9c442741743d6ff63 (diff)
downloadegit-github-c53b9b34fb56f3e80a0e0bfb17625fb09fa0e49d.tar.gz
egit-github-c53b9b34fb56f3e80a0e0bfb17625fb09fa0e49d.tar.xz
egit-github-c53b9b34fb56f3e80a0e0bfb17625fb09fa0e49d.zip
Milestone creation needs a special DTO
Milestone creation accepts only a stripped-down creation DTO, not a full Milestone object.[1] This is similar to issue creation. Editing a milestone had the same problem; the update endpoint also accepts only the same DTO.[2] Create a DTO parameter map containing only the needed fields. [1] https://developer.github.com/v3/issues/milestones/#create-a-milestone [2] https://developer.github.com/v3/issues/milestones/#update-a-milestone Bug: 558649 Change-Id: I3b244b93822bc9596556fa7f712d61bcf5fe4586 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.egit.github.core.tests/src')
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/MilestoneServiceTest.java189
1 files changed, 181 insertions, 8 deletions
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/MilestoneServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/MilestoneServiceTest.java
index d5cf5b83..ab804ca3 100644
--- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/MilestoneServiceTest.java
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/MilestoneServiceTest.java
@@ -1,5 +1,6 @@
/*******************************************************************************
- * Copyright (c) 2011 Christian Trutz
+ * Copyright (c) 2011, 2019 Christian Trutz and others.
+ *
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -12,22 +13,29 @@
*******************************************************************************/
package org.eclipse.egit.github.core.tests;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
import org.eclipse.egit.github.core.Milestone;
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.GsonUtils;
import org.eclipse.egit.github.core.service.MilestoneService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@@ -43,6 +51,9 @@ public class MilestoneServiceTest {
@Mock
private GitHubResponse response;
+ @Captor
+ private ArgumentCaptor<Object> dtoCaptor;
+
private MilestoneService milestoneService;
/**
@@ -203,6 +214,16 @@ public class MilestoneServiceTest {
}
/**
+ * Create milestone with an empty milestone
+ *
+ * @throws IOException
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void createMilestoneEmptyMilestone() throws IOException {
+ milestoneService.createMilestone("user", "repo", new Milestone());
+ }
+
+ /**
* Create valid milestone
*
* @throws IOException
@@ -210,9 +231,24 @@ public class MilestoneServiceTest {
@Test
public void createMilestone() throws IOException {
Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestoneService.createMilestone("user", "repo", milestone);
+ verify(gitHubClient).post(eq("/repos/user/repo/milestones"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"A title\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Create invalid milestone without title.
+ *
+ * @throws IOException
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void createMilestoneNoTitle() throws IOException {
+ Milestone milestone = new Milestone();
+ milestone.setDescription("A description");
milestoneService.createMilestone("user", "repo", milestone);
- verify(gitHubClient).post("/repos/user/repo/milestones", milestone,
- Milestone.class);
}
/**
@@ -224,9 +260,89 @@ public class MilestoneServiceTest {
public void createMilestoneWithRepositoryId() throws IOException {
RepositoryId id = new RepositoryId("user", "repo");
Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestone.setDescription("A description");
+ milestoneService.createMilestone(id, milestone);
+ verify(gitHubClient).post(eq("/repos/user/repo/milestones"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"A title\",\"description\":\"A description\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Create an invalid milestone with a wrong state.
+ *
+ * @throws IOException
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void createMilestoneInvalidState() throws IOException {
+ RepositoryId id = new RepositoryId("user", "repo");
+ Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestone.setState("bogus");
+ milestoneService.createMilestone(id, milestone);
+ }
+
+ /**
+ * Create valid closed milestone.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void createMilestoneClosed() throws IOException {
+ RepositoryId id = new RepositoryId("user", "repo");
+ Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestone.setState("closed");
+ milestoneService.createMilestone(id, milestone);
+ verify(gitHubClient).post(eq("/repos/user/repo/milestones"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"A title\",\"state\":\"closed\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Create valid open milestone.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void createMilestoneOpen() throws IOException {
+ RepositoryId id = new RepositoryId("user", "repo");
+ Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestone.setState("open");
+ milestoneService.createMilestone(id, milestone);
+ verify(gitHubClient).post(eq("/repos/user/repo/milestones"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"A title\",\"state\":\"open\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Create valid open milestone.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void createMilestoneFull() throws IOException {
+ RepositoryId id = new RepositoryId("user", "repo");
+ Milestone milestone = new Milestone();
+ milestone.setTitle("A title");
+ milestone.setDescription("A description");
+ milestone.setState("open");
+ Calendar cal = Calendar.getInstance();
+ cal.set(2019, 1, 1, 12, 0, 0);
+ Date date = Date.from(cal.toInstant());
+ milestone.setDueOn(date);
+ milestone.setCreatedAt(date);
+ String serializedDate = GsonUtils.getGson().toJson(date);
milestoneService.createMilestone(id, milestone);
- verify(gitHubClient).post("/repos/user/repo/milestones", milestone,
- Milestone.class);
+ verify(gitHubClient).post(eq("/repos/user/repo/milestones"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"A title\",\"state\":\"open\",\"description\":\"A description\",\"due_on\":"
+ + serializedDate + "}", GsonUtils.getGson().toJson(dto));
}
/**
@@ -405,11 +521,21 @@ public class MilestoneServiceTest {
* @throws IOException
*/
@Test(expected = IllegalArgumentException.class)
- public void editMilestoneNullUser() throws IOException {
+ public void editMilestoneNullMilestone() throws IOException {
milestoneService.editMilestone(RepositoryId.create("a", "b"), null);
}
/**
+ * Edit milestone with empty milestone
+ *
+ * @throws IOException
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void editMilestoneEmptyMilestone() throws IOException {
+ milestoneService.editMilestone(RepositoryId.create("a", "b"), new Milestone());
+ }
+
+ /**
* Edit milestone
*
* @throws IOException
@@ -421,7 +547,54 @@ public class MilestoneServiceTest {
milestone.setTitle("a milestone");
milestoneService
.editMilestone(RepositoryId.create("a", "b"), milestone);
- verify(gitHubClient).post("/repos/a/b/milestones/1234", milestone,
- Milestone.class);
+ verify(gitHubClient).post(eq("/repos/a/b/milestones/1234"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"a milestone\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Edit milestone description
+ *
+ * @throws IOException
+ */
+ @Test
+ public void editMilestoneDescription() throws IOException {
+ Milestone milestone = new Milestone();
+ milestone.setNumber(1234);
+ milestone.setDescription("A description");
+ milestoneService
+ .editMilestone(RepositoryId.create("a", "b"), milestone);
+ verify(gitHubClient).post(eq("/repos/a/b/milestones/1234"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"description\":\"A description\"}", GsonUtils.getGson().toJson(dto));
+ }
+
+ /**
+ * Edit milestone with all fields
+ *
+ * @throws IOException
+ */
+ @Test
+ public void editMilestoneFull() throws IOException {
+ Milestone milestone = new Milestone();
+ milestone.setNumber(1234);
+ milestone.setTitle("a milestone");
+ milestone.setDescription("a description");
+ milestone.setState("closed");
+ Calendar cal = Calendar.getInstance();
+ cal.set(2019, 1, 1, 12, 0, 0);
+ Date date = Date.from(cal.toInstant());
+ milestone.setDueOn(date);
+ milestone.setCreatedAt(date);
+ String serializedDate = GsonUtils.getGson().toJson(date);
+ milestoneService
+ .editMilestone(RepositoryId.create("a", "b"), milestone);
+ verify(gitHubClient).post(eq("/repos/a/b/milestones/1234"), dtoCaptor.capture(),
+ eq(Milestone.class));
+ Object dto = dtoCaptor.getValue();
+ assertEquals("{\"title\":\"a milestone\",\"state\":\"closed\",\"description\":\"a description\",\"due_on\":"
+ + serializedDate + "}", GsonUtils.getGson().toJson(dto));
}
}

Back to the top