Merge remote-tracking branch 'refs/remotes/origin/master' into JSR310-Clients

This commit is contained in:
ant3 2016-07-08 23:00:04 +01:00
commit 836aed999e
7 changed files with 448 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package io.swagger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -0,0 +1,239 @@
package io.swagger.client;
import io.swagger.client.auth.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
public class ApiClientTest {
ApiClient apiClient = null;
@Before
public void setup() {
apiClient = new ApiClient();
}
@Test
public void testParseAndFormatDate() {
// default date format
String dateStr = "2015-11-07T03:49:09.356Z";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09.356Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T05:49:09.356+02:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T02:49:09.356-01:00")));
// custom date format: without milli-seconds, custom time zone
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
format.setTimeZone(TimeZone.getTimeZone("GMT+10"));
apiClient.setDateFormat(format);
dateStr = "2015-11-07T13:49:09+10:00";
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09+00:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T03:49:09Z")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T00:49:09-03:00")));
assertEquals(dateStr, apiClient.formatDate(apiClient.parseDate("2015-11-07T13:49:09+10:00")));
}
@Test
public void testIsJsonMime() {
assertFalse(apiClient.isJsonMime(null));
assertFalse(apiClient.isJsonMime(""));
assertFalse(apiClient.isJsonMime("text/plain"));
assertFalse(apiClient.isJsonMime("application/xml"));
assertFalse(apiClient.isJsonMime("application/jsonp"));
assertTrue(apiClient.isJsonMime("application/json"));
assertTrue(apiClient.isJsonMime("application/json; charset=UTF8"));
assertTrue(apiClient.isJsonMime("APPLICATION/JSON"));
}
@Test
public void testSelectHeaderAccept() {
String[] accepts = {"application/json", "application/xml"};
assertEquals("application/json", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"APPLICATION/XML", "APPLICATION/JSON"};
assertEquals("APPLICATION/JSON", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"application/xml", "application/json; charset=UTF8"};
assertEquals("application/json; charset=UTF8", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{"text/plain", "application/xml"};
assertEquals("text/plain,application/xml", apiClient.selectHeaderAccept(accepts));
accepts = new String[]{};
assertNull(apiClient.selectHeaderAccept(accepts));
}
@Test
public void testSelectHeaderContentType() {
String[] contentTypes = {"application/json", "application/xml"};
assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"APPLICATION/JSON", "APPLICATION/XML"};
assertEquals("APPLICATION/JSON", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"application/xml", "application/json; charset=UTF8"};
assertEquals("application/json; charset=UTF8", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{"text/plain", "application/xml"};
assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes));
contentTypes = new String[]{};
assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes));
}
@Test
public void testGetAuthentications() {
Map<String, Authentication> auths = apiClient.getAuthentications();
Authentication auth = auths.get("api_key");
assertNotNull(auth);
assertTrue(auth instanceof ApiKeyAuth);
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth;
assertEquals("header", apiKeyAuth.getLocation());
assertEquals("api_key", apiKeyAuth.getParamName());
auth = auths.get("petstore_auth");
assertTrue(auth instanceof OAuth);
assertSame(auth, apiClient.getAuthentication("petstore_auth"));
assertNull(auths.get("unknown"));
try {
auths.put("my_auth", new HttpBasicAuth());
fail("the authentications returned should not be modifiable");
} catch (UnsupportedOperationException e) {
}
}
@Ignore("There is no more basic auth in petstore security definitions")
@Test
public void testSetUsernameAndPassword() {
HttpBasicAuth auth = null;
for (Authentication _auth : apiClient.getAuthentications().values()) {
if (_auth instanceof HttpBasicAuth) {
auth = (HttpBasicAuth) _auth;
break;
}
}
auth.setUsername(null);
auth.setPassword(null);
apiClient.setUsername("my-username");
apiClient.setPassword("my-password");
assertEquals("my-username", auth.getUsername());
assertEquals("my-password", auth.getPassword());
// reset values
auth.setUsername(null);
auth.setPassword(null);
}
@Test
public void testSetApiKeyAndPrefix() {
ApiKeyAuth auth = null;
for (Authentication _auth : apiClient.getAuthentications().values()) {
if (_auth instanceof ApiKeyAuth) {
auth = (ApiKeyAuth) _auth;
break;
}
}
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
apiClient.setApiKey("my-api-key");
apiClient.setApiKeyPrefix("Token");
assertEquals("my-api-key", auth.getApiKey());
assertEquals("Token", auth.getApiKeyPrefix());
// reset values
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
}
@Test
public void testParameterToPairsWhenNameIsInvalid() throws Exception {
List<Pair> pairs_a = apiClient.parameterToPairs("csv", null, new Integer(1));
List<Pair> pairs_b = apiClient.parameterToPairs("csv", "", new Integer(1));
assertTrue(pairs_a.isEmpty());
assertTrue(pairs_b.isEmpty());
}
@Test
public void testParameterToPairsWhenValueIsNull() throws Exception {
List<Pair> pairs = apiClient.parameterToPairs("csv", "param-a", null);
assertTrue(pairs.isEmpty());
}
@Test
public void testParameterToPairsWhenValueIsEmptyStrings() throws Exception {
// single empty string
List<Pair> pairs = apiClient.parameterToPairs("csv", "param-a", " ");
assertEquals(1, pairs.size());
// list of empty strings
List<String> strs = new ArrayList<String>();
strs.add(" ");
strs.add(" ");
strs.add(" ");
List<Pair> concatStrings = apiClient.parameterToPairs("csv", "param-a", strs);
assertEquals(1, concatStrings.size());
assertFalse(concatStrings.get(0).getValue().isEmpty()); // should contain some delimiters
}
@Test
public void testParameterToPairsWhenValueIsNotCollection() throws Exception {
String name = "param-a";
Integer value = 1;
List<Pair> pairs = apiClient.parameterToPairs("csv", name, value);
assertEquals(1, pairs.size());
assertEquals(value, Integer.valueOf(pairs.get(0).getValue()));
}
@Test
public void testParameterToPairsWhenValueIsCollection() throws Exception {
Map<String, String> collectionFormatMap = new HashMap<String, String>();
collectionFormatMap.put("csv", ",");
collectionFormatMap.put("tsv", "\t");
collectionFormatMap.put("ssv", " ");
collectionFormatMap.put("pipes", "\\|");
collectionFormatMap.put("", ","); // no format, must default to csv
collectionFormatMap.put("unknown", ","); // all other formats, must default to csv
String name = "param-a";
List<Object> values = new ArrayList<Object>();
values.add("value-a");
values.add(123);
values.add(new Date());
// check for multi separately
List<Pair> multiPairs = apiClient.parameterToPairs("multi", name, values);
assertEquals(values.size(), multiPairs.size());
// all other formats
for (String collectionFormat : collectionFormatMap.keySet()) {
List<Pair> pairs = apiClient.parameterToPairs(collectionFormat, name, values);
assertEquals(1, pairs.size());
String delimiter = collectionFormatMap.get(collectionFormat);
String[] pairValueSplit = pairs.get(0).getValue().split(delimiter);
// must equal input values
assertEquals(values.size(), pairValueSplit.length);
}
}
}

View File

@ -0,0 +1,15 @@
package io.swagger.client;
import org.junit.*;
import static org.junit.Assert.*;
public class ConfigurationTest {
@Test
public void testDefaultApiClient() {
ApiClient apiClient = Configuration.getDefaultApiClient();
assertNotNull(apiClient);
assertEquals("http://petstore.swagger.io/v2", apiClient.getBasePath());
assertFalse(apiClient.isDebugging());
}
}

View File

@ -0,0 +1,45 @@
package io.swagger.client;
import io.swagger.client.model.Order;
import java.lang.Exception;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.*;
import static org.junit.Assert.*;
public class JSONTest {
JSON json = null;
Order order = null;
@Before
public void setup() {
json = new JSON();
order = new Order();
}
@Test
public void testDefaultDate() throws Exception {
final DateTimeFormatter dateFormat = ISODateTimeFormat.dateTime();
final String dateStr = "2015-11-07T14:11:05.267Z";
order.setShipDate(dateFormat.parseDateTime(dateStr));
String str = json.getContext(null).writeValueAsString(order);
Order o = json.getContext(null).readValue(str, Order.class);
assertEquals(dateStr, dateFormat.print(o.getShipDate()));
}
@Test
public void testCustomDate() throws Exception {
final DateTimeFormatter dateFormat = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.forID("Etc/GMT+2"));
final String dateStr = "2015-11-07T14:11:05-02:00";
order.setShipDate(dateFormat.parseDateTime(dateStr));
String str = json.getContext(null).writeValueAsString(order);
Order o = json.getContext(null).readValue(str, Order.class);
assertEquals(dateStr, dateFormat.print(o.getShipDate()));
}
}

View File

@ -0,0 +1,33 @@
package io.swagger.client;
import org.junit.*;
import static org.junit.Assert.*;
public class StringUtilTest {
@Test
public void testContainsIgnoreCase() {
assertTrue(StringUtil.containsIgnoreCase(new String[]{"abc"}, "abc"));
assertTrue(StringUtil.containsIgnoreCase(new String[]{"abc"}, "ABC"));
assertTrue(StringUtil.containsIgnoreCase(new String[]{"ABC"}, "abc"));
assertTrue(StringUtil.containsIgnoreCase(new String[]{null, "abc"}, "ABC"));
assertTrue(StringUtil.containsIgnoreCase(new String[]{null, "abc"}, null));
assertFalse(StringUtil.containsIgnoreCase(new String[]{"abc"}, "def"));
assertFalse(StringUtil.containsIgnoreCase(new String[]{}, "ABC"));
assertFalse(StringUtil.containsIgnoreCase(new String[]{}, null));
}
@Test
public void testJoin() {
String[] array = {"aa", "bb", "cc"};
assertEquals("aa,bb,cc", StringUtil.join(array, ","));
assertEquals("aa, bb, cc", StringUtil.join(array, ", "));
assertEquals("aabbcc", StringUtil.join(array, ""));
assertEquals("aa bb cc", StringUtil.join(array, " "));
assertEquals("aa\nbb\ncc", StringUtil.join(array, "\n"));
assertEquals("", StringUtil.join(new String[]{}, ","));
assertEquals("abc", StringUtil.join(new String[]{"abc"}, ","));
}
}

View File

@ -0,0 +1,47 @@
package io.swagger.client.auth;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;
import io.swagger.client.Pair;
import org.junit.*;
import static org.junit.Assert.*;
public class ApiKeyAuthTest {
@Test
public void testApplyToParamsInQuery() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
auth.setApiKey("my-api-key");
auth.applyToParams(queryParams, headerParams);
assertEquals(1, queryParams.size());
for (Pair queryParam : queryParams) {
assertEquals("my-api-key", queryParam.getValue());
}
// no changes to header parameters
assertEquals(0, headerParams.size());
}
@Test
public void testApplyToParamsInHeaderWithPrefix() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
auth.setApiKey("my-api-token");
auth.setApiKeyPrefix("Token");
auth.applyToParams(queryParams, headerParams);
// no changes to query parameters
assertEquals(0, queryParams.size());
assertEquals(1, headerParams.size());
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
}
}

View File

@ -0,0 +1,52 @@
package io.swagger.client.auth;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;
import io.swagger.client.Pair;
import org.junit.*;
import static org.junit.Assert.*;
public class HttpBasicAuthTest {
HttpBasicAuth auth = null;
@Before
public void setup() {
auth = new HttpBasicAuth();
}
@Test
public void testApplyToParams() {
List<Pair> queryParams = new ArrayList<Pair>();
Map<String, String> headerParams = new HashMap<String, String>();
auth.setUsername("my-username");
auth.setPassword("my-password");
auth.applyToParams(queryParams, headerParams);
// no changes to query parameters
assertEquals(0, queryParams.size());
assertEquals(1, headerParams.size());
// the string below is base64-encoded result of "my-username:my-password" with the "Basic " prefix
String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
assertEquals(expected, headerParams.get("Authorization"));
// null username should be treated as empty string
auth.setUsername(null);
auth.applyToParams(queryParams, headerParams);
// the string below is base64-encoded result of ":my-password" with the "Basic " prefix
expected = "Basic Om15LXBhc3N3b3Jk";
assertEquals(expected, headerParams.get("Authorization"));
// null password should be treated as empty string
auth.setUsername("my-username");
auth.setPassword(null);
auth.applyToParams(queryParams, headerParams);
// the string below is base64-encoded result of "my-username:" with the "Basic " prefix
expected = "Basic bXktdXNlcm5hbWU6";
assertEquals(expected, headerParams.get("Authorization"));
}
}