1   /*
2    * Copyright (C) 2012 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.gen;
18  
19  import com.atlassian.jira.rest.client.JSONObjectMatcher;
20  import com.atlassian.jira.rest.client.domain.BasicUser;
21  import com.atlassian.jira.rest.client.domain.Visibility;
22  import com.atlassian.jira.rest.client.domain.input.WorklogInput;
23  import com.atlassian.jira.rest.client.internal.json.JsonParseUtil;
24  import com.atlassian.jira.rest.client.internal.json.ResourceUtil;
25  import org.codehaus.jettison.json.JSONException;
26  import org.joda.time.DateTimeZone;
27  import org.junit.Test;
28  
29  import java.net.URI;
30  import java.net.URISyntaxException;
31  
32  import static com.atlassian.jira.rest.client.TestUtil.toUri;
33  import static org.junit.Assert.*;
34  
35  public class WorklogInputJsonGeneratorTest {
36  
37  	private final BasicUser USER;
38  	private final BasicUser ADMIN;
39  	private final WorklogInputJsonGenerator generator = new WorklogInputJsonGenerator(
40  			JsonParseUtil.JIRA_DATE_TIME_FORMATTER.withZone(DateTimeZone.forID("+02:00"))
41  	);
42  
43  	public WorklogInputJsonGeneratorTest() throws URISyntaxException {
44  		USER = new BasicUser(new URI("http://localhost:2990/jira/rest/api/2/user?username=wseliga"), "wseliga", "Wojciech Seliga");
45  		ADMIN = new BasicUser(new URI("http://localhost:2990/jira/rest/api/2/user?username=admin"), "admin", "Administrator");
46  	}
47  
48  	@Test
49  	public void testGenerate() throws JSONException {
50  		final WorklogInput worklogInput = new WorklogInput(
51  				toUri("http://localhost:8090/jira/rest/api/latest/worklog/10010"),
52  				toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-2"), USER, ADMIN, "my first work",
53  				JsonParseUtil.parseDateTime("2010-08-15T16:35:00.000+0200"), 60, Visibility.group("some-group"));
54  
55  		assertThat(generator.generate(worklogInput), JSONObjectMatcher.isEqual(
56  				ResourceUtil.getJsonObjectFromResource("/json/worklogInput/valid.json")));
57  	}
58  
59  	@Test
60  	public void testGenerateWithoutVisibility() throws JSONException {
61  		final WorklogInput worklogInput = new WorklogInput(
62  				toUri("http://localhost:8090/jira/rest/api/latest/worklog/10010"),
63  				toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-2"), ADMIN, USER, "my first work",
64  				JsonParseUtil.parseDateTime("2010-08-15T16:35:00.000+0200"), 60, null);
65  
66  		assertThat(generator.generate(worklogInput), JSONObjectMatcher.isEqual(
67  				ResourceUtil.getJsonObjectFromResource("/json/worklogInput/valid-without-visibility.json")));
68  	}
69  
70  	@Test
71  	public void testGenerateWithoutAuthorAndUpdateAuthor() throws JSONException {
72  		final WorklogInput worklogInput = new WorklogInput(
73  				toUri("http://localhost:8090/jira/rest/api/latest/worklog/10010"),
74  				toUri("http://localhost:8090/jira/rest/api/latest/issue/TST-2"), null, null, "my first work",
75  				JsonParseUtil.parseDateTime("2010-08-15T16:35:00.000+0200"), 60, Visibility.group("some-group"));
76  
77  		assertThat(generator.generate(worklogInput), JSONObjectMatcher.isEqual(
78  				ResourceUtil.getJsonObjectFromResource("/json/worklogInput/valid-without-users.json")));
79  	}
80  }