From c53b9b34fb56f3e80a0e0bfb17625fb09fa0e49d Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Sat, 28 Dec 2019 23:01:22 +0100 Subject: 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 --- .../github/core/tests/MilestoneServiceTest.java | 189 ++++++++++++++++++++- 1 file changed, 181 insertions(+), 8 deletions(-) (limited to 'org.eclipse.egit.github.core.tests/src/org/eclipse') 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 dtoCaptor; + private MilestoneService milestoneService; /** @@ -202,6 +213,16 @@ public class MilestoneServiceTest { milestoneService.createMilestone("user", "repo", null); } + /** + * 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 * @@ -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,10 +521,20 @@ 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 * @@ -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)); } } -- cgit v1.2.3