Include Javascript client into integration test

and return the promise-like result of $.ajax for API
This commit is contained in:
xhh 2015-12-17 20:41:09 +08:00
parent 794783a4bb
commit f07b75f419
11 changed files with 424 additions and 94 deletions

View File

@ -62,10 +62,13 @@ var {{classname}} = function {{classname}}() {
var options = {type: "{{httpMethod}}", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
{{#returnType}}
@ -84,6 +87,8 @@ var {{classname}} = function {{classname}}() {
}
{{/returnType}}
});
return request;
}
{{/operation}}
{{/operations}}

13
pom.xml
View File

@ -389,6 +389,18 @@
<module>samples/client/petstore/java/feign</module>
</modules>
</profile>
<profile>
<id>javascript-client</id>
<activation>
<property>
<name>env</name>
<value>javascript</value>
</property>
</activation>
<modules>
<module>samples/client/petstore/javascript</module>
</modules>
</profile>
<profile>
<id>scala-client</id>
<activation>
@ -465,6 +477,7 @@
<module>samples/client/petstore/java/okhttp-gson</module>
<module>samples/client/petstore/java/retrofit</module>
<module>samples/client/petstore/java/retrofit2</module>
<module>samples/client/petstore/javascript</module>
<module>samples/client/petstore/scala</module>
<module>samples/server/petstore/spring-mvc</module>
<!--module>samples/client/petstore/objc</module-->

View File

@ -0,0 +1,45 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-petstore-javascript</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Swagger Petstore - Javascript Client</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>npm-install</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>mocha</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mocha</executable>
<arguments>
<argument>--recursive</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -48,10 +48,13 @@ var PetApi = function PetApi() {
var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -60,6 +63,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -94,10 +99,13 @@ var PetApi = function PetApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -106,6 +114,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -142,10 +152,13 @@ var PetApi = function PetApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -160,6 +173,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -196,10 +211,13 @@ var PetApi = function PetApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -214,6 +232,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -256,10 +276,13 @@ var PetApi = function PetApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -274,6 +297,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -322,10 +347,13 @@ var PetApi = function PetApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -334,6 +362,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -379,10 +409,13 @@ var PetApi = function PetApi() {
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -391,6 +424,8 @@ var PetApi = function PetApi() {
}
});
return request;
}
/**
@ -439,10 +474,13 @@ var PetApi = function PetApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -451,6 +489,8 @@ var PetApi = function PetApi() {
}
});
return request;
}

View File

@ -47,10 +47,13 @@ var StoreApi = function StoreApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -64,6 +67,8 @@ var StoreApi = function StoreApi() {
}
});
return request;
}
/**
@ -98,10 +103,13 @@ var StoreApi = function StoreApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -116,6 +124,8 @@ var StoreApi = function StoreApi() {
}
});
return request;
}
/**
@ -158,10 +168,13 @@ var StoreApi = function StoreApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -176,6 +189,8 @@ var StoreApi = function StoreApi() {
}
});
return request;
}
/**
@ -218,10 +233,13 @@ var StoreApi = function StoreApi() {
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -230,6 +248,8 @@ var StoreApi = function StoreApi() {
}
});
return request;
}

View File

@ -48,10 +48,13 @@ var UserApi = function UserApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -60,6 +63,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -94,10 +99,13 @@ var UserApi = function UserApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -106,6 +114,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -140,10 +150,13 @@ var UserApi = function UserApi() {
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -152,6 +165,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -191,10 +206,13 @@ var UserApi = function UserApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -208,6 +226,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -241,10 +261,13 @@ var UserApi = function UserApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -253,6 +276,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -295,10 +320,13 @@ var UserApi = function UserApi() {
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -313,6 +341,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -356,10 +386,13 @@ var UserApi = function UserApi() {
var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -368,6 +401,8 @@ var UserApi = function UserApi() {
}
});
return request;
}
/**
@ -410,10 +445,13 @@ var UserApi = function UserApi() {
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
//request.fail(function(jqXHR, textStatus, errorThrown){
// errorHandler(jqXHR, textStatus, errorThrown);
//});
request.fail(function(jqXHR, textStatus, errorThrown){
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
@ -422,6 +460,8 @@ var UserApi = function UserApi() {
}
});
return request;
}

View File

@ -33,15 +33,21 @@ var createRandomPet = function() {
describe('PetApi', function() {
it('should create and get pet', function (done) {
var pet = createRandomPet();
api.addPet(pet);
api.addPet(pet).then(function() {
api.getPetById(pet.id, function(fetched, textStatus, jqXHR, error) {
if (error) throw error;
api.getPetById(pet.id, function(fetched, textStatus, jqXHR) {
expect(textStatus).to.be('success');
expect(fetched).to.be.ok();
expect(fetched.id).to.be(pet.id);
expect(fetched.getCategory()).to.be.ok();
expect(fetched.getCategory().getName()).to.be(pet.getCategory().getName());
done();
expect(textStatus).to.be('success');
expect(fetched).to.be.ok();
expect(fetched.id).to.be(pet.id);
expect(fetched.getCategory()).to.be.ok();
expect(fetched.getCategory().getName()).to.be(pet.getCategory().getName());
api.deletePet(pet.id);
done();
});
}, function(jqXHR, textStatus, errorThrown) {
throw errorThrown || textStatus;
});
});
});

View File

@ -0,0 +1,75 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiException;
import io.swagger.client.*;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
import java.util.Map;
import java.text.SimpleDateFormat;
import org.junit.*;
import static org.junit.Assert.*;
public class StoreApiTest {
StoreApi api = null;
@Before
public void setup() {
api = new StoreApi();
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
// set custom date format that is used by the petstore server
api.getApiClient().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
}
@Test
public void testGetInventory() throws Exception {
Map<String, Integer> inventory = api.getInventory();
assertTrue(inventory.keySet().size() > 0);
}
@Test
public void testPlaceOrder() throws Exception {
Order order = createOrder();
api.placeOrder(order);
Order fetched = api.getOrderById(String.valueOf(order.getId()));
assertEquals(order.getId(), fetched.getId());
assertEquals(order.getPetId(), fetched.getPetId());
assertEquals(order.getQuantity(), fetched.getQuantity());
}
@Test
public void testDeleteOrder() throws Exception {
Order order = createOrder();
api.placeOrder(order);
Order fetched = api.getOrderById(String.valueOf(order.getId()));
assertEquals(fetched.getId(), order.getId());
api.deleteOrder(String.valueOf(order.getId()));
try {
api.getOrderById(String.valueOf(order.getId()));
// fail("expected an error");
} catch (ApiException e) {
// ok
}
}
private Order createOrder() {
Order order = new Order();
order.setId(new Long(System.currentTimeMillis()));
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());
order.setStatus(Order.StatusEnum.PLACED);
order.setComplete(true);
return order;
}
}

View File

@ -0,0 +1,86 @@
package io.swagger.petstore.test;
import io.swagger.client.api.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
import java.util.Arrays;
import org.junit.*;
import static org.junit.Assert.*;
public class UserApiTest {
UserApi api = null;
@Before
public void setup() {
api = new UserApi();
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
}
@Test
public void testCreateUser() throws Exception {
User user = createUser();
api.createUser(user);
User fetched = api.getUserByName(user.getUsername());
assertEquals(user.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithArray() throws Exception {
User user1 = createUser();
user1.setUsername("abc123");
User user2 = createUser();
user2.setUsername("123abc");
api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2}));
User fetched = api.getUserByName(user1.getUsername());
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithList() throws Exception {
User user1 = createUser();
user1.setUsername("abc123");
User user2 = createUser();
user2.setUsername("123abc");
api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2}));
User fetched = api.getUserByName(user1.getUsername());
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testLoginUser() throws Exception {
User user = createUser();
api.createUser(user);
String token = api.loginUser(user.getUsername(), user.getPassword());
assertTrue(token.startsWith("logged in user session:"));
}
@Test
public void logoutUser() throws Exception {
api.logoutUser();
}
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setUsername("fred" + user.getId());
user.setFirstName("Fred");
user.setLastName("Meyer");
user.setEmail("fred@fredmeyer.com");
user.setPassword("xxXXxx");
user.setPhone("408-867-5309");
user.setUserStatus(123);
return user;
}
}

View File

@ -1 +1 @@
--timeout 5000
--timeout 10000

View File

@ -14,7 +14,7 @@
<script>
mocha.setup({
ui: 'bdd',
timeout: 5000
timeout: 10000
});
</script>