1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.TestUtil;
20  import com.atlassian.jira.rest.client.domain.Visibility;
21  import com.atlassian.jira.rest.client.domain.Worklog;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  import static com.atlassian.jira.rest.client.TestUtil.toUri;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNull;
28  
29  public class WorklogJsonParserTest {
30      @Test
31      public void testParse() throws Exception {
32          final WorklogJsonParser parser = new WorklogJsonParser();
33          final Worklog worklog = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/worklog/valid.json"));
34          assertEquals(toUri("http://localhost:8090/jira/rest/api/latest/worklog/10010"), worklog.getSelf());
35          assertEquals(toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-2"), worklog.getIssueUri());
36          assertEquals(TestConstants.USER_ADMIN, worklog.getAuthor());
37          assertEquals(TestConstants.USER_ADMIN, worklog.getUpdateAuthor());
38          assertEquals("my first work", worklog.getComment());
39          assertEquals(TestUtil.toDateTime("2010-08-17T16:35:47.466+0200"), worklog.getCreationDate());
40          assertEquals(TestUtil.toDateTime("2010-08-17T16:35:47.466+0200"), worklog.getUpdateDate());
41          assertEquals(TestUtil.toDateTime("2010-08-15T16:35:00.000+0200"), worklog.getStartDate());
42          assertEquals(60, worklog.getMinutesSpent());
43          Assert.assertNull(worklog.getVisibility());
44      }
45  
46      @Test
47      public void testParseWithRoleLevel() throws Exception {
48          final WorklogJsonParser parser = new WorklogJsonParser();
49          final Worklog worklog = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/worklog/valid-roleLevel.json"));
50          assertEquals(toUri("http://localhost:8090/jira/rest/api/latest/worklog/10011"), worklog.getSelf());
51          assertEquals(toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-2"), worklog.getIssueUri());
52          assertEquals(TestConstants.USER1, worklog.getAuthor());
53          assertEquals(TestConstants.USER1, worklog.getUpdateAuthor());
54          assertEquals("another piece of work", worklog.getComment());
55          assertEquals(TestUtil.toDateTime("2010-08-17T16:38:00.013+0200"), worklog.getCreationDate());
56          assertEquals(TestUtil.toDateTime("2010-08-17T16:38:24.948+0200"), worklog.getUpdateDate());
57          assertEquals(TestUtil.toDateTime("2010-08-17T16:37:00.000+0200"), worklog.getStartDate());
58          assertEquals(Visibility.role("Developers"), worklog.getVisibility());
59          assertEquals(15, worklog.getMinutesSpent());
60      }
61  
62      @Test
63      public void testParseWithGroupLevel() throws Exception {
64          final WorklogJsonParser parser = new WorklogJsonParser();
65          final Worklog worklog = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/worklog/valid-groupLevel.json"));
66          assertEquals(Visibility.group("jira-users"), worklog.getVisibility());
67      }
68  
69  	@Test
70  	public void testParseWhenAuthorIsAnonymous() throws Exception {
71  		final WorklogJsonParser parser = new WorklogJsonParser();
72  		final Worklog worklog = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/worklog/valid-anonymousAuthor.json"));
73  		assertNull(worklog.getAuthor());
74  		assertNull(worklog.getUpdateAuthor());
75  	}
76  
77  	@Test
78  	public void testParseWhenAuthorIsAnonymousInOldRepresentation() throws Exception {
79  		final WorklogJsonParser parser = new WorklogJsonParser();
80  		final Worklog worklog = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/worklog/valid-anonymousAuthor-oldRepresentation.json"));
81  		assertNull(worklog.getAuthor());
82  		assertNull(worklog.getUpdateAuthor());
83  	}
84  
85  }