Merge pull request #15429 from dreis2211

* pr/15429:
  Drop ContentContainingCondition in favor of Assertions.contentOf
pull/15543/head
Stephane Nicoll 6 years ago
commit b8c82ec425

@ -1,54 +0,0 @@
/*
* Copyright 2012-2017 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.boot.test.autoconfigure.restdocs;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import org.assertj.core.api.Condition;
import org.assertj.core.description.TextDescription;
import org.springframework.util.FileCopyUtils;
/**
* A {@link Condition} to assert that a file's contents contain a given string.
*
* @author Andy Wilkinson
*/
class ContentContainingCondition extends Condition<File> {
private final String toContain;
ContentContainingCondition(String toContain) {
super(new TextDescription("content containing %s", toContain));
this.toContain = toContain;
}
@Override
public boolean matches(File value) {
try (Reader reader = new FileReader(value)) {
String content = FileCopyUtils.copyToString(reader);
return content.contains(this.toContain);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}

@ -18,7 +18,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -37,6 +36,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
@ -77,16 +77,12 @@ public class MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTes
linkWithRel("self").description("Canonical location of this resource")))); linkWithRel("self").description("Canonical location of this resource"))));
File defaultSnippetsDir = new File(this.generatedSnippets, "snippet-generation"); File defaultSnippetsDir = new File(this.generatedSnippets, "snippet-generation");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.md")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.md")))
.has(contentContaining("'http://localhost:8080/'")); .contains("'http://localhost:8080/'");
assertThat(new File(defaultSnippetsDir, "links.md")).isFile(); assertThat(new File(defaultSnippetsDir, "links.md")).isFile();
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile(); assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
@TestConfiguration @TestConfiguration
public static class CustomizationConfiguration { public static class CustomizationConfiguration {

@ -18,7 +18,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -31,6 +30,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -61,15 +61,11 @@ public class MockMvcRestDocsAutoConfigurationIntegrationTests {
this.mvc.perform(get("/")).andDo(document("default-snippets")); this.mvc.perform(get("/")).andDo(document("default-snippets"));
File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets"); File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.adoc")))
.has(contentContaining("'https://api.example.com/'")); .contains("'https://api.example.com/'");
assertThat(new File(defaultSnippetsDir, "http-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "http-request.adoc")))
.has(contentContaining("api.example.com")); .contains("api.example.com");
assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile(); assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
} }

@ -19,7 +19,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import io.restassured.specification.RequestSpecification; import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -39,6 +38,7 @@ import org.springframework.util.FileSystemUtils;
import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris; import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
@ -81,18 +81,14 @@ public class RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegratio
.when().port(this.port).get("/").then().assertThat().statusCode(is(200)); .when().port(this.port).get("/").then().assertThat().statusCode(is(200));
File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets"); File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.md")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.md")))
.has(contentContaining("'https://api.example.com/'")); .contains("'https://api.example.com/'");
assertThat(new File(defaultSnippetsDir, "http-request.md")) assertThat(contentOf(new File(defaultSnippetsDir, "http-request.md")))
.has(contentContaining("api.example.com")); .contains("api.example.com");
assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile(); assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile();
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile(); assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
@TestConfiguration @TestConfiguration
public static class CustomizationConfiguration { public static class CustomizationConfiguration {

@ -19,7 +19,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import io.restassured.specification.RequestSpecification; import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -34,6 +33,7 @@ import org.springframework.util.FileSystemUtils;
import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris; import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
@ -73,15 +73,11 @@ public class RestAssuredRestDocsAutoConfigurationIntegrationTests {
.when().port(this.port).get("/").then().assertThat().statusCode(is(200)); .when().port(this.port).get("/").then().assertThat().statusCode(is(200));
File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets"); File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.adoc")))
.has(contentContaining("'https://api.example.com/'")); .contains("'https://api.example.com/'");
assertThat(new File(defaultSnippetsDir, "http-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "http-request.adoc")))
.has(contentContaining("api.example.com")); .contains("api.example.com");
assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile(); assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
} }

@ -18,7 +18,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -37,6 +36,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document; import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
@ -70,18 +70,14 @@ public class WebTestClientRestDocsAutoConfigurationAdvancedConfigurationIntegrat
.expectBody().consumeWith(document("default-snippets")); .expectBody().consumeWith(document("default-snippets"));
File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets"); File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.md")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.md")))
.has(contentContaining("'https://api.example.com/'")); .contains("'https://api.example.com/'");
assertThat(new File(defaultSnippetsDir, "http-request.md")) assertThat(contentOf(new File(defaultSnippetsDir, "http-request.md")))
.has(contentContaining("api.example.com")); .contains("api.example.com");
assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile(); assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile();
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile(); assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
@TestConfiguration @TestConfiguration
public static class CustomizationConfiguration { public static class CustomizationConfiguration {

@ -18,7 +18,6 @@ package org.springframework.boot.test.autoconfigure.restdocs;
import java.io.File; import java.io.File;
import org.assertj.core.api.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -32,6 +31,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document; import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
/** /**
@ -63,15 +63,11 @@ public class WebTestClientRestDocsAutoConfigurationIntegrationTests {
.expectBody().consumeWith(document("default-snippets")); .expectBody().consumeWith(document("default-snippets"));
File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets"); File defaultSnippetsDir = new File(this.generatedSnippets, "default-snippets");
assertThat(defaultSnippetsDir).exists(); assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "curl-request.adoc")))
.has(contentContaining("'https://api.example.com/'")); .contains("'https://api.example.com/'");
assertThat(new File(defaultSnippetsDir, "http-request.adoc")) assertThat(contentOf(new File(defaultSnippetsDir, "http-request.adoc")))
.has(contentContaining("api.example.com")); .contains("api.example.com");
assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile(); assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile();
} }
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
} }

Loading…
Cancel
Save