Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'release/1.0.3'

* release/1.0.3:
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] prepare release halbuilder-1.0.3
  Fixed up my local tests following Joost's merge.
  Fix ResourceReader.read implementation signatures
  Revert import rearrangement
  Added simple UriBuilder based test.
  Replace tabs with spaces
  Change references to ReadableResource
  Remove RenderableResource
  Add URI convenience Resource.withLink methods
  Fold RenderableResource into ReadableResource
  Renamed newResource(Reader) to readResource(Reader)
  Add URI convenience methods to ResourceFactory
  Make constants final
  Fix small typo in example.
  • Loading branch information...
commit 99eaad0097c0e6fe38cc14757fb46b2a33ec4e14 2 parents dab54ec + e74041f
@talios talios authored
Showing with 195 additions and 122 deletions.
  1. +2 −2 README.md
  2. +7 −1 pom.xml
  3. +1 −1  src/it/clojure-hal/pom.xml
  4. +1 −1  src/it/clojure-hal/src/test/clojure/haltest.clj
  5. +12 −3 src/main/java/com/theoryinpractise/halbuilder/ResourceFactory.java
  6. +1 −2  src/main/java/com/theoryinpractise/halbuilder/impl/api/ResourceReader.java
  7. +2 −4 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonResourceReader.java
  8. +1 −4 src/main/java/com/theoryinpractise/halbuilder/impl/resources/BaseResource.java
  9. +1 −2  src/main/java/com/theoryinpractise/halbuilder/impl/resources/ImmutableResource.java
  10. +65 −2 src/main/java/com/theoryinpractise/halbuilder/impl/resources/MutableResource.java
  11. +3 −5 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlResourceReader.java
  12. +15 −3 src/main/java/com/theoryinpractise/halbuilder/spi/ReadableResource.java
  13. +0 −28 src/main/java/com/theoryinpractise/halbuilder/spi/RenderableResource.java
  14. +10 −2 src/main/java/com/theoryinpractise/halbuilder/spi/Resource.java
  15. +4 −6 src/test/java/com/theoryinpractise/halbuilder/InterfaceSatisfactionTest.java
  16. +62 −39 src/test/java/com/theoryinpractise/halbuilder/RenderingTest.java
  17. +6 −6 src/test/java/com/theoryinpractise/halbuilder/ResourceReaderTest.java
  18. +0 −8 src/test/java/com/theoryinpractise/halbuilder/ResourceTest.java
  19. +2 −3 src/test/java/com/theoryinpractise/halbuilder/ValidationTest.java
View
4 README.md
@@ -19,8 +19,8 @@ Halbuilder is a simple Java API for generating and consuming HAL documents confo
.withProperty("summary", "An example list")
.withSubresource("td:owner", owner);
- String xml = halResource.renderContent(ResourceFactory.HALXML);
- String json = halResource.renderContent(ResourceFactory.HALJSON);
+ String xml = halResource.asRenderableResource().renderContent(ResourceFactory.HAL_XML);
+ String json = halResource.asRenderableResource().renderContent(ResourceFactory.HAL_JSON);
### Reading Local Resources
View
8 pom.xml
@@ -3,7 +3,7 @@
<groupId>com.theoryinpractise</groupId>
<artifactId>halbuilder</artifactId>
- <version>1.0.3-SNAPSHOT</version>
+ <version>1.0.4-SNAPSHOT</version>
<description>Java based builder for the Hal specification http://stateless.co/hal_specification.html</description>
<packaging>jar</packaging>
@@ -130,6 +130,12 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-bundle</artifactId>
+ <version>1.13-b01</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>11.0.1</version>
View
2  src/it/clojure-hal/pom.xml
@@ -29,7 +29,7 @@
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
- <version>1.3.0</version>
+ <version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.theoryinpractise</groupId>
View
2  src/it/clojure-hal/src/test/clojure/haltest.clj
@@ -6,4 +6,4 @@
resource (doto (.newResource resource-factory "/foo")
(.withProperty "name" "Mark")
(.withLink "/home" "home"))]
- (println (.renderContent (.asRenderableResource resource) "application/hal+xml"))) )
+ (println (.renderContent resource "application/hal+xml"))) )
View
15 src/main/java/com/theoryinpractise/halbuilder/ResourceFactory.java
@@ -19,6 +19,7 @@
import java.io.BufferedReader;
import java.io.Reader;
+import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -27,8 +28,8 @@
public class ResourceFactory {
- public static String HAL_XML = "application/hal+xml";
- public static String HAL_JSON = "application/hal+json";
+ public static final String HAL_XML = "application/hal+xml";
+ public static final String HAL_JSON = "application/hal+json";
private Map<ContentType, Class<? extends Renderer>> contentRenderers = Maps.newHashMap();
private TreeMap<String, String> namespaces = Maps.newTreeMap(Ordering.usingToString());
@@ -39,6 +40,10 @@ public ResourceFactory() {
this("http://localhost");
}
+ public ResourceFactory(URI baseUri) {
+ this(baseUri.toASCIIString());
+ }
+
public ResourceFactory(String baseHref) {
this.baseHref = baseHref;
this.contentRenderers.put(new ContentType(HAL_XML), XmlRenderer.class);
@@ -67,6 +72,10 @@ public ResourceFactory withLink(String url, String rel) {
return this;
}
+ public Resource newResource(URI uri) {
+ return newResource(uri.toASCIIString());
+ }
+
public Resource newResource(String href) {
MutableResource resource = new MutableResource(this, href);
@@ -84,7 +93,7 @@ public Resource newResource(String href) {
return resource;
}
- public ReadableResource newResource(Reader reader) {
+ public ReadableResource readResource(Reader reader) {
try {
Reader bufferedReader = new BufferedReader(reader);
bufferedReader.mark(1);
View
3  src/main/java/com/theoryinpractise/halbuilder/impl/api/ResourceReader.java
@@ -1,10 +1,9 @@
package com.theoryinpractise.halbuilder.impl.api;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import java.io.Reader;
public interface ResourceReader {
- RenderableResource read(Reader source);
+ ReadableResource read(Reader source);
}
View
6 src/main/java/com/theoryinpractise/halbuilder/impl/json/JsonResourceReader.java
@@ -6,7 +6,6 @@
import com.theoryinpractise.halbuilder.impl.api.ResourceReader;
import com.theoryinpractise.halbuilder.impl.resources.MutableResource;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import com.theoryinpractise.halbuilder.spi.ResourceException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
@@ -23,7 +22,6 @@
import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
-
public class JsonResourceReader implements ResourceReader {
private ResourceFactory resourceFactory;
@@ -31,7 +29,7 @@ public JsonResourceReader(ResourceFactory resourceFactory) {
this.resourceFactory = resourceFactory;
}
- public RenderableResource read(Reader reader) {
+ public ReadableResource read(Reader reader) {
try {
ObjectMapper mapper = new ObjectMapper();
@@ -39,7 +37,7 @@ public RenderableResource read(Reader reader) {
MutableResource resource = readResource(rootNode);
- return resource.asRenderableResource();
+ return resource.toImmutableResource();
} catch (Exception e) {
throw new ResourceException(e);
}
View
5 src/main/java/com/theoryinpractise/halbuilder/impl/resources/BaseResource.java
@@ -5,7 +5,6 @@
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
-import com.google.common.base.Strings;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -21,7 +20,6 @@
import com.theoryinpractise.halbuilder.spi.Contract;
import com.theoryinpractise.halbuilder.spi.Link;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import com.theoryinpractise.halbuilder.spi.Resource;
import com.theoryinpractise.halbuilder.spi.ResourceException;
@@ -44,7 +42,6 @@
import static com.theoryinpractise.halbuilder.impl.resources.LinkPredicate.newLinkPredicate;
import static java.lang.String.format;
-
public abstract class BaseResource implements ReadableResource {
public static final Ordering<Link> RELATABLE_ORDERING = Ordering.from(new Comparator<Link>() {
@@ -252,7 +249,7 @@ protected String resolveRelativeHref(final String baseHref, String href) {
}
- public RenderableResource asRenderableResource() {
+ public ImmutableResource toImmutableResource() {
return new ImmutableResource(resourceFactory, getNamespaces(), getCanonicalLinks(), getProperties(), getResources());
}
View
3  src/main/java/com/theoryinpractise/halbuilder/impl/resources/ImmutableResource.java
@@ -5,7 +5,6 @@
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceRenderer;
import com.theoryinpractise.halbuilder.spi.Link;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import com.theoryinpractise.halbuilder.spi.Renderer;
import com.theoryinpractise.halbuilder.spi.Resource;
@@ -13,7 +12,7 @@
import java.util.List;
import java.util.Map;
-public class ImmutableResource extends BaseResource implements RenderableResource {
+public class ImmutableResource extends BaseResource {
private final Link resourceLink;
View
67 src/main/java/com/theoryinpractise/halbuilder/impl/resources/MutableResource.java
@@ -4,8 +4,11 @@
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.theoryinpractise.halbuilder.ResourceFactory;
+import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
+import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceRenderer;
import com.theoryinpractise.halbuilder.spi.Link;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
+import com.theoryinpractise.halbuilder.spi.Renderer;
import com.theoryinpractise.halbuilder.spi.Resource;
import com.theoryinpractise.halbuilder.spi.ResourceException;
import com.theoryinpractise.halbuilder.spi.Serializable;
@@ -14,9 +17,11 @@
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
+import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
+import java.net.URI;
import static com.theoryinpractise.halbuilder.impl.api.Support.WHITESPACE_SPLITTER;
import static java.lang.String.format;
@@ -49,6 +54,17 @@ public MutableResource withLink(String href, String rel) {
/**
* Add a link to this resource
+ * @param uri The target URI for the link, possibly relative to the href of
+ * this resource.
+ * @param rel
+ * @return
+ */
+ public MutableResource withLink(URI uri, String rel) {
+ return withLink(uri.toASCIIString(), rel);
+ }
+
+ /**
+ * Add a link to this resource
* @param href The target href for the link, relative to the href of this resource.
* @param rel
* @return
@@ -64,11 +80,22 @@ public MutableResource withLink(String href, String rel, Predicate<ReadableResou
/**
* Add a link to this resource
- * @param href The target href for the link, relative to the href of this resource.
+ * @param uri The target URI for the link, possibly relative to the href of
+ * this resource.
* @param rel
* @return
*/
- public MutableResource withLink(String href, String rel, Optional<Predicate<ReadableResource>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
+ public MutableResource withLink(URI uri, String rel, Predicate<ReadableResource> predicate) {
+ return withLink(uri.toASCIIString(), rel, predicate);
+ }
+
+ /**
+ * Add a link to this resource
+ * @param href The target href for the link, relative to the href of this resource.
+ * @param rel
+ * @return
+ */
+ public MutableResource withLink(String href, String rel, Optional<Predicate<ReadableResource>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
if (predicate.or(Predicates.<ReadableResource>alwaysTrue()).apply(this)) {
String resolvedHref = resolvableUri.matcher(href).matches() ? resolveRelativeHref(href) : href;
for (String reltype : WHITESPACE_SPLITTER.split(rel)) {
@@ -80,6 +107,17 @@ public MutableResource withLink(String href, String rel, Optional<Predicate<Read
return this;
}
+ /**
+ * Add a link to this resource
+ * @param uri The target URI for the link, possibly relative to the href of
+ * this resource.
+ * @param rel
+ * @return
+ */
+ public MutableResource withLink(URI uri, String rel, Optional<Predicate<ReadableResource>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang) {
+ return withLink(uri.toASCIIString(), rel, predicate, name, title, hreflang);
+ }
+
public Resource withProperty(String name, Object value) {
if (properties.containsKey(name)) {
throw new ResourceException(format("Duplicate property '%s' found for resource", name));
@@ -156,4 +194,29 @@ public MutableResource withSubresource(String rel, Resource resource) {
return this;
}
+ /**
+ * Renders the current Resource as a proxy to the provider interface
+ *
+ * @param anInterface The interface we wish to proxy the resource as
+ * @return A Guava Optional of the rendered class, this will be absent if the interface doesn't satisfy the interface
+ */
+ public <T> Optional<T> renderClass(Class<T> anInterface) {
+ if (InterfaceContract.newInterfaceContract(anInterface).isSatisfiedBy(this)) {
+ return InterfaceRenderer.newInterfaceRenderer(anInterface).render(toImmutableResource(), null);
+ } else {
+ return Optional.absent();
+ }
+ }
+
+ public String renderContent(String contentType) {
+ Renderer<String> renderer = resourceFactory.lookupRenderer(contentType);
+ return renderAsString(renderer);
+ }
+
+ private String renderAsString(final Renderer renderer) {
+ validateNamespaces(this);
+ StringWriter sw = new StringWriter();
+ renderer.render(toImmutableResource(), sw);
+ return sw.toString();
+ }
}
View
8 src/main/java/com/theoryinpractise/halbuilder/impl/xml/XmlResourceReader.java
@@ -6,10 +6,8 @@
import com.theoryinpractise.halbuilder.impl.api.ResourceReader;
import com.theoryinpractise.halbuilder.impl.resources.MutableResource;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import com.theoryinpractise.halbuilder.spi.Resource;
import com.theoryinpractise.halbuilder.spi.ResourceException;
-import org.codehaus.jackson.JsonNode;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
@@ -31,11 +29,11 @@ public XmlResourceReader(ResourceFactory resourceFactory) {
this.resourceFactory = resourceFactory;
}
- public RenderableResource read(Reader reader) {
+ public ReadableResource read(Reader reader) {
try {
Document d = new SAXBuilder().build(reader);
Element root = d.getRootElement();
- return readResource(root).asRenderableResource();
+ return readResource(root).toImmutableResource();
} catch (JDOMException e) {
throw new ResourceException(e);
} catch (IOException e) {
@@ -43,7 +41,7 @@ public RenderableResource read(Reader reader) {
}
}
- private Resource readResource(Element root) {
+ private MutableResource readResource(Element root) {
String href = root.getAttributeValue("href");
MutableResource resource = new MutableResource(resourceFactory, href);
View
18 src/main/java/com/theoryinpractise/halbuilder/spi/ReadableResource.java
@@ -96,9 +96,21 @@
<T, V> Optional<V> ifSatisfiedBy(Class<T> anInterface, Function<T, V> function);
/**
- * Return the resource as a optimized for rendering resource.
- * @return A RenderableResource
+ * Returns an optional proxy to the given interface mirroring the resource.
+ *
+ * @param anInterface An interface to mirror
+ * @return A Guava Optional Resource Proxy
*/
- RenderableResource asRenderableResource();
+ <T> Optional<T> renderClass(Class<T> anInterface);
+ /**
+ * Returns the resource in the request content-type.
+ *
+ * application/hal+xml and application/hal+json are provided by default,
+ * additional Renderers can be added to a ResourceFactory.
+ *
+ * @param contentType The content type requested
+ * @return A String
+ */
+ String renderContent(String contentType);
}
View
28 src/main/java/com/theoryinpractise/halbuilder/spi/RenderableResource.java
@@ -1,28 +0,0 @@
-package com.theoryinpractise.halbuilder.spi;
-
-import com.google.common.base.Optional;
-
-/**
- * A RenderableResource is a Resource that has been optimized for rendering.
- */
-public interface RenderableResource extends ReadableResource {
-
- /**
- * Returns an optional proxy to the given interface mirroring the resource.
- * @param anInterface An interface to mirror
- * @return A Guava Optional Resource Proxy
- */
- <T> Optional<T> renderClass(Class<T> anInterface);
-
- /**
- * Returns the resource in the request content-type.
- *
- * application/hal+xml and application/hal+json are provided by default,
- * additional Renderers can be added to a ResourceFactory.
- *
- * @param contentType The content type requested
- * @return A String
- */
- String renderContent(String contentType);
-
-}
View
12 src/main/java/com/theoryinpractise/halbuilder/spi/Resource.java
@@ -1,16 +1,24 @@
package com.theoryinpractise.halbuilder.spi;
+import java.net.URI;
+
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
public interface Resource extends ReadableResource {
- Resource withLink(String url, String rel);
+ Resource withLink(String href, String rel);
+
+ Resource withLink(URI uri, String rel);
- Resource withLink(String url, String rel, Predicate<ReadableResource> predicate);
+ Resource withLink(String href, String rel, Predicate<ReadableResource> predicate);
+
+ Resource withLink(URI uri, String rel, Predicate<ReadableResource> predicate);
Resource withLink(String href, String rel, Optional<Predicate<ReadableResource>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang);
+ Resource withLink(URI uri, String rel, Optional<Predicate<ReadableResource>> predicate, Optional<String> name, Optional<String> title, Optional<String> hreflang);
+
Resource withProperty(String name, Object value);
Resource withBean(Object value);
View
10 src/test/java/com/theoryinpractise/halbuilder/InterfaceSatisfactionTest.java
@@ -4,7 +4,6 @@
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
import com.theoryinpractise.halbuilder.spi.Contract;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -52,7 +51,7 @@
@Test(dataProvider = "providerSatisfactionData")
public void testSimpleInterfaceSatisfaction(Class<?> aClass, boolean shouldBeSatisfied) {
- ReadableResource resource = resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableResource resource = resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
assertThat(resource.isSatisfiedBy(InterfaceContract.newInterfaceContract(aClass))).isEqualTo(shouldBeSatisfied);
}
@@ -78,7 +77,7 @@ public boolean isSatisfiedBy(ReadableResource resource) {
}
};
- ReadableResource resource = resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableResource resource = resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
assertThat(resource.isSatisfiedBy(contractHasName)).isEqualTo(true);
assertThat(resource.isSatisfiedBy(contractHasOptional)).isEqualTo(true);
@@ -88,8 +87,7 @@ public boolean isSatisfiedBy(ReadableResource resource) {
@Test
public void testClassRendering() {
- RenderableResource resource = resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")))
- .asRenderableResource();
+ ReadableResource resource = resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
assertThat(resource.renderClass(INamed.class).get().name()).isEqualTo("Example Resource");
assertThat(resource.renderClass(IPerson.class).get().getName()).isEqualTo("Example Resource");
@@ -100,7 +98,7 @@ public void testClassRendering() {
@Test
public void testFunctionalInterfaceSatisfaction() {
- ReadableResource resource = resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableResource resource = resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
String name = resource.ifSatisfiedBy(IPerson.class, new Function<IPerson, String>() {
public String apply(@Nullable IPerson iPerson) {
View
101 src/test/java/com/theoryinpractise/halbuilder/RenderingTest.java
@@ -3,24 +3,26 @@
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
-import com.theoryinpractise.halbuilder.spi.RenderableResource;
import com.theoryinpractise.halbuilder.spi.Resource;
import com.theoryinpractise.halbuilder.spi.ResourceException;
import com.theoryinpractise.halbuilder.spi.Serializable;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
+import java.net.URI;
import static org.fest.assertions.Assertions.assertThat;
public class RenderingTest {
private ResourceFactory resourceFactory = new ResourceFactory("https://example.com/api/")
- .withNamespace("ns", "/apidocs/accounts")
- .withNamespace("role", "/apidocs/roles");
+ .withNamespace("ns", "/apidocs/accounts")
+ .withNamespace("role", "/apidocs/roles");
private String exampleXml;
private String exampleJson;
@@ -40,26 +42,34 @@ public void setup() throws IOException {
exampleWithSubresourceJson = Resources.toString(RenderingTest.class.getResource("exampleWithSubresource.json"), Charsets.UTF_8)
.trim();
exampleWithMultipleSubresourcesXml = Resources.toString(RenderingTest.class.getResource("exampleWithMultipleSubresources.xml"), Charsets.UTF_8)
- .trim().replaceAll("\n", "\r\n");
+ .trim().replaceAll("\n", "\r\n");
exampleWithMultipleSubresourcesJson = Resources.toString(RenderingTest.class.getResource("exampleWithMultipleSubresources.json"), Charsets.UTF_8)
- .trim();
+ .trim();
+ }
+
+
+ private Resource newBaseResource(final Resource resource) {
+ return resource.withLink("/api/customer/1234", "ns:parent",
+ Optional.<Predicate<ReadableResource>>absent(),
+ Optional.of("bob"),
+ Optional.of("The Parent"),
+ Optional.of("en"));
}
+ private Resource newBaseResource(final URI uri) {
+ return newBaseResource(resourceFactory.newResource(uri));
+
+ }
private Resource newBaseResource(final String href) {
- return resourceFactory.newResource(href)
- .withLink("/api/customer/1234", "ns:parent",
- Optional.<Predicate<ReadableResource>>absent(),
- Optional.of("bob"),
- Optional.of("The Parent"),
- Optional.of("en") );
+ return newBaseResource(resourceFactory.newResource(href));
}
@Test
public void testFactoryWithLinks() {
ResourceFactory resourceFactory = new ResourceFactory("https://example.com/api/")
- .withLink("/home", "home");
+ .withLink("/home", "home");
Resource resource = resourceFactory.newResource("/");
@@ -71,24 +81,43 @@ public void testFactoryWithLinks() {
@Test(expectedExceptions = ResourceException.class)
public void testFactoryWithDuplicateNamespaces() {
- ResourceFactory resourceFactory = new ResourceFactory()
+ new ResourceFactory()
.withNamespace("home", "https://example.com/api/")
.withNamespace("home", "https://example.com/api/");
}
@Test
+ public void testUriBuilderHal() {
+
+ URI path = UriBuilder.fromPath("customer/{id}").buildFromMap(ImmutableMap.of("id", "123456"));
+
+ ReadableResource party = newBaseResource(path)
+ .withLink("?users", "ns:users")
+ .withProperty("id", 123456)
+ .withProperty("age", 33)
+ .withProperty("name", "Example Resource")
+ .withProperty("optional", Boolean.TRUE)
+ .withProperty("expired", Boolean.FALSE);
+
+ assertThat(party.getResourceLink().getHref()).isEqualTo("https://example.com/api/customer/123456");
+ assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
+ assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
+
+ }
+
+ @Test
public void testCustomerHal() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
.withProperty("id", 123456)
.withProperty("age", 33)
.withProperty("name", "Example Resource")
.withProperty("optional", Boolean.TRUE)
- .withProperty("expired", Boolean.FALSE)
- .asRenderableResource();
+ .withProperty("expired", Boolean.FALSE);
+ assertThat(party.getResourceLink().getHref()).isEqualTo("https://example.com/api/customer/123456");
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
@@ -97,7 +126,7 @@ public void testCustomerHal() {
@Test
public void testWithSerializable() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
.withSerializable(new Serializable() {
public void serializeResource(Resource resource) {
@@ -107,8 +136,7 @@ public void serializeResource(Resource resource) {
.withProperty("optional", Boolean.TRUE)
.withProperty("expired", Boolean.FALSE);
}
- })
- .asRenderableResource();
+ });
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
@@ -119,10 +147,9 @@ public void serializeResource(Resource resource) {
@Test
public void testHalWithBean() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
- .withBean(new Customer(123456, "Example Resource", 33))
- .asRenderableResource();
+ .withBean(new Customer(123456, "Example Resource", 33));
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
@@ -132,10 +159,9 @@ public void testHalWithBean() {
@Test
public void testHalWithFields() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
- .withFields(new OtherCustomer(123456, "Example Resource", 33))
- .asRenderableResource();
+ .withFields(new OtherCustomer(123456, "Example Resource", 33));
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleJson);
@@ -145,16 +171,15 @@ public void testHalWithFields() {
@Test
public void testHalWithSubResources() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
.withSubresource("ns:user role:admin", resourceFactory
- .newResource("/user/11")
- .withProperty("id", 11)
- .withProperty("name", "Example User")
- .withProperty("expired", false)
- .withProperty("age", 32)
- .withProperty("optional", true))
- .asRenderableResource();
+ .newResource("/user/11")
+ .withProperty("id", 11)
+ .withProperty("name", "Example User")
+ .withProperty("expired", false)
+ .withProperty("age", 32)
+ .withProperty("optional", true));
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithSubresourceXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithSubresourceJson);
@@ -164,10 +189,9 @@ public void testHalWithSubResources() {
@Test
public void testHalWithBeanSubResource() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
- .withBeanBasedSubresource("ns:user role:admin", "/user/11", new Customer(11, "Example User", 32))
- .asRenderableResource();
+ .withBeanBasedSubresource("ns:user role:admin", "/user/11", new Customer(11, "Example User", 32));
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithSubresourceXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithSubresourceJson);
@@ -177,11 +201,10 @@ public void testHalWithBeanSubResource() {
@Test
public void testHalWithBeanMultipleSubResources() {
- RenderableResource party = newBaseResource("customer/123456")
+ ReadableResource party = newBaseResource("customer/123456")
.withLink("?users", "ns:users")
.withBeanBasedSubresource("ns:user role:admin", "/user/11", new Customer(11, "Example User", 32))
- .withBeanBasedSubresource("ns:user role:admin", "/user/12", new Customer(12, "Example User", 32))
- .asRenderableResource();
+ .withBeanBasedSubresource("ns:user role:admin", "/user/12", new Customer(12, "Example User", 32));
assertThat(party.renderContent(ResourceFactory.HAL_XML)).isEqualTo(exampleWithMultipleSubresourcesXml);
assertThat(party.renderContent(ResourceFactory.HAL_JSON)).isEqualTo(exampleWithMultipleSubresourcesJson);
View
12 src/test/java/com/theoryinpractise/halbuilder/ResourceReaderTest.java
@@ -19,16 +19,16 @@
@DataProvider
public Object[][] provideResources() {
return new Object[][]{
- {resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")))},
- {resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.json")))},
+ {resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")))},
+ {resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.json")))},
};
}
@DataProvider
public Object[][] provideSubResources() {
return new Object[][]{
- {resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithSubresource.xml")))},
- {resourceFactory.newResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithSubresource.json")))},
+ {resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithSubresource.xml")))},
+ {resourceFactory.readResource(new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("exampleWithSubresource.json")))},
};
}
@@ -62,12 +62,12 @@ public void testSubReader(ReadableResource resource) {
@Test(expectedExceptions = ResourceException.class)
public void testUnknownFormat() {
- resourceFactory.newResource(new StringReader("!!!"));
+ resourceFactory.readResource(new StringReader("!!!"));
}
@Test(expectedExceptions = ResourceException.class)
public void testNullReader() {
- resourceFactory.newResource((Reader) null);
+ resourceFactory.readResource((Reader) null);
}
}
View
8 src/test/java/com/theoryinpractise/halbuilder/ResourceTest.java
@@ -13,7 +13,6 @@
public void testUndeclaredLinkNamespace() {
resourceFactory.newResource("/test")
.withLink("http://localhost/test/2", "td:test")
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
}
@@ -21,7 +20,6 @@ public void testUndeclaredLinkNamespace() {
public void testUndeclaredResourceNamespace() {
resourceFactory.newResource("http://localhost/test")
.withSubresource("td:test", resourceFactory.newResource("/"))
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
}
@@ -29,7 +27,6 @@ public void testUndeclaredResourceNamespace() {
public void testUndeclaredResourceLinkNamespace() {
resourceFactory.newResource("http://localhost/test")
.withSubresource("test", resourceFactory.newResource("/").withLink("/", "td:test"))
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
}
@@ -38,14 +35,12 @@ public void testDuplicatePropertyDefinitions() {
resourceFactory.newResource("http://localhost/test")
.withProperty("name", "Example User")
.withProperty("name", "Example User")
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
}
@Test
public void testHalResourceHrefShouldBeFullyQualified() {
String xml = resourceFactory.newResource("/test")
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/test");
@@ -55,7 +50,6 @@ public void testHalResourceHrefShouldBeFullyQualified() {
public void testRelativeLinksRenderFullyQualified() {
String xml = resourceFactory.newResource("/")
.withLink("/test", "test")
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/test");
@@ -65,7 +59,6 @@ public void testRelativeLinksRenderFullyQualified() {
public void testRelativeResourceRenderFullyQualified() {
String xml = resourceFactory.newResource("/")
.withSubresource("test", resourceFactory.newResource("subresource"))
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/subresource");
@@ -78,7 +71,6 @@ public void testRelativeResourceLinksRenderFullyQualified() {
.newResource("subresource/")
.withLink("/sublink1", "sub")
.withLink("~/sublink2", "sub2"))
- .asRenderableResource()
.renderContent(ResourceFactory.HAL_XML);
assertThat(xml).contains("http://localhost/sublink1");
View
5 src/test/java/com/theoryinpractise/halbuilder/ValidationTest.java
@@ -8,7 +8,6 @@
import javax.annotation.Nullable;
import java.io.InputStreamReader;
-import java.util.regex.Pattern;
import static org.fest.assertions.Assertions.assertThat;
@@ -17,8 +16,8 @@
ResourceFactory resourceFactory = new ResourceFactory();
- ReadableResource resource = resourceFactory.newResource(
- new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
+ ReadableResource resource = resourceFactory.readResource(
+ new InputStreamReader(ResourceReaderTest.class.getResourceAsStream("example.xml")));
public static interface Namable {
Please sign in to comment.
Something went wrong with that request. Please try again.