1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.jira.rest.client;
18
19 import com.atlassian.jira.rest.client.api.RestClientException;
20 import com.atlassian.jira.rest.client.api.domain.OperationGroup;
21 import com.atlassian.jira.rest.client.api.domain.OperationLink;
22 import com.atlassian.jira.rest.client.api.domain.Transition;
23 import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
24 import com.google.common.collect.Iterators;
25 import junit.framework.Assert;
26 import org.apache.commons.lang.StringUtils;
27 import org.hamcrest.Matchers;
28 import org.joda.time.DateTime;
29 import org.joda.time.DateTimeZone;
30 import org.joda.time.format.DateTimeFormatter;
31 import org.joda.time.format.ISODateTimeFormat;
32
33 import javax.annotation.Nullable;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriBuilder;
36 import java.net.URI;
37 import java.util.Collection;
38 import java.util.Collections;
39
40 import static com.google.common.collect.Iterators.getOnlyElement;
41
42 public class TestUtil {
43 private static DateTimeFormatter universalDateTimeParser = ISODateTimeFormat.dateTimeParser();
44 private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
45 private static DateTimeFormatter dateFormatter = ISODateTimeFormat.date();
46 public static Iterable<OperationGroup> EMPTY_GROUPS = Collections.emptyList();
47 public static Iterable<OperationLink> EMPTY_LINKS = Collections.emptyList();
48
49 public static URI toUri(String str) {
50 return UriBuilder.fromUri(str).build();
51 }
52
53 public static DateTime toDateTime(String isoDateTimeSt) {
54 return universalDateTimeParser.parseDateTime(isoDateTimeSt);
55 }
56
57 @SuppressWarnings("unused")
58 public static DateTime toDateTime(String isoDateTimeSt, DateTimeZone zone) {
59 return formatter.withZone(zone).parseDateTime(isoDateTimeSt);
60 }
61
62 public static DateTime toDateTimeFromIsoDate(String isoDate) {
63 return dateFormatter.parseDateTime(isoDate);
64 }
65
66 public static void assertErrorCode(int errorCode, Runnable runnable) {
67 assertErrorCode(errorCode, StringUtils.EMPTY, runnable);
68 }
69
70 @SuppressWarnings("unused")
71 public static <T extends Throwable> void assertThrows(Class<T> clazz, String regexp, Runnable runnable) {
72 try {
73 runnable.run();
74 Assert.fail(clazz.getName() + " exception expected");
75 } catch (Throwable e) {
76 Assert.assertTrue("Expected exception of class " + clazz.getName() + " but was caught " + e.getClass().getName(),
77 clazz.isInstance(e));
78 if (e.getMessage() == null && regexp != null) {
79 Assert.fail("Exception with no message caught, while expected regexp [" + regexp + "]");
80 }
81 if (regexp != null && e.getMessage() != null) {
82 Assert.assertTrue("Message [" + e.getMessage() + "] does not match regexp [" + regexp + "]", e.getMessage()
83 .matches(regexp));
84 }
85 }
86
87 }
88
89 public static void assertErrorCode(Response.Status status, String message, Runnable runnable) {
90 assertErrorCode(status.getStatusCode(), message, runnable);
91 }
92
93 public static void assertExpectedErrorCollection(final Collection<ErrorCollection> errors, final Runnable runnable) {
94 assertExpectedErrors(errors, runnable);
95 }
96
97 public static void assertErrorCodeWithRegexp(Response.Status status, String regexp, Runnable runnable) {
98 assertErrorCodeWithRegexp(status.getStatusCode(), regexp, runnable);
99 }
100
101 public static void assertErrorCode(Response.Status status, Runnable runnable) {
102 assertErrorCode(status.getStatusCode(), null, runnable);
103 }
104
105 public static void assertErrorCode(int errorCode, String message, Runnable runnable) {
106 try {
107 runnable.run();
108 Assert.fail(RestClientException.class + " exception expected");
109 } catch (com.atlassian.jira.rest.client.api.RestClientException e) {
110 Assert.assertTrue(e.getStatusCode().isPresent());
111 Assert.assertEquals(errorCode, e.getStatusCode().get().intValue());
112 if (!StringUtils.isEmpty(message)) {
113
114 Assert.assertEquals(1, e.getErrorCollections().size());
115 if (Iterators.getOnlyElement(e.getErrorCollections().iterator()).getErrorMessages().size() > 0) {
116 Assert.assertEquals(getOnlyElement(getOnlyElement(e.getErrorCollections().iterator()).getErrorMessages()
117 .iterator()), message);
118 } else if (Iterators.getOnlyElement(e.getErrorCollections().iterator()).getErrors().size() > 0) {
119 Assert.assertEquals(getOnlyElement(getOnlyElement(e.getErrorCollections().iterator()).getErrors().values()
120 .iterator()), message);
121 } else {
122 Assert.fail("Expected an error message.");
123 }
124 }
125 }
126 }
127
128 public static void assertErrorCodeWithRegexp(int errorCode, String regExp, Runnable runnable) {
129 try {
130 runnable.run();
131 Assert.fail(RestClientException.class + " exception expected");
132 } catch (com.atlassian.jira.rest.client.api.RestClientException ex) {
133 final ErrorCollection errorElement = getOnlyElement(ex.getErrorCollections().iterator());
134 final String errorMessage = getOnlyElement(errorElement.getErrorMessages().iterator());
135 Assert.assertTrue("'" + ex.getMessage() + "' does not match regexp '" + regExp + "'", errorMessage.matches(regExp));
136 Assert.assertTrue(ex.getStatusCode().isPresent());
137 Assert.assertEquals(errorCode, ex.getStatusCode().get().intValue());
138 }
139 }
140
141
142 public static String getLastPathSegment(URI uri) {
143 final String path = uri.getPath();
144 final int index = path.lastIndexOf('/');
145 if (index == -1) {
146 return path;
147 }
148 if (index == path.length()) {
149 return "";
150 }
151 return path.substring(index + 1);
152 }
153
154 public static <E> void assertEqualsSymmetrical(E a, E b) {
155 Assert.assertEquals(a, b);
156 Assert.assertEquals(b, a);
157 }
158
159 public static <E> void assertNotEquals(E a, E b) {
160 if (a == null) {
161 Assert.assertFalse("[" + a + "] not equals [" + b + "]", b.equals(a));
162 } else if (b == null) {
163 Assert.assertFalse("[" + a + "] not equals [" + b + "]", a.equals(b));
164 } else if (a.equals(b) || b.equals(a)) {
165 Assert.fail("[" + a + "] not equals [" + b + "]");
166 }
167 }
168
169 @Nullable
170 public static Transition getTransitionByName(Iterable<Transition> transitions, String transitionName) {
171 Transition transitionFound = null;
172 for (Transition transition : transitions) {
173 if (transition.getName().equals(transitionName)) {
174 transitionFound = transition;
175 break;
176 }
177 }
178 return transitionFound;
179 }
180
181 private static void assertExpectedErrors(final Collection<ErrorCollection> expectedErrors, final Runnable runnable) {
182 try {
183 runnable.run();
184 Assert.fail(RestClientException.class + " exception expected");
185 } catch (com.atlassian.jira.rest.client.api.RestClientException e) {
186 Assert.assertEquals(e.getErrorCollections(), expectedErrors);
187 }
188 }
189
190 public static <K> void assertEmptyIterable(Iterable<K> iterable) {
191 org.junit.Assert.assertThat(iterable, Matchers.<K>emptyIterable());
192 }
193 }