diff --git a/build.gradle b/build.gradle index ee1baef9..d2fb8d36 100644 --- a/build.gradle +++ b/build.gradle @@ -11,9 +11,33 @@ buildscript { allprojects { group = 'org.springframework.restdocs' + repositories { + jcenter() + } } apply plugin: 'samples' +apply plugin: 'sonar-runner' + +sonarRunner { + sonarProperties { + property 'sonar.jacoco.reportPath', "${buildDir.name}/jacoco.exec" + property 'sonar.java.coveragePlugin', 'jacoco' + property 'sonar.links.ci', 'https://build.spring.io/browse/SRD' + property 'sonar.links.homepage', 'https://github.com/spring-projects/spring-restdocs' + property 'sonar.links.issue', 'https://github.com/spring-projects/spring-restdocs' + property 'sonar.links.scm', 'https://github.com/spring-projects/spring-restdocs' + } +} + +ext { + springVersion = '4.1.7.RELEASE' + javadocLinks = [ + 'http://docs.oracle.com/javase/8/docs/api/', + "http://docs.spring.io/spring-framework/docs/$springVersion/javadoc-api/", + 'https://docs.jboss.org/hibernate/stable/beanvalidation/api/' + ] as String[] +} subprojects { apply plugin: 'io.spring.dependency-management' @@ -22,10 +46,14 @@ subprojects { apply plugin: 'propdeps' apply plugin: 'propdeps-eclipse' apply plugin: 'propdeps-maven' + apply plugin: 'maven' + + sourceCompatibility = 1.7 + targetCompatibility = 1.7 dependencyManagement { imports { - mavenBom 'org.springframework:spring-framework-bom:4.1.7.RELEASE' + mavenBom "org.springframework:spring-framework-bom:$springVersion" } dependencies { dependency 'com.fasterxml.jackson.core:jackson-databind:2.4.6' @@ -41,10 +69,53 @@ subprojects { dependency 'org.jacoco:org.jacoco.agent:0.7.2.201409121644' } } + + configurations { + jacoco + } + + dependencies { + jacoco 'org.jacoco:org.jacoco.agent::runtime' + } + + test { + testLogging { + exceptionFormat "full" + } + } + + javadoc { + description = "Generates project-level javadoc for use in -javadoc jar" + options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED + options.author = true + options.header = "Spring REST Docs $version" + options.docTitle = "${options.header} API" + options.links = javadocLinks + options.addStringOption '-quiet' + } + + eclipseJdt.onlyIf { false } + cleanEclipseJdt.onlyIf { false } + + task sourcesJar(type: Jar) { + classifier = 'sources' + from project.sourceSets.main.allSource + } + + task javadocJar(type: Jar) { + classifier = "javadoc" + from javadoc + } + + artifacts { + archives sourcesJar + archives javadocJar + } } samples { - dependOn 'spring-restdocs:install' + dependOn 'spring-restdocs-core:install' + dependOn 'spring-restdocs-mockmvc:install' restNotesSpringHateoas { workingDir 'samples/rest-notes-spring-hateoas' @@ -55,18 +126,44 @@ samples { } } -task docsZip(type: Zip, dependsOn: [':docs:asciidoctor', ':spring-restdocs:javadoc']) { +task api (type: Javadoc) { + group = "Documentation" + description = "Generates aggregated Javadoc API documentation." + dependsOn { + subprojects.collect { + it.tasks.getByName("jar") + } + } + options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED + options.author = true + options.header = "Spring REST Docs $version" + options.splitIndex = true + options.links = javadocLinks + options.addStringOption '-quiet' + + source subprojects.collect { project -> + project.sourceSets.main.allJava + } + + destinationDir = new File(buildDir, "api") + + doFirst { + classpath = files(subprojects.collect { it.sourceSets.main.compileClasspath }) + } +} + +task docsZip(type: Zip, dependsOn: [':docs:asciidoctor', ':api']) { group = 'Distribution' baseName = 'spring-restdocs' classifier = 'docs' - description = 'Builds -${classifier} archive containing API and reference documentation' + description = "Builds -${classifier} archive containing API and reference documentation" destinationDir = file("${project.buildDir}/distributions") - from (project.tasks.findByPath(':docs:asciidoctor')) { + from(project.tasks.findByPath(':docs:asciidoctor')) { into 'reference' } - from (project.tasks.findByPath(':spring-restdocs:javadoc')) { + from(api) { into 'api' } } diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy index 7bf9293f..7551ecf2 100644 --- a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy @@ -37,14 +37,19 @@ public class SampleBuildConfigurer { } Task createTask(Project project, Object... dependencies) { - Task mavenBuild = mavenBuild(project, dependencies) - Task gradleBuild = gradleBuild(project, dependencies) Task verifyIncludes = verifyIncludes(project) - verifyIncludes.dependsOn mavenBuild, gradleBuild + if (new File(this.workingDir, 'build.gradle').isFile()) { + Task gradleBuild = gradleBuild(project, dependencies) + verifyIncludes.dependsOn gradleBuild + } + if (new File(this.workingDir, 'pom.xml').isFile()) { + Task mavenBuild = mavenBuild(project, dependencies) + verifyIncludes.dependsOn(mavenBuild) + } Task sampleBuild = project.tasks.create name sampleBuild.description = "Builds the ${name} sample" sampleBuild.group = "Build" - sampleBuild.dependsOn mavenBuild, gradleBuild, verifyIncludes + sampleBuild.dependsOn verifyIncludes return sampleBuild } diff --git a/docs/build.gradle b/docs/build.gradle index b3184452..5241018c 100644 --- a/docs/build.gradle +++ b/docs/build.gradle @@ -3,12 +3,8 @@ plugins { } dependencies { - compile 'org.springframework:spring-webmvc' - - testCompile project(':spring-restdocs') + testCompile project(':spring-restdocs-mockmvc') testCompile 'javax.validation:validation-api' - testCompile 'junit:junit' - testCompile 'org.springframework:spring-test' } tasks.findByPath("artifactoryPublish")?.enabled = false diff --git a/docs/src/docs/asciidoc/configuration.adoc b/docs/src/docs/asciidoc/configuration.adoc index e7a0a375..acd0fd2f 100644 --- a/docs/src/docs/asciidoc/configuration.adoc +++ b/docs/src/docs/asciidoc/configuration.adoc @@ -64,24 +64,4 @@ by default: [source,java,indent=0] ---- include::{examples-dir}/com/example/CustomDefaultSnippetsConfiguration.java[tags=custom-default-snippets] ----- - - - -[[configuration-output-directory]] -=== Snippet output directory - -As described in <> the snippet output directory is -configured in your `pom.xml` or `build.gradle` file. This configuration applies to builds -on the command line, but it may not apply when running your tests in your IDE. In the -absence of the property, Spring REST Docs will write the generated snippets to standard -out. - -If you'd prefer that your IDE writes the snippets to disk you can use a file in -`src/test/resources` named `documentation.properties` to specify the output directory that -should be used: - -[source,properties] ----- -org.springframework.restdocs.outputDir: target/generated-snippets ---- \ No newline at end of file diff --git a/docs/src/docs/asciidoc/getting-started.adoc b/docs/src/docs/asciidoc/getting-started.adoc index f07f9da6..c013ce47 100644 --- a/docs/src/docs/asciidoc/getting-started.adoc +++ b/docs/src/docs/asciidoc/getting-started.adoc @@ -36,10 +36,9 @@ The first step in using Spring REST Docs is to configure your project's build. [[getting-started-build-configuration-gradle]] ==== Gradle build configuration -Both {samples}[sample applications] contain `build.gradle` files that you may wish to -use as a reference. The key parts of the configuration are described below. - - +The {samples/rest-notes-spring-hateoas}[Spring HATEOAS sample] contains a `build.gradle` +file that you may wish to use as a reference. The key parts of the configuration are +described below. [source,groovy,indent=0,subs="verbatim,attributes"] ---- @@ -48,7 +47,7 @@ use as a reference. The key parts of the configuration are described below. } dependencies { <2> - testCompile 'org.springframework.restdocs:spring-restdocs:{project-version}' + testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:{project-version}' } ext { <3> @@ -56,24 +55,25 @@ use as a reference. The key parts of the configuration are described below. } test { <4> - systemProperty 'org.springframework.restdocs.outputDir', snippetsDir outputs.dir snippetsDir } asciidoctor { <5> - attributes 'snippets': snippetsDir - inputs.dir snippetsDir - dependsOn test + attributes 'snippets': snippetsDir <6> + inputs.dir snippetsDir <7> + dependsOn test <8> } ---- <1> Apply the Asciidoctor plugin. <2> Add a dependency on spring-restdocs in the `testCompile` configuration. <3> Configure a property to define the output location for generated snippets. -<4> Configure the `test` task with the `org.springframework.restdocs.outputDir` system -property. This property controls the location into which Spring REST Docs will write the -snippets that it generates. -<5> Configure the `asciidoctor` task and define an attribute named `snippets`. You can -then use this attribute when including the generated snippets in your documentation. +<4> Configure the `test` task to add the snippets directory as an output. +<5> Configure the `asciidoctor` task +<6> Define an attribute named `snippets` that can be used when including the generated + snippets in your documentation. +<7> Configure the snippets directory as an input. +<8> Make the task depend on the test task so that the tests are run before the + documentation is created. [[getting-started-build-configuration-gradle-packaging-the-documentation]] @@ -100,16 +100,15 @@ directory: [[getting-started-build-configuration-maven]] ==== Maven build configuration -Both {samples}[sample applications] contain `pom.xml` files that you may wish to -use as a reference. The key parts of the configuration are described below. - - +The {samples/rest-notes-spring-data-rest}[Spring Data REST sample] contains a `pom.xml` +file that you may wish to use as a reference. The key parts of the configuration are +described below. [source,xml,indent=0,subs="verbatim,attributes"] ---- <1> org.springframework.restdocs - spring-restdocs + spring-restdocs-mockmvc {project-version} test @@ -127,11 +126,6 @@ use as a reference. The key parts of the configuration are described below. **/*Documentation.java - - - ${snippetsDirectory} - - <4> @@ -159,14 +153,12 @@ use as a reference. The key parts of the configuration are described below. ---- -<1> Add a dependency on `spring-restdocs` in the `test` scope. +<1> Add a dependency on `spring-restdocs-mockmvc` in the `test` scope. <2> Configure a property to define the output location for generated snippets. -<3> Configure the SureFire plugin with the `org.springframework.restdocs.outputDir` system -property. This property controls the location into which Spring REST Docs will write the -snippets that it generates. The plugin is also configured to include files whose names end -with `Documentation.java`. -<4> Configure the Asciidoctor plugin and define an attribute named `snippets`. You can -then use this attribute when including the generated snippets in your documentation. +<3> Add the SureFire plugin and configure it to include files whose names end with + `Documentation.java`. +<4> Add the Asciidoctor plugin and configure it to define an attribute named `snippets` + that can be used when including the generated snippets in your documentation. <5> [[getting-started-build-configuration-maven-plugin-phase]] If you want to <> in your project's jar you should use the `prepare-package` phase. @@ -233,7 +225,8 @@ documentation snippets for the result's request and response. [[getting-started-documentation-snippets-setup]] ==== Setting up Spring MVC test -The first step in generating documentation snippets is to provide an `@Before` method +The first step in generating documentation snippets is to declare a `public` +`RestDocumentation` that's annotated as a JUnit `@Rule` and to provide an `@Before` method that creates a `MockMvc` instance: [source,java,indent=0] @@ -241,6 +234,10 @@ that creates a `MockMvc` instance: include::{examples-dir}/com/example/ExampleApplicationTests.java[tags=mock-mvc-setup] ---- +The `RestDocumentation` rule is configured with the output directory into which +generated snippets should be written. This output directory should match the snippets +directory that you have configured in your `build.gradle` or `pom.xml` file. + The `MockMvc` instance is configured using a `RestDocumentationConfigurer`. An instance of this class can be obtained from the static `documentationConfiguration()` method on `org.springframework.restdocs.RestDocumentation`. `RestDocumentationConfigurer` applies @@ -259,13 +256,14 @@ service and document the request and response. ---- include::{examples-dir}/com/example/InvokeService.java[tags=invoke-service] ---- -<1> Invoke the root (`/`) of the service an indicate that an `application/json` response +<1> Invoke the root (`/`) of the service and indicate that an `application/json` response is required. -<2> Assert that the service is produced the expected response. +<2> Assert that the service produced the expected response. <3> Document the call to the service, writing the snippets into a directory named `index` that will be located beneath the configured output directory. The snippets are written by a `RestDocumentationResultHandler`. An instance of this class can be obtained from the -static `document` method on `org.springframework.restdocs.RestDocumentation`. +static `document` method on +`org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`. By default, three snippets a written: diff --git a/docs/src/test/java/com/example/AlwaysDo.java b/docs/src/test/java/com/example/AlwaysDo.java index d4575b9a..efc64f6c 100644 --- a/docs/src/test/java/com/example/AlwaysDo.java +++ b/docs/src/test/java/com/example/AlwaysDo.java @@ -16,25 +16,32 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import org.junit.Before; +import org.junit.Rule; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; public class AlwaysDo { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build"); private MockMvc mockMvc; private WebApplicationContext context; + + // tag::always-do[] @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()) + .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document("{method-name}/{step}/")) .build(); } diff --git a/docs/src/test/java/com/example/Constraints.java b/docs/src/test/java/com/example/Constraints.java index 6370b30c..5bcea859 100644 --- a/docs/src/test/java/com/example/Constraints.java +++ b/docs/src/test/java/com/example/Constraints.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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 com.example; import java.util.List; @@ -15,17 +31,17 @@ public class Constraints { ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserInput.class); // <1> List descriptions = userConstraints.descriptionsForProperty("name"); // <2> } - + static class UserInput { - + @NotNull @Size(min = 1) String name; - + @NotNull @Size(min = 8) String password; } // end::constraints[] - + } diff --git a/docs/src/test/java/com/example/CustomDefaultSnippetsConfiguration.java b/docs/src/test/java/com/example/CustomDefaultSnippetsConfiguration.java index 0a74af66..a964c964 100644 --- a/docs/src/test/java/com/example/CustomDefaultSnippetsConfiguration.java +++ b/docs/src/test/java/com/example/CustomDefaultSnippetsConfiguration.java @@ -1,16 +1,37 @@ +/* + * Copyright 2014-2015 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 com.example; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; import static org.springframework.restdocs.curl.CurlDocumentation.curlRequest; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import org.junit.Before; +import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; public class CustomDefaultSnippetsConfiguration { + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build"); + @Autowired private WebApplicationContext context; @@ -20,7 +41,7 @@ public class CustomDefaultSnippetsConfiguration { public void setUp() { // tag::custom-default-snippets[] this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration().snippets() + .apply(documentationConfiguration(this.restDocumentation).snippets() .withDefaults(curlRequest())) .build(); // end::custom-default-snippets[] diff --git a/docs/src/test/java/com/example/CustomEncoding.java b/docs/src/test/java/com/example/CustomEncoding.java index 1c72fcff..4490e8a4 100644 --- a/docs/src/test/java/com/example/CustomEncoding.java +++ b/docs/src/test/java/com/example/CustomEncoding.java @@ -16,15 +16,20 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import org.junit.Before; +import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; public class CustomEncoding { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build"); @Autowired private WebApplicationContext context; @@ -35,7 +40,7 @@ public class CustomEncoding { public void setUp() { // tag::custom-encoding[] this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration().snippets() + .apply(documentationConfiguration(this.restDocumentation).snippets() .withEncoding("ISO-8859-1")) .build(); // end::custom-encoding[] diff --git a/docs/src/test/java/com/example/CustomUriConfiguration.java b/docs/src/test/java/com/example/CustomUriConfiguration.java index 038d5354..b70d4e08 100644 --- a/docs/src/test/java/com/example/CustomUriConfiguration.java +++ b/docs/src/test/java/com/example/CustomUriConfiguration.java @@ -16,15 +16,20 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import org.junit.Before; +import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; public class CustomUriConfiguration { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build"); @Autowired private WebApplicationContext context; @@ -35,7 +40,7 @@ public class CustomUriConfiguration { public void setUp() { // tag::custom-uri-configuration[] this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration().uris() + .apply(documentationConfiguration(this.restDocumentation).uris() .withScheme("https") .withHost("example.com") .withPort(443)) diff --git a/docs/src/test/java/com/example/ExampleApplicationTests.java b/docs/src/test/java/com/example/ExampleApplicationTests.java index e6c97e44..cc6c6406 100644 --- a/docs/src/test/java/com/example/ExampleApplicationTests.java +++ b/docs/src/test/java/com/example/ExampleApplicationTests.java @@ -17,16 +17,21 @@ package com.example; import org.junit.Before; +import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; public class ExampleApplicationTests { - + // tag::mock-mvc-setup[] + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets"); + @Autowired private WebApplicationContext context; @@ -35,7 +40,7 @@ public class ExampleApplicationTests { @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()) + .apply(documentationConfiguration(this.restDocumentation)) .build(); } // end::mock-mvc-setup[] diff --git a/docs/src/test/java/com/example/Hypermedia.java b/docs/src/test/java/com/example/Hypermedia.java index 103529ea..3cf6c1bc 100644 --- a/docs/src/test/java/com/example/Hypermedia.java +++ b/docs/src/test/java/com/example/Hypermedia.java @@ -16,12 +16,12 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.halLinks; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; diff --git a/docs/src/test/java/com/example/InvokeService.java b/docs/src/test/java/com/example/InvokeService.java index 32fdaf65..097899f2 100644 --- a/docs/src/test/java/com/example/InvokeService.java +++ b/docs/src/test/java/com/example/InvokeService.java @@ -16,8 +16,8 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.springframework.http.MediaType; diff --git a/docs/src/test/java/com/example/PathParameters.java b/docs/src/test/java/com/example/PathParameters.java index 1754947e..c7810598 100644 --- a/docs/src/test/java/com/example/PathParameters.java +++ b/docs/src/test/java/com/example/PathParameters.java @@ -16,8 +16,8 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; diff --git a/docs/src/test/java/com/example/Payload.java b/docs/src/test/java/com/example/Payload.java index 132568a2..5ee1a890 100644 --- a/docs/src/test/java/com/example/Payload.java +++ b/docs/src/test/java/com/example/Payload.java @@ -16,9 +16,9 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; diff --git a/docs/src/test/java/com/example/Preprocessing.java b/docs/src/test/java/com/example/Preprocessing.java index cf2d9ad2..dbb83713 100644 --- a/docs/src/test/java/com/example/Preprocessing.java +++ b/docs/src/test/java/com/example/Preprocessing.java @@ -16,13 +16,13 @@ package com.example; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders; import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; -import static org.springframework.restdocs.RestDocumentation.document; import org.springframework.test.web.servlet.MockMvc; diff --git a/docs/src/test/java/com/example/RequestParameters.java b/docs/src/test/java/com/example/RequestParameters.java index 9155a5b9..5a314958 100644 --- a/docs/src/test/java/com/example/RequestParameters.java +++ b/docs/src/test/java/com/example/RequestParameters.java @@ -16,9 +16,9 @@ package com.example; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; import static org.springframework.restdocs.request.RequestDocumentation.requestParameters; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; diff --git a/samples/rest-notes-spring-data-rest/build.gradle b/samples/rest-notes-spring-data-rest/build.gradle deleted file mode 100644 index b30c212b..00000000 --- a/samples/rest-notes-spring-data-rest/build.gradle +++ /dev/null @@ -1,68 +0,0 @@ -buildscript { - repositories { - mavenCentral() - } - dependencies { - classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE' - } -} - -plugins { - id "org.asciidoctor.convert" version "1.5.2" -} - -apply plugin: 'java' -apply plugin: 'spring-boot' -apply plugin: 'eclipse' - -repositories { - mavenLocal() - maven { url 'https://repo.spring.io/snapshot' } - mavenCentral() -} - -group = 'com.example' - -sourceCompatibility = 1.7 -targetCompatibility = 1.7 - -dependencies { - compile 'org.springframework.boot:spring-boot-starter-data-rest' - compile 'org.springframework.boot:spring-boot-starter-data-jpa' - - runtime 'com.h2database:h2' - - testCompile 'com.jayway.jsonpath:json-path' - testCompile 'org.springframework.boot:spring-boot-starter-test' - testCompile 'org.springframework.restdocs:spring-restdocs:1.0.0.BUILD-SNAPSHOT' -} - -ext { - snippetsDir = file('build/generated-snippets') -} - -test { - systemProperty 'org.springframework.restdocs.outputDir', snippetsDir - outputs.dir snippetsDir -} - -asciidoctor { - sourceDir 'src/main/asciidoc' // Align with Maven's default location - attributes 'snippets': snippetsDir - inputs.dir snippetsDir - dependsOn test -} - -jar { - dependsOn asciidoctor - from ("${asciidoctor.outputDir}/html5") { - into 'static/docs' - } -} - -task wrapper(type: Wrapper) { - gradleVersion = '2.3' -} - -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } \ No newline at end of file diff --git a/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.jar b/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 3d0dee6e..00000000 Binary files a/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.properties b/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 72d2eb27..00000000 --- a/samples/rest-notes-spring-data-rest/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Thu Apr 16 12:33:42 BST 2015 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-bin.zip diff --git a/samples/rest-notes-spring-data-rest/gradlew b/samples/rest-notes-spring-data-rest/gradlew deleted file mode 100755 index 91a7e269..00000000 --- a/samples/rest-notes-spring-data-rest/gradlew +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env bash - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn ( ) { - echo "$*" -} - -die ( ) { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; -esac - -# For Cygwin, ensure paths are in UNIX format before anything is touched. -if $cygwin ; then - [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` -fi - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >&- -APP_HOME="`pwd -P`" -cd "$SAVED" >&- - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") -} -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" - -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/samples/rest-notes-spring-data-rest/gradlew.bat b/samples/rest-notes-spring-data-rest/gradlew.bat deleted file mode 100644 index 8a0b282a..00000000 --- a/samples/rest-notes-spring-data-rest/gradlew.bat +++ /dev/null @@ -1,90 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/samples/rest-notes-spring-data-rest/pom.xml b/samples/rest-notes-spring-data-rest/pom.xml index b553763a..7e668af4 100644 --- a/samples/rest-notes-spring-data-rest/pom.xml +++ b/samples/rest-notes-spring-data-rest/pom.xml @@ -18,7 +18,6 @@ UTF-8 1.7 - ${project.build.directory}/generated-snippets @@ -49,7 +48,7 @@ org.springframework.restdocs - spring-restdocs + spring-restdocs-mockmvc 1.0.0.BUILD-SNAPSHOT test @@ -68,9 +67,6 @@ **/*Documentation.java - - ${snippetsDirectory} - @@ -88,7 +84,7 @@ html book - ${snippetsDirectory} + ${project.build.directory}/generated-snippets @@ -96,7 +92,6 @@ maven-resources-plugin - 2.7 copy-resources diff --git a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java index cfa00bde..51581811 100644 --- a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java +++ b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java @@ -18,16 +18,16 @@ package com.example.notes; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; -import static org.springframework.restdocs.RestDocumentation.document; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; -import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.patch; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -39,11 +39,13 @@ import java.util.Map; import javax.servlet.RequestDispatcher; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentation; import org.springframework.restdocs.payload.JsonFieldType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -57,6 +59,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; @SpringApplicationConfiguration(classes = RestNotesSpringDataRest.class) @WebAppConfiguration public class ApiDocumentation { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets"); @Autowired private NoteRepository noteRepository; @@ -75,18 +80,7 @@ public class ApiDocumentation { @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()).build(); - this.mockMvc = MockMvcBuilders - .webAppContextSetup(this.context) - .apply(documentationConfiguration() - .uris() - .withScheme("https") - .withHost("localhost") - .withPort(8443) - .and().snippets() - .withEncoding("ISO-8859-1")) - .build(); - + .apply(documentationConfiguration(this.restDocumentation)).build(); } @Test diff --git a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java index a6afc59f..3770023b 100644 --- a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java +++ b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java @@ -19,11 +19,11 @@ package com.example.notes; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.patch; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -34,11 +34,13 @@ import java.util.HashMap; import java.util.Map; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -54,6 +56,9 @@ import com.jayway.jsonpath.JsonPath; @SpringApplicationConfiguration(classes = RestNotesSpringDataRest.class) @WebAppConfiguration public class GettingStartedDocumentation { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets"); @Autowired private ObjectMapper objectMapper; @@ -66,7 +71,7 @@ public class GettingStartedDocumentation { @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()) + .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document("{method-name}/{step}/")) .build(); } diff --git a/samples/rest-notes-spring-hateoas/build.gradle b/samples/rest-notes-spring-hateoas/build.gradle index 5a2e164d..a8041b90 100644 --- a/samples/rest-notes-spring-hateoas/build.gradle +++ b/samples/rest-notes-spring-hateoas/build.gradle @@ -35,7 +35,7 @@ dependencies { testCompile 'com.jayway.jsonpath:json-path' testCompile 'org.springframework.boot:spring-boot-starter-test' - testCompile 'org.springframework.restdocs:spring-restdocs:1.0.0.BUILD-SNAPSHOT' + testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:1.0.0.BUILD-SNAPSHOT' } ext { @@ -43,7 +43,6 @@ ext { } test { - systemProperty 'org.springframework.restdocs.outputDir', snippetsDir outputs.dir snippetsDir } @@ -61,9 +60,5 @@ jar { } } -task wrapper(type: Wrapper) { - gradleVersion = '2.3' -} - eclipseJdt.onlyIf { false } cleanEclipseJdt.onlyIf { false } \ No newline at end of file diff --git a/samples/rest-notes-spring-hateoas/pom.xml b/samples/rest-notes-spring-hateoas/pom.xml deleted file mode 100644 index a9a1a339..00000000 --- a/samples/rest-notes-spring-hateoas/pom.xml +++ /dev/null @@ -1,137 +0,0 @@ - - - 4.0.0 - - com.example - rest-notes-spring-hateoas - 0.0.1-SNAPSHOT - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.2.5.RELEASE - - - - - UTF-8 - 1.7 - ${project.build.directory}/generated-snippets - - - - - org.springframework.boot - spring-boot-starter-hateoas - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - runtime - - - org.atteo - evo-inflector - 1.2 - runtime - - - - org.springframework.boot - spring-boot-starter-test - test - - - com.jayway.jsonpath - json-path - test - - - org.springframework.restdocs - spring-restdocs - 1.0.0.BUILD-SNAPSHOT - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*Documentation.java - - - ${snippetsDirectory} - - - - - org.asciidoctor - asciidoctor-maven-plugin - 1.5.2 - - - generate-docs - prepare-package - - process-asciidoc - - - html - book - - ${snippetsDirectory} - - - - - - - maven-resources-plugin - 2.7 - - - copy-resources - prepare-package - - copy-resources - - - ${project.build.outputDirectory}/static/docs - - - ${project.build.directory}/generated-docs - - - - - - - - - - - - spring-snapshots - Spring snapshots - https://repo.spring.io/snapshot - - true - - - - - \ No newline at end of file diff --git a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java index 06e22615..8a0978df 100644 --- a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java +++ b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java @@ -18,11 +18,11 @@ package com.example.notes; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.patch; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; @@ -40,11 +40,13 @@ import java.util.Map; import javax.servlet.RequestDispatcher; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentation; import org.springframework.restdocs.constraints.ConstraintDescriptions; import org.springframework.restdocs.payload.FieldDescriptor; import org.springframework.restdocs.payload.JsonFieldType; @@ -61,6 +63,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; @SpringApplicationConfiguration(classes = RestNotesSpringHateoas.class) @WebAppConfiguration public class ApiDocumentation { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets"); @Autowired private NoteRepository noteRepository; @@ -79,7 +84,7 @@ public class ApiDocumentation { @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()).build(); + .apply(documentationConfiguration(this.restDocumentation)).build(); } @Test diff --git a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java index 79b78051..b1365729 100644 --- a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java +++ b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java @@ -19,11 +19,11 @@ package com.example.notes; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.springframework.restdocs.RestDocumentation.document; -import static org.springframework.restdocs.RestDocumentation.documentationConfiguration; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.patch; -import static org.springframework.restdocs.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -34,11 +34,13 @@ import java.util.HashMap; import java.util.Map; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentation; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -54,6 +56,9 @@ import com.jayway.jsonpath.JsonPath; @SpringApplicationConfiguration(classes = RestNotesSpringHateoas.class) @WebAppConfiguration public class GettingStartedDocumentation { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets"); @Autowired private ObjectMapper objectMapper; @@ -66,7 +71,7 @@ public class GettingStartedDocumentation { @Before public void setUp() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) - .apply(documentationConfiguration()) + .apply(documentationConfiguration(this.restDocumentation)) .alwaysDo(document("{method-name}/{step}/")) .build(); } diff --git a/settings.gradle b/settings.gradle index ad08f6b3..7552e239 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ -rootProject.name = 'spring-restdocs-build' +rootProject.name = 'spring-restdocs' include 'docs' -include 'spring-restdocs' \ No newline at end of file +include 'spring-restdocs-core' +include 'spring-restdocs-mockmvc' \ No newline at end of file diff --git a/spring-restdocs/.settings/org.eclipse.jdt.core.prefs b/spring-restdocs-core/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from spring-restdocs/.settings/org.eclipse.jdt.core.prefs rename to spring-restdocs-core/.settings/org.eclipse.jdt.core.prefs diff --git a/spring-restdocs/.settings/org.eclipse.jdt.ui.prefs b/spring-restdocs-core/.settings/org.eclipse.jdt.ui.prefs similarity index 100% rename from spring-restdocs/.settings/org.eclipse.jdt.ui.prefs rename to spring-restdocs-core/.settings/org.eclipse.jdt.ui.prefs diff --git a/spring-restdocs-core/build.gradle b/spring-restdocs-core/build.gradle new file mode 100644 index 00000000..5f5be34f --- /dev/null +++ b/spring-restdocs-core/build.gradle @@ -0,0 +1,59 @@ +configurations { + jarjar + jmustache + testArtifacts.extendsFrom testRuntime +} + +task jmustacheRepackJar(type: Jar) { repackJar -> + repackJar.baseName = "restdocs-jmustache-repack" + repackJar.version = dependencyManagement.managedVersions['com.samskivert:jmustache'] + + doLast() { + project.ant { + taskdef name: "jarjar", classname: "com.tonicsystems.jarjar.JarJarTask", + classpath: configurations.jarjar.asPath + jarjar(destfile: repackJar.archivePath) { + configurations.jmustache.each { originalJar -> + zipfileset(src: originalJar, includes: '**/*.class') + } + rule(pattern: 'com.samskivert.**', result: 'org.springframework.restdocs.@1') + } + } + } +} + +dependencies { + compile 'com.fasterxml.jackson.core:jackson-databind' + compile 'junit:junit' + compile 'org.springframework:spring-webmvc' + compile 'javax.servlet:javax.servlet-api' + compile files(jmustacheRepackJar) + jarjar 'com.googlecode.jarjar:jarjar:1.3' + jmustache 'com.samskivert:jmustache@jar' + optional 'javax.validation:validation-api' + testCompile 'org.mockito:mockito-core' + testCompile 'org.hamcrest:hamcrest-core' + testCompile 'org.hamcrest:hamcrest-library' + testCompile 'org.hibernate:hibernate-validator' + testRuntime 'org.glassfish:javax.el:3.0.0' +} + +jar { + dependsOn jmustacheRepackJar + from(zipTree(jmustacheRepackJar.archivePath)) { + include "org/springframework/restdocs/**" + } +} + +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output +} + +artifacts { + testArtifacts testJar +} + +test { + jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=org.springframework.restdocs.*,excludes=org.springframework.restdocs.mustache.*" +} \ No newline at end of file diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentation.java new file mode 100644 index 00000000..fc72501e --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentation.java @@ -0,0 +1,81 @@ +/* + * Copyright 2014-2015 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.restdocs; + +import java.io.File; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A JUnit {@link TestRule} used to bootstrap the generation of REST documentation from + * JUnit tests. + * + * @author Andy Wilkinson + * + */ +public class RestDocumentation implements TestRule { + + private final String outputDirectory; + + private RestDocumentationContext context; + + /** + * Creates a new {@code RestDocumentation} instance that will generate snippets to the + * given {@code outputDirectory} + * + * @param outputDirectory the output directory + */ + public RestDocumentation(String outputDirectory) { + this.outputDirectory = outputDirectory; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + Class testClass = description.getTestClass(); + String methodName = description.getMethodName(); + RestDocumentation.this.context = new RestDocumentationContext(testClass, + methodName, new File(RestDocumentation.this.outputDirectory)); + try { + base.evaluate(); + } + finally { + RestDocumentation.this.context = null; + } + } + + }; + + } + + /** + * Notification that a RESTful operation that should be documented is about to be + * performed. Returns a {@link RestDocumentationContext} for the operation. + * + * @return the context for the operation + */ + public RestDocumentationContext beforeOperation() { + this.context.getAndIncrementStepCount(); + return this.context; + } + +} diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationContext.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationContext.java new file mode 100644 index 00000000..cad0c71e --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationContext.java @@ -0,0 +1,99 @@ +/* + * Copyright 2014-2015 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.restdocs; + +import java.io.File; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * {@code RestDocumentationContext} encapsulates the context in which the documentation of + * a RESTful API is being performed. + * + * @author Andy Wilkinson + */ +public final class RestDocumentationContext { + + private final AtomicInteger stepCount = new AtomicInteger(0); + + private final Class testClass; + + private final String testMethodName; + + private final File outputDirectory; + + /** + * Creates a new {@code RestDocumentationContext} for a test on the given + * {@code testClass} with given {@code testMethodName} that will generate + * documentation to the given {@code outputDirectory}. + * + * @param testClass the class whose test is being executed + * @param testMethodName the name of the test method that is being executed + * @param outputDirectory the directory to which documentation should be written. + */ + public RestDocumentationContext(Class testClass, String testMethodName, + File outputDirectory) { + this.testClass = testClass; + this.testMethodName = testMethodName; + this.outputDirectory = outputDirectory; + } + + /** + * Returns the class whose tests are currently executing + * + * @return The test class + */ + public Class getTestClass() { + return this.testClass; + } + + /** + * Returns the name of the test method that is currently executing + * + * @return The name of the test method + */ + public String getTestMethodName() { + return this.testMethodName; + } + + /** + * Returns the current step count and then increments it + * + * @return The step count prior to it being incremented + */ + int getAndIncrementStepCount() { + return this.stepCount.getAndIncrement(); + } + + /** + * Returns the current step count + * + * @return The current step count + */ + public int getStepCount() { + return this.stepCount.get(); + } + + /** + * Returns the output directory to which generated snippets should be written. + * + * @return the output directory + */ + public File getOutputDirectory() { + return this.outputDirectory; + } + +} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/Constraint.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/Constraint.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/Constraint.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/Constraint.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptionResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptionResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptionResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptionResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptions.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptions.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptions.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintDescriptions.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ConstraintResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ConstraintResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ValidatorConstraintResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ValidatorConstraintResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/constraints/ValidatorConstraintResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/constraints/ValidatorConstraintResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/curl/CurlDocumentation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/curl/CurlRequestSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/curl/CurlRequestSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/curl/CurlRequestSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpDocumentation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpDocumentation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpDocumentation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/AbstractJsonLinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/AbstractJsonLinkExtractor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/AbstractJsonLinkExtractor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/AbstractJsonLinkExtractor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/AtomLinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/AtomLinkExtractor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/AtomLinkExtractor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/AtomLinkExtractor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java similarity index 73% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java index 1c31ad3a..ec36eb63 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.hypermedia; import java.io.IOException; @@ -12,7 +28,7 @@ import org.springframework.restdocs.operation.OperationResponse; /** * {@link LinkExtractor} that delegates to other link extractors based on the response's * content type. - * + * * @author Andy Wilkinson * */ diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/HalLinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/HalLinkExtractor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/HalLinkExtractor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/HalLinkExtractor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/HypermediaDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/HypermediaDocumentation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/HypermediaDocumentation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/HypermediaDocumentation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/Link.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/Link.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/Link.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/Link.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkDescriptor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinkDescriptor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkDescriptor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinkDescriptor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinksSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinksSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinksSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/LinksSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/Operation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/Operation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/Operation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/Operation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationRequest.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationRequest.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationRequestPart.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestPart.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationRequestPart.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestPart.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationResponse.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/OperationResponse.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/Parameters.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/Parameters.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/Parameters.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/Parameters.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationRequestPart.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequestPart.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationRequestPart.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequestPart.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifier.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifier.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifier.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifier.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifier.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifier.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationPreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationPreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationPreprocessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationRequestPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationRequestPreprocessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationRequestPreprocessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationRequestPreprocessor.java diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java new file mode 100644 index 00000000..4763b05b --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; + +import org.springframework.restdocs.operation.OperationResponse; + +/** + * An {@code OperationRequestPreprocessor} is used to modify an {@code OperationRequest} + * prior to it being documented. + * + * @author Andy Wilkinson + */ +public interface OperationResponsePreprocessor { + + /** + * Processes and potentially modifies the given {@code response} before it is + * documented. + * + * @param response the response + * @return the modified response + */ + OperationResponse preprocess(OperationResponse response); + +} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/ContentHandler.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ContentHandler.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/ContentHandler.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ContentHandler.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldDescriptor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldDescriptor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldDescriptor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldDescriptor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldDoesNotExistException.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldDoesNotExistException.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldDoesNotExistException.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldDoesNotExistException.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldTypeRequiredException.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldTypeRequiredException.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/FieldTypeRequiredException.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldTypeRequiredException.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java similarity index 80% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java index 2fe9e897..e20b4f8d 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonContentHandler.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.payload; import java.io.IOException; @@ -11,7 +27,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; /** * A {@link ContentHandler} for JSON content - * + * * @author Andy Wilkinson */ class JsonContentHandler implements ContentHandler { diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldPath.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldPath.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldPath.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldPath.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldProcessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldProcessor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldProcessor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldProcessor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldType.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldType.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldType.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldType.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldTypeResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldTypeResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/JsonFieldTypeResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/JsonFieldTypeResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/PayloadDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/PayloadDocumentation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/PayloadDocumentation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/PayloadDocumentation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextHolder.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java similarity index 50% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextHolder.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java index cefb3fbb..10362718 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextHolder.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java @@ -14,29 +14,23 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.payload; -import org.springframework.core.NamedInheritableThreadLocal; +/** + * Thrown to indicate that a failure has occurred during payload handling + * + * @author Andy Wilkinson + * + */ +@SuppressWarnings("serial") +class PayloadHandlingException extends RuntimeException { -final class RestDocumentationContextHolder { - - private static final NamedInheritableThreadLocal currentContext = new NamedInheritableThreadLocal<>( - "REST Documentation Context"); - - private RestDocumentationContextHolder() { - - } - - static RestDocumentationContext getCurrentContext() { - return currentContext.get(); - } - - static void setCurrentContext(RestDocumentationContext context) { - currentContext.set(context); - } - - static void removeCurrentContext() { - currentContext.remove(); + /** + * Creates a new {@code PayloadHandlingException} with the given cause + * @param cause the cause of the failure + */ + PayloadHandlingException(Throwable cause) { + super(cause); } } diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/XmlContentHandler.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/XmlContentHandler.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/payload/XmlContentHandler.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/XmlContentHandler.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/RequestDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestDocumentation.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/request/RequestDocumentation.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestDocumentation.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java similarity index 60% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java index d217b002..4766a171 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/AbstractDescriptor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.snippet; import java.util.HashMap; @@ -8,7 +24,7 @@ import org.springframework.restdocs.snippet.Attributes.Attribute; /** * Base class for descriptors. Provides the ability to associate arbitrary attributes with * a descriptor. - * + * * @author Andy Wilkinson * * @param the type of the descriptor @@ -19,7 +35,7 @@ public abstract class AbstractDescriptor> { /** * Sets the descriptor's attributes - * + * * @param attributes the attributes * @return the descriptor */ @@ -33,7 +49,7 @@ public abstract class AbstractDescriptor> { /** * Returns the descriptor's attributes - * + * * @return the attributes */ protected Map getAttributes() { diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/Attributes.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/Attributes.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/Attributes.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/Attributes.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextPlaceholderResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java similarity index 81% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextPlaceholderResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java index 6a9645cd..706078d0 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContextPlaceholderResolver.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java @@ -14,11 +14,12 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.snippet; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.springframework.restdocs.RestDocumentationContext; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; /** @@ -28,14 +29,14 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; *
  • {@code step} – the {@link RestDocumentationContext#getStepCount() step current * count}. *
  • {@code methodName} - the name of the - * {@link RestDocumentationContext#getTestMethod() current test method} formatted using - * camelCase + * {@link RestDocumentationContext#getTestMethodName() current test method} formatted + * using camelCase *
  • {@code method-name} - the name of the - * {@link RestDocumentationContext#getTestMethod() current test method} formatted using - * kebab-case + * {@link RestDocumentationContext#getTestMethodName() current test method} formatted + * using kebab-case *
  • {@code method_name} - the name of the - * {@link RestDocumentationContext#getTestMethod() current test method} formatted using - * snake_case + * {@link RestDocumentationContext#getTestMethodName() current test method} formatted + * using snake_case * * * @author Andy Wilkinson @@ -62,13 +63,13 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR return Integer.toString(this.context.getStepCount()); } if ("methodName".equals(placeholderName)) { - return this.context.getTestMethod().getName(); + return this.context.getTestMethodName(); } if ("method-name".equals(placeholderName)) { - return camelCaseToDash(this.context.getTestMethod().getName()); + return camelCaseToDash(this.context.getTestMethodName()); } if ("method_name".equals(placeholderName)) { - return camelCaseToUnderscore(this.context.getTestMethod().getName()); + return camelCaseToUnderscore(this.context.getTestMethodName()); } return null; } diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/Snippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/Snippet.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/Snippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/Snippet.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/SnippetException.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/SnippetException.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/SnippetException.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/SnippetException.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java similarity index 85% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java index fd4a52a7..2ba5d366 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import org.springframework.restdocs.RestDocumentationContext; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; @@ -51,9 +52,10 @@ public class StandardWriterResolver implements WriterResolver { } @Override - public Writer resolve(String operationName, String snippetName) throws IOException { + public Writer resolve(String operationName, String snippetName, + RestDocumentationContext context) throws IOException { File outputFile = resolveFile(this.propertyPlaceholderHelper.replacePlaceholders( - operationName, this.placeholderResolver), snippetName + ".adoc"); + operationName, this.placeholderResolver), snippetName + ".adoc", context); if (outputFile != null) { createDirectoriesIfNecessary(outputFile); @@ -69,16 +71,18 @@ public class StandardWriterResolver implements WriterResolver { this.encoding = encoding; } - protected File resolveFile(String outputDirectory, String fileName) { + protected File resolveFile(String outputDirectory, String fileName, + RestDocumentationContext context) { File outputFile = new File(outputDirectory, fileName); if (!outputFile.isAbsolute()) { - outputFile = makeRelativeToConfiguredOutputDir(outputFile); + outputFile = makeRelativeToConfiguredOutputDir(outputFile, context); } return outputFile; } - private File makeRelativeToConfiguredOutputDir(File outputFile) { - File configuredOutputDir = new DocumentationProperties().getOutputDir(); + private File makeRelativeToConfiguredOutputDir(File outputFile, + RestDocumentationContext context) { + File configuredOutputDir = context.getOutputDirectory(); if (configuredOutputDir != null) { return new File(configuredOutputDir, outputFile.getPath()); } diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java similarity index 86% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java index 7fc188bc..4e0cf65d 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/TemplatedSnippet.java @@ -21,6 +21,7 @@ import java.io.Writer; import java.util.HashMap; import java.util.Map; +import org.springframework.restdocs.RestDocumentationContext; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.templates.Template; import org.springframework.restdocs.templates.TemplateEngine; @@ -46,10 +47,12 @@ public abstract class TemplatedSnippet implements Snippet { @Override public void document(Operation operation) throws IOException { + RestDocumentationContext context = (RestDocumentationContext) operation + .getAttributes().get(RestDocumentationContext.class.getName()); WriterResolver writerResolver = (WriterResolver) operation.getAttributes().get( WriterResolver.class.getName()); - try (Writer writer = writerResolver - .resolve(operation.getName(), this.snippetName)) { + try (Writer writer = writerResolver.resolve(operation.getName(), + this.snippetName, context)) { Map model = createModel(operation); model.putAll(this.attributes); TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes() diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java similarity index 84% rename from spring-restdocs/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java index 45948fc5..9adca5ca 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/WriterResolver.java @@ -19,6 +19,8 @@ package org.springframework.restdocs.snippet; import java.io.IOException; import java.io.Writer; +import org.springframework.restdocs.RestDocumentationContext; + /** * A {@code WriterResolver} is used to access the {@link Writer} that should be used to * write a snippet for an operation that is being documented. @@ -32,10 +34,12 @@ public interface WriterResolver { * operation with the given name. * @param operationName the name of the operation that is being documented * @param snippetName the name of the snippet + * @param restDocumentationContext the current documentation context * @return the writer * @throws IOException if a writer cannot be resolved */ - Writer resolve(String operationName, String snippetName) throws IOException; + Writer resolve(String operationName, String snippetName, + RestDocumentationContext restDocumentationContext) throws IOException; /** * Configures the encoding that should be used by any writers produced by this diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/StandardTemplateResourceResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/StandardTemplateResourceResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/templates/StandardTemplateResourceResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/StandardTemplateResourceResolver.java diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/Template.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/Template.java new file mode 100644 index 00000000..7275c529 --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/Template.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014-2015 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.restdocs.templates; + +import java.util.Map; + +/** + * A compiled {@code Template} that can be rendered to a {@link String}. + * + * @author Andy Wilkinson + * + */ +public interface Template { + + /** + * Renders the template to a {@link String} using the given {@code context} for + * variable/property resolution. + * + * @param context The context to use + * @return The rendered template + */ + String render(Map context); + +} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/TemplateEngine.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateEngine.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/templates/TemplateEngine.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateEngine.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/TemplateResourceResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateResourceResolver.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/templates/TemplateResourceResolver.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateResourceResolver.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplate.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplate.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplate.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplate.java diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java similarity index 100% rename from spring-restdocs/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java rename to spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/constraints/DefaultConstraintDescriptions.properties b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/constraints/DefaultConstraintDescriptions.properties similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/constraints/DefaultConstraintDescriptions.properties rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/constraints/DefaultConstraintDescriptions.properties diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-curl-request.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-curl-request.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-curl-request.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-curl-request.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-http-request.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-http-request.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-http-request.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-http-request.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-http-response.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-http-response.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-http-response.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-http-response.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-links.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-links.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-links.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-links.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-request-fields.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-request-fields.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-request-fields.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-request-fields.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-request-parameters.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-request-parameters.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-request-parameters.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-request-parameters.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-response-fields.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-response-fields.snippet similarity index 100% rename from spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-response-fields.snippet rename to spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/default-response-fields.snippet diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java similarity index 87% rename from spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java index 8aa88989..e69192e2 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ConstraintDescriptionsTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.constraints; import static org.hamcrest.Matchers.contains; @@ -25,7 +41,7 @@ import org.junit.Test; /** * Tests for {@link ConstraintDescriptions} - * + * * @author Andy Wilkinson */ public class ConstraintDescriptionsTests { diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java similarity index 91% rename from spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java index 03202540..efb0f235 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ResourceBundleConstraintDescriptionResolverTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.constraints; import static org.hamcrest.CoreMatchers.equalTo; @@ -29,7 +45,7 @@ import org.junit.Test; /** * Tests for {@link ResourceBundleConstraintDescriptionResolver} - * + * * @author Andy Wilkinson */ public class ResourceBundleConstraintDescriptionResolverTests { diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java similarity index 86% rename from spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java index 26ab45ca..e07abd7f 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/constraints/ValidatorConstraintResolverTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.constraints; import static org.hamcrest.CoreMatchers.is; @@ -29,7 +45,7 @@ import org.junit.Test; /** * Tests for {@link ValidatorConstraintResolver} - * + * * @author Andy Wilkinson */ public class ValidatorConstraintResolverTests { diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java similarity index 80% rename from spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java index 48a8a566..96d6927a 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/curl/CurlRequestSnippetTests.java @@ -31,7 +31,6 @@ import org.junit.rules.ExpectedException; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.restdocs.templates.TemplateEngine; import org.springframework.restdocs.templates.TemplateResourceResolver; import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine; @@ -58,16 +57,18 @@ public class CurlRequestSnippetTests { public void getRequest() throws IOException { this.snippet.expectCurlRequest("get-request").withContents( codeBlock("bash").content("$ curl 'http://localhost/foo' -i")); - new CurlRequestSnippet().document(new OperationBuilder("get-request").request( - "http://localhost/foo").build()); + new CurlRequestSnippet().document(new OperationBuilder("get-request", + this.snippet.getOutputDirectory()).request("http://localhost/foo") + .build()); } @Test public void nonGetRequest() throws IOException { this.snippet.expectCurlRequest("non-get-request").withContents( codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X POST")); - new CurlRequestSnippet().document(new OperationBuilder("non-get-request") - .request("http://localhost/foo").method("POST").build()); + new CurlRequestSnippet().document(new OperationBuilder("non-get-request", + this.snippet.getOutputDirectory()).request("http://localhost/foo") + .method("POST").build()); } @Test @@ -75,8 +76,9 @@ public class CurlRequestSnippetTests { this.snippet.expectCurlRequest("request-with-content").withContents( codeBlock("bash") .content("$ curl 'http://localhost/foo' -i -d 'content'")); - new CurlRequestSnippet().document(new OperationBuilder("request-with-content") - .request("http://localhost/foo").content("content").build()); + new CurlRequestSnippet().document(new OperationBuilder("request-with-content", + this.snippet.getOutputDirectory()).request("http://localhost/foo") + .content("content").build()); } @Test @@ -86,8 +88,8 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo?param=value' -i")); new CurlRequestSnippet().document(new OperationBuilder( - "request-with-query-string").request("http://localhost/foo?param=value") - .build()); + "request-with-query-string", this.snippet.getOutputDirectory()).request( + "http://localhost/foo?param=value").build()); } @Test @@ -95,9 +97,11 @@ public class CurlRequestSnippetTests { this.snippet.expectCurlRequest("post-request-with-one-parameter").withContents( codeBlock("bash").content( "$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'")); - new CurlRequestSnippet().document(new OperationBuilder( - "post-request-with-one-parameter").request("http://localhost/foo") - .method("POST").param("k1", "v1").build()); + new CurlRequestSnippet() + .document(new OperationBuilder("post-request-with-one-parameter", + this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("POST").param("k1", "v1") + .build()); } @Test @@ -108,7 +112,8 @@ public class CurlRequestSnippetTests { "$ curl 'http://localhost/foo' -i -X POST" + " -d 'k1=v1&k1=v1-bis&k2=v2'")); new CurlRequestSnippet().document(new OperationBuilder( - "post-request-with-multiple-parameters").request("http://localhost/foo") + "post-request-with-multiple-parameters", this.snippet + .getOutputDirectory()).request("http://localhost/foo") .method("POST").param("k1", "v1", "v1-bis").param("k2", "v2").build()); } @@ -120,9 +125,9 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'")); new CurlRequestSnippet().document(new OperationBuilder( - "post-request-with-url-encoded-parameter") - .request("http://localhost/foo").method("POST").param("k1", "a&b") - .build()); + "post-request-with-url-encoded-parameter", this.snippet + .getOutputDirectory()).request("http://localhost/foo") + .method("POST").param("k1", "a&b").build()); } @Test @@ -131,8 +136,8 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'")); new CurlRequestSnippet().document(new OperationBuilder( - "put-request-with-one-parameter").request("http://localhost/foo") - .method("PUT").param("k1", "v1").build()); + "put-request-with-one-parameter", this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("PUT").param("k1", "v1").build()); } @Test @@ -142,10 +147,11 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo' -i -X PUT" + " -d 'k1=v1&k1=v1-bis&k2=v2'")); - new CurlRequestSnippet().document(new OperationBuilder( - "put-request-with-multiple-parameters").request("http://localhost/foo") - .method("PUT").param("k1", "v1").param("k1", "v1-bis").param("k2", "v2") - .build()); + new CurlRequestSnippet() + .document(new OperationBuilder("put-request-with-multiple-parameters", + this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("PUT").param("k1", "v1") + .param("k1", "v1-bis").param("k2", "v2").build()); } @Test @@ -155,7 +161,8 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'")); new CurlRequestSnippet().document(new OperationBuilder( - "put-request-with-url-encoded-parameter").request("http://localhost/foo") + "put-request-with-url-encoded-parameter", this.snippet + .getOutputDirectory()).request("http://localhost/foo") .method("PUT").param("k1", "a&b").build()); } @@ -165,8 +172,8 @@ public class CurlRequestSnippetTests { codeBlock("bash").content( "$ curl 'http://localhost/foo' -i" + " -H 'Content-Type: application/json' -H 'a: alpha'")); - new CurlRequestSnippet().document(new OperationBuilder("request-with-headers") - .request("http://localhost/foo") + new CurlRequestSnippet().document(new OperationBuilder("request-with-headers", + this.snippet.getOutputDirectory()).request("http://localhost/foo") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .header("a", "alpha").build()); } @@ -179,8 +186,8 @@ public class CurlRequestSnippetTests { this.snippet.expectCurlRequest("multipart-post-no-original-filename") .withContents(codeBlock("bash").content(expectedContent)); new CurlRequestSnippet().document(new OperationBuilder( - "multipart-post-no-original-filename").request("http://localhost/upload") - .method("POST") + "multipart-post-no-original-filename", this.snippet.getOutputDirectory()) + .request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("metadata", "{\"description\": \"foo\"}".getBytes()).build()); } @@ -193,8 +200,8 @@ public class CurlRequestSnippetTests { this.snippet.expectCurlRequest("multipart-post-with-content-type").withContents( codeBlock("bash").content(expectedContent)); new CurlRequestSnippet().document(new OperationBuilder( - "multipart-post-with-content-type").request("http://localhost/upload") - .method("POST") + "multipart-post-with-content-type", this.snippet.getOutputDirectory()) + .request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("image", new byte[0]) .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) @@ -208,8 +215,9 @@ public class CurlRequestSnippetTests { + "'image=@documents/images/example.png'"; this.snippet.expectCurlRequest("multipart-post").withContents( codeBlock("bash").content(expectedContent)); - new CurlRequestSnippet().document(new OperationBuilder("multipart-post") - .request("http://localhost/upload").method("POST") + new CurlRequestSnippet().document(new OperationBuilder("multipart-post", + this.snippet.getOutputDirectory()).request("http://localhost/upload") + .method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("image", new byte[0]) .submittedFileName("documents/images/example.png").build()); @@ -224,8 +232,8 @@ public class CurlRequestSnippetTests { this.snippet.expectCurlRequest("multipart-post-with-parameters").withContents( codeBlock("bash").content(expectedContent)); new CurlRequestSnippet().document(new OperationBuilder( - "multipart-post-with-parameters").request("http://localhost/upload") - .method("POST") + "multipart-post-with-parameters", this.snippet.getOutputDirectory()) + .request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("image", new byte[0]) .submittedFileName("documents/images/example.png").and() @@ -236,16 +244,14 @@ public class CurlRequestSnippetTests { public void customAttributes() throws IOException { this.snippet.expectCurlRequest("custom-attributes").withContents( containsString("curl request title")); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); TemplateResourceResolver resolver = mock(TemplateResourceResolver.class); when(resolver.resolveTemplateResource("curl-request")) .thenReturn( new FileSystemResource( "src/test/resources/custom-snippet-templates/curl-request-with-title.snippet")); - request.setAttribute(TemplateEngine.class.getName(), new MustacheTemplateEngine( - resolver)); new CurlRequestSnippet(attributes(key("title").value("curl request title"))) - .document(new OperationBuilder("custom-attributes") + .document(new OperationBuilder("custom-attributes", this.snippet + .getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) .request("http://localhost/foo").build()); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java similarity index 85% rename from spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java index 3eacda6e..8da93faa 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java @@ -59,8 +59,9 @@ public class HttpRequestSnippetTests { httpRequest(GET, "/foo").header(HttpHeaders.HOST, "localhost").header( "Alpha", "a")); - new HttpRequestSnippet().document(new OperationBuilder("get-request") - .request("http://localhost/foo").header("Alpha", "a").build()); + new HttpRequestSnippet().document(new OperationBuilder("get-request", + this.snippet.getOutputDirectory()).request("http://localhost/foo") + .header("Alpha", "a").build()); } @Test @@ -69,8 +70,8 @@ public class HttpRequestSnippetTests { httpRequest(GET, "/foo?bar=baz").header(HttpHeaders.HOST, "localhost")); new HttpRequestSnippet().document(new OperationBuilder( - "get-request-with-query-string").request("http://localhost/foo?bar=baz") - .build()); + "get-request-with-query-string", this.snippet.getOutputDirectory()) + .request("http://localhost/foo?bar=baz").build()); } @Test @@ -80,8 +81,9 @@ public class HttpRequestSnippetTests { "Hello, world")); new HttpRequestSnippet().document(new OperationBuilder( - "post-request-with-content").request("http://localhost/foo") - .method("POST").content("Hello, world").build()); + "post-request-with-content", this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("POST").content("Hello, world") + .build()); } @Test @@ -92,8 +94,9 @@ public class HttpRequestSnippetTests { .content("b%26r=baz&a=alpha")); new HttpRequestSnippet().document(new OperationBuilder( - "post-request-with-parameter").request("http://localhost/foo") - .method("POST").param("b&r", "baz").param("a", "alpha").build()); + "post-request-with-parameter", this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("POST").param("b&r", "baz") + .param("a", "alpha").build()); } @Test @@ -102,10 +105,10 @@ public class HttpRequestSnippetTests { httpRequest(PUT, "/foo").header(HttpHeaders.HOST, "localhost").content( "Hello, world")); - new HttpRequestSnippet() - .document(new OperationBuilder("put-request-with-content") - .request("http://localhost/foo").method("PUT") - .content("Hello, world").build()); + new HttpRequestSnippet().document(new OperationBuilder( + "put-request-with-content", this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("PUT").content("Hello, world") + .build()); } @Test @@ -116,8 +119,9 @@ public class HttpRequestSnippetTests { .content("b%26r=baz&a=alpha")); new HttpRequestSnippet().document(new OperationBuilder( - "put-request-with-parameter").request("http://localhost/foo") - .method("PUT").param("b&r", "baz").param("a", "alpha").build()); + "put-request-with-parameter", this.snippet.getOutputDirectory()) + .request("http://localhost/foo").method("PUT").param("b&r", "baz") + .param("a", "alpha").build()); } @Test @@ -130,8 +134,9 @@ public class HttpRequestSnippetTests { .header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY) .content(expectedContent)); - new HttpRequestSnippet().document(new OperationBuilder("multipart-post") - .request("http://localhost/upload").method("POST") + new HttpRequestSnippet().document(new OperationBuilder("multipart-post", + this.snippet.getOutputDirectory()).request("http://localhost/upload") + .method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("image", "<< data >>".getBytes()).build()); } @@ -154,8 +159,8 @@ public class HttpRequestSnippetTests { "multipart/form-data; boundary=" + BOUNDARY) .content(expectedContent)); new HttpRequestSnippet().document(new OperationBuilder( - "multipart-post-with-parameters").request("http://localhost/upload") - .method("POST") + "multipart-post-with-parameters", this.snippet.getOutputDirectory()) + .request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .param("a", "apple", "avocado").param("b", "banana") .part("image", "<< data >>".getBytes()).build()); @@ -173,8 +178,8 @@ public class HttpRequestSnippetTests { "multipart/form-data; boundary=" + BOUNDARY) .content(expectedContent)); new HttpRequestSnippet().document(new OperationBuilder( - "multipart-post-with-content-type").request("http://localhost/upload") - .method("POST") + "multipart-post-with-content-type", this.snippet.getOutputDirectory()) + .request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) .part("image", "<< data >>".getBytes()) .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE).build()); @@ -184,8 +189,8 @@ public class HttpRequestSnippetTests { public void getRequestWithCustomHost() throws IOException { this.snippet.expectHttpRequest("get-request-custom-host").withContents( httpRequest(GET, "/foo").header(HttpHeaders.HOST, "api.example.com")); - new HttpRequestSnippet().document(new OperationBuilder("get-request-custom-host") - .request("http://localhost/foo") + new HttpRequestSnippet().document(new OperationBuilder("get-request-custom-host", + this.snippet.getOutputDirectory()).request("http://localhost/foo") .header(HttpHeaders.HOST, "api.example.com").build()); } @@ -199,7 +204,8 @@ public class HttpRequestSnippetTests { new FileSystemResource( "src/test/resources/custom-snippet-templates/http-request-with-title.snippet")); new HttpRequestSnippet(attributes(key("title").value("Title for the request"))) - .document(new OperationBuilder("request-with-snippet-attributes") + .document(new OperationBuilder("request-with-snippet-attributes", + this.snippet.getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) .request("http://localhost/foo").build()); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java similarity index 86% rename from spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java index 068d6b88..cfe68c60 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java @@ -52,16 +52,17 @@ public class HttpResponseSnippetTests { @Test public void basicResponse() throws IOException { this.snippet.expectHttpResponse("basic-response").withContents(httpResponse(OK)); - new HttpResponseSnippet() - .document(new OperationBuilder("basic-response").build()); + new HttpResponseSnippet().document(new OperationBuilder("basic-response", + this.snippet.getOutputDirectory()).build()); } @Test public void nonOkResponse() throws IOException { this.snippet.expectHttpResponse("non-ok-response").withContents( httpResponse(BAD_REQUEST)); - new HttpResponseSnippet().document(new OperationBuilder("non-ok-response") - .response().status(BAD_REQUEST.value()).build()); + new HttpResponseSnippet().document(new OperationBuilder("non-ok-response", + this.snippet.getOutputDirectory()).response().status(BAD_REQUEST.value()) + .build()); } @Test @@ -70,8 +71,8 @@ public class HttpResponseSnippetTests { httpResponse(OK) // .header("Content-Type", "application/json") // .header("a", "alpha")); - new HttpResponseSnippet().document(new OperationBuilder("response-with-headers") - .response() + new HttpResponseSnippet().document(new OperationBuilder("response-with-headers", + this.snippet.getOutputDirectory()).response() .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .header("a", "alpha").build()); } @@ -80,8 +81,8 @@ public class HttpResponseSnippetTests { public void responseWithContent() throws IOException { this.snippet.expectHttpResponse("response-with-content").withContents( httpResponse(OK).content("content")); - new HttpResponseSnippet().document(new OperationBuilder("response-with-content") - .response().content("content").build()); + new HttpResponseSnippet().document(new OperationBuilder("response-with-content", + this.snippet.getOutputDirectory()).response().content("content").build()); } @Test @@ -94,9 +95,10 @@ public class HttpResponseSnippetTests { new FileSystemResource( "src/test/resources/custom-snippet-templates/http-response-with-title.snippet")); new HttpResponseSnippet(attributes(key("title").value("Title for the response"))) - .document(new OperationBuilder("response-with-snippet-attributes") - .attribute(TemplateEngine.class.getName(), - new MustacheTemplateEngine(resolver)).build()); + .document(new OperationBuilder("response-with-snippet-attributes", + this.snippet.getOutputDirectory()).attribute( + TemplateEngine.class.getName(), + new MustacheTemplateEngine(resolver)).build()); } } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java similarity index 90% rename from spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java index 97979bdc..f5a51480 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java @@ -32,7 +32,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.core.io.FileSystemResource; -import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.snippet.SnippetException; import org.springframework.restdocs.templates.TemplateEngine; @@ -45,7 +44,7 @@ import org.springframework.util.MultiValueMap; /** * Tests for {@link LinksSnippet} - * + * * @author Andy Wilkinson */ public class LinksSnippetTests { @@ -63,7 +62,7 @@ public class LinksSnippetTests { + " documented: [foo]")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")), Collections. emptyList()).document(new OperationBuilder( - "undocumented-link").build()); + "undocumented-link", this.snippet.getOutputDirectory()).build()); } @Test @@ -72,8 +71,8 @@ public class LinksSnippetTests { this.thrown.expectMessage(equalTo("Links with the following relations were not" + " found in the response: [foo]")); new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo") - .description("bar"))).document(new OperationBuilder("missing-link") - .build()); + .description("bar"))).document(new OperationBuilder("missing-link", + this.snippet.getOutputDirectory()).build()); } @Test @@ -83,7 +82,8 @@ public class LinksSnippetTests { .row("foo", "bar")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")), Arrays.asList(new LinkDescriptor("foo").description("bar").optional())) - .document(new OperationBuilder("documented-optional-link").build()); + .document(new OperationBuilder("documented-optional-link", this.snippet + .getOutputDirectory()).build()); } @Test @@ -93,7 +93,7 @@ public class LinksSnippetTests { .row("foo", "bar")); new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo") .description("bar").optional())).document(new OperationBuilder( - "missing-optional-link").build()); + "missing-optional-link", this.snippet.getOutputDirectory()).build()); } @Test @@ -104,8 +104,8 @@ public class LinksSnippetTests { + " found in the response: [foo]")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")), Arrays.asList(new LinkDescriptor("foo").description("bar"))) - .document(new OperationBuilder("undocumented-link-and-missing-link") - .build()); + .document(new OperationBuilder("undocumented-link-and-missing-link", + this.snippet.getOutputDirectory()).build()); } @Test @@ -118,7 +118,8 @@ public class LinksSnippetTests { new Link("b", "bravo")), Arrays.asList( new LinkDescriptor("a").description("one"), new LinkDescriptor("b").description("two"))) - .document(new OperationBuilder("documented-links").build()); + .document(new OperationBuilder("documented-links", this.snippet + .getOutputDirectory()).build()); } @Test @@ -127,23 +128,20 @@ public class LinksSnippetTests { tableWithHeader("Relation", "Description", "Foo") // .row("a", "one", "alpha") // .row("b", "two", "bravo")); - MockHttpServletRequest request = new MockHttpServletRequest(); TemplateResourceResolver resolver = mock(TemplateResourceResolver.class); when(resolver.resolveTemplateResource("links")) .thenReturn( new FileSystemResource( "src/test/resources/custom-snippet-templates/links-with-extra-column.snippet")); - request.setAttribute(TemplateEngine.class.getName(), new MustacheTemplateEngine( - resolver)); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha"), new Link("b", "bravo")), Arrays.asList( new LinkDescriptor("a").description("one").attributes( key("foo").value("alpha")), new LinkDescriptor("b").description("two").attributes( key("foo").value("bravo")))).document(new OperationBuilder( - "links-with-custom-descriptor-attributes").attribute( - TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) - .build()); + "links-with-custom-descriptor-attributes", this.snippet + .getOutputDirectory()).attribute(TemplateEngine.class.getName(), + new MustacheTemplateEngine(resolver)).build()); } @Test @@ -160,7 +158,8 @@ public class LinksSnippetTests { "Title for the links")), Arrays.asList( new LinkDescriptor("a").description("one"), new LinkDescriptor("b").description("two"))) - .document(new OperationBuilder("links-with-custom-attributes").attribute( + .document(new OperationBuilder("links-with-custom-attributes", + this.snippet.getOutputDirectory()).attribute( TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).build()); } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java similarity index 82% rename from spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java index 0a31fd8f..f13509b5 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; import static org.hamcrest.CoreMatchers.equalTo; @@ -20,7 +36,7 @@ import org.springframework.restdocs.operation.StandardOperationResponse; /** * Tests for {@link ContentModifyingOperationPreprocessor} - * + * * @author Andy Wilkinson * */ diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessorTests.java new file mode 100644 index 00000000..fdfe17c7 --- /dev/null +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationRequestPreprocessorTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +import org.junit.Test; +import org.springframework.restdocs.operation.OperationRequest; + +/** + * Tests for {@link DelegatingOperationRequestPreprocessor} + * + * @author Andy Wilkinson + */ +public class DelegatingOperationRequestPreprocessorTests { + + @Test + public void delegationOccurs() { + OperationRequest originalRequest = mock(OperationRequest.class); + + OperationPreprocessor preprocessor1 = mock(OperationPreprocessor.class); + OperationRequest preprocessedRequest1 = mock(OperationRequest.class); + when(preprocessor1.preprocess(originalRequest)).thenReturn(preprocessedRequest1); + + OperationPreprocessor preprocessor2 = mock(OperationPreprocessor.class); + OperationRequest preprocessedRequest2 = mock(OperationRequest.class); + when(preprocessor2.preprocess(preprocessedRequest1)).thenReturn( + preprocessedRequest2); + + OperationPreprocessor preprocessor3 = mock(OperationPreprocessor.class); + OperationRequest preprocessedRequest3 = mock(OperationRequest.class); + when(preprocessor3.preprocess(preprocessedRequest2)).thenReturn( + preprocessedRequest3); + + OperationRequest result = new DelegatingOperationRequestPreprocessor( + Arrays.asList(preprocessor1, preprocessor2, preprocessor3)) + .preprocess(originalRequest); + + assertThat(result, is(preprocessedRequest3)); + } +} diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessorTests.java new file mode 100644 index 00000000..764eb63d --- /dev/null +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/DelegatingOperationResponsePreprocessorTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +import org.junit.Test; +import org.springframework.restdocs.operation.OperationResponse; + +/** + * Tests for {@link DelegatingOperationResponsePreprocessor} + * + * @author Andy Wilkinson + */ +public class DelegatingOperationResponsePreprocessorTests { + + @Test + public void delegationOccurs() { + OperationResponse originalResponse = mock(OperationResponse.class); + + OperationPreprocessor preprocessor1 = mock(OperationPreprocessor.class); + OperationResponse preprocessedResponse1 = mock(OperationResponse.class); + when(preprocessor1.preprocess(originalResponse)) + .thenReturn(preprocessedResponse1); + + OperationPreprocessor preprocessor2 = mock(OperationPreprocessor.class); + OperationResponse preprocessedResponse2 = mock(OperationResponse.class); + when(preprocessor2.preprocess(preprocessedResponse1)).thenReturn( + preprocessedResponse2); + + OperationPreprocessor preprocessor3 = mock(OperationPreprocessor.class); + OperationResponse preprocessedResponse3 = mock(OperationResponse.class); + when(preprocessor3.preprocess(preprocessedResponse2)).thenReturn( + preprocessedResponse3); + + OperationResponse result = new DelegatingOperationResponsePreprocessor( + Arrays.asList(preprocessor1, preprocessor2, preprocessor3)) + .preprocess(originalResponse); + + assertThat(result, is(preprocessedResponse3)); + } +} diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java similarity index 78% rename from spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java index c90f4afd..cf710c81 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; import static org.hamcrest.CoreMatchers.equalTo; @@ -22,7 +38,7 @@ import org.springframework.restdocs.operation.StandardOperationResponse; /** * Tests for {@link HeaderRemovingOperationPreprocessorTests} - * + * * @author Andy Wilkinson * */ diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java similarity index 86% rename from spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java index 2763d646..aa881ba8 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/LinkMaskingContentModifierTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; import static org.hamcrest.CoreMatchers.equalTo; @@ -20,7 +36,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; /** * Tests for {@link LinkMaskingContentModifier} - * + * * @author Andy Wilkinson * */ diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java similarity index 58% rename from spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java index f419296d..c7a1e2c9 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014-2015 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.restdocs.operation.preprocess; import static org.hamcrest.CoreMatchers.equalTo; @@ -10,7 +26,7 @@ import org.junit.Test; /** * Tests for {@link PatternReplacingContentModifier} - * + * * @author Andy Wilkinson * */ diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldPathTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldPathTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldPathTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldPathTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldProcessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldProcessorTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldProcessorTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldProcessorTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldTypeResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypeResolverTests.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/payload/JsonFieldTypeResolverTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypeResolverTests.java diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java similarity index 87% rename from spring-restdocs/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java index bec7ee7e..35168549 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java @@ -67,7 +67,8 @@ public class RequestFieldsSnippetTests { new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"), fieldWithPath("a.c").description("two"), fieldWithPath("a").description("three"))).document(new OperationBuilder( - "map-request-with-fields").request("http://localhost") + "map-request-with-fields", this.snippet.getOutputDirectory()) + .request("http://localhost") .content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build()); } @@ -82,7 +83,8 @@ public class RequestFieldsSnippetTests { new RequestFieldsSnippet(Arrays.asList(fieldWithPath("[]a.b").description("one"), fieldWithPath("[]a.c").description("two"), fieldWithPath("[]a") .description("three"))).document(new OperationBuilder( - "array-request-with-fields").request("http://localhost") + "array-request-with-fields", this.snippet.getOutputDirectory()) + .request("http://localhost") .content("[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]").build()); } @@ -93,8 +95,9 @@ public class RequestFieldsSnippetTests { .expectMessage(startsWith("The following parts of the payload were not" + " documented:")); new RequestFieldsSnippet(Collections. emptyList()) - .document(new OperationBuilder("undocumented-request-field") - .request("http://localhost").content("{\"a\": 5}").build()); + .document(new OperationBuilder("undocumented-request-field", this.snippet + .getOutputDirectory()).request("http://localhost") + .content("{\"a\": 5}").build()); } @Test @@ -104,8 +107,9 @@ public class RequestFieldsSnippetTests { .expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a.b]")); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"))) - .document(new OperationBuilder("missing-request-fields") - .request("http://localhost").content("{}").build()); + .document(new OperationBuilder("missing-request-fields", this.snippet + .getOutputDirectory()).request("http://localhost").content("{}") + .build()); } @Test @@ -113,8 +117,9 @@ public class RequestFieldsSnippetTests { this.thrown.expect(FieldTypeRequiredException.class); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one") .optional())).document(new OperationBuilder( - "missing-optional-request-field-with-no-type") - .request("http://localhost").content("{ }").build()); + "missing-optional-request-field-with-no-type", this.snippet + .getOutputDirectory()).request("http://localhost").content("{ }") + .build()); } @Test @@ -128,9 +133,9 @@ public class RequestFieldsSnippetTests { + " in the payload: [a.b]")); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"))) .document(new OperationBuilder( - "undocumented-request-field-and-missing-request-field") - .request("http://localhost").content("{ \"a\": { \"c\": 5 }}") - .build()); + "undocumented-request-field-and-missing-request-field", + this.snippet.getOutputDirectory()).request("http://localhost") + .content("{ \"a\": { \"c\": 5 }}").build()); } @Test @@ -151,7 +156,8 @@ public class RequestFieldsSnippetTests { key("foo").value("bravo")), fieldWithPath("a").description("three").attributes( key("foo").value("charlie")))).document(new OperationBuilder( - "request-fields-with-custom-descriptor-attributes") + "request-fields-with-custom-descriptor-attributes", this.snippet + .getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).request("http://localhost") .content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build()); @@ -166,7 +172,8 @@ public class RequestFieldsSnippetTests { snippetResource("request-fields-with-title")); new RequestFieldsSnippet(attributes(key("title").value("Custom title")), Arrays.asList(fieldWithPath("a").description("one"))) - .document(new OperationBuilder("request-fields-with-custom-attributes") + .document(new OperationBuilder("request-fields-with-custom-attributes", + this.snippet.getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) .request("http://localhost").content("{\"a\": \"foo\"}").build()); @@ -183,7 +190,8 @@ public class RequestFieldsSnippetTests { new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one") .type("b"), fieldWithPath("a/c").description("two").type("c"), fieldWithPath("a").description("three").type("a"))) - .document(new OperationBuilder("xml-request") + .document(new OperationBuilder("xml-request", this.snippet + .getOutputDirectory()) .request("http://localhost") .content("5charlie") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -197,7 +205,8 @@ public class RequestFieldsSnippetTests { .expectMessage(startsWith("The following parts of the payload were not" + " documented:")); new RequestFieldsSnippet(Collections. emptyList()) - .document(new OperationBuilder("undocumented-xml-request-field") + .document(new OperationBuilder("undocumented-xml-request-field", + this.snippet.getOutputDirectory()) .request("http://localhost") .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -208,7 +217,8 @@ public class RequestFieldsSnippetTests { public void xmlRequestFieldWithNoType() throws IOException { this.thrown.expect(FieldTypeRequiredException.class); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one"))) - .document(new OperationBuilder("missing-xml-request") + .document(new OperationBuilder("missing-xml-request", this.snippet + .getOutputDirectory()) .request("http://localhost") .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -223,8 +233,8 @@ public class RequestFieldsSnippetTests { + " in the payload: [a/b]")); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one"))).document(new OperationBuilder( - "missing-xml-request-fields").request("http://localhost") - .content("") + "missing-xml-request-fields", this.snippet.getOutputDirectory()) + .request("http://localhost").content("") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) .build()); } @@ -240,7 +250,8 @@ public class RequestFieldsSnippetTests { + " in the payload: [a/b]")); new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"))) .document(new OperationBuilder( - "undocumented-xml-request-field-and-missing-xml-request-field") + "undocumented-xml-request-field-and-missing-xml-request-field", + this.snippet.getOutputDirectory()) .request("http://localhost") .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java similarity index 87% rename from spring-restdocs/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java index 9018f96d..16f5a853 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java @@ -35,7 +35,6 @@ import org.junit.rules.ExpectedException; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.restdocs.snippet.SnippetException; import org.springframework.restdocs.templates.TemplateEngine; import org.springframework.restdocs.templates.TemplateResourceResolver; @@ -44,7 +43,7 @@ import org.springframework.restdocs.test.ExpectedSnippet; import org.springframework.restdocs.test.OperationBuilder; /** - * Tests for {@link PayloadDocumentation} + * Tests for {@link ReponseFieldsSnippet} * * @author Andy Wilkinson */ @@ -72,7 +71,8 @@ public class ResponseFieldsSnippetTests { fieldWithPath("assets[]").description("four"), fieldWithPath("assets[].id").description("five"), fieldWithPath("assets[].name").description("six"))) - .document(new OperationBuilder("map-response-with-fields") + .document(new OperationBuilder("map-response-with-fields", this.snippet + .getOutputDirectory()) .response() .content( "{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":" @@ -90,7 +90,8 @@ public class ResponseFieldsSnippetTests { new ResponseFieldsSnippet(Arrays.asList( fieldWithPath("[]a.b").description("one"), fieldWithPath("[]a.c") .description("two"), fieldWithPath("[]a").description("three"))) - .document(new OperationBuilder("array-response-with-fields").response() + .document(new OperationBuilder("array-response-with-fields", this.snippet + .getOutputDirectory()).response() .content("[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]") .build()); } @@ -101,7 +102,8 @@ public class ResponseFieldsSnippetTests { tableWithHeader("Path", "Type", "Description") // .row("[]", "String", "one")); new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]").description("one"))) - .document(new OperationBuilder("array-response").response() + .document(new OperationBuilder("array-response", this.snippet + .getOutputDirectory()).response() .content("[\"a\", \"b\", \"c\"]").build()); } @@ -123,7 +125,8 @@ public class ResponseFieldsSnippetTests { key("foo").value("bravo")), fieldWithPath("a").description("three").attributes( key("foo").value("charlie")))).document(new OperationBuilder( - "response-fields-with-custom-attributes") + "response-fields-with-custom-attributes", this.snippet + .getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).response() .content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build()); @@ -138,7 +141,8 @@ public class ResponseFieldsSnippetTests { snippetResource("response-fields-with-title")); new ResponseFieldsSnippet(attributes(key("title").value("Custom title")), Arrays.asList(fieldWithPath("a").description("one"))) - .document(new OperationBuilder("response-fields-with-custom-attributes") + .document(new OperationBuilder("response-fields-with-custom-attributes", + this.snippet.getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).response() .content("{\"a\": \"foo\"}").build()); @@ -154,7 +158,8 @@ public class ResponseFieldsSnippetTests { new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one") .type("b"), fieldWithPath("a/c").description("two").type("c"), fieldWithPath("a").description("three").type("a"))) - .document(new OperationBuilder("xml-response") + .document(new OperationBuilder("xml-response", this.snippet + .getOutputDirectory()) .response() .content("5charlie") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -167,11 +172,9 @@ public class ResponseFieldsSnippetTests { this.thrown .expectMessage(startsWith("The following parts of the payload were not" + " documented:")); - MockHttpServletResponse response = new MockHttpServletResponse(); - response.setContentType(MediaType.APPLICATION_XML_VALUE); - response.getOutputStream().print("5"); new ResponseFieldsSnippet(Collections. emptyList()) - .document(new OperationBuilder("undocumented-xml-response-field") + .document(new OperationBuilder("undocumented-xml-response-field", + this.snippet.getOutputDirectory()) .response() .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -181,11 +184,9 @@ public class ResponseFieldsSnippetTests { @Test public void xmlResponseFieldWithNoType() throws IOException { this.thrown.expect(FieldTypeRequiredException.class); - MockHttpServletResponse response = new MockHttpServletResponse(); - response.setContentType(MediaType.APPLICATION_XML_VALUE); - response.getOutputStream().print("5"); new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one"))) - .document(new OperationBuilder("xml-response-no-field-type") + .document(new OperationBuilder("xml-response-no-field-type", this.snippet + .getOutputDirectory()) .response() .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) @@ -198,12 +199,10 @@ public class ResponseFieldsSnippetTests { this.thrown .expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a/b]")); - MockHttpServletResponse response = new MockHttpServletResponse(); - response.setContentType(MediaType.APPLICATION_XML_VALUE); - response.getOutputStream().print(""); new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one"))).document(new OperationBuilder( - "missing-xml-response-field").response().content("") + "missing-xml-response-field", this.snippet.getOutputDirectory()) + .response().content("") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) .build()); } @@ -218,12 +217,10 @@ public class ResponseFieldsSnippetTests { this.thrown .expectMessage(endsWith("Fields with the following paths were not found" + " in the payload: [a/b]")); - MockHttpServletResponse response = new MockHttpServletResponse(); - response.setContentType(MediaType.APPLICATION_XML_VALUE); - response.getOutputStream().print("5"); new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"))) .document(new OperationBuilder( - "undocumented-xml-request-field-and-missing-xml-request-field") + "undocumented-xml-request-field-and-missing-xml-request-field", + this.snippet.getOutputDirectory()) .response() .content("5") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java similarity index 87% rename from spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index c4204d8c..5c5657c8 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -61,7 +61,8 @@ public class PathParametersSnippetTests { this.thrown.expectMessage(equalTo("Path parameters with the following names were" + " not documented: [a]")); new PathParametersSnippet(Collections. emptyList()) - .document(new OperationBuilder("undocumented-path-parameter").attribute( + .document(new OperationBuilder("undocumented-path-parameter", + this.snippet.getOutputDirectory()).attribute( "org.springframework.restdocs.urlTemplate", "/{a}/").build()); } @@ -72,7 +73,8 @@ public class PathParametersSnippetTests { + " not found in the request: [a]")); new PathParametersSnippet( Arrays.asList(parameterWithName("a").description("one"))) - .document(new OperationBuilder("missing-path-parameter").attribute( + .document(new OperationBuilder("missing-path-parameter", this.snippet + .getOutputDirectory()).attribute( "org.springframework.restdocs.urlTemplate", "/").build()); } @@ -84,9 +86,10 @@ public class PathParametersSnippetTests { + " names were not found in the request: [a]")); new PathParametersSnippet( Arrays.asList(parameterWithName("a").description("one"))) - .document(new OperationBuilder("undocumented-and-missing-path-parameters") - .attribute("org.springframework.restdocs.urlTemplate", "/{b}") - .build()); + .document(new OperationBuilder( + "undocumented-and-missing-path-parameters", this.snippet + .getOutputDirectory()).attribute( + "org.springframework.restdocs.urlTemplate", "/{b}").build()); } @Test @@ -97,8 +100,8 @@ public class PathParametersSnippetTests { new PathParametersSnippet(Arrays.asList( parameterWithName("a").description("one"), parameterWithName("b") .description("two"))).document(new OperationBuilder( - "path-parameters").attribute("org.springframework.restdocs.urlTemplate", - "/{a}/{b}").build()); + "path-parameters", this.snippet.getOutputDirectory()).attribute( + "org.springframework.restdocs.urlTemplate", "/{a}/{b}").build()); } @Test @@ -109,9 +112,11 @@ public class PathParametersSnippetTests { .row("a", "one").row("b", "two")); new PathParametersSnippet(Arrays.asList( parameterWithName("a").description("one"), parameterWithName("b") - .description("two"))).document(new OperationBuilder( - "path-parameters-with-query-string").attribute( - "org.springframework.restdocs.urlTemplate", "/{a}/{b}?foo=bar").build()); + .description("two"))) + .document(new OperationBuilder("path-parameters-with-query-string", + this.snippet.getOutputDirectory()).attribute( + "org.springframework.restdocs.urlTemplate", "/{a}/{b}?foo=bar") + .build()); } @Test @@ -127,7 +132,8 @@ public class PathParametersSnippetTests { .attributes(key("foo").value("alpha")), parameterWithName("b") .description("two").attributes(key("foo").value("bravo")))) .document(new OperationBuilder( - "path-parameters-with-custom-descriptor-attributes") + "path-parameters-with-custom-descriptor-attributes", this.snippet + .getOutputDirectory()) .attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}") .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).build()); @@ -146,7 +152,8 @@ public class PathParametersSnippetTests { parameterWithName("a").description("one").attributes( key("foo").value("alpha")), parameterWithName("b") .description("two").attributes(key("foo").value("bravo")))) - .document(new OperationBuilder("path-parameters-with-custom-attributes") + .document(new OperationBuilder("path-parameters-with-custom-attributes", + this.snippet.getOutputDirectory()) .attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}") .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).build()); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java similarity index 87% rename from spring-restdocs/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java index 0ec85773..5a071ffd 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java @@ -60,8 +60,9 @@ public class RequestParametersSnippetTests { .expectMessage(equalTo("Request parameters with the following names were" + " not documented: [a]")); new RequestParametersSnippet(Collections. emptyList()) - .document(new OperationBuilder("undocumented-parameter") - .request("http://localhost").param("a", "alpha").build()); + .document(new OperationBuilder("undocumented-parameter", this.snippet + .getOutputDirectory()).request("http://localhost") + .param("a", "alpha").build()); } @Test @@ -71,8 +72,8 @@ public class RequestParametersSnippetTests { .expectMessage(equalTo("Request parameters with the following names were" + " not found in the request: [a]")); new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description( - "one"))).document(new OperationBuilder("missing-parameter").request( - "http://localhost").build()); + "one"))).document(new OperationBuilder("missing-parameter", this.snippet + .getOutputDirectory()).request("http://localhost").build()); } @Test @@ -84,8 +85,8 @@ public class RequestParametersSnippetTests { + " names were not found in the request: [a]")); new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description( "one"))).document(new OperationBuilder( - "undocumented-and-missing-parameters").request("http://localhost") - .param("b", "bravo").build()); + "undocumented-and-missing-parameters", this.snippet.getOutputDirectory()) + .request("http://localhost").param("b", "bravo").build()); } @Test @@ -96,8 +97,9 @@ public class RequestParametersSnippetTests { new RequestParametersSnippet(Arrays.asList( parameterWithName("a").description("one"), parameterWithName("b") .description("two"))).document(new OperationBuilder( - "request-parameters").request("http://localhost").param("a", "bravo") - .param("b", "bravo").build()); + "request-parameters", this.snippet.getOutputDirectory()) + .request("http://localhost").param("a", "bravo").param("b", "bravo") + .build()); } @Test @@ -114,7 +116,8 @@ public class RequestParametersSnippetTests { key("foo").value("alpha")), parameterWithName("b").description("two").attributes( key("foo").value("bravo")))).document(new OperationBuilder( - "request-parameters-with-custom-descriptor-attributes") + "request-parameters-with-custom-descriptor-attributes", this.snippet + .getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)).request("http://localhost") .param("a", "alpha").param("b", "bravo").build()); @@ -134,7 +137,8 @@ public class RequestParametersSnippetTests { key("foo").value("alpha")), parameterWithName("b") .description("two").attributes(key("foo").value("bravo")))) .document(new OperationBuilder( - "request-parameters-with-custom-attributes") + "request-parameters-with-custom-attributes", this.snippet + .getOutputDirectory()) .attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) .request("http://localhost").param("a", "alpha") diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java new file mode 100644 index 00000000..ff06d8b3 --- /dev/null +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2014-2015 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.restdocs.snippet; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.restdocs.RestDocumentationContext; +import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; + +/** + * Tests for {@link RestDocumentationContextPlaceholderResolver} + * + * @author Andy Wilkinson + * + */ +public class RestDocumentationContextPlaceholderResolverTests { + + @Test + public void dashSeparatedMethodName() throws Exception { + assertThat( + createResolver("dashSeparatedMethodName").resolvePlaceholder( + "method-name"), equalTo("dash-separated-method-name")); + } + + @Test + public void underscoreSeparatedMethodName() throws Exception { + assertThat( + createResolver("underscoreSeparatedMethodName").resolvePlaceholder( + "method_name"), equalTo("underscore_separated_method_name")); + } + + @Test + public void camelCaseMethodName() throws Exception { + assertThat( + createResolver("camelCaseMethodName").resolvePlaceholder("methodName"), + equalTo("camelCaseMethodName")); + } + + @Test + public void stepCount() throws Exception { + assertThat(createResolver("stepCount").resolvePlaceholder("step"), equalTo("0")); + } + + private PlaceholderResolver createResolver(String methodName) { + return new RestDocumentationContextPlaceholderResolver( + new RestDocumentationContext(null, methodName, null)); + } + +} diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java similarity index 65% rename from spring-restdocs/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java index c1882d27..2f186dd0 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java @@ -24,7 +24,7 @@ import static org.mockito.Mockito.mock; import java.io.File; import org.junit.Test; -import org.springframework.restdocs.snippet.StandardWriterResolver; +import org.springframework.restdocs.RestDocumentationContext; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; /** @@ -41,41 +41,33 @@ public class StandardWriterResolverTests { @Test public void noConfiguredOutputDirectoryAndRelativeInput() { - assertThat(this.resolver.resolveFile("foo", "bar.txt"), is(nullValue())); + assertThat(this.resolver.resolveFile("foo", "bar.txt", + new RestDocumentationContext(null, null, null)), is(nullValue())); } @Test public void absoluteInput() { String absolutePath = new File("foo").getAbsolutePath(); - assertThat(this.resolver.resolveFile(absolutePath, "bar.txt"), is(new File( + assertThat(this.resolver.resolveFile(absolutePath, "bar.txt", + new RestDocumentationContext(null, null, null)), is(new File( absolutePath, "bar.txt"))); } @Test public void configuredOutputAndRelativeInput() { - String outputDir = new File("foo").getAbsolutePath(); - System.setProperty("org.springframework.restdocs.outputDir", outputDir); - try { - assertThat(this.resolver.resolveFile("bar", "baz.txt"), is(new File( - outputDir, "bar/baz.txt"))); - } - finally { - System.clearProperty("org.springframework.restdocs.outputDir"); - } + File outputDir = new File("foo").getAbsoluteFile(); + assertThat(this.resolver.resolveFile("bar", "baz.txt", + new RestDocumentationContext(null, null, outputDir)), is(new File( + outputDir, "bar/baz.txt"))); } @Test public void configuredOutputAndAbsoluteInput() { - String outputDir = new File("foo").getAbsolutePath(); + File outputDir = new File("foo").getAbsoluteFile(); String absolutePath = new File("bar").getAbsolutePath(); - System.setProperty("org.springframework.restdocs.outputDir", outputDir); - try { - assertThat(this.resolver.resolveFile(absolutePath, "baz.txt"), is(new File( - absolutePath, "baz.txt"))); - } - finally { - System.clearProperty("org.springframework.restdocs.outputDir"); - } + assertThat(this.resolver.resolveFile(absolutePath, "baz.txt", + new RestDocumentationContext(null, null, outputDir)), is(new File( + absolutePath, "baz.txt"))); } } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java similarity index 77% rename from spring-restdocs/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java index c3b72eb3..9786aa20 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/ExpectedSnippet.java @@ -43,37 +43,13 @@ public class ExpectedSnippet implements TestRule { private SnippetMatcher snippet = SnippetMatchers.snippet(); - private File outputDir; + private File outputDirectory; @Override public Statement apply(final Statement base, Description description) { - this.outputDir = new File("build/" + description.getTestClass().getSimpleName()); - return new OutputDirectoryStatement(new ExpectedSnippetStatement(base), - this.outputDir); - } - - private static final class OutputDirectoryStatement extends Statement { - - private final Statement delegate; - - private final File outputDir; - - public OutputDirectoryStatement(Statement delegate, File outputDir) { - this.delegate = delegate; - this.outputDir = outputDir; - } - - @Override - public void evaluate() throws Throwable { - System.setProperty("org.springframework.restdocs.outputDir", - this.outputDir.getAbsolutePath()); - try { - this.delegate.evaluate(); - } - finally { - System.clearProperty("org.springframework.restdocs.outputDir"); - } - } + this.outputDirectory = new File("build/" + + description.getTestClass().getSimpleName()); + return new ExpectedSnippetStatement(base); } private final class ExpectedSnippetStatement extends Statement { @@ -93,8 +69,8 @@ public class ExpectedSnippet implements TestRule { } private void verifySnippet() throws IOException { - if (this.outputDir != null && this.expectedName != null) { - File snippetDir = new File(this.outputDir, this.expectedName); + if (this.outputDirectory != null && this.expectedName != null) { + File snippetDir = new File(this.outputDirectory, this.expectedName); File snippetFile = new File(snippetDir, this.expectedType + ".adoc"); assertThat(snippetFile, is(this.snippet)); } @@ -150,4 +126,8 @@ public class ExpectedSnippet implements TestRule { this.snippet.withContents(matcher); } + public File getOutputDirectory() { + return this.outputDirectory; + } + } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/test/OperationBuilder.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java similarity index 86% rename from spring-restdocs/src/test/java/org/springframework/restdocs/test/OperationBuilder.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java index fbb19337..e28d5751 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/test/OperationBuilder.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java @@ -1,5 +1,22 @@ +/* + * Copyright 2014-2015 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.restdocs.test; +import java.io.File; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; @@ -9,8 +26,7 @@ import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; -import org.springframework.restdocs.config.RestDocumentationContext; -import org.springframework.restdocs.config.RestDocumentationContextPlaceholderResolver; +import org.springframework.restdocs.RestDocumentationContext; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; @@ -20,6 +36,7 @@ import org.springframework.restdocs.operation.StandardOperation; import org.springframework.restdocs.operation.StandardOperationRequest; import org.springframework.restdocs.operation.StandardOperationRequestPart; import org.springframework.restdocs.operation.StandardOperationResponse; +import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver; import org.springframework.restdocs.snippet.StandardWriterResolver; import org.springframework.restdocs.snippet.WriterResolver; import org.springframework.restdocs.templates.StandardTemplateResourceResolver; @@ -34,10 +51,13 @@ public class OperationBuilder { private final String name; + private final File outputDirectory; + private OperationRequestBuilder requestBuilder; - public OperationBuilder(String name) { + public OperationBuilder(String name, File outputDirectory) { this.name = name; + this.outputDirectory = outputDirectory; } public OperationRequestBuilder request(String uri) { @@ -59,7 +79,8 @@ public class OperationBuilder { this.attributes.put(TemplateEngine.class.getName(), new MustacheTemplateEngine(new StandardTemplateResourceResolver())); } - RestDocumentationContext context = new RestDocumentationContext(null); + RestDocumentationContext context = new RestDocumentationContext(null, null, + this.outputDirectory); this.attributes.put(RestDocumentationContext.class.getName(), context); this.attributes.put(WriterResolver.class.getName(), new StandardWriterResolver( new RestDocumentationContextPlaceholderResolver(context))); diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java similarity index 100% rename from spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java rename to spring-restdocs-core/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/curl-request-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/curl-request-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/curl-request-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/curl-request-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/http-request-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/http-request-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/http-request-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/http-request-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/http-response-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/http-response-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/http-response-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/http-response-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/links-with-extra-column.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/links-with-extra-column.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/links-with-extra-column.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/links-with-extra-column.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/links-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/links-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/links-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/links-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/path-parameters-with-extra-column.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/path-parameters-with-extra-column.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/path-parameters-with-extra-column.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/path-parameters-with-extra-column.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/path-parameters-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/path-parameters-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/path-parameters-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/path-parameters-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/request-fields-with-extra-column.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/request-fields-with-extra-column.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/request-fields-with-extra-column.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/request-fields-with-extra-column.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/request-fields-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/request-fields-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/request-fields-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/request-fields-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/request-parameters-with-extra-column.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/request-parameters-with-extra-column.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/request-parameters-with-extra-column.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/request-parameters-with-extra-column.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/request-parameters-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/request-parameters-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/request-parameters-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/request-parameters-with-title.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/response-fields-with-extra-column.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/response-fields-with-extra-column.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/response-fields-with-extra-column.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/response-fields-with-extra-column.snippet diff --git a/spring-restdocs/src/test/resources/custom-snippet-templates/response-fields-with-title.snippet b/spring-restdocs-core/src/test/resources/custom-snippet-templates/response-fields-with-title.snippet similarity index 100% rename from spring-restdocs/src/test/resources/custom-snippet-templates/response-fields-with-title.snippet rename to spring-restdocs-core/src/test/resources/custom-snippet-templates/response-fields-with-title.snippet diff --git a/spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-embedded-and-links.json b/spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-embedded-and-links.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-embedded-and-links.json rename to spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-embedded-and-links.json diff --git a/spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-embedded.json b/spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-embedded.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-embedded.json rename to spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-embedded.json diff --git a/spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-links.json b/spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-links.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/multiple-fields-and-links.json rename to spring-restdocs-core/src/test/resources/field-payloads/multiple-fields-and-links.json diff --git a/spring-restdocs/src/test/resources/field-payloads/multiple-fields.json b/spring-restdocs-core/src/test/resources/field-payloads/multiple-fields.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/multiple-fields.json rename to spring-restdocs-core/src/test/resources/field-payloads/multiple-fields.json diff --git a/spring-restdocs/src/test/resources/field-payloads/no-fields.json b/spring-restdocs-core/src/test/resources/field-payloads/no-fields.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/no-fields.json rename to spring-restdocs-core/src/test/resources/field-payloads/no-fields.json diff --git a/spring-restdocs/src/test/resources/field-payloads/single-field.json b/spring-restdocs-core/src/test/resources/field-payloads/single-field.json similarity index 100% rename from spring-restdocs/src/test/resources/field-payloads/single-field.json rename to spring-restdocs-core/src/test/resources/field-payloads/single-field.json diff --git a/spring-restdocs/src/test/resources/link-payloads/atom/multiple-links-different-rels.json b/spring-restdocs-core/src/test/resources/link-payloads/atom/multiple-links-different-rels.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/atom/multiple-links-different-rels.json rename to spring-restdocs-core/src/test/resources/link-payloads/atom/multiple-links-different-rels.json diff --git a/spring-restdocs/src/test/resources/link-payloads/atom/multiple-links-same-rels.json b/spring-restdocs-core/src/test/resources/link-payloads/atom/multiple-links-same-rels.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/atom/multiple-links-same-rels.json rename to spring-restdocs-core/src/test/resources/link-payloads/atom/multiple-links-same-rels.json diff --git a/spring-restdocs/src/test/resources/link-payloads/atom/no-links.json b/spring-restdocs-core/src/test/resources/link-payloads/atom/no-links.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/atom/no-links.json rename to spring-restdocs-core/src/test/resources/link-payloads/atom/no-links.json diff --git a/spring-restdocs/src/test/resources/link-payloads/atom/single-link.json b/spring-restdocs-core/src/test/resources/link-payloads/atom/single-link.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/atom/single-link.json rename to spring-restdocs-core/src/test/resources/link-payloads/atom/single-link.json diff --git a/spring-restdocs/src/test/resources/link-payloads/atom/wrong-format.json b/spring-restdocs-core/src/test/resources/link-payloads/atom/wrong-format.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/atom/wrong-format.json rename to spring-restdocs-core/src/test/resources/link-payloads/atom/wrong-format.json diff --git a/spring-restdocs/src/test/resources/link-payloads/hal/multiple-links-different-rels.json b/spring-restdocs-core/src/test/resources/link-payloads/hal/multiple-links-different-rels.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/hal/multiple-links-different-rels.json rename to spring-restdocs-core/src/test/resources/link-payloads/hal/multiple-links-different-rels.json diff --git a/spring-restdocs/src/test/resources/link-payloads/hal/multiple-links-same-rels.json b/spring-restdocs-core/src/test/resources/link-payloads/hal/multiple-links-same-rels.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/hal/multiple-links-same-rels.json rename to spring-restdocs-core/src/test/resources/link-payloads/hal/multiple-links-same-rels.json diff --git a/spring-restdocs/src/test/resources/link-payloads/hal/no-links.json b/spring-restdocs-core/src/test/resources/link-payloads/hal/no-links.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/hal/no-links.json rename to spring-restdocs-core/src/test/resources/link-payloads/hal/no-links.json diff --git a/spring-restdocs/src/test/resources/link-payloads/hal/single-link.json b/spring-restdocs-core/src/test/resources/link-payloads/hal/single-link.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/hal/single-link.json rename to spring-restdocs-core/src/test/resources/link-payloads/hal/single-link.json diff --git a/spring-restdocs/src/test/resources/link-payloads/hal/wrong-format.json b/spring-restdocs-core/src/test/resources/link-payloads/hal/wrong-format.json similarity index 100% rename from spring-restdocs/src/test/resources/link-payloads/hal/wrong-format.json rename to spring-restdocs-core/src/test/resources/link-payloads/hal/wrong-format.json diff --git a/spring-restdocs/src/test/resources/org/springframework/restdocs/constraints/TestConstraintDescriptions.properties b/spring-restdocs-core/src/test/resources/org/springframework/restdocs/constraints/TestConstraintDescriptions.properties similarity index 100% rename from spring-restdocs/src/test/resources/org/springframework/restdocs/constraints/TestConstraintDescriptions.properties rename to spring-restdocs-core/src/test/resources/org/springframework/restdocs/constraints/TestConstraintDescriptions.properties diff --git a/spring-restdocs-mockmvc/build.gradle b/spring-restdocs-mockmvc/build.gradle new file mode 100644 index 00000000..59774105 --- /dev/null +++ b/spring-restdocs-mockmvc/build.gradle @@ -0,0 +1,14 @@ +dependencies { + compile 'org.springframework:spring-test' + compile project(':spring-restdocs-core') + + testCompile 'org.mockito:mockito-core' + testCompile 'org.hamcrest:hamcrest-library' + testCompile 'org.springframework.hateoas:spring-hateoas' + testCompile project(path: ':spring-restdocs-core', configuration: 'testArtifacts') + testRuntime 'commons-logging:commons-logging:1.2' +} + +test { + jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=org.springframework.restdocs.*" +} \ No newline at end of file diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractConfigurer.java similarity index 95% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractConfigurer.java index f8453058..ebb0a4c4 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractConfigurer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.mockmvc; import org.springframework.mock.web.MockHttpServletRequest; diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractNestedConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractNestedConfigurer.java similarity index 97% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractNestedConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractNestedConfigurer.java index ce542926..57153d70 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/AbstractNestedConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/AbstractNestedConfigurer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.mockmvc; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationRequestFactory.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationRequestFactory.java similarity index 91% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationRequestFactory.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationRequestFactory.java index 60c82404..70d4351e 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationRequestFactory.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationRequestFactory.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.springframework.restdocs.operation; +package org.springframework.restdocs.mockmvc; -import static org.springframework.restdocs.util.IterableEnumeration.iterable; +import static org.springframework.restdocs.mockmvc.util.IterableEnumeration.iterable; import java.io.IOException; import java.io.PrintWriter; @@ -34,6 +34,11 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockMultipartHttpServletRequest; +import org.springframework.restdocs.operation.OperationRequest; +import org.springframework.restdocs.operation.OperationRequestPart; +import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.StandardOperationRequest; +import org.springframework.restdocs.operation.StandardOperationRequestPart; import org.springframework.util.FileCopyUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; @@ -45,7 +50,7 @@ import org.springframework.web.multipart.MultipartFile; * @author Andy Wilkinson * */ -public class MockMvcOperationRequestFactory { +class MockMvcOperationRequestFactory { private static final String SCHEME_HTTP = "http"; diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationResponseFactory.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationResponseFactory.java similarity index 88% rename from spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationResponseFactory.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationResponseFactory.java index 5ace0f06..0f6f6b8a 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/MockMvcOperationResponseFactory.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcOperationResponseFactory.java @@ -14,11 +14,13 @@ * limitations under the License. */ -package org.springframework.restdocs.operation; +package org.springframework.restdocs.mockmvc; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.restdocs.operation.OperationResponse; +import org.springframework.restdocs.operation.StandardOperationResponse; /** * A factory for creating an {@link OperationResponse} derived from a @@ -26,7 +28,7 @@ import org.springframework.mock.web.MockHttpServletResponse; * * @author Andy Wilkinson */ -public class MockMvcOperationResponseFactory { +class MockMvcOperationResponseFactory { /** * Create a new {@code OperationResponse} derived from the given {@code mockResponse}. diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentation.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentation.java similarity index 90% rename from spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentation.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentation.java index 952ca423..2a2cc2d6 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentation.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentation.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.springframework.restdocs; +package org.springframework.restdocs.mockmvc; -import org.springframework.restdocs.config.RestDocumentationConfigurer; +import org.springframework.restdocs.RestDocumentation; import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor; import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor; import org.springframework.restdocs.snippet.Snippet; @@ -30,21 +30,23 @@ import org.springframework.test.web.servlet.setup.MockMvcConfigurer; * * @author Andy Wilkinson */ -public abstract class RestDocumentation { +public abstract class MockMvcRestDocumentation { - private RestDocumentation() { + private MockMvcRestDocumentation() { } /** - * Provides access to a {@link MockMvcConfigurer} that can be used to configure the - * REST documentation when building a {@link MockMvc} instance. + * Provides access to a {@link MockMvcConfigurer} that can be used to configure a + * {@link MockMvc} instance using the given {@code restDocumentation}. * - * @see ConfigurableMockMvcBuilder#apply(MockMvcConfigurer) + * @param restDocumentation the REST documentation * @return the configurer + * @see ConfigurableMockMvcBuilder#apply(MockMvcConfigurer) */ - public static RestDocumentationConfigurer documentationConfiguration() { - return new RestDocumentationConfigurer(); + public static RestDocumentationMockMvcConfigurer documentationConfiguration( + RestDocumentation restDocumentation) { + return new RestDocumentationMockMvcConfigurer(restDocumentation); } /** diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/NestedConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/NestedConfigurer.java similarity index 86% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/NestedConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/NestedConfigurer.java index 25c57767..2fc73bb3 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/NestedConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/NestedConfigurer.java @@ -14,17 +14,17 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.mockmvc; import org.springframework.test.web.servlet.setup.MockMvcConfigurer; /** * A configurer that is nested and, therefore, has a parent. * - * @author awilkinson + * @author Andy Wilkinson * @param The parent's type */ -public interface NestedConfigurer { +interface NestedConfigurer { /** * Returns the configurer's parent diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationMockMvcConfigurer.java similarity index 79% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationMockMvcConfigurer.java index 2f9417b9..e13025eb 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationMockMvcConfigurer.java @@ -14,13 +14,12 @@ * limitations under the License. */ -package org.springframework.restdocs.config; - -import java.util.Arrays; -import java.util.List; +package org.springframework.restdocs.mockmvc; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.restdocs.RestDocumentation; +import org.springframework.restdocs.RestDocumentationContext; +import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver; import org.springframework.restdocs.snippet.StandardWriterResolver; import org.springframework.restdocs.snippet.WriterResolver; import org.springframework.restdocs.templates.StandardTemplateResourceResolver; @@ -39,9 +38,9 @@ import org.springframework.web.context.WebApplicationContext; * @author Andy Wilkinson * @author Dmitriy Mayboroda * @see ConfigurableMockMvcBuilder#apply(MockMvcConfigurer) - * @see RestDocumentation#documentationConfiguration() + * @see MockMvcRestDocumentation#documentationConfiguration(RestDocumentation) */ -public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { +public class RestDocumentationMockMvcConfigurer extends MockMvcConfigurerAdapter { private final UriConfigurer uriConfigurer = new UriConfigurer(this); @@ -54,15 +53,17 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { private final WriterResolverConfigurer writerResolverConfigurer = new WriterResolverConfigurer(); /** - * Creates a new {@link RestDocumentationConfigurer}. - * @see RestDocumentation#documentationConfiguration() + * Creates a new {code RestDocumentationMockMvcConfigurer} that will use the given + * {@code restDocumentation} when configuring MockMvc. + * + * @param restDocumentation the rest documentation + * @see MockMvcRestDocumentation#documentationConfiguration(RestDocumentation) */ - public RestDocumentationConfigurer() { + RestDocumentationMockMvcConfigurer(RestDocumentation restDocumentation) { this.requestPostProcessor = new ConfigurerApplyingRequestPostProcessor( - Arrays. asList(this.uriConfigurer, - this.writerResolverConfigurer, this.snippetConfigurer, - new StepCountConfigurer(), new ContentLengthHeaderConfigurer(), - this.templateEngineConfigurer)); + restDocumentation, this.uriConfigurer, this.writerResolverConfigurer, + this.snippetConfigurer, new ContentLengthHeaderConfigurer(), + this.templateEngineConfigurer); } /** @@ -91,7 +92,7 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { * @param templateEngine the template engine to use * @return {@code this} */ - public RestDocumentationConfigurer templateEngine(TemplateEngine templateEngine) { + public RestDocumentationMockMvcConfigurer templateEngine(TemplateEngine templateEngine) { this.templateEngineConfigurer.setTemplateEngine(templateEngine); return this; } @@ -103,7 +104,7 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { * @param writerResolver The writer resolver to use * @return {@code this} */ - public RestDocumentationConfigurer writerResolver(WriterResolver writerResolver) { + public RestDocumentationMockMvcConfigurer writerResolver(WriterResolver writerResolver) { this.writerResolverConfigurer.setWriterResolver(writerResolver); return this; } @@ -114,19 +115,6 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { return this.requestPostProcessor; } - private static class StepCountConfigurer extends AbstractConfigurer { - - @Override - void apply(MockHttpServletRequest request) { - RestDocumentationContext context = (RestDocumentationContext) request - .getAttribute(RestDocumentationContext.class.getName()); - if (context != null) { - context.getAndIncrementStepCount(); - } - } - - } - private static class ContentLengthHeaderConfigurer extends AbstractConfigurer { @Override @@ -182,17 +170,20 @@ public class RestDocumentationConfigurer extends MockMvcConfigurerAdapter { private static class ConfigurerApplyingRequestPostProcessor implements RequestPostProcessor { - private final List configurers; + private final RestDocumentation restDocumentation; + + private final AbstractConfigurer[] configurers; private ConfigurerApplyingRequestPostProcessor( - List configurers) { + RestDocumentation restDocumentation, AbstractConfigurer... configurers) { + this.restDocumentation = restDocumentation; this.configurers = configurers; } @Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.setAttribute(RestDocumentationContext.class.getName(), - RestDocumentationContextHolder.getCurrentContext()); + this.restDocumentation.beforeOperation()); for (AbstractConfigurer configurer : this.configurers) { configurer.apply(request); } diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationRequestBuilders.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationRequestBuilders.java similarity index 99% rename from spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationRequestBuilders.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationRequestBuilders.java index 160006d4..dc6f7a25 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationRequestBuilders.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationRequestBuilders.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs; +package org.springframework.restdocs.mockmvc; import java.net.URI; diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationResultHandler.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationResultHandler.java similarity index 93% rename from spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationResultHandler.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationResultHandler.java index e1fc99f1..2a7e4ab4 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/RestDocumentationResultHandler.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/RestDocumentationResultHandler.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.springframework.restdocs; +package org.springframework.restdocs.mockmvc; -import static org.springframework.restdocs.util.IterableEnumeration.iterable; +import static org.springframework.restdocs.mockmvc.util.IterableEnumeration.iterable; import java.util.ArrayList; import java.util.Arrays; @@ -24,8 +24,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.springframework.restdocs.operation.MockMvcOperationRequestFactory; -import org.springframework.restdocs.operation.MockMvcOperationResponseFactory; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationResponse; @@ -42,7 +40,7 @@ import org.springframework.util.Assert; * * @author Andy Wilkinson * @author Andreas Evers - * @see RestDocumentation#document(String, Snippet...) + * @see MockMvcRestDocumentation#document(String, Snippet...) */ public class RestDocumentationResultHandler implements ResultHandler { diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/SnippetConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/SnippetConfigurer.java similarity index 93% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/SnippetConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/SnippetConfigurer.java index 992086f7..ace722ea 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/SnippetConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/SnippetConfigurer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.mockmvc; import java.util.Arrays; import java.util.List; @@ -29,10 +29,9 @@ import org.springframework.restdocs.snippet.WriterResolver; * A configurer that can be used to configure the generated documentation snippets. * * @author Andy Wilkinson - * */ public class SnippetConfigurer extends - AbstractNestedConfigurer { + AbstractNestedConfigurer { private List defaultSnippets = Arrays.asList( CurlDocumentation.curlRequest(), HttpDocumentation.httpRequest(), @@ -46,7 +45,7 @@ public class SnippetConfigurer extends private String snippetEncoding = DEFAULT_SNIPPET_ENCODING; - SnippetConfigurer(RestDocumentationConfigurer parent) { + SnippetConfigurer(RestDocumentationMockMvcConfigurer parent) { super(parent); } diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/UriConfigurer.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/UriConfigurer.java similarity index 93% rename from spring-restdocs/src/main/java/org/springframework/restdocs/config/UriConfigurer.java rename to spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/UriConfigurer.java index 30baa03c..c931ea96 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/UriConfigurer.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/UriConfigurer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.config; +package org.springframework.restdocs.mockmvc; import org.springframework.mock.web.MockHttpServletRequest; @@ -23,7 +23,7 @@ import org.springframework.mock.web.MockHttpServletRequest; * * @author Andy Wilkinson */ -public class UriConfigurer extends AbstractNestedConfigurer { +public class UriConfigurer extends AbstractNestedConfigurer { /** * The default scheme for documented URIs @@ -49,7 +49,7 @@ public class UriConfigurer extends AbstractNestedConfigurer - repackJar.baseName = "restdocs-jmustache-repack" - repackJar.version = dependencyManagement.managedVersions['com.samskivert:jmustache'] - - doLast() { - project.ant { - taskdef name: "jarjar", classname: "com.tonicsystems.jarjar.JarJarTask", - classpath: configurations.jarjar.asPath - jarjar(destfile: repackJar.archivePath) { - configurations.jmustache.each { originalJar -> - zipfileset(src: originalJar, includes: '**/*.class') - } - rule(pattern: 'com.samskivert.**', result: 'org.springframework.restdocs.@1') - } - } - } -} - -dependencies { - compile 'com.fasterxml.jackson.core:jackson-databind' - compile 'junit:junit' - compile 'org.springframework:spring-core' - compile 'org.springframework:spring-test' - compile 'org.springframework:spring-web' - compile 'org.springframework:spring-webmvc' - compile 'javax.servlet:javax.servlet-api' - compile files(jmustacheRepackJar) - jacoco 'org.jacoco:org.jacoco.agent::runtime' - jarjar 'com.googlecode.jarjar:jarjar:1.3' - jmustache 'com.samskivert:jmustache@jar' - optional 'javax.validation:validation-api' - testCompile 'org.springframework.hateoas:spring-hateoas' - testCompile 'org.mockito:mockito-core' - testCompile 'org.hamcrest:hamcrest-core' - testCompile 'org.hamcrest:hamcrest-library' - testCompile 'org.hibernate:hibernate-validator' - testRuntime 'org.glassfish:javax.el:3.0.0' -} - -jar { - dependsOn jmustacheRepackJar - from(zipTree(jmustacheRepackJar.archivePath)) { - include "org/springframework/restdocs/**" - } -} - -task sourcesJar(type: Jar) { - classifier = 'sources' - from project.sourceSets.main.allSource -} - -javadoc { - description = "Generates project-level javadoc for use in -javadoc jar" - options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED - options.author = true - options.header = "Spring REST Docs $version" - options.docTitle = "${options.header} API" - options.links = [ - 'http://docs.oracle.com/javase/8/docs/api/', - "http://docs.spring.io/spring-framework/docs/${dependencyManagement.managedVersions['org.springframework:spring-core']}/javadoc-api/", - 'https://docs.jboss.org/hibernate/stable/beanvalidation/api/' - ] as String[] - options.addStringOption '-quiet' -} - -task javadocJar(type: Jar) { - classifier = "javadoc" - from javadoc -} - -artifacts { - archives sourcesJar - archives javadocJar -} - -test { - jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=org.springframework.restdocs.*,excludes=org.springframework.restdocs.mustache.*" - testLogging { - exceptionFormat "full" - } -} \ No newline at end of file diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContext.java b/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContext.java deleted file mode 100644 index 2a95a7f1..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationContext.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2014-2015 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.restdocs.config; - -import java.lang.reflect.Method; -import java.util.concurrent.atomic.AtomicInteger; - -import org.springframework.test.context.TestContext; - -/** - * {@code RestDocumentationContext} encapsulates the context in which the documentation of - * a RESTful API is being performed. - * - * @author Andy Wilkinson - */ -public final class RestDocumentationContext { - - private final AtomicInteger stepCount = new AtomicInteger(0); - - private final TestContext testContext; - - /** - * Creates a new {@code RestDocumentationContext} backed by the given - * {@code testContext}. - * - * @param testContext the test context - */ - public RestDocumentationContext(TestContext testContext) { - this.testContext = testContext; - } - - /** - * Returns the test {@link Method method} that is currently executing - * - * @return The test method - */ - public Method getTestMethod() { - return this.testContext == null ? null : this.testContext.getTestMethod(); - } - - /** - * Gets and then increments the current step count - * - * @return The step count prior to it being incremented - */ - int getAndIncrementStepCount() { - return this.stepCount.getAndIncrement(); - } - - /** - * Gets the current step count - * - * @return The current step count - */ - public int getStepCount() { - return this.stepCount.get(); - } - -} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationTestExecutionListener.java b/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationTestExecutionListener.java deleted file mode 100644 index 7189ad0a..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/config/RestDocumentationTestExecutionListener.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2014-2015 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.restdocs.config; - -import org.springframework.test.context.TestContext; -import org.springframework.test.context.TestExecutionListener; -import org.springframework.test.context.support.AbstractTestExecutionListener; - -/** - * A {@link TestExecutionListener} that sets up and tears down the Spring REST Docs - * context for each test method - * - * @author Andy Wilkinson - */ -public class RestDocumentationTestExecutionListener extends AbstractTestExecutionListener { - - @Override - public void beforeTestMethod(TestContext testContext) throws Exception { - RestDocumentationContextHolder.setCurrentContext(new RestDocumentationContext( - testContext)); - } - - @Override - public void afterTestMethod(TestContext testContext) throws Exception { - RestDocumentationContextHolder.removeCurrentContext(); - } -} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java b/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java deleted file mode 100644 index a8ea46fc..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/operation/preprocess/OperationResponsePreprocessor.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.springframework.restdocs.operation.preprocess; - -import org.springframework.restdocs.operation.OperationResponse; - -/** - * An {@code OperationRequestPreprocessor} is used to modify an {@code OperationRequest} - * prior to it being documented. - * - * @author Andy Wilkinson - */ -public interface OperationResponsePreprocessor { - - /** - * Processes and potentially modifies the given {@code response} before it is - * documented. - * - * @param response the response - * @return the modified response - */ - OperationResponse preprocess(OperationResponse response); - -} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java b/spring-restdocs/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java deleted file mode 100644 index a569b39b..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/payload/PayloadHandlingException.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.springframework.restdocs.payload; - -/** - * Thrown to indicate that a failure has occurred during payload handling - * - * @author Andy Wilkinson - * - */ -@SuppressWarnings("serial") -class PayloadHandlingException extends RuntimeException { - - /** - * Creates a new {@code PayloadHandlingException} with the given cause - * @param cause the cause of the failure - */ - PayloadHandlingException(Throwable cause) { - super(cause); - } - -} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/DocumentationProperties.java b/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/DocumentationProperties.java deleted file mode 100644 index 3241ce04..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/snippet/DocumentationProperties.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2014-2015 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.restdocs.snippet; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -import org.springframework.util.StringUtils; - -class DocumentationProperties { - - private final Properties properties = new Properties(); - - DocumentationProperties() { - try (InputStream stream = getClass().getClassLoader().getResourceAsStream( - "documentation.properties")) { - if (stream != null) { - this.properties.load(stream); - } - } - catch (IOException ex) { - throw new IllegalStateException("Failed to read documentation.properties", ex); - } - - this.properties.putAll(System.getProperties()); - } - - File getOutputDir() { - String outputDir = this.properties - .getProperty("org.springframework.restdocs.outputDir"); - if (StringUtils.hasText(outputDir)) { - return new File(outputDir).getAbsoluteFile(); - } - return null; - } -} diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/Template.java b/spring-restdocs/src/main/java/org/springframework/restdocs/templates/Template.java deleted file mode 100644 index 302e5f6c..00000000 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/templates/Template.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.springframework.restdocs.templates; - -import java.util.Map; - -/** - * A compiled {@code Template} that can be rendered to a {@link String}. - * - * @author Andy Wilkinson - * - */ -public interface Template { - - /** - * Renders the template to a {@link String} using the given {@code context} for - * variable/property resolution. - * - * @param context The context to use - * @return The rendered template - */ - String render(Map context); - -} diff --git a/spring-restdocs/src/main/resources/META-INF/spring.factories b/spring-restdocs/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 64519d78..00000000 --- a/spring-restdocs/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1 +0,0 @@ -org.springframework.test.context.TestExecutionListener=org.springframework.restdocs.config.RestDocumentationTestExecutionListener \ No newline at end of file diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationContexts.java b/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationContexts.java deleted file mode 100644 index 629d6908..00000000 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/config/RestDocumentationContexts.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.springframework.restdocs.config; - - -public class RestDocumentationContexts { - -} diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java deleted file mode 100644 index 03b21641..00000000 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.springframework.restdocs.snippet; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Method; - -import org.junit.Test; -import org.springframework.restdocs.config.RestDocumentationContext; -import org.springframework.restdocs.config.RestDocumentationContextPlaceholderResolver; -import org.springframework.test.context.TestContext; -import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; - -/** - * Tests for {@link RestDocumentationContextPlaceholderResolver} - * - * @author Andy Wilkinson - * - */ -public class RestDocumentationContextPlaceholderResolverTests { - - private final TestContext testContext = mock(TestContext.class); - - private final RestDocumentationContext context = new RestDocumentationContext( - this.testContext); - - private final PlaceholderResolver resolver = new RestDocumentationContextPlaceholderResolver( - this.context); - - @Test - public void dashSeparatedMethodName() throws Exception { - when(this.testContext.getTestMethod()).thenReturn( - getClass().getMethod("dashSeparatedMethodName")); - assertThat(this.resolver.resolvePlaceholder("method-name"), - equalTo("dash-separated-method-name")); - } - - @Test - public void underscoreSeparatedMethodName() throws Exception { - when(this.testContext.getTestMethod()).thenReturn( - getClass().getMethod("underscoreSeparatedMethodName")); - assertThat(this.resolver.resolvePlaceholder("method_name"), - equalTo("underscore_separated_method_name")); - } - - @Test - public void camelCaseMethodName() throws Exception { - Method method = getClass().getMethod("camelCaseMethodName"); - when(this.testContext.getTestMethod()).thenReturn(method); - assertThat(this.resolver.resolvePlaceholder("methodName"), - equalTo("camelCaseMethodName")); - } - - @Test - public void stepCount() throws Exception { - assertThat(this.resolver.resolvePlaceholder("step"), equalTo("0")); - } - -}