From 8ddca10ed3ffb591875ad4f18daea97483bb992e Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Sun, 3 Mar 2019 13:19:33 +0100 Subject: [PATCH] #837 - Moved sample code for fundamentals into test cases. --- pom.xml | 4 +- .../hateoas/FundamentalsTest.java | 60 +++++++++++++++++++ src/main/asciidoc/fundamentals.adoc | 31 +++------- 3 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 src/docs/java/org/springframework/hateoas/FundamentalsTest.java diff --git a/pom.xml b/pom.xml index 724a3799..2270ea26 100644 --- a/pom.xml +++ b/pom.xml @@ -317,7 +317,7 @@ ${project.build.directory}/site/reference/html false - ${basedir} + ${basedir}/src/docs/java/org/springframework/hateoas shared true font @@ -343,7 +343,7 @@ pdf coderay - ${basedir} + ${basedir}/src/docs/java/org/springframework/hateoas diff --git a/src/docs/java/org/springframework/hateoas/FundamentalsTest.java b/src/docs/java/org/springframework/hateoas/FundamentalsTest.java new file mode 100644 index 00000000..edd965fe --- /dev/null +++ b/src/docs/java/org/springframework/hateoas/FundamentalsTest.java @@ -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 values = new HashMap<>(); + values.put("segment", "path"); + values.put("parameter", 42); + + assertThat(link.expand(values).getHref()) // <3> + .isEqualTo("/path/something?parameter=42"); + // end::templatedLinks[] + } +} diff --git a/src/main/asciidoc/fundamentals.adoc b/src/main/asciidoc/fundamentals.adoc index b6566ae8..1fabb9fd 100644 --- a/src/main/asciidoc/fundamentals.adoc +++ b/src/main/asciidoc/fundamentals.adoc @@ -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 <>. + .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 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] ---- ====