#837 - Moved sample code for fundamentals into test cases.

This commit is contained in:
Oliver Drotbohm
2019-03-03 13:19:33 +01:00
parent 70a072244c
commit 8ddca10ed3
3 changed files with 71 additions and 24 deletions

View File

@@ -317,7 +317,7 @@
<outputDirectory>${project.build.directory}/site/reference/html</outputDirectory>
<sectids>false</sectids>
<attributes>
<code-dir>${basedir}</code-dir>
<code-dir>${basedir}/src/docs/java/org/springframework/hateoas</code-dir>
<docinfo>shared</docinfo>
<linkcss>true</linkcss>
<icons>font</icons>
@@ -343,7 +343,7 @@
<backend>pdf</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<code-dir>${basedir}</code-dir>
<code-dir>${basedir}/src/docs/java/org/springframework/hateoas</code-dir>
</attributes>
</configuration>
</execution>

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2019 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;
import static org.assertj.core.api.Assertions.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
/**
* @author Oliver Drotbohm
*/
public class FundamentalsTest {
@Test
public void links() {
// tag::links[]
Link link = new Link("/something");
assertThat(link.getHref()).isEqualTo("/something");
assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);
link = new Link("/something", "my-rel");
assertThat(link.getHref()).isEqualTo("/something");
assertThat(link.getRel()).isEqualTo(LinkRelation.of("my-rel"));
// end::links[]
}
@Test
public void templatedLinks() {
// tag::templatedLinks[]
Link link = new Link("/{segment}/something{?parameter}");
assertThat(link.isTemplated()).isTrue(); // <1>
assertThat(link.getVariableNames()).contains("segment", "parameter"); // <2>
Map<String, Object> values = new HashMap<>();
values.put("segment", "path");
values.put("parameter", 42);
assertThat(link.expand(values).getHref()) // <3>
.isEqualTo("/path/something?parameter=42");
// end::templatedLinks[]
}
}

View File

@@ -1,5 +1,6 @@
[[fundamentals]]
= Fundamentals
:code-dir: ../../../src/docs/java/org/springframework/hateoas
This section covers the basics of Spring HATEOAS and its fundamental domain abstractions.
@@ -28,17 +29,12 @@ Spring HATEOAS let's you work with links through its immutable `Link` value type
Its constructor take both an hypertext reference and a link relation, the latter being defaulted to the IANA link relation `self`.
Read more on the latter in <<fundamentals.link-relations>>.
.Using links
====
[source, java]
[source, java, indent=0, tabsize=2]
----
Link link = new Link("/something");
assertThat(link.getHref()).isEqualTo("/something");
assertThat(link.getRel()).isEqualTo(IanaLinkRelation.SELF);
Link link = new Link("/something", "my-rel");
assertThat(link.getHref()).isEqualTo("/something");
assertThat(link.getRel()).isEqualTo(LinkRelation.of"my-rel");
include::{code-dir}/FundamentalsTest.java[tags=links]
----
====
@@ -56,18 +52,9 @@ This allows clients to turn parameterized templates into URIs without having to
.Using links with templated URIs
====
[source, java]
[source, java, indent=0]
----
Link link = new Link("/{segment}/something{?parameter}");
assertThat(link.isTemplated()).isTrue(); <1>
assertThat(link.getVariableNames()).containsAll("segment", "parameter"); <2>
Map<String, Object> values = new HashMap<>();
values.put("segment", "path");
values.put("parameter", 42);
assertThat(link.expand(values).getHref()) <3>
.isEqualTo("/path/something?parameter=42");
include::{code-dir}/FundamentalsTest.java[tags=templatedLinks]
----
<1> The `Link` instance indicates that is templated, i.e. it contains a URI template.
<2> It exposes the parameters contained in the template.
@@ -226,7 +213,7 @@ The following code shows how to take a *self* link and associate two more afford
====
[source,java,indent=0]
----
include::{code-dir}/src/docs/java/org/springframework/hateoas/EmployeeController.java[tag=get]
include::{code-dir}/EmployeeController.java[tag=get]
----
<1> Create the *self* link.
<2> Associate the `updateEmployee` method with the `self` link.
@@ -241,7 +228,7 @@ Imagine that the related methods *afforded* above looking like this:
====
[source,java,indent=0]
----
include::{code-dir}/src/docs/java/org/springframework/hateoas/EmployeeController.java[tag=put]
include::{code-dir}/EmployeeController.java[tag=put]
----
====
@@ -249,7 +236,7 @@ include::{code-dir}/src/docs/java/org/springframework/hateoas/EmployeeController
====
[source,java,indent=0]
----
include::{code-dir}/src/docs/java/org/springframework/hateoas/EmployeeController.java[tag=patch]
include::{code-dir}/EmployeeController.java[tag=patch]
----
====