Refactor petstore tests and refresh build setup

This commit is contained in:
2026-02-15 14:02:49 +03:00
parent 32f7be7cf0
commit b97c369f46
16 changed files with 655 additions and 316 deletions

View File

@@ -0,0 +1,21 @@
package ru.otus.petstore.config;
public final class TestConfig {
private static final String BASE_URI =
System.getProperty("base.uri", "https://petstore.swagger.io");
private static final String BASE_PATH =
System.getProperty("base.path", "/v2");
private TestConfig() {
}
public static String getBaseUri() {
return BASE_URI;
}
public static String getBasePath() {
return BASE_PATH;
}
}

View File

@@ -0,0 +1,23 @@
// Category.java
package ru.otus.petstore.model;
public class Category {
private long id;
private String name;
public Category() { }
public Category(long id, String name) {
this.id = id;
this.name = name;
}
public Category(Category other) {
this.id = other.id;
this.name = other.name;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

View File

@@ -0,0 +1,58 @@
// Pet.java
package ru.otus.petstore.model;
import java.util.ArrayList;
import java.util.List;
public class Pet {
private long id;
private Category category;
private String name;
private List<String> photoUrls;
private List<Tag> tags;
private String status;
public Pet() { }
public Pet(long id, Category category, String name,
List<String> photoUrls, List<Tag> tags, String status) {
this.id = id;
this.category = copyCategory(category);
this.name = name;
this.photoUrls = copyPhotoUrls(photoUrls);
this.tags = copyTags(tags);
this.status = status;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public Category getCategory() { return copyCategory(category); }
public void setCategory(Category category) { this.category = copyCategory(category); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public List<String> getPhotoUrls() { return copyPhotoUrls(photoUrls); }
public void setPhotoUrls(List<String> photoUrls) { this.photoUrls = copyPhotoUrls(photoUrls); }
public List<Tag> getTags() { return copyTags(tags); }
public void setTags(List<Tag> tags) { this.tags = copyTags(tags); }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
private static Category copyCategory(Category source) {
return source == null ? null : new Category(source);
}
private static List<String> copyPhotoUrls(List<String> source) {
return source == null ? null : List.copyOf(source);
}
private static List<Tag> copyTags(List<Tag> source) {
if (source == null) {
return null;
}
List<Tag> target = new ArrayList<>(source.size());
for (Tag tag : source) {
target.add(new Tag(tag));
}
return target;
}
}

View File

@@ -0,0 +1,23 @@
// Tag.java
package ru.otus.petstore.model;
public class Tag {
private long id;
private String name;
public Tag() { }
public Tag(long id, String name) {
this.id = id;
this.name = name;
}
public Tag(Tag other) {
this.id = other.id;
this.name = other.name;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

View File

@@ -0,0 +1,36 @@
package ru.otus.petstore.service;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import ru.otus.petstore.config.TestConfig;
import static io.restassured.RestAssured.given;
public abstract class BaseService {
protected final RequestSpecification requestSpec = new RequestSpecBuilder()
.setBaseUri(TestConfig.getBaseUri())
.setBasePath(TestConfig.getBasePath())
.setContentType(ContentType.JSON)
.log(LogDetail.URI)
.log(LogDetail.METHOD)
.log(LogDetail.PARAMS)
.log(LogDetail.BODY)
.build();
protected final ResponseSpecification okSpec = new ResponseSpecBuilder()
.expectStatusCode(200)
.build();
protected final ResponseSpecification notFoundSpec = new ResponseSpecBuilder()
.expectStatusCode(404)
.build();
protected RequestSpecification givenBase() {
return given().spec(requestSpec);
}
}

View File

@@ -0,0 +1,72 @@
package ru.otus.petstore.service;
import io.restassured.response.Response;
import ru.otus.petstore.model.Pet;
public class PetService extends BaseService {
public Response findByStatus(String status) {
return givenBase()
.queryParam("status", status)
.when()
.get("/pet/findByStatus");
}
public Response createPet(Pet pet) {
return givenBase()
.body(pet)
.when()
.post("/pet");
}
public Response createPetExpectSuccess(Pet pet) {
return createPet(pet)
.then()
.spec(okSpec)
.extract()
.response();
}
public Response createPetRaw(String rawJson) {
return givenBase()
.body(rawJson)
.when()
.post("/pet");
}
public Response getPetById(long id) {
return givenBase()
.when()
.get("/pet/{id}", id);
}
public Response getPetByIdExpectSuccess(long id) {
return getPetById(id)
.then()
.spec(okSpec)
.extract()
.response();
}
public Response getPetByIdExpectNotFound(long id) {
return getPetById(id)
.then()
.spec(notFoundSpec)
.extract()
.response();
}
public Response deletePet(long id) {
return givenBase()
.when()
.delete("/pet/{id}", id);
}
public void deletePetIfExists(long id) {
Response response = deletePet(id);
int statusCode = response.statusCode();
if (statusCode != 200 && statusCode != 404) {
throw new IllegalStateException("Unexpected status for DELETE /pet/{id}: " + statusCode);
}
}
}