-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPetstoreCRUDTest.java
69 lines (55 loc) · 2.07 KB
/
PetstoreCRUDTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.petstore.karate;
import com.petstore.karate.client.api.PetApi;
import com.petstore.karate.client.model.CategoryDto;
import com.petstore.karate.client.model.PetDto;
import com.petstore.karate.client.model.TagDto;
import io.github.apimock.MockServer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.client.HttpClientErrorException;
import java.util.Arrays;
public class PetstoreCRUDTest {
io.github.apimock.MockServer server;
@Before
public void setup() throws Exception {
server = MockServer.builder()
.openapi("petstore-openapi.yml")
.features("classpath:mocks/PetMock.feature")
.pathPrefix("api/v3")
.http(0).build();
}
@Test
public void testCrudPet() {
PetApi petApiClient = new PetApi();
petApiClient.getApiClient().setBasePath("http://localhost:" + server.getPort() + "/api/v3");
PetDto pet = new PetDto();
pet.setName("Name");
pet.setStatus(PetDto.StatusEnum.AVAILABLE);
pet.setTags(Arrays.asList(new TagDto().id(0L).name("dog")));
pet.setCategory(new CategoryDto().id(0L).name("dogs"));
// addPet
PetDto created = petApiClient.addPet(pet);
Assert.assertNotNull(created);
Assert.assertNotNull(created.getId());
PetDto found = petApiClient.getPetById(created.getId());
Assert.assertNotNull(found);
// updatePet
created.setName("Updated Name");
PetDto updated = petApiClient.updatePet(created);
Assert.assertEquals("Updated Name", updated.getName());
// deletePet
petApiClient.deletePet(created.getId(), null);
try {
PetDto notfound = petApiClient.getPetById(created.getId());
Assert.fail("Pet was not deleted");
} catch (Exception e) {
Assert.assertTrue(e instanceof HttpClientErrorException.NotFound);
}
}
@After
public void tearDown() throws Exception {
server.stop();
}
}