From 90c3549f554a544926460e1cd9c5eb938050233c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 7 Nov 2017 17:52:02 +0000 Subject: [PATCH] Refine ordering of default, operation, and additional snippets Previously, the ordering was: 1. Operation snippets 2. Default snippets 3. Additional snippets This meant that any operation snippets (those specifically configured for the documentation of the particular operation) would be overwritten by the output of the default snippets. This commit updates the ordering to the following: 1. Default snippets 2. Operation snippets 3. Additional snippets Closes gh-453 --- .../generate/RestDocumentationGenerator.java | 5 +-- .../RestDocumentationGeneratorTests.java | 33 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/generate/RestDocumentationGenerator.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/generate/RestDocumentationGenerator.java index c169b9d8..53ca064d 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/generate/RestDocumentationGenerator.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/generate/RestDocumentationGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-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. @@ -229,12 +229,13 @@ public final class RestDocumentationGenerator { @SuppressWarnings("unchecked") private List getSnippets(Map configuration) { - List combinedSnippets = new ArrayList<>(this.snippets); + List combinedSnippets = new ArrayList<>(); List defaultSnippets = (List) configuration .get(ATTRIBUTE_NAME_DEFAULT_SNIPPETS); if (defaultSnippets != null) { combinedSnippets.addAll(defaultSnippets); } + combinedSnippets.addAll(this.snippets); combinedSnippets.addAll(this.additionalSnippets); this.additionalSnippets.clear(); return combinedSnippets; diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java index f1071215..be48e6d9 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-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. @@ -24,6 +24,7 @@ import java.util.Map; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; import org.mockito.Mockito; import org.springframework.http.HttpHeaders; @@ -102,9 +103,10 @@ public class RestDocumentationGeneratorTests { new RestDocumentationGenerator<>("id", this.requestConverter, this.responseConverter, this.snippet).handle(this.request, this.response, configuration); - verifySnippetInvocation(this.snippet, configuration); - verifySnippetInvocation(defaultSnippet1, configuration); - verifySnippetInvocation(defaultSnippet2, configuration); + InOrder inOrder = Mockito.inOrder(defaultSnippet1, defaultSnippet2, this.snippet); + verifySnippetInvocation(inOrder, defaultSnippet1, configuration); + verifySnippetInvocation(inOrder, defaultSnippet2, configuration); + verifySnippetInvocation(inOrder, this.snippet, configuration); } @Test @@ -122,9 +124,13 @@ public class RestDocumentationGeneratorTests { HashMap configuration = new HashMap<>(); generator.handle(this.request, this.response, configuration); generator.handle(this.request, this.response, configuration); - verifySnippetInvocation(this.snippet, configuration, 2); - verifySnippetInvocation(additionalSnippet1, configuration); - verifySnippetInvocation(additionalSnippet2, configuration); + InOrder inOrder = Mockito.inOrder(this.snippet, additionalSnippet1, + additionalSnippet2); + verifySnippetInvocation(inOrder, this.snippet, configuration); + verifySnippetInvocation(inOrder, additionalSnippet1, configuration); + verifySnippetInvocation(inOrder, additionalSnippet2, configuration); + verifySnippetInvocation(inOrder, this.snippet, configuration); + verifyNoMoreInteractions(this.snippet, additionalSnippet1, additionalSnippet2); } @Test @@ -156,13 +162,18 @@ public class RestDocumentationGeneratorTests { private void verifySnippetInvocation(Snippet snippet, Map attributes) throws IOException { - verifySnippetInvocation(snippet, attributes, 1); + ArgumentCaptor operation = ArgumentCaptor.forClass(Operation.class); + verify(snippet).document(operation.capture()); + assertThat(this.operationRequest, is(equalTo(operation.getValue().getRequest()))); + assertThat(this.operationResponse, + is(equalTo(operation.getValue().getResponse()))); + assertThat(attributes, is(equalTo(operation.getValue().getAttributes()))); } - private void verifySnippetInvocation(Snippet snippet, Map attributes, - int times) throws IOException { + private void verifySnippetInvocation(InOrder inOrder, Snippet snippet, + Map attributes) throws IOException { ArgumentCaptor operation = ArgumentCaptor.forClass(Operation.class); - verify(snippet, Mockito.times(times)).document(operation.capture()); + inOrder.verify(snippet).document(operation.capture()); assertThat(this.operationRequest, is(equalTo(operation.getValue().getRequest()))); assertThat(this.operationResponse, is(equalTo(operation.getValue().getResponse())));