|
| 1 | +package org.openapitools.client; |
| 2 | + |
| 3 | +import org.openapitools.client.auth.*; |
| 4 | + |
| 5 | +import java.text.DateFormat; |
| 6 | +import java.text.SimpleDateFormat; |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +import org.junit.*; |
| 10 | +import static org.junit.Assert.*; |
| 11 | + |
| 12 | + |
| 13 | +public class ApiClientTest { |
| 14 | + ApiClient apiClient = null; |
| 15 | + |
| 16 | + @Before |
| 17 | + public void setup() { |
| 18 | + apiClient = new ApiClient(); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testParseAndFormatDate() { |
| 23 | + // default date format |
| 24 | + String dateStr = "2015-11-07T03:49:09.356Z"; |
| 25 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356+00:00"))); |
| 26 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356Z"))); |
| 27 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T05:49:09.356+02:00"))); |
| 28 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T02:49:09.356-01:00"))); |
| 29 | + |
| 30 | + // custom date format: without milli-seconds, custom time zone |
| 31 | + DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); |
| 32 | + format.setTimeZone(TimeZone.getTimeZone("GMT+10")); |
| 33 | + apiClient.setDateFormat(format); |
| 34 | + dateStr = "2015-11-07T13:49:09+10:00"; |
| 35 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09+00:00"))); |
| 36 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09Z"))); |
| 37 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T00:49:09-03:00"))); |
| 38 | + assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T13:49:09+10:00"))); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testIsJsonMime() { |
| 43 | + assertFalse(apiClient.isJsonMime(null)); |
| 44 | + assertFalse(apiClient.isJsonMime("")); |
| 45 | + assertFalse(apiClient.isJsonMime("text/plain")); |
| 46 | + assertFalse(apiClient.isJsonMime("application/xml")); |
| 47 | + assertFalse(apiClient.isJsonMime("application/jsonp")); |
| 48 | + assertFalse(apiClient.isJsonMime("example/json")); |
| 49 | + assertFalse(apiClient.isJsonMime("example/foo+bar+jsonx")); |
| 50 | + assertFalse(apiClient.isJsonMime("example/foo+bar+xjson")); |
| 51 | + |
| 52 | + assertTrue(apiClient.isJsonMime("application/json")); |
| 53 | + assertTrue(apiClient.isJsonMime("application/json; charset=UTF8")); |
| 54 | + assertTrue(apiClient.isJsonMime("APPLICATION/JSON")); |
| 55 | + |
| 56 | + assertTrue(apiClient.isJsonMime("application/problem+json")); |
| 57 | + assertTrue(apiClient.isJsonMime("APPLICATION/PROBLEM+JSON")); |
| 58 | + assertTrue(apiClient.isJsonMime("application/json\t")); |
| 59 | + assertTrue(apiClient.isJsonMime("example/foo+bar+json")); |
| 60 | + assertTrue(apiClient.isJsonMime("example/foo+json;x;y")); |
| 61 | + assertTrue(apiClient.isJsonMime("example/foo+json\t;")); |
| 62 | + assertTrue(apiClient.isJsonMime("Example/fOO+JSON")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSelectHeaderAccept() { |
| 67 | + String[] accepts = {"application/json", "application/xml"}; |
| 68 | + assertEquals("application/json", apiClient.selectHeaderAccept(accepts)); |
| 69 | + |
| 70 | + accepts = new String[]{"APPLICATION/XML", "APPLICATION/JSON"}; |
| 71 | + assertEquals("APPLICATION/JSON", apiClient.selectHeaderAccept(accepts)); |
| 72 | + |
| 73 | + accepts = new String[]{"application/xml", "application/json; charset=UTF8"}; |
| 74 | + assertEquals("application/json; charset=UTF8", apiClient.selectHeaderAccept(accepts)); |
| 75 | + |
| 76 | + accepts = new String[]{"text/plain", "application/xml"}; |
| 77 | + assertEquals("text/plain,application/xml", apiClient.selectHeaderAccept(accepts)); |
| 78 | + |
| 79 | + accepts = new String[]{}; |
| 80 | + assertNull(apiClient.selectHeaderAccept(accepts)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSelectHeaderContentType() { |
| 85 | + String[] contentTypes = {"application/json", "application/xml"}; |
| 86 | + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); |
| 87 | + |
| 88 | + contentTypes = new String[]{"APPLICATION/JSON", "APPLICATION/XML"}; |
| 89 | + assertEquals("APPLICATION/JSON", apiClient.selectHeaderContentType(contentTypes)); |
| 90 | + |
| 91 | + contentTypes = new String[]{"application/xml", "application/json; charset=UTF8"}; |
| 92 | + assertEquals("application/json; charset=UTF8", apiClient.selectHeaderContentType(contentTypes)); |
| 93 | + |
| 94 | + contentTypes = new String[]{"text/plain", "application/xml"}; |
| 95 | + assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes)); |
| 96 | + |
| 97 | + contentTypes = new String[]{}; |
| 98 | + assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testGetAuthentications() { |
| 103 | + Map<String, Authentication> auths = apiClient.getAuthentications(); |
| 104 | + |
| 105 | + Authentication auth = auths.get("api_key"); |
| 106 | + assertNotNull(auth); |
| 107 | + assertTrue(auth instanceof ApiKeyAuth); |
| 108 | + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth; |
| 109 | + assertEquals("header", apiKeyAuth.getLocation()); |
| 110 | + assertEquals("api_key", apiKeyAuth.getParamName()); |
| 111 | + |
| 112 | + auth = auths.get("petstore_auth"); |
| 113 | + assertTrue(auth instanceof OAuth); |
| 114 | + assertSame(auth, apiClient.getAuthentication("petstore_auth")); |
| 115 | + |
| 116 | + assertNull(auths.get("unknown")); |
| 117 | + |
| 118 | + try { |
| 119 | + auths.put("my_auth", new HttpBasicAuth()); |
| 120 | + fail("the authentications returned should not be modifiable"); |
| 121 | + } catch (UnsupportedOperationException e) { |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Ignore("There is no more basic auth in petstore security definitions") |
| 126 | + @Test |
| 127 | + public void testSetUsernameAndPassword() { |
| 128 | + HttpBasicAuth auth = null; |
| 129 | + for (Authentication _auth : apiClient.getAuthentications().values()) { |
| 130 | + if (_auth instanceof HttpBasicAuth) { |
| 131 | + auth = (HttpBasicAuth) _auth; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + auth.setUsername(null); |
| 136 | + auth.setPassword(null); |
| 137 | + |
| 138 | + apiClient.setUsername("my-username"); |
| 139 | + apiClient.setPassword("my-password"); |
| 140 | + assertEquals("my-username", auth.getUsername()); |
| 141 | + assertEquals("my-password", auth.getPassword()); |
| 142 | + |
| 143 | + // reset values |
| 144 | + auth.setUsername(null); |
| 145 | + auth.setPassword(null); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testSetApiKeyAndPrefix() { |
| 150 | + ApiKeyAuth auth = null; |
| 151 | + for (Authentication _auth : apiClient.getAuthentications().values()) { |
| 152 | + if (_auth instanceof ApiKeyAuth) { |
| 153 | + auth = (ApiKeyAuth) _auth; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + auth.setApiKey(null); |
| 158 | + auth.setApiKeyPrefix(null); |
| 159 | + |
| 160 | + apiClient.setApiKey("my-api-key"); |
| 161 | + apiClient.setApiKeyPrefix("Token"); |
| 162 | + assertEquals("my-api-key", auth.getApiKey()); |
| 163 | + assertEquals("Token", auth.getApiKeyPrefix()); |
| 164 | + |
| 165 | + // reset values |
| 166 | + auth.setApiKey(null); |
| 167 | + auth.setApiKeyPrefix(null); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testParameterToPairWhenNameIsInvalid() throws Exception { |
| 172 | + List<Pair> pairs_a = apiClient.parameterToPair(null, new Integer(1)); |
| 173 | + List<Pair> pairs_b = apiClient.parameterToPair("", new Integer(1)); |
| 174 | + |
| 175 | + assertTrue(pairs_a.isEmpty()); |
| 176 | + assertTrue(pairs_b.isEmpty()); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testParameterToPairWhenValueIsNull() throws Exception { |
| 181 | + List<Pair> pairs = apiClient.parameterToPair("param-a", null); |
| 182 | + |
| 183 | + assertTrue(pairs.isEmpty()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testParameterToPairWhenValueIsEmptyString() throws Exception { |
| 188 | + // single empty string |
| 189 | + List<Pair> pairs = apiClient.parameterToPair("param-a", " "); |
| 190 | + assertEquals(1, pairs.size()); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testParameterToPairWhenValueIsNotCollection() throws Exception { |
| 195 | + String name = "param-a"; |
| 196 | + Integer value = 1; |
| 197 | + |
| 198 | + List<Pair> pairs = apiClient.parameterToPair(name, value); |
| 199 | + |
| 200 | + assertEquals(1, pairs.size()); |
| 201 | + assertEquals(value, Integer.valueOf(pairs.get(0).getValue())); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testParameterToPairWhenValueIsCollection() throws Exception { |
| 206 | + List<Object> values = new ArrayList<Object>(); |
| 207 | + values.add("value-a"); |
| 208 | + values.add(123); |
| 209 | + values.add(new Date()); |
| 210 | + |
| 211 | + List<Pair> pairs = apiClient.parameterToPair("param-a", values); |
| 212 | + assertEquals(0, pairs.size()); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testParameterToPairsWhenNameIsInvalid() throws Exception { |
| 217 | + List<Integer> objects = new ArrayList<Integer>(); |
| 218 | + objects.add(new Integer(1)); |
| 219 | + |
| 220 | + List<Pair> pairs_a = apiClient.parameterToPairs("csv", null, objects); |
| 221 | + List<Pair> pairs_b = apiClient.parameterToPairs("csv", "", objects); |
| 222 | + |
| 223 | + assertTrue(pairs_a.isEmpty()); |
| 224 | + assertTrue(pairs_b.isEmpty()); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testParameterToPairsWhenValueIsNull() throws Exception { |
| 229 | + List<Pair> pairs = apiClient.parameterToPairs("csv", "param-a", null); |
| 230 | + |
| 231 | + assertTrue(pairs.isEmpty()); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + public void testParameterToPairsWhenValueIsEmptyStrings() throws Exception { |
| 236 | + // list of empty strings |
| 237 | + List<String> strs = new ArrayList<String>(); |
| 238 | + strs.add(" "); |
| 239 | + strs.add(" "); |
| 240 | + strs.add(" "); |
| 241 | + |
| 242 | + List<Pair> concatStrings = apiClient.parameterToPairs("csv", "param-a", strs); |
| 243 | + |
| 244 | + assertEquals(1, concatStrings.size()); |
| 245 | + assertFalse(concatStrings.get(0).getValue().isEmpty()); // should contain some delimiters |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testParameterToPairsWhenValueIsCollection() throws Exception { |
| 250 | + Map<String, String> collectionFormatMap = new HashMap<String, String>(); |
| 251 | + collectionFormatMap.put("csv", ","); |
| 252 | + collectionFormatMap.put("tsv", "\t"); |
| 253 | + collectionFormatMap.put("ssv", " "); |
| 254 | + collectionFormatMap.put("pipes", "|"); |
| 255 | + collectionFormatMap.put("", ","); // no format, must default to csv |
| 256 | + collectionFormatMap.put("unknown", ","); // all other formats, must default to csv |
| 257 | + |
| 258 | + String name = "param-a"; |
| 259 | + |
| 260 | + List<Object> values = new ArrayList<Object>(); |
| 261 | + values.add("value-a"); |
| 262 | + values.add(123); |
| 263 | + values.add(new Date()); |
| 264 | + |
| 265 | + // check for multi separately |
| 266 | + List<Pair> multiPairs = apiClient.parameterToPairs("multi", name, values); |
| 267 | + assertEquals(values.size(), multiPairs.size()); |
| 268 | + for (int i = 0; i < values.size(); i++) { |
| 269 | + assertEquals(apiClient.escapeString(apiClient.parameterToString(values.get(i))), multiPairs.get(i).getValue()); |
| 270 | + } |
| 271 | + |
| 272 | + // all other formats |
| 273 | + for (String collectionFormat : collectionFormatMap.keySet()) { |
| 274 | + List<Pair> pairs = apiClient.parameterToPairs(collectionFormat, name, values); |
| 275 | + |
| 276 | + assertEquals(1, pairs.size()); |
| 277 | + |
| 278 | + String delimiter = collectionFormatMap.get(collectionFormat); |
| 279 | + if (!delimiter.equals(",")) { |
| 280 | + // commas are not escaped because they are reserved characters in URIs |
| 281 | + delimiter = apiClient.escapeString(delimiter); |
| 282 | + } |
| 283 | + String[] pairValueSplit = pairs.get(0).getValue().split(delimiter); |
| 284 | + |
| 285 | + // must equal input values |
| 286 | + assertEquals(values.size(), pairValueSplit.length); |
| 287 | + for (int i = 0; i < values.size(); i++) { |
| 288 | + assertEquals(apiClient.escapeString(apiClient.parameterToString(values.get(i))), pairValueSplit[i]); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | +} |
0 commit comments