|
1 | 1 | package com.paypal.core.rest; |
2 | 2 |
|
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
3 | 6 | import org.testng.Assert; |
4 | 7 | import org.testng.annotations.Test; |
5 | 8 |
|
6 | 9 | public class RESTUtilTest { |
7 | | - |
| 10 | + |
8 | 11 | @Test() |
9 | 12 | public void testFormatURIPathForNull() { |
10 | | - String nullString = RESTUtil.formatURIPath((String) null, (Object []) null); |
| 13 | + String nullString = RESTUtil.formatURIPath((String) null, |
| 14 | + (Object[]) null); |
11 | 15 | Assert.assertNull(nullString); |
12 | 16 | } |
13 | | - |
14 | | - @Test(dependsOnMethods = {"testFormatURIPathForNull"}) |
| 17 | + |
| 18 | + @Test(dependsOnMethods = { "testFormatURIPathForNull" }) |
15 | 19 | public void testFormatURIPathNoPattern() { |
16 | 20 | String pattern = "/a/b/c"; |
17 | | - String uriPath = RESTUtil.formatURIPath(pattern, (Object []) null); |
| 21 | + String uriPath = RESTUtil.formatURIPath(pattern, (Object[]) null); |
18 | 22 | Assert.assertEquals(uriPath, pattern); |
19 | 23 | } |
20 | | - |
21 | | - @Test(dependsOnMethods = {"testFormatURIPathNoPattern"}) |
| 24 | + |
| 25 | + @Test(dependsOnMethods = { "testFormatURIPathNoPattern" }) |
22 | 26 | public void testFormatURIPathNoQS() { |
23 | 27 | String pattern = "/a/b/{0}"; |
24 | | - Object[] parameters = new Object[] {"replace"}; |
| 28 | + Object[] parameters = new Object[] { "replace" }; |
25 | 29 | String uriPath = RESTUtil.formatURIPath(pattern, parameters); |
26 | 30 | Assert.assertEquals(uriPath, "/a/b/replace"); |
27 | 31 | } |
28 | | - |
29 | | - @Test(dependsOnMethods = {"testFormatURIPathNoQS"}) |
| 32 | + |
| 33 | + @Test(dependsOnMethods = { "testFormatURIPathNoQS" }) |
30 | 34 | public void testFormatURIPath() { |
31 | 35 | String pattern = "/a/b/{0}?name={1}"; |
32 | | - Object[] parameters = new Object[] {"replace", "nameValue"}; |
| 36 | + Object[] parameters = new Object[] { "replace", "nameValue" }; |
33 | 37 | String uriPath = RESTUtil.formatURIPath(pattern, parameters); |
34 | 38 | Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue"); |
35 | 39 | } |
36 | | - |
37 | | - @Test(dependsOnMethods = {"testFormatURIPath"}) |
| 40 | + |
| 41 | + @Test(dependsOnMethods = { "testFormatURIPath" }) |
38 | 42 | public void testFormatURIPathWithNull() { |
39 | 43 | String pattern = "/a/b/{0}?name={1}&age={2}"; |
40 | | - Object[] parameters = new Object[] {"replace", "nameValue", null}; |
| 44 | + Object[] parameters = new Object[] { "replace", "nameValue", null }; |
41 | 45 | String uriPath = RESTUtil.formatURIPath(pattern, parameters); |
42 | 46 | Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue"); |
43 | 47 | } |
44 | | - |
45 | | - @Test(dependsOnMethods = {"testFormatURIPathWithNull"}) |
| 48 | + |
| 49 | + @Test(dependsOnMethods = { "testFormatURIPathWithNull" }) |
46 | 50 | public void testFormatURIPathWithEmpty() { |
47 | 51 | String pattern = "/a/b/{0}?name={1}&age="; |
48 | | - Object[] parameters = new Object[] {"replace", "nameValue", null}; |
| 52 | + Object[] parameters = new Object[] { "replace", "nameValue", null }; |
49 | 53 | String uriPath = RESTUtil.formatURIPath(pattern, parameters); |
50 | 54 | Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue"); |
51 | 55 | } |
52 | | - |
53 | | - @Test(dependsOnMethods = {"testFormatURIPathWithEmpty"}) |
| 56 | + |
| 57 | + @Test(dependsOnMethods = { "testFormatURIPathWithEmpty" }) |
54 | 58 | public void testFormatURIPathTwoQS() { |
55 | 59 | String pattern = "/a/b/{0}?name={1}&age={2}"; |
56 | | - Object[] parameters = new Object[] {"replace", "nameValue", "1"}; |
| 60 | + Object[] parameters = new Object[] { "replace", "nameValue", "1" }; |
57 | 61 | String uriPath = RESTUtil.formatURIPath(pattern, parameters); |
58 | 62 | Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue&age=1"); |
59 | 63 | } |
60 | 64 |
|
| 65 | + @Test(dependsOnMethods = { "testFormatURIPathTwoQS" }) |
| 66 | + public void testFormatURIPathMap() throws PayPalRESTException { |
| 67 | + String pattern = "/a/b/{first}/{second}"; |
| 68 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 69 | + pathParameters.put("first", "value1"); |
| 70 | + pathParameters.put("second", "value2"); |
| 71 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters); |
| 72 | + Assert.assertEquals(uriPath, "/a/b/value1/value2"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test(dependsOnMethods = { "testFormatURIPathMap" }) |
| 76 | + public void testFormatURIPathMapTraillingSlash() throws PayPalRESTException { |
| 77 | + String pattern = "/a/b/{first}/{second}/"; |
| 78 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 79 | + pathParameters.put("first", "value1"); |
| 80 | + pathParameters.put("second", "value2"); |
| 81 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters); |
| 82 | + Assert.assertEquals(uriPath, "/a/b/value1/value2/"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test(dependsOnMethods = { "testFormatURIPathMapTraillingSlash" }) |
| 86 | + public void testFormatURIPathMapNullMap() throws PayPalRESTException { |
| 87 | + String pattern = "/a/b/first/second"; |
| 88 | + String uriPath = RESTUtil.formatURIPath(pattern, |
| 89 | + (Map<String, String>) null); |
| 90 | + Assert.assertEquals(uriPath, "/a/b/first/second"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(dependsOnMethods = { "testFormatURIPathMapNullMap" }) |
| 94 | + public void testFormatURIPathMapIncorrectMap() throws PayPalRESTException { |
| 95 | + String pattern = "/a/b/first/second"; |
| 96 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 97 | + pathParameters.put("invalid1", "value1"); |
| 98 | + pathParameters.put("invalid2", "value2"); |
| 99 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters); |
| 100 | + Assert.assertEquals(uriPath, "/a/b/first/second"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test(dependsOnMethods = { "testFormatURIPathMapIncorrectMap" }, expectedExceptions = PayPalRESTException.class) |
| 104 | + public void testFormatURIPathMapInsufficientMap() |
| 105 | + throws PayPalRESTException { |
| 106 | + String pattern = "/a/b/{first}/{second}"; |
| 107 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 108 | + pathParameters.put("first", "value1"); |
| 109 | + RESTUtil.formatURIPath(pattern, pathParameters); |
| 110 | + } |
| 111 | + |
| 112 | + @Test(dependsOnMethods = { "testFormatURIPathMapInsufficientMap" }) |
| 113 | + public void testFormatURIPathMapNullQueryMap() throws PayPalRESTException { |
| 114 | + String pattern = "/a/b/{first}/{second}"; |
| 115 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 116 | + pathParameters.put("first", "value1"); |
| 117 | + pathParameters.put("second", "value2"); |
| 118 | + Map<String, String> queryParameters = null; |
| 119 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 120 | + queryParameters); |
| 121 | + Assert.assertEquals(uriPath, "/a/b/value1/value2"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test(dependsOnMethods = { "testFormatURIPathMapNullQueryMap" }) |
| 125 | + public void testFormatURIPathMapEmptyQueryMap() throws PayPalRESTException { |
| 126 | + String pattern = "/a/b/{first}/{second}"; |
| 127 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 128 | + pathParameters.put("first", "value1"); |
| 129 | + pathParameters.put("second", "value2"); |
| 130 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 131 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 132 | + queryParameters); |
| 133 | + Assert.assertEquals(uriPath, "/a/b/value1/value2"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test(dependsOnMethods = { "testFormatURIPathMapEmptyQueryMap" }) |
| 137 | + public void testFormatURIPathMapQueryMap() throws PayPalRESTException { |
| 138 | + String pattern = "/a/b/first/second"; |
| 139 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 140 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 141 | + queryParameters.put("query1", "value1"); |
| 142 | + queryParameters.put("query2", "value2"); |
| 143 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 144 | + queryParameters); |
| 145 | + Assert.assertEquals(uriPath, |
| 146 | + "/a/b/first/second?query1=value1&query2=value2&"); |
| 147 | + } |
| 148 | + |
| 149 | + @Test(dependsOnMethods = { "testFormatURIPathMapQueryMap" }) |
| 150 | + public void testFormatURIPathMapQueryMapQueryURIPath() |
| 151 | + throws PayPalRESTException { |
| 152 | + String pattern = "/a/b/first/second?"; |
| 153 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 154 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 155 | + queryParameters.put("query1", "value1"); |
| 156 | + queryParameters.put("query2", "value2"); |
| 157 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 158 | + queryParameters); |
| 159 | + Assert.assertEquals(uriPath, |
| 160 | + "/a/b/first/second?query1=value1&query2=value2&"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test(dependsOnMethods = { "testFormatURIPathMapQueryMapQueryURIPath" }) |
| 164 | + public void testFormatURIPathMapQueryMapQueryURIPathEncode() |
| 165 | + throws PayPalRESTException { |
| 166 | + String pattern = "/a/b/first/second"; |
| 167 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 168 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 169 | + queryParameters.put("query1", "value&1"); |
| 170 | + queryParameters.put("query2", "value2"); |
| 171 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 172 | + queryParameters); |
| 173 | + Assert.assertEquals(uriPath, |
| 174 | + "/a/b/first/second?query1=value%261&query2=value2&"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test(dependsOnMethods = { "testFormatURIPathMapQueryMapQueryURIPathEncode" }) |
| 178 | + public void testFormatURIPathMapQueryMapQueryValueURIPath() |
| 179 | + throws PayPalRESTException { |
| 180 | + String pattern = "/a/b/first/second?alreadypresent=value"; |
| 181 | + Map<String, String> pathParameters = new HashMap<String, String>(); |
| 182 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 183 | + queryParameters.put("query1", "value1"); |
| 184 | + queryParameters.put("query2", "value2"); |
| 185 | + String uriPath = RESTUtil.formatURIPath(pattern, pathParameters, |
| 186 | + queryParameters); |
| 187 | + Assert.assertEquals(uriPath, |
| 188 | + "/a/b/first/second?alreadypresent=value&query1=value1&query2=value2&"); |
| 189 | + } |
| 190 | + |
61 | 191 | } |
0 commit comments