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;
18  
19  import com.sun.jersey.api.client.UniformInterfaceException;
20  import junit.framework.Assert;
21  import org.joda.time.DateTime;
22  import org.joda.time.format.DateTimeFormatter;
23  import org.joda.time.format.ISODateTimeFormat;
24  
25  import javax.ws.rs.core.Response;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  
29  public class TestUtil {
30  	private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
31  
32  	public static URI toUri(String str) {
33  		try {
34  			return new URI(str);
35  		} catch (URISyntaxException e) {
36  			throw new RuntimeException(e);
37  		}
38  	}
39  
40  	public static DateTime toDateTime(String isoDateTimeSt) {
41  		return formatter.parseDateTime(isoDateTimeSt);
42  	}
43  
44  	public static void assertErrorCode(int errorCode, Runnable runnable) {
45  		assertErrorCode(errorCode, null, runnable);
46  	}
47  
48  	public static <T extends Throwable> void assertThrows(Class<T> clazz, String regexp, Runnable runnable) {
49  		try {
50  			runnable.run();
51  			Assert.fail(clazz.getName() + " exception expected");
52  		} catch (Throwable e) {
53  			Assert.assertTrue("Expected exception of class " + clazz.getName() + " but was caught " + e.getClass().getName(),
54  					clazz.isInstance(e));
55  			if (e.getMessage() == null && regexp != null) {
56  				Assert.fail("Exception with no message caught, while expected regexp [" + regexp + "]");
57  			}
58  			if (regexp != null && e.getMessage() != null) {
59  				Assert.assertTrue("Message [" + e.getMessage() + "] does not match regexp [" + regexp + "]", e.getMessage().matches(regexp));
60  			}
61  		}
62  
63  	}
64  
65  
66  	public static void assertErrorCode(Response.Status status, String message, Runnable runnable) {
67  		assertErrorCode(status.getStatusCode(), message, runnable);
68  	}
69  
70  	public static void assertErrorCodeWithRegexp(Response.Status status, String regexp, Runnable runnable) {
71  		assertErrorCodeWithRegexp(status.getStatusCode(), regexp, runnable);
72  	}
73  
74  	public static void assertErrorCode(Response.Status status, Runnable runnable) {
75  		assertErrorCode(status.getStatusCode(), null, runnable);
76  	}
77  
78  	public static void assertErrorCode(int errorCode, String message, Runnable runnable) {
79  		try {
80  			runnable.run();
81  			Assert.fail(UniformInterfaceException.class + " exception expected");
82  		} catch (UniformInterfaceException e) {
83  			final String msg = e.getResponse().getEntity(String.class);
84  			if (errorCode != e.getResponse().getStatus()) {
85  				Assert.fail("Unexpected error code and message [" + msg
86  						+ "]. Expected [" + errorCode + "], actual [" + e.getResponse().getStatus() + "]");
87  			}
88  //			Assert.assertEquals(errorCode, );
89  		} catch (RestClientException e) {
90  			Assert.assertTrue("Expected UniformInterfaceException cause, but was [" + e.getCause() + "]", e.getCause() instanceof UniformInterfaceException);
91  			Assert.assertEquals(errorCode, ((UniformInterfaceException) e.getCause()).getResponse().getStatus());
92  			if (message != null) {
93  				Assert.assertEquals(message, e.getMessage());
94  			}
95  		}
96  	}
97  
98  	public static void assertErrorCodeWithRegexp(int errorCode, String regExp, Runnable runnable) {
99  		try {
100 			runnable.run();
101 			Assert.fail(UniformInterfaceException.class + " exception expected");
102 		} catch (UniformInterfaceException e) {
103 			Assert.assertEquals(errorCode, e.getResponse().getStatus());
104 		} catch (RestClientException e) {
105 			Assert.assertTrue(e.getCause() instanceof UniformInterfaceException);
106 			Assert.assertEquals(errorCode, ((UniformInterfaceException) e.getCause()).getResponse().getStatus());
107 			Assert.assertTrue("'" + e.getMessage() + "' does not match regexp '" + regExp + "'", e.getMessage().matches(regExp));
108 		}
109 	}
110 
111 
112 	public static String getLastPathSegment(URI uri) {
113 		final String path = uri.getPath();
114 		final int index = path.lastIndexOf('/');
115 		if (index == -1) {
116 			return path;
117 		}
118 		if (index == path.length()) {
119 			return "";
120 		}
121 		return path.substring(index + 1);
122 	}
123 
124 	public static <E> void assertEqualsSymmetrical(E a, E b) {
125 		Assert.assertEquals(a, b);
126 		Assert.assertEquals(b, a);
127 	}
128 
129 	public static <E> void assertNotEquals(E a, E b) {
130 		if (a == null) {
131 			Assert.assertFalse("[" + a + "] not equals [" + b + "]", b.equals(a));
132 		} else if (b == null) {
133 			Assert.assertFalse("[" + a + "] not equals [" + b + "]", a.equals(b));
134 		} else if (a.equals(b) || b.equals(a)) {
135 			Assert.fail("[" + a + "] not equals [" + b + "]");
136 		}
137 	}
138 
139 }