Skip to content

Commit adb262e

Browse files
committed
added custom tests for java v3
1 parent 69b9f1d commit adb262e

File tree

12 files changed

+1735
-0
lines changed

12 files changed

+1735
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.ApiClient;
4+
import java.io.File;
5+
import io.swagger.client.model.ModelApiResponse;
6+
import io.swagger.client.model.Pet;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import com.github.tomakehurst.wiremock.WireMockServer;
12+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
13+
import java.net.HttpURLConnection;
14+
import org.junit.AfterClass;
15+
16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
23+
24+
/**
25+
* API tests for PetApi
26+
*/
27+
public class PetApiTest {
28+
29+
private PetApi api;
30+
31+
private static WireMockServer wireMockServer;
32+
33+
@Before
34+
public void setup() {
35+
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
36+
wireMockServer.start();
37+
38+
api = new ApiClient().setBasePath("http://localhost:" + wireMockServer.port()).buildClient(PetApi.class);
39+
40+
configureFor(wireMockServer.port());
41+
42+
stubFor(post(urlPathMatching("/pet"))
43+
.willReturn(aResponse()
44+
.withStatus(HttpURLConnection.HTTP_OK)));
45+
46+
stubFor(put(urlPathMatching("/pet"))
47+
.willReturn(aResponse()
48+
.withStatus(HttpURLConnection.HTTP_OK)));
49+
50+
stubFor(get(urlPathMatching("/pet/10"))
51+
.willReturn(aResponse()
52+
.withStatus(HttpURLConnection.HTTP_OK)
53+
.withBody("{\"id\":10,\"category\":{\"id\":7007,\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":7007,\"name\":\"string\"}],\"status\":\"available\"}")));
54+
55+
stubFor(delete(urlPathMatching("/pet/10"))
56+
.willReturn(aResponse()
57+
.withStatus(HttpURLConnection.HTTP_OK)));
58+
59+
stubFor(get(urlPathMatching("/pet/findByStatus"))
60+
.willReturn(aResponse()
61+
.withStatus(HttpURLConnection.HTTP_OK)
62+
.withBody("[{\"id\":7007,\"category\":{\"id\":7007,\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":7007,\"name\":\"string\"}],\"status\":\"available\"},{\"id\":44550,\"category\":{\"id\":0,\"name\":\"string\"},\"name\":\"SuperHund\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":0,\"name\":\"string\"}],\"status\":\"available\"},{\"id\":3377,\"category\":{\"id\":0,\"name\":\"Lambo\"},\"name\":\"Lambo\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":0,\"name\":\"Lambo\"}],\"status\":\"available\"}]")
63+
));
64+
65+
}
66+
67+
@AfterClass
68+
public static void tearDown() {
69+
wireMockServer.stop();
70+
}
71+
72+
73+
/**
74+
* Add a new pet to the store
75+
*
76+
*
77+
*/
78+
@Test
79+
public void addPetTest() {
80+
Pet body = new Pet()
81+
.id(10L)
82+
.name("doggie")
83+
.status(Pet.StatusEnum.AVAILABLE)
84+
.photoUrls(Arrays.asList("http://some.pic"));
85+
api.addPet(body);
86+
87+
verify(exactly(1), postRequestedFor(urlEqualTo("/pet")));
88+
}
89+
90+
91+
/**
92+
* Deletes a pet
93+
*
94+
*
95+
*/
96+
@Test
97+
public void deletePetTest() {
98+
Long petId = 10L;
99+
String apiKey = "abc123";
100+
api.deletePet(petId, apiKey);
101+
102+
verify(exactly(1), deleteRequestedFor(urlEqualTo("/pet/10")));
103+
}
104+
105+
106+
/**
107+
* Finds Pets by status
108+
*
109+
* Multiple status values can be provided with comma separated strings
110+
*/
111+
@Test
112+
public void findPetsByStatusTest() {
113+
List<Pet> response = api.findPetsByStatus("available");
114+
115+
Assert.assertNotNull(response);
116+
Assert.assertFalse(response.isEmpty());
117+
118+
verify(exactly(1), getRequestedFor(urlEqualTo("/pet/findByStatus?status=available")));
119+
}
120+
121+
/**
122+
* Find pet by ID
123+
*
124+
* Returns a single pet
125+
*/
126+
@Test
127+
public void getPetByIdTest() {
128+
Long petId = 10L;
129+
Pet response = api.getPetById(petId);
130+
131+
Assert.assertNotNull(response);
132+
Assert.assertEquals(10L, response.getId().longValue());
133+
Assert.assertEquals("doggie", response.getName());
134+
135+
verify(exactly(1), getRequestedFor(urlEqualTo("/pet/10")));
136+
}
137+
138+
139+
/**
140+
* Update an existing pet
141+
*
142+
*
143+
*/
144+
@Test
145+
public void updatePetTest() {
146+
Pet body = new Pet()
147+
.id(10L)
148+
.name("doggie")
149+
.status(Pet.StatusEnum.AVAILABLE)
150+
.photoUrls(Arrays.asList("http://some.pic"));
151+
152+
api.updatePet(body);
153+
154+
verify(exactly(1), putRequestedFor(urlEqualTo("/pet")));
155+
}
156+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.ApiClient;
4+
import io.swagger.client.model.Order;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import com.github.tomakehurst.wiremock.WireMockServer;
10+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
11+
import java.net.HttpURLConnection;
12+
import org.junit.AfterClass;
13+
14+
import java.util.ArrayList;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
20+
21+
/**
22+
* API tests for StoreApi
23+
*/
24+
public class StoreApiTest {
25+
26+
private StoreApi api;
27+
28+
private static WireMockServer wireMockServer;
29+
30+
@Before
31+
public void setup() {
32+
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
33+
wireMockServer.start();
34+
35+
configureFor(wireMockServer.port());
36+
api = new ApiClient().setBasePath("http://localhost:" + wireMockServer.port()).buildClient(StoreApi.class);
37+
38+
stubFor(delete(urlPathMatching("/store/order/10"))
39+
.willReturn(aResponse()
40+
.withStatus(HttpURLConnection.HTTP_OK)));
41+
42+
stubFor(get(urlPathMatching("/store/inventory"))
43+
.willReturn(aResponse()
44+
.withStatus(HttpURLConnection.HTTP_OK)
45+
.withBody("{\"id\":10}")));
46+
47+
stubFor(get(urlPathMatching("/store/order/10"))
48+
.willReturn(aResponse()
49+
.withStatus(HttpURLConnection.HTTP_OK)
50+
.withBody("{\"id\":10, \"petId\":10, \"quantity\":1, \"status\":\"approved\", \"complete\": true}")));
51+
52+
stubFor(post(urlPathMatching("/store/order"))
53+
.willReturn(aResponse()
54+
.withStatus(HttpURLConnection.HTTP_OK)
55+
.withBody("{\"id\":10, \"petId\":10, \"quantity\":1, \"status\":\"approved\", \"complete\": true}")));
56+
57+
}
58+
59+
@AfterClass
60+
public static void tearDown() {
61+
wireMockServer.stop();
62+
}
63+
64+
65+
/**
66+
* Delete purchase order by ID
67+
*
68+
* For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors
69+
*/
70+
@Test
71+
public void deleteOrderTest() {
72+
Long orderId = 10L;
73+
api.deleteOrder(orderId);
74+
75+
verify(exactly(1), deleteRequestedFor(urlEqualTo("/store/order/10")));
76+
}
77+
78+
79+
/**
80+
* Returns pet inventories by status
81+
*
82+
* Returns a map of status codes to quantities
83+
*/
84+
@Test
85+
public void getInventoryTest() {
86+
Map<String, Integer> response = api.getInventory();
87+
88+
Assert.assertNotNull(response);
89+
Assert.assertFalse(response.isEmpty());
90+
91+
verify(exactly(1), getRequestedFor(urlEqualTo("/store/inventory")));
92+
}
93+
94+
95+
/**
96+
* Find purchase order by ID
97+
*
98+
* For valid response try integer IDs with value &gt;&#x3D; 1 and &lt;&#x3D; 10. Other values will generated exceptions
99+
*/
100+
@Test
101+
public void getOrderByIdTest() {
102+
Long orderId = 10L;
103+
Order response = api.getOrderById(orderId);
104+
Assert.assertNotNull(response);
105+
Assert.assertEquals(10L, response.getId().longValue());
106+
Assert.assertEquals(10L, response.getPetId().longValue());
107+
Assert.assertEquals(1, response.getQuantity().intValue());
108+
109+
verify(exactly(1), getRequestedFor(urlEqualTo("/store/order/10")));
110+
}
111+
112+
113+
/**
114+
* Place an order for a pet
115+
*
116+
*
117+
*/
118+
@Test
119+
public void placeOrderTest() {
120+
Order body = new Order().id(10L)
121+
.petId(10L)
122+
.complete(false)
123+
.status(Order.StatusEnum.PLACED)
124+
.quantity(1);
125+
Order response = api.placeOrder(body);
126+
127+
Assert.assertEquals(10L, response.getId().longValue());
128+
Assert.assertEquals(10L, response.getPetId().longValue());
129+
Assert.assertEquals(1, response.getQuantity().intValue());
130+
Assert.assertEquals(true, response.isComplete());
131+
Assert.assertEquals(Order.StatusEnum.APPROVED, response.getStatus());
132+
133+
verify(exactly(1), postRequestedFor(urlEqualTo("/store/order")));
134+
}
135+
136+
137+
}

0 commit comments

Comments
 (0)