|
| 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 | +} |
0 commit comments