[BAEL-11601] Move and update Testing REST with multiple MIME types article code
Moved the code and cleaned spring-rest-full module.
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package org.baeldung.common.web;
|
||||
|
||||
import static org.baeldung.Consts.APPLICATION_PORT;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.baeldung.test.IMarshaller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
public abstract class AbstractLiveTest<T extends Serializable> {
|
||||
|
||||
protected final Class<T> clazz;
|
||||
|
||||
@Autowired
|
||||
protected IMarshaller marshaller;
|
||||
|
||||
public AbstractLiveTest(final Class<T> clazzToSet) {
|
||||
super();
|
||||
|
||||
Preconditions.checkNotNull(clazzToSet);
|
||||
clazz = clazzToSet;
|
||||
}
|
||||
|
||||
// template method
|
||||
|
||||
public abstract void create();
|
||||
|
||||
public abstract String createAsUri();
|
||||
|
||||
protected final void create(final T resource) {
|
||||
createAsUri(resource);
|
||||
}
|
||||
|
||||
protected final String createAsUri(final T resource) {
|
||||
final Response response = createAsResponse(resource);
|
||||
Preconditions.checkState(response.getStatusCode() == 201, "create operation: " + response.getStatusCode());
|
||||
|
||||
final String locationOfCreatedResource = response.getHeader(HttpHeaders.LOCATION);
|
||||
Preconditions.checkNotNull(locationOfCreatedResource);
|
||||
return locationOfCreatedResource;
|
||||
}
|
||||
|
||||
final Response createAsResponse(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
|
||||
final String resourceAsString = marshaller.encode(resource);
|
||||
return RestAssured.given()
|
||||
.contentType(marshaller.getMime())
|
||||
.body(resourceAsString)
|
||||
.post(getURL());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
protected String getURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-rest-full/auth/foos";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.test")
|
||||
public class ConfigIntegrationTest extends WebMvcConfigurerAdapter {
|
||||
|
||||
public ConfigIntegrationTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package org.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IMarshaller {
|
||||
|
||||
<T> String encode(final T entity);
|
||||
|
||||
<T> T decode(final String entityAsString, final Class<T> clazz);
|
||||
|
||||
<T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz);
|
||||
|
||||
String getMime();
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package org.baeldung.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public final class JacksonMarshaller implements IMarshaller {
|
||||
private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class);
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JacksonMarshaller() {
|
||||
super();
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final <T> String encode(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
String entityAsJSON = null;
|
||||
try {
|
||||
entityAsJSON = objectMapper.writeValueAsString(resource);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entityAsJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourceAsString);
|
||||
|
||||
T entity = null;
|
||||
try {
|
||||
entity = objectMapper.readValue(resourceAsString, clazz);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourcesAsString);
|
||||
|
||||
List<T> entities = null;
|
||||
try {
|
||||
if (clazz.equals(Foo.class)) {
|
||||
entities = objectMapper.readValue(resourcesAsString, new TypeReference<List<Foo>>() {
|
||||
// ...
|
||||
});
|
||||
} else {
|
||||
entities = objectMapper.readValue(resourcesAsString, List.class);
|
||||
}
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getMime() {
|
||||
return MediaType.APPLICATION_JSON.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.baeldung.test;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("test")
|
||||
public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public TestMarshallerFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public IMarshaller getObject() {
|
||||
final String testMime = env.getProperty("test.mime");
|
||||
if (testMime != null) {
|
||||
switch (testMime) {
|
||||
case "json":
|
||||
return new JacksonMarshaller();
|
||||
case "xml":
|
||||
return new XStreamMarshaller();
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
return new JacksonMarshaller();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<IMarshaller> getObjectType() {
|
||||
return IMarshaller.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package org.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public final class XStreamMarshaller implements IMarshaller {
|
||||
|
||||
private XStream xstream;
|
||||
|
||||
public XStreamMarshaller() {
|
||||
super();
|
||||
|
||||
xstream = new XStream();
|
||||
xstream.autodetectAnnotations(true);
|
||||
xstream.processAnnotations(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final <T> String encode(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
return xstream.toXML(resource);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourceAsString);
|
||||
return (T) xstream.fromXML(resourceAsString);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
|
||||
return this.decode(resourcesAsString, List.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getMime() {
|
||||
return MediaType.APPLICATION_XML.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user