Skip to content

Commit db01548

Browse files
Fixed queryParameter addition logic flaw in RESTUtil, updated testcases
1 parent 072e1f6 commit db01548

2 files changed

Lines changed: 155 additions & 23 deletions

File tree

src/main/java/com/paypal/core/rest/RESTUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ public static String formatURIPath(String pattern,
134134
formattedURIPath = pattern;
135135
if (queryParameters != null && queryParameters.size() > 0) {
136136
StringBuilder stringBuilder = new StringBuilder(formattedURIPath);
137-
if (stringBuilder.toString().contains("?")
138-
&& !stringBuilder.toString().endsWith("&")) {
139-
stringBuilder.append("&");
137+
if (stringBuilder.toString().contains("?")) {
138+
if (!(stringBuilder.toString().endsWith("?")
139+
|| stringBuilder.toString().endsWith("&"))) {
140+
stringBuilder.append("&");
141+
}
140142
} else {
141143
stringBuilder.append("?");
142144
}
Lines changed: 150 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,191 @@
11
package com.paypal.core.rest;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import org.testng.Assert;
47
import org.testng.annotations.Test;
58

69
public class RESTUtilTest {
7-
10+
811
@Test()
912
public void testFormatURIPathForNull() {
10-
String nullString = RESTUtil.formatURIPath((String) null, (Object []) null);
13+
String nullString = RESTUtil.formatURIPath((String) null,
14+
(Object[]) null);
1115
Assert.assertNull(nullString);
1216
}
13-
14-
@Test(dependsOnMethods = {"testFormatURIPathForNull"})
17+
18+
@Test(dependsOnMethods = { "testFormatURIPathForNull" })
1519
public void testFormatURIPathNoPattern() {
1620
String pattern = "/a/b/c";
17-
String uriPath = RESTUtil.formatURIPath(pattern, (Object []) null);
21+
String uriPath = RESTUtil.formatURIPath(pattern, (Object[]) null);
1822
Assert.assertEquals(uriPath, pattern);
1923
}
20-
21-
@Test(dependsOnMethods = {"testFormatURIPathNoPattern"})
24+
25+
@Test(dependsOnMethods = { "testFormatURIPathNoPattern" })
2226
public void testFormatURIPathNoQS() {
2327
String pattern = "/a/b/{0}";
24-
Object[] parameters = new Object[] {"replace"};
28+
Object[] parameters = new Object[] { "replace" };
2529
String uriPath = RESTUtil.formatURIPath(pattern, parameters);
2630
Assert.assertEquals(uriPath, "/a/b/replace");
2731
}
28-
29-
@Test(dependsOnMethods = {"testFormatURIPathNoQS"})
32+
33+
@Test(dependsOnMethods = { "testFormatURIPathNoQS" })
3034
public void testFormatURIPath() {
3135
String pattern = "/a/b/{0}?name={1}";
32-
Object[] parameters = new Object[] {"replace", "nameValue"};
36+
Object[] parameters = new Object[] { "replace", "nameValue" };
3337
String uriPath = RESTUtil.formatURIPath(pattern, parameters);
3438
Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue");
3539
}
36-
37-
@Test(dependsOnMethods = {"testFormatURIPath"})
40+
41+
@Test(dependsOnMethods = { "testFormatURIPath" })
3842
public void testFormatURIPathWithNull() {
3943
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 };
4145
String uriPath = RESTUtil.formatURIPath(pattern, parameters);
4246
Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue");
4347
}
44-
45-
@Test(dependsOnMethods = {"testFormatURIPathWithNull"})
48+
49+
@Test(dependsOnMethods = { "testFormatURIPathWithNull" })
4650
public void testFormatURIPathWithEmpty() {
4751
String pattern = "/a/b/{0}?name={1}&age=";
48-
Object[] parameters = new Object[] {"replace", "nameValue", null};
52+
Object[] parameters = new Object[] { "replace", "nameValue", null };
4953
String uriPath = RESTUtil.formatURIPath(pattern, parameters);
5054
Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue");
5155
}
52-
53-
@Test(dependsOnMethods = {"testFormatURIPathWithEmpty"})
56+
57+
@Test(dependsOnMethods = { "testFormatURIPathWithEmpty" })
5458
public void testFormatURIPathTwoQS() {
5559
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" };
5761
String uriPath = RESTUtil.formatURIPath(pattern, parameters);
5862
Assert.assertEquals(uriPath, "/a/b/replace?name=nameValue&age=1");
5963
}
6064

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+
61191
}

0 commit comments

Comments
 (0)