From c0b9f1ae11aba61a2db26e56c768493bd7f9d7e7 Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 00:57:23 +0300 Subject: [PATCH 1/6] Add worklog to issues --- src/main/java/net/rcarz/jiraclient/Issue.java | 35 +++++ .../java/net/rcarz/utils/WorklogUtils.java | 39 ++++++ .../rcarz/jiraclient/IssueWorklogTest.java | 130 ++++++++++++++++++ .../net/rcarz/jiraclient/WorklogTest.java | 9 +- .../utils/WorklogUtilsFormatDurationTest.java | 71 ++++++++++ 5 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/rcarz/utils/WorklogUtils.java create mode 100644 src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java create mode 100644 src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index 1cb586a..e5f2d4c 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -23,11 +23,16 @@ import java.io.File; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; +import java.text.SimpleDateFormat; import java.util.*; +import net.rcarz.utils.WorklogUtils; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; /** * Represents a JIRA issue. @@ -1075,6 +1080,36 @@ public class Issue extends Resource { return new Comment(restclient, (JSONObject) result, key); } + /** + * Adds {@link WorkLog} to this issue + * @param comment provided comment + * @param startDate provided start date + * @param timeSpentSeconds provided time spent. This cannot be lower than 1m inute + * @return + * @throws JiraException when worklog creation fails + */ + public WorkLog addWorkLog(String comment, DateTime startDate, long timeSpentSeconds) throws JiraException { + try { + if (comment == null) + throw new IllegalArgumentException("Invalid comment."); + if (startDate == null) + throw new IllegalArgumentException("Invalid start time."); + if (timeSpentSeconds < 60) // We do not add a worklog that duration is below a minute + throw new IllegalArgumentException("Time spent cannot be lower than 1 minute."); + + JSONObject req = new JSONObject(); + req.put("comment", comment); + req.put("started", DateTimeFormat.forPattern(Field.DATETIME_FORMAT).print(startDate.getMillis())); + req.put("timeSpent", WorklogUtils.formatDurationFromSeconds(timeSpentSeconds)); + + JSON result = restclient.post(getRestUri(key) + "/worklog", req); + JSONObject jo = (JSONObject) result; + return new WorkLog(restclient, jo); + } catch (Exception ex) { + throw new JiraException("Failed add worklog to issue " + key, ex); + } + } + /** * Links this issue with another issue. * diff --git a/src/main/java/net/rcarz/utils/WorklogUtils.java b/src/main/java/net/rcarz/utils/WorklogUtils.java new file mode 100644 index 0000000..cf0d44e --- /dev/null +++ b/src/main/java/net/rcarz/utils/WorklogUtils.java @@ -0,0 +1,39 @@ +package net.rcarz.utils; + +import net.rcarz.jiraclient.WorkLog; +import org.joda.time.DurationFieldType; +import org.joda.time.Period; +import org.joda.time.PeriodType; + +/** + * Created by mariusmerkevicius on 1/30/16. + * A set of utils static methods help set {@link WorkLog} + */ +public class WorklogUtils { + + /** + * Formats duration time into pretty string format + * Does not output seconds + * @param durationInSeconds provided duration to format + * @return formatted duration + */ + public static String formatDurationFromSeconds(long durationInSeconds) { + if (durationInSeconds < 60) + return "0m"; + StringBuilder builder = new StringBuilder(); + PeriodType type = PeriodType.forFields(new DurationFieldType[]{ + DurationFieldType.hours(), + DurationFieldType.minutes() + }); + + Period period = new Period(1000 * durationInSeconds, type); + if (period.getHours() != 0) + builder.append(period.getHours()).append("h").append(" "); + if (period.getMinutes() != 0) + builder.append(period.getMinutes()).append("m").append(" "); + if ((builder.length() > 0) && builder.charAt(builder.length()-1) == " ".charAt(0)) + builder.deleteCharAt(builder.length()-1); + return builder.toString(); + } + +} diff --git a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java new file mode 100644 index 0000000..03e1bd0 --- /dev/null +++ b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java @@ -0,0 +1,130 @@ +package net.rcarz.jiraclient; + +import net.sf.json.JSON; +import net.sf.json.JSONObject; +import net.sf.json.JSONSerializer; +import org.joda.time.DateTime; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * Created by mariusmerkevicius on 1/30/16. + */ +public class IssueWorklogTest { + + @Test + public void testParsing_inputValid_shouldCreateJsonObject() throws Exception { + // Arrange + // Act + JSONObject worklogObject = (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY); + + // Assert + assertNotNull(worklogObject); + } + + @Test + public void testParsing_inputValidJson_shouldCreateWorklog() throws Exception { + // Arrange + // Act + WorkLog workLog = new WorkLog(mock(RestClient.class), (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY)); + + // Assert + assertThat(workLog).isNotNull(); + assertThat(workLog.getAuthor()).isNotNull(); + assertThat(workLog.getSelf()).isEqualTo("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720"); + assertThat(workLog.getId()).isEqualTo("80720"); + assertThat(workLog.getComment()).isEqualTo("Test"); + assertThat(workLog.getCreatedDate().getTime()).isEqualTo(1454104800000L); + assertThat(workLog.getUpdatedDate().getTime()).isEqualTo(1454104800000L); + assertThat(workLog.getStarted().getTime()).isEqualTo(1453879853201L); + assertThat(workLog.getTimeSpent()).isEqualTo("5m"); + assertThat(workLog.getTimeSpentSeconds()).isEqualTo(300); + } + + @Test + public void testAdding_inputValid_shouldInvokeAdding() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + issue.addWorkLog("test", DateTime.now(), 60); + + // Assert + verify(issue.restclient).post(anyString(), any(JSON.class)); + } + + @Test + public void testAdding_inputNullComment_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog(null, DateTime.now(), 120); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } + + @Test + public void testAdding_inputNullDateTime_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog("asdf", null, 120); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } + + @Test + public void testAdding_inputDurationTooLow_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog("asdf", DateTime.now(), 30); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } + + + //region Constants + + // Mock response from jira + public static final String RESPONSE_WORKLOG_BODY = "{\"self\":\"https://jira.test.lt/rest/api/2/issue/32374/worklog/80720\",\"author\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"updateAuthor\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"comment\":\"Test\",\"created\":\"2016-01-30T20:46:16.583+0200\",\"updated\":\"2016-01-30T20:46:16.583+0200\",\"started\":\"2016-01-27T09:30:53.201+0200\",\"timeSpent\":\"5m\",\"timeSpentSeconds\":300,\"id\":\"80720\"}"; + + //endregion + + +} \ No newline at end of file diff --git a/src/test/java/net/rcarz/jiraclient/WorklogTest.java b/src/test/java/net/rcarz/jiraclient/WorklogTest.java index 2d6e12f..014fa57 100644 --- a/src/test/java/net/rcarz/jiraclient/WorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/WorklogTest.java @@ -59,11 +59,10 @@ public class WorklogTest { assertEquals("45517", workLog.getId()); String author = "joseph"; assertEquals(author, workLog.getAuthor().getName()); - final long expectedStartedUnixTimeStamp = 1439803140000L; //unix timestamp in millis of 2015-08-17T13:19:00.000+0400 - assertEquals(expectedStartedUnixTimeStamp, workLog.getStarted().getTime()); - final long expectedCreatedAndUpdatedUnitTimeStamp = 1440062384000L; //unix timestamp in millis of 2015-08-20T13:19:44.000+0400 - assertEquals(expectedCreatedAndUpdatedUnitTimeStamp, workLog.getCreatedDate().getTime()); - assertEquals(expectedCreatedAndUpdatedUnitTimeStamp, workLog.getUpdatedDate().getTime()); + String started = "2015-08-17T12:19:00.000"; + assertEquals(started, simpleDateFormat.format(workLog.getStarted())); + String created = "2015-08-20T00:00:00.000"; + assertEquals(created, simpleDateFormat.format(workLog.getCreatedDate())); assertEquals(21600, workLog.getTimeSpentSeconds()); assertEquals(author, workLog.getUpdateAuthor().getName()); } diff --git a/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java b/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java new file mode 100644 index 0000000..d7ca723 --- /dev/null +++ b/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java @@ -0,0 +1,71 @@ +package net.rcarz.utils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by mariusmerkevicius on 1/30/16. + */ +public class WorklogUtilsFormatDurationTest { + @Test + public void testEmpty() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(0)); + } + + @Test + public void testNegative() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(-200)); + } + + @Test public void testLowSecond() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(1)); + } + + @Test public void testSeconds() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(59)); + } + + @Test public void testMinutes() throws Exception { + assertEquals("1m", WorklogUtils.formatDurationFromSeconds(60)); + } + + @Test public void testMinutesAndSeconds() throws Exception { + assertEquals("1m", WorklogUtils.formatDurationFromSeconds( + 60 // 1 minute + + 2) // 2 seconds + ); + } + + @Test public void testMinutesAndSeconds2() throws Exception { + assertEquals("2m", WorklogUtils.formatDurationFromSeconds( + 60 // 1 minute + + 72) // 72 seconds + ); + } + + @Test public void testHours() throws Exception { + assertEquals("1h 10m", WorklogUtils.formatDurationFromSeconds( + (60 * 60) // 1 hour + + (10 * 60) // 10 minutes + + 3) // 3 seconds + ); + } + + @Test public void testDays() throws Exception { + assertEquals("50h 20m", WorklogUtils.formatDurationFromSeconds( + (60 * 60 * 50) // 50 hours + + (60 * 20) // 20 minutes + + (3) // s seconds + )); + } + + @Test public void testDays2() throws Exception { + assertEquals("50h 22m", WorklogUtils.formatDurationFromSeconds( + (60 * 60 * 50) // 50 hours + + (60 * 20) // 20 minutes + + (125) // 125 seconds + )); + } + +} \ No newline at end of file From 46c1a0f4aebf6c2698cc80c7f705ea99dda73900 Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 08:10:06 +0300 Subject: [PATCH 2/6] Formatting --- .../java/net/rcarz/utils/WorklogUtils.java | 47 ++--- .../rcarz/jiraclient/IssueWorklogTest.java | 187 +++++++++--------- .../utils/WorklogUtilsFormatDurationTest.java | 109 +++++----- 3 files changed, 175 insertions(+), 168 deletions(-) diff --git a/src/main/java/net/rcarz/utils/WorklogUtils.java b/src/main/java/net/rcarz/utils/WorklogUtils.java index cf0d44e..d50d29a 100644 --- a/src/main/java/net/rcarz/utils/WorklogUtils.java +++ b/src/main/java/net/rcarz/utils/WorklogUtils.java @@ -11,29 +11,30 @@ import org.joda.time.PeriodType; */ public class WorklogUtils { - /** - * Formats duration time into pretty string format - * Does not output seconds - * @param durationInSeconds provided duration to format - * @return formatted duration - */ - public static String formatDurationFromSeconds(long durationInSeconds) { - if (durationInSeconds < 60) - return "0m"; - StringBuilder builder = new StringBuilder(); - PeriodType type = PeriodType.forFields(new DurationFieldType[]{ - DurationFieldType.hours(), - DurationFieldType.minutes() - }); + /** + * Formats duration time into pretty string format + * Does not output seconds + * + * @param durationInSeconds provided duration to format + * @return formatted duration + */ + public static String formatDurationFromSeconds(long durationInSeconds) { + if (durationInSeconds < 60) + return "0m"; + StringBuilder builder = new StringBuilder(); + PeriodType type = PeriodType.forFields(new DurationFieldType[]{ + DurationFieldType.hours(), + DurationFieldType.minutes() + }); - Period period = new Period(1000 * durationInSeconds, type); - if (period.getHours() != 0) - builder.append(period.getHours()).append("h").append(" "); - if (period.getMinutes() != 0) - builder.append(period.getMinutes()).append("m").append(" "); - if ((builder.length() > 0) && builder.charAt(builder.length()-1) == " ".charAt(0)) - builder.deleteCharAt(builder.length()-1); - return builder.toString(); - } + Period period = new Period(1000 * durationInSeconds, type); + if (period.getHours() != 0) + builder.append(period.getHours()).append("h").append(" "); + if (period.getMinutes() != 0) + builder.append(period.getMinutes()).append("m").append(" "); + if ((builder.length() > 0) && builder.charAt(builder.length() - 1) == " ".charAt(0)) + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } } diff --git a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java index 03e1bd0..49bb1f1 100644 --- a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java @@ -7,124 +7,121 @@ import org.joda.time.DateTime; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doCallRealMethod; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; /** * Created by mariusmerkevicius on 1/30/16. */ public class IssueWorklogTest { - @Test - public void testParsing_inputValid_shouldCreateJsonObject() throws Exception { - // Arrange - // Act - JSONObject worklogObject = (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY); + @Test + public void testParsing_inputValid_shouldCreateJsonObject() throws Exception { + // Arrange + // Act + JSONObject worklogObject = (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY); - // Assert - assertNotNull(worklogObject); - } - - @Test - public void testParsing_inputValidJson_shouldCreateWorklog() throws Exception { - // Arrange - // Act - WorkLog workLog = new WorkLog(mock(RestClient.class), (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY)); - - // Assert - assertThat(workLog).isNotNull(); - assertThat(workLog.getAuthor()).isNotNull(); - assertThat(workLog.getSelf()).isEqualTo("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720"); - assertThat(workLog.getId()).isEqualTo("80720"); - assertThat(workLog.getComment()).isEqualTo("Test"); - assertThat(workLog.getCreatedDate().getTime()).isEqualTo(1454104800000L); - assertThat(workLog.getUpdatedDate().getTime()).isEqualTo(1454104800000L); - assertThat(workLog.getStarted().getTime()).isEqualTo(1453879853201L); - assertThat(workLog.getTimeSpent()).isEqualTo("5m"); - assertThat(workLog.getTimeSpentSeconds()).isEqualTo(300); - } - - @Test - public void testAdding_inputValid_shouldInvokeAdding() throws Exception { - // Arrange - Issue issue = mock(Issue.class); - issue.restclient = mock(RestClient.class); - doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); - - // Act - issue.addWorkLog("test", DateTime.now(), 60); - - // Assert - verify(issue.restclient).post(anyString(), any(JSON.class)); - } - - @Test - public void testAdding_inputNullComment_shouldNotAdd() throws Exception { - // Arrange - Issue issue = mock(Issue.class); - issue.restclient = mock(RestClient.class); - doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); - - // Act - try { - issue.addWorkLog(null, DateTime.now(), 120); - } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + // Assert + assertNotNull(worklogObject); } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); - } + @Test + public void testParsing_inputValidJson_shouldCreateWorklog() throws Exception { + // Arrange + // Act + WorkLog workLog = new WorkLog(mock(RestClient.class), (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY)); - @Test - public void testAdding_inputNullDateTime_shouldNotAdd() throws Exception { - // Arrange - Issue issue = mock(Issue.class); - issue.restclient = mock(RestClient.class); - doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); - - // Act - try { - issue.addWorkLog("asdf", null, 120); - } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + // Assert + assertThat(workLog).isNotNull(); + assertThat(workLog.getAuthor()).isNotNull(); + assertThat(workLog.getSelf()).isEqualTo("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720"); + assertThat(workLog.getId()).isEqualTo("80720"); + assertThat(workLog.getComment()).isEqualTo("Test"); + assertThat(workLog.getCreatedDate().getTime()).isEqualTo(1454104800000L); + assertThat(workLog.getUpdatedDate().getTime()).isEqualTo(1454104800000L); + assertThat(workLog.getStarted().getTime()).isEqualTo(1453879853201L); + assertThat(workLog.getTimeSpent()).isEqualTo("5m"); + assertThat(workLog.getTimeSpentSeconds()).isEqualTo(300); } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); - } + @Test + public void testAdding_inputValid_shouldInvokeAdding() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); - @Test - public void testAdding_inputDurationTooLow_shouldNotAdd() throws Exception { - // Arrange - Issue issue = mock(Issue.class); - issue.restclient = mock(RestClient.class); - doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + // Act + issue.addWorkLog("test", DateTime.now(), 60); - // Act - try { - issue.addWorkLog("asdf", DateTime.now(), 30); - } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + // Assert + verify(issue.restclient).post(anyString(), any(JSON.class)); } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); - } + @Test + public void testAdding_inputNullComment_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog(null, DateTime.now(), 120); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } + + @Test + public void testAdding_inputNullDateTime_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog("asdf", null, 120); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } + + @Test + public void testAdding_inputDurationTooLow_shouldNotAdd() throws Exception { + // Arrange + Issue issue = mock(Issue.class); + issue.restclient = mock(RestClient.class); + doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); + + // Act + try { + issue.addWorkLog("asdf", DateTime.now(), 30); + } catch (JiraException e) { + assertThat(e).hasMessageContaining("Failed add worklog to issue"); + } + + // Assert + verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + } - //region Constants + //region Constants - // Mock response from jira - public static final String RESPONSE_WORKLOG_BODY = "{\"self\":\"https://jira.test.lt/rest/api/2/issue/32374/worklog/80720\",\"author\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"updateAuthor\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"comment\":\"Test\",\"created\":\"2016-01-30T20:46:16.583+0200\",\"updated\":\"2016-01-30T20:46:16.583+0200\",\"started\":\"2016-01-27T09:30:53.201+0200\",\"timeSpent\":\"5m\",\"timeSpentSeconds\":300,\"id\":\"80720\"}"; + // Mock response from jira - //endregion + public static final String RESPONSE_WORKLOG_BODY = "{\"self\":\"https://jira.test.lt/rest/api/2/issue/32374/worklog/80720\",\"author\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"updateAuthor\":{\"self\":\"https://jira.test.lt/rest/api/2/user?username=test%40test.lt\",\"name\":\"test@test.lt\",\"key\":\"test@test.lt\",\"emailAddress\":\"test@test.lt\",\"avatarUrls\":{\"48x48\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=48\",\"24x24\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=24\",\"16x16\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=16\",\"32x32\":\"https://secure.gravatar.com/avatar/e4dacfe8f27cb89341bf990e556a4be0?d=mm&s=32\"},\"displayName\":\"Marius Merkevicius\",\"active\":true,\"timeZone\":\"Europe/Vilnius\"},\"comment\":\"Test\",\"created\":\"2016-01-30T20:46:16.583+0200\",\"updated\":\"2016-01-30T20:46:16.583+0200\",\"started\":\"2016-01-27T09:30:53.201+0200\",\"timeSpent\":\"5m\",\"timeSpentSeconds\":300,\"id\":\"80720\"}"; + //endregion } \ No newline at end of file diff --git a/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java b/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java index d7ca723..3447457 100644 --- a/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java +++ b/src/test/java/net/rcarz/utils/WorklogUtilsFormatDurationTest.java @@ -8,64 +8,73 @@ import static org.junit.Assert.assertEquals; * Created by mariusmerkevicius on 1/30/16. */ public class WorklogUtilsFormatDurationTest { - @Test - public void testEmpty() throws Exception { - assertEquals("0m", WorklogUtils.formatDurationFromSeconds(0)); - } - @Test - public void testNegative() throws Exception { - assertEquals("0m", WorklogUtils.formatDurationFromSeconds(-200)); - } + @Test + public void testEmpty() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(0)); + } - @Test public void testLowSecond() throws Exception { - assertEquals("0m", WorklogUtils.formatDurationFromSeconds(1)); - } + @Test + public void testNegative() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(-200)); + } - @Test public void testSeconds() throws Exception { - assertEquals("0m", WorklogUtils.formatDurationFromSeconds(59)); - } + @Test + public void testLowSecond() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(1)); + } - @Test public void testMinutes() throws Exception { - assertEquals("1m", WorklogUtils.formatDurationFromSeconds(60)); - } + @Test + public void testSeconds() throws Exception { + assertEquals("0m", WorklogUtils.formatDurationFromSeconds(59)); + } - @Test public void testMinutesAndSeconds() throws Exception { - assertEquals("1m", WorklogUtils.formatDurationFromSeconds( - 60 // 1 minute - + 2) // 2 seconds - ); - } + @Test + public void testMinutes() throws Exception { + assertEquals("1m", WorklogUtils.formatDurationFromSeconds(60)); + } - @Test public void testMinutesAndSeconds2() throws Exception { - assertEquals("2m", WorklogUtils.formatDurationFromSeconds( - 60 // 1 minute - + 72) // 72 seconds - ); - } + @Test + public void testMinutesAndSeconds() throws Exception { + assertEquals("1m", WorklogUtils.formatDurationFromSeconds( + 60 // 1 minute + + 2) // 2 seconds + ); + } - @Test public void testHours() throws Exception { - assertEquals("1h 10m", WorklogUtils.formatDurationFromSeconds( - (60 * 60) // 1 hour - + (10 * 60) // 10 minutes - + 3) // 3 seconds - ); - } + @Test + public void testMinutesAndSeconds2() throws Exception { + assertEquals("2m", WorklogUtils.formatDurationFromSeconds( + 60 // 1 minute + + 72) // 72 seconds + ); + } - @Test public void testDays() throws Exception { - assertEquals("50h 20m", WorklogUtils.formatDurationFromSeconds( - (60 * 60 * 50) // 50 hours - + (60 * 20) // 20 minutes - + (3) // s seconds - )); - } + @Test + public void testHours() throws Exception { + assertEquals("1h 10m", WorklogUtils.formatDurationFromSeconds( + (60 * 60) // 1 hour + + (10 * 60) // 10 minutes + + 3) // 3 seconds + ); + } - @Test public void testDays2() throws Exception { - assertEquals("50h 22m", WorklogUtils.formatDurationFromSeconds( - (60 * 60 * 50) // 50 hours - + (60 * 20) // 20 minutes - + (125) // 125 seconds - )); - } + @Test + public void testDays() throws Exception { + assertEquals("50h 20m", WorklogUtils.formatDurationFromSeconds( + (60 * 60 * 50) // 50 hours + + (60 * 20) // 20 minutes + + (3) // s seconds + )); + } + + @Test + public void testDays2() throws Exception { + assertEquals("50h 22m", WorklogUtils.formatDurationFromSeconds( + (60 * 60 * 50) // 50 hours + + (60 * 20) // 20 minutes + + (125) // 125 seconds + )); + } } \ No newline at end of file From 54e9a6f16e5e5fe29afddfe48a9e057e8aa23e38 Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 08:36:18 +0300 Subject: [PATCH 3/6] Update tests --- .../rcarz/jiraclient/IssueWorklogTest.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java index 49bb1f1..fe7b396 100644 --- a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java @@ -6,7 +6,7 @@ import net.sf.json.JSONSerializer; import org.joda.time.DateTime; import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; @@ -35,16 +35,16 @@ public class IssueWorklogTest { WorkLog workLog = new WorkLog(mock(RestClient.class), (JSONObject) JSONSerializer.toJSON(RESPONSE_WORKLOG_BODY)); // Assert - assertThat(workLog).isNotNull(); - assertThat(workLog.getAuthor()).isNotNull(); - assertThat(workLog.getSelf()).isEqualTo("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720"); - assertThat(workLog.getId()).isEqualTo("80720"); - assertThat(workLog.getComment()).isEqualTo("Test"); - assertThat(workLog.getCreatedDate().getTime()).isEqualTo(1454104800000L); - assertThat(workLog.getUpdatedDate().getTime()).isEqualTo(1454104800000L); - assertThat(workLog.getStarted().getTime()).isEqualTo(1453879853201L); - assertThat(workLog.getTimeSpent()).isEqualTo("5m"); - assertThat(workLog.getTimeSpentSeconds()).isEqualTo(300); + assertNotNull(workLog); + assertNotNull(workLog.getAuthor()); + assertEquals("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720", workLog.getSelf()); + assertEquals("80720", workLog.getId()); + assertEquals("Test", workLog.getComment()); + assertEquals(1454104800000L, workLog.getCreatedDate().getTime()); + assertEquals(1454104800000L, workLog.getUpdatedDate().getTime()); + assertEquals(1453879853201L, workLog.getStarted().getTime()); + assertEquals("5m", workLog.getTimeSpent()); + assertEquals(300, workLog.getTimeSpentSeconds()); } @Test @@ -72,7 +72,7 @@ public class IssueWorklogTest { try { issue.addWorkLog(null, DateTime.now(), 120); } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + assertEquals("Failed add worklog to issue", e.getMessage()); } // Assert @@ -90,7 +90,7 @@ public class IssueWorklogTest { try { issue.addWorkLog("asdf", null, 120); } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + assertEquals("Failed add worklog to issue", e.getMessage()); } // Assert @@ -108,7 +108,7 @@ public class IssueWorklogTest { try { issue.addWorkLog("asdf", DateTime.now(), 30); } catch (JiraException e) { - assertThat(e).hasMessageContaining("Failed add worklog to issue"); + assertEquals("Failed add worklog to issue", e.getMessage()); } // Assert From 97a48432c6f4a4a84435c1714e83505b6765a293 Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 08:50:12 +0300 Subject: [PATCH 4/6] Update test --- .../rcarz/jiraclient/IssueWorklogTest.java | 34 +++++-------------- .../net/rcarz/jiraclient/WorklogTest.java | 2 +- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java index fe7b396..f6fd2d5 100644 --- a/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/IssueWorklogTest.java @@ -40,8 +40,8 @@ public class IssueWorklogTest { assertEquals("https://jira.test.lt/rest/api/2/issue/32374/worklog/80720", workLog.getSelf()); assertEquals("80720", workLog.getId()); assertEquals("Test", workLog.getComment()); - assertEquals(1454104800000L, workLog.getCreatedDate().getTime()); - assertEquals(1454104800000L, workLog.getUpdatedDate().getTime()); + assertEquals(1454179576583L, workLog.getCreatedDate().getTime()); + assertEquals(1454179576583L, workLog.getUpdatedDate().getTime()); assertEquals(1453879853201L, workLog.getStarted().getTime()); assertEquals("5m", workLog.getTimeSpent()); assertEquals(300, workLog.getTimeSpentSeconds()); @@ -61,7 +61,7 @@ public class IssueWorklogTest { verify(issue.restclient).post(anyString(), any(JSON.class)); } - @Test + @Test(expected = JiraException.class) public void testAdding_inputNullComment_shouldNotAdd() throws Exception { // Arrange Issue issue = mock(Issue.class); @@ -69,17 +69,11 @@ public class IssueWorklogTest { doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); // Act - try { - issue.addWorkLog(null, DateTime.now(), 120); - } catch (JiraException e) { - assertEquals("Failed add worklog to issue", e.getMessage()); - } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + issue.addWorkLog(null, DateTime.now(), 120); } - @Test + @Test(expected = JiraException.class) public void testAdding_inputNullDateTime_shouldNotAdd() throws Exception { // Arrange Issue issue = mock(Issue.class); @@ -87,17 +81,11 @@ public class IssueWorklogTest { doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); // Act - try { - issue.addWorkLog("asdf", null, 120); - } catch (JiraException e) { - assertEquals("Failed add worklog to issue", e.getMessage()); - } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + issue.addWorkLog("asdf", null, 120); } - @Test + @Test(expected = JiraException.class) public void testAdding_inputDurationTooLow_shouldNotAdd() throws Exception { // Arrange Issue issue = mock(Issue.class); @@ -105,14 +93,8 @@ public class IssueWorklogTest { doCallRealMethod().when(issue).addWorkLog(anyString(), any(DateTime.class), anyLong()); // Act - try { - issue.addWorkLog("asdf", DateTime.now(), 30); - } catch (JiraException e) { - assertEquals("Failed add worklog to issue", e.getMessage()); - } - // Assert - verify(issue.restclient, never()).post(anyString(), any(JSON.class)); + issue.addWorkLog("asdf", DateTime.now(), 30); } diff --git a/src/test/java/net/rcarz/jiraclient/WorklogTest.java b/src/test/java/net/rcarz/jiraclient/WorklogTest.java index 014fa57..be3e4f1 100644 --- a/src/test/java/net/rcarz/jiraclient/WorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/WorklogTest.java @@ -61,7 +61,7 @@ public class WorklogTest { assertEquals(author, workLog.getAuthor().getName()); String started = "2015-08-17T12:19:00.000"; assertEquals(started, simpleDateFormat.format(workLog.getStarted())); - String created = "2015-08-20T00:00:00.000"; + String created = "2015-08-20T12:19:44.000"; assertEquals(created, simpleDateFormat.format(workLog.getCreatedDate())); assertEquals(21600, workLog.getTimeSpentSeconds()); assertEquals(author, workLog.getUpdateAuthor().getName()); From 8b7b527069816777ba0f8611c94779fc59d0273b Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 09:03:00 +0300 Subject: [PATCH 5/6] Test update --- src/test/java/net/rcarz/jiraclient/WorklogTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/rcarz/jiraclient/WorklogTest.java b/src/test/java/net/rcarz/jiraclient/WorklogTest.java index be3e4f1..2d2ce68 100644 --- a/src/test/java/net/rcarz/jiraclient/WorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/WorklogTest.java @@ -10,6 +10,7 @@ import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.Locale; import static junit.framework.Assert.assertEquals; import static org.mockito.Matchers.anyString; @@ -17,7 +18,7 @@ import static org.mockito.Matchers.anyString; @RunWith(PowerMockRunner.class) public class WorklogTest { - private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH); @Test(expected = JiraException.class) public void testJiraExceptionFromRestException() throws Exception { From 4ac69cd66bed704dc204ed9f1406475c261c7df4 Mon Sep 17 00:00:00 2001 From: Marius Merkevicius Date: Mon, 17 Oct 2016 09:24:38 +0300 Subject: [PATCH 6/6] Test update --- src/test/java/net/rcarz/jiraclient/WorklogTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/net/rcarz/jiraclient/WorklogTest.java b/src/test/java/net/rcarz/jiraclient/WorklogTest.java index 2d2ce68..43b513b 100644 --- a/src/test/java/net/rcarz/jiraclient/WorklogTest.java +++ b/src/test/java/net/rcarz/jiraclient/WorklogTest.java @@ -18,7 +18,7 @@ import static org.mockito.Matchers.anyString; @RunWith(PowerMockRunner.class) public class WorklogTest { - private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH); + private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Field.DATETIME_FORMAT); @Test(expected = JiraException.class) public void testJiraExceptionFromRestException() throws Exception { @@ -49,7 +49,7 @@ public class WorklogTest { } @Test - public void testWorklog() { + public void testWorklog() throws Exception { List workLogs = Field.getResourceArray(WorkLog.class, Utils.getTestIssueWorklogs().get("worklogs"), null); assertEquals(2, workLogs.size()); @@ -60,10 +60,10 @@ public class WorklogTest { assertEquals("45517", workLog.getId()); String author = "joseph"; assertEquals(author, workLog.getAuthor().getName()); - String started = "2015-08-17T12:19:00.000"; - assertEquals(started, simpleDateFormat.format(workLog.getStarted())); - String created = "2015-08-20T12:19:44.000"; - assertEquals(created, simpleDateFormat.format(workLog.getCreatedDate())); + String started = "2015-08-17T13:19:00.000+0400"; + assertEquals(simpleDateFormat.parse(started), workLog.getStarted()); + String created = "2015-08-20T13:19:44.000+0400"; + assertEquals(simpleDateFormat.parse(created), workLog.getCreatedDate()); assertEquals(21600, workLog.getTimeSpentSeconds()); assertEquals(author, workLog.getUpdateAuthor().getName()); }