#51 - Added HalLinkDiscoverer.

Added LinkDiscoverer implementation that discovers Links from HAL style representations. Extracted AbstractLinkDiscovererUnitTest to define expected behavior for LinkDiscoverers in general.
This commit is contained in:
Oliver Gierke
2013-02-14 19:38:13 +01:00
parent 29b433445c
commit 72837210ee
4 changed files with 166 additions and 42 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.hal;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
/**
* {@link LinkDiscoverer} implementation based on HAL link structure.
*
* @author Oliver Gierke
*/
public class HalLinkDiscoverer extends JsonPathLinkDiscoverer {
public HalLinkDiscoverer() {
super("$_links..%s.href");
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
/**
* Base class for unit tests for {@link LinkDiscoverer} implementations.
*
* @author Oliver Gierke
*/
public abstract class AbstractLinkDiscovererUnitTests {
@Test
public void findsSingleLink() {
assertThat(getDiscoverer().findLinkWithRel("self", getInputString()), is(new Link("selfHref")));
List<Link> links = getDiscoverer().findLinksWithRel("self", getInputString());
assertThat(links, hasSize(1));
assertThat(links, hasItem(new Link("selfHref")));
}
@Test
public void findsFirstLink() {
assertThat(getDiscoverer().findLinkWithRel("relation", getInputString()), is(new Link("firstHref", "relation")));
}
@Test
public void findsAllLinks() {
List<Link> links = getDiscoverer().findLinksWithRel("relation", getInputString());
assertThat(links, hasSize(2));
assertThat(links, hasItems(new Link("firstHref", "relation"), new Link("secondHref", "relation")));
}
@Test
public void returnsForInexistingLink() {
assertThat(getDiscoverer().findLinkWithRel("something", getInputString()), is(nullValue()));
}
@Test
public void returnsForInxistingLinkFromInputStream() throws Exception {
InputStream inputStream = new ByteArrayInputStream(getInputString().getBytes("UTF-8"));
assertThat(getDiscoverer().findLinkWithRel("something", inputStream), is(nullValue()));
}
/**
* Return the {@link LinkDiscoverer} to be tested.
*
* @return
*/
protected abstract LinkDiscoverer getDiscoverer();
/**
* Return the JSON structure we expect to find the links in.
*
* @return
*/
protected abstract String getInputString();
}

View File

@@ -15,15 +15,6 @@
*/
package org.springframework.hateoas.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
/**
@@ -31,7 +22,7 @@ import org.springframework.hateoas.LinkDiscoverer;
*
* @author Oliver Gierke
*/
public class DefaultLinkDiscovererUnitTest {
public class DefaultLinkDiscovererUnitTest extends AbstractLinkDiscovererUnitTests {
static final String SAMPLE = "{ links : [ " + //
"{ rel : 'self', href : 'selfHref' }, " + //
@@ -40,39 +31,13 @@ public class DefaultLinkDiscovererUnitTest {
static final LinkDiscoverer discoverer = new DefaultLinkDiscoverer();
@Test
public void findsSingleLink() {
assertThat(discoverer.findLinkWithRel("self", SAMPLE), is(new Link("selfHref")));
List<Link> links = discoverer.findLinksWithRel("self", SAMPLE);
assertThat(links, hasSize(1));
assertThat(links, hasItem(new Link("selfHref")));
@Override
protected LinkDiscoverer getDiscoverer() {
return discoverer;
}
@Test
public void findsFirstLink() {
assertThat(discoverer.findLinkWithRel("relation", SAMPLE), is(new Link("firstHref", "relation")));
}
@Test
public void findsAllLinks() {
List<Link> links = discoverer.findLinksWithRel("relation", SAMPLE);
assertThat(links, hasSize(2));
assertThat(links, hasItems(new Link("firstHref", "relation"), new Link("secondHref", "relation")));
}
@Test
public void returnsForInexistingLink() {
assertThat(discoverer.findLinkWithRel("something", SAMPLE), is(nullValue()));
}
@Test
public void returnsForInxistingLinkFromInputStream() throws Exception {
InputStream inputStream = new ByteArrayInputStream(SAMPLE.getBytes("UTF-8"));
assertThat(discoverer.findLinkWithRel("something", inputStream), is(nullValue()));
@Override
protected String getInputString() {
return SAMPLE;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.hal;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.core.AbstractLinkDiscovererUnitTests;
/**
* Unit tests for {@link HalLinkDiscoverer}.
*
* @author Oliver Gierke
*/
public class HalLinkDiscovererUnitTest extends AbstractLinkDiscovererUnitTests {
static final LinkDiscoverer discoverer = new HalLinkDiscoverer();
static final String SAMPLE = "{ _links : { " + //
"self : { href : 'selfHref' }, " + //
"relation : [ " + //
"{ href : 'firstHref' }, { href : 'secondHref' }]}}";
@Override
protected LinkDiscoverer getDiscoverer() {
return discoverer;
}
@Override
protected String getInputString() {
return SAMPLE;
}
}