com.testdroid.api.APIEntity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testdroid-api Show documentation
Show all versions of testdroid-api Show documentation
The Testdroid API library for Java
The newest version!
package com.testdroid.api;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.testdroid.api.dto.Context;
import com.testdroid.api.serialization.level.Log4jLevelModule;
import java.io.File;
import java.io.InputStream;
import java.util.Map;
import static com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature.WRITE_XML_DECLARATION;
/**
* @author Łukasz Kajda
* @author Slawomir Pawluk
*/
public abstract class APIEntity {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final XmlMapper XML_MAPPER = new XmlMapper();
static {
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
OBJECT_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
OBJECT_MAPPER.registerModule(new JavaTimeModule());
OBJECT_MAPPER.registerModule(new Log4jLevelModule());
XML_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
XML_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
XML_MAPPER.registerModule(new JavaTimeModule());
XML_MAPPER.registerModule(new Log4jLevelModule());
XML_MAPPER.configure(WRITE_XML_DECLARATION, true);
XML_MAPPER.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true);
}
protected APIClient client;
protected Long id;
protected String selfURI;
//Technical field to allow group APIEntity with CriteriaQuery by parent ID.
@JsonIgnore
protected Long parentId;
protected APIEntity() {
}
protected APIEntity(Long id) {
this.id = id;
}
@JsonIgnore
protected static String createUri(String preUri, String postUri) {
if (preUri.contains("?")) {
String[] parts = preUri.split("\\?");
return String.format("%s%s", parts[0], postUri);
} else {
return String.format("%s%s", preUri, postUri);
}
}
/**
* Returns ID of entity if such ID exists. Usually it does not exist for
* lists. Please use
* hasId()
method to check if ID exists.
*/
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getSelfURI() {
return selfURI;
}
public void setSelfURI(String selfURI) {
this.selfURI = selfURI;
}
public Long getParentId() {
return parentId;
}
@JsonIgnore
protected APIResource getResource(String uri, Class type)
throws APIException {
checkClient(client);
return new APIResource<>(client, uri, type);
}
@JsonIgnore
protected APIListResource getListResource(String uri, Class type) throws APIException {
checkClient(client);
return new APIListResource<>(client, uri, type);
}
@JsonIgnore
protected APIListResource getListResource(String uri, Context context)
throws APIException {
checkClient(client);
return new APIListResource<>(client, uri, context);
}
@JsonIgnore
protected T postResource(String uri, Object body, Class type) throws APIException {
checkClient(client);
return client.post(uri, body, type);
}
@JsonIgnore
protected T postFile(
String uri, File file, Map fileExtraParams, String contentType, Class type)
throws APIException {
checkClient(client);
return client.postFile(uri, contentType, file, fileExtraParams, type);
}
@JsonIgnore
protected void deleteResource(String uri) throws APIException {
checkClient(client);
client.delete(uri);
}
@JsonIgnore
protected InputStream getFile(String uri) throws APIException {
checkClient(client);
return client.get(uri);
}
@JsonIgnore
public void refresh() throws APIException {
checkClient(client);
clone(client.get(selfURI, getClass()));
}
@JsonIgnore
private void checkClient(APIClient client) throws APIException {
if (client == null) {
throw new APIException("Missing API client");
}
}
@JsonIgnore
public String toJson() throws JsonProcessingException {
return OBJECT_MAPPER.writeValueAsString(this);
}
@JsonIgnore
protected abstract void clone(T from);
@JsonIgnore
protected void cloneBase(T from) {
this.id = from.id;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy