Skip to content

Commit 98a5912

Browse files
committed
* added editing of issue
* added unit tests for editing of issue
1 parent d0b227c commit 98a5912

3 files changed

Lines changed: 83 additions & 5 deletions

File tree

src/main/java/de/linsin/github/rest/resource/OpenIssueRequest.java renamed to src/main/java/de/linsin/github/rest/resource/ManipulateIssueRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
*
2121
* @author David Linsin - [email protected]
2222
*/
23-
public class OpenIssueRequest extends IssueRequest {
23+
public class ManipulateIssueRequest extends IssueRequest {
2424
private String title;
2525
private String body;
2626

27-
public OpenIssueRequest(String argLogin, String argToken, String argTitle, String argBody) {
27+
public ManipulateIssueRequest(String argLogin, String argToken, String argTitle, String argBody) {
2828
super(argLogin, argToken);
2929
title = argTitle;
3030
body = argBody;
3131
}
3232

33-
public OpenIssueRequest() {
33+
public ManipulateIssueRequest() {
3434
}
3535

3636
public String getTitle() {

src/main/java/de/linsin/github/rest/service/IssueBrowser.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import de.linsin.github.rest.resource.IssueRequest;
2525
import de.linsin.github.rest.resource.IssueResponse;
2626
import de.linsin.github.rest.resource.IssuesResponse;
27-
import de.linsin.github.rest.resource.OpenIssueRequest;
27+
import de.linsin.github.rest.resource.ManipulateIssueRequest;
2828
import org.springframework.util.Assert;
2929
import org.springframework.web.client.RestTemplate;
3030

@@ -126,7 +126,7 @@ public Issue open(Repository argRepository, Issue argIssue) {
126126
Assert.hasText(argIssue.getTitle());
127127
Assert.hasText(argIssue.getBody());
128128

129-
OpenIssueRequest req = new OpenIssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
129+
ManipulateIssueRequest req = new ManipulateIssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
130130
IssueResponse resp = template.postForObject(OPEN_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName());
131131

132132
return resp.getIssue();
@@ -171,6 +171,33 @@ public void close(Repository argRepository, Issue argIssue) {
171171
template.postForObject(CLOSE_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
172172
}
173173

174+
175+
/**
176+
* Edits the existing {@link Issue} passed, which resides in the provided {@link Repository}
177+
* Note: so far only id, title and body are used from passed instance
178+
*
179+
* @param argRepository {@link Repository} instance used to open issue
180+
* @param argIssue {@link Issue} instance containing id, title and body of the issue
181+
* @return the {@link Issue} instance which was edited
182+
* @throws IllegalArgumentException in case passed Issue doesn't contain an id, body or title
183+
* @throws NullPointerException in case passed repository or issue is null
184+
* @throws HttpClientErrorException in case passed user or repository doesn't exist
185+
*/
186+
public Issue edit(Repository argRepository, Issue argIssue) {
187+
RestTemplate template = initTemplate();
188+
189+
Assert.isTrue(argIssue.getNumber() > 0);
190+
Assert.hasText(argIssue.getTitle());
191+
Assert.hasText(argIssue.getBody());
192+
193+
ManipulateIssueRequest req = new ManipulateIssueRequest(username, apiToken, argIssue.getTitle(), argIssue.getBody());
194+
IssueResponse resp = template.postForObject(EDIT_ISSUE_URL, req, IssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
195+
196+
return resp.getIssue();
197+
}
198+
199+
// TODO implement comment
200+
174201
protected List<Issue> doBrowse(Repository argRepository, String argUrl) {
175202
RestTemplate template = initTemplate();
176203
IssuesResponse resp = template.getForObject(argUrl, IssuesResponse.class, argRepository.getOwner(), argRepository.getName());

src/test/java/de/linsin/github/rest/service/IssueBrowserTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import de.linsin.github.rest.resource.IssuesResponse;
2121
import de.linsin.github.rest.domain.Repository;
2222
import static org.easymock.classextension.EasyMock.*;
23+
import static org.easymock.EasyMock.eq;
2324
import org.junit.After;
2425
import static org.junit.Assert.*;
2526
import org.junit.Before;
@@ -250,6 +251,56 @@ public void reopen_issue_null_repo_passed() {
250251
classUnderTest.reopen(null, issue);
251252
}
252253

254+
@Test
255+
public void edit_issue() {
256+
Issue issue = new Issue();
257+
issue.setNumber(1);
258+
issue.setTitle("test");
259+
issue.setBody("test body");
260+
Repository repo = setupTestRepo();
261+
IssueResponse response = new IssueResponse();
262+
response.setIssue(issue);
263+
expect(mockRestTemplate.postForObject(eq(IssueBrowser.EDIT_ISSUE_URL), anyObject(), eq(IssueResponse.class), eq(repo.getOwner()),
264+
eq(repo.getName()), eq(String.valueOf(issue.getNumber())))).andReturn(response);
265+
replay(mockRestTemplate);
266+
assertNotNull(classUnderTest.edit(repo, issue));
267+
verify(mockRestTemplate);
268+
}
269+
270+
@Test(expected = IllegalArgumentException.class)
271+
public void edit_issue_no_number_in_issue() {
272+
classUnderTest.edit(setupTestRepo(), new Issue());
273+
}
274+
275+
@Test(expected = IllegalArgumentException.class)
276+
public void edit_issue_no_title() {
277+
Issue issue = new Issue();
278+
issue.setNumber(1);
279+
classUnderTest.edit(setupTestRepo(), issue);
280+
}
281+
282+
@Test(expected = IllegalArgumentException.class)
283+
public void edit_issue_no_body() {
284+
Issue issue = new Issue();
285+
issue.setNumber(1);
286+
issue.setTitle("test");
287+
classUnderTest.edit(setupTestRepo(), issue);
288+
}
289+
290+
@Test(expected = NullPointerException.class)
291+
public void edit_issue_null_issue_passed() {
292+
classUnderTest.edit(setupTestRepo(), null);
293+
}
294+
295+
@Test(expected = NullPointerException.class)
296+
public void edit_issue_null_repo_passed() {
297+
Issue issue = new Issue();
298+
issue.setNumber(1);
299+
issue.setTitle("test");
300+
issue.setBody("test body");
301+
classUnderTest.edit(null, issue);
302+
}
303+
253304
private Repository setupTestRepo() {
254305
Repository repo = new Repository();
255306
repo.setName("area51");

0 commit comments

Comments
 (0)