Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to specify priority and allow for template override #1882

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ protected Map<String, Object> createModel(Operation operation) {
@Override
public void document(Operation operation) throws IOException {
TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes().get(TemplateEngine.class.getName());
String renderedContract = templateEngine.compileTemplate("default-dsl-contract-only")
String renderedContract = templateEngine.compileTemplate(getTemplate())
.render(createModelForContract(operation));
this.model.put("contract", renderedContract);
storeDslContract(operation, renderedContract);
super.document(operation);
}

protected String getTemplate() {
return "default-dsl-contract-only";
}

private void insertResponseModel(Operation operation, Map<String, Object> model) {
OperationResponse response = operation.getResponse();
model.put("response_status", response.getStatus().value());
Expand Down Expand Up @@ -152,9 +156,18 @@ private Map<String, Object> createModelForContract(Operation operation) {
Map<String, Object> modelForContract = new HashMap<>();
insertRequestModel(operation, modelForContract);
insertResponseModel(operation, modelForContract);
insertAdditionalModel(operation, modelForContract);
return modelForContract;
}

protected void insertAdditionalModel(Operation operation, Map<String, Object> modelForContract) {
boolean hasPriority = getAttributes().containsKey("priority");
modelForContract.put("priority_present", hasPriority);
if (hasPriority) {
modelForContract.put("priority", getAttributes().get("priority"));
}
}

private void storeDslContract(Operation operation, String content) throws IOException {
RestDocumentationContext context = (RestDocumentationContext) operation.getAttributes()
.get(RestDocumentationContext.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ Contract.make {
}
{{/response_headers_present}}
}
{{#priority_present}}
priority {{priority}}
{{/priority_present}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Set;

import org.apache.groovy.util.Maps;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -94,7 +95,7 @@ public void should_create_contract_template_and_doc() throws Exception {
.jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]")
.contentType(MediaType.valueOf("application/json")))
// then Contract DSL documentation
.andDo(document("index", SpringCloudContractRestDocs.dslContract()));
.andDo(document("index", SpringCloudContractRestDocs.dslContract(Maps.of("priority", 1))));
// end::contract_snippet[]

then(file("/contracts/index.groovy")).exists();
Expand All @@ -116,6 +117,7 @@ public void should_create_contract_template_and_doc() throws Exception {
then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
then(parsedContract.getResponse().getHeaders().getEntries()).isNotEmpty();
then(parsedContract.getResponse().getBody().getClientValue()).isNotNull();
then(parsedContract.getPriority().intValue()).isEqualTo(1);
}

@Test
Expand Down