org.springframework.restdocs.RestDocumentationContext Java Examples

The following examples show how to use org.springframework.restdocs.RestDocumentationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RamlResourceSnippet.java    From restdocs-raml with MIT License 6 votes vote down vote up
private File getOutputFile(Operation operation, String filename) {
    Object context = operation.getAttributes().get(RestDocumentationContext.class.getName());
    try {
        //use reflection here because of binary incompatibility between spring-restdocs 1 and 2
        //RestDocumentationContext changed from a class to an interface
        //if our code should work against both versions we need to avoid compiling against a version directly
        //see https://github.com/ePages-de/restdocs-raml/issues/7
        //we can remove the use of reflection when we drop support for spring-restdocs 1
        Method getOutputDirectory = context.getClass().getDeclaredMethod("getOutputDirectory");
        getOutputDirectory.setAccessible(true);
        File outputFile = (File) getOutputDirectory.invoke(context);
        return new File(outputFile, operation.getName() + "/" + filename);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        return null;
    }
}
 
Example #2
Source File: OperationBuilder.java    From restdocs-raml with MIT License 6 votes vote down vote up
public Operation build() {
	if (this.attributes.get(TemplateEngine.class.getName()) == null) {
		Map<String, Object> templateContext = new HashMap<>();
		templateContext.put("tableCellContent",
				new AsciidoctorTableCellContentLambda());
		this.attributes.put(TemplateEngine.class.getName(),
				new MustacheTemplateEngine(
						new StandardTemplateResourceResolver(this.templateFormat),
						Mustache.compiler().escapeHTML(false), templateContext));
	}
	RestDocumentationContext context = createContext();
	this.attributes.put(RestDocumentationContext.class.getName(), context);
	this.attributes.put(WriterResolver.class.getName(),
			new StandardWriterResolver(
					new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8",
					this.templateFormat));
	return new StandardOperation(this.name,
			(this.requestBuilder == null
					? new OperationRequestBuilder("http://localhost/").buildRequest()
					: this.requestBuilder.buildRequest()),
			this.responseBuilder == null
					? new OperationResponseBuilder().buildResponse()
					: this.responseBuilder.buildResponse(),
			this.attributes);
}
 
Example #3
Source File: OperationBuilder.java    From restdocs-wiremock with Apache License 2.0 6 votes vote down vote up
public Operation build() {
	if (this.attributes.get(TemplateEngine.class.getName()) == null) {
		Map<String, Object> templateContext = new HashMap<>();
		templateContext.put("tableCellContent",
				new AsciidoctorTableCellContentLambda());
		this.attributes.put(TemplateEngine.class.getName(),
				new MustacheTemplateEngine(
						new StandardTemplateResourceResolver(this.templateFormat),
						Mustache.compiler().escapeHTML(false), templateContext));
	}
	RestDocumentationContext context = createContext();
	this.attributes.put(RestDocumentationContext.class.getName(), context);
	this.attributes.put(WriterResolver.class.getName(),
			new StandardWriterResolver(
					new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8",
					this.templateFormat));
	return new StandardOperation(this.name,
			(this.requestBuilder == null
					? new OperationRequestBuilder("http://localhost/").buildRequest()
					: this.requestBuilder.buildRequest()),
			this.responseBuilder.buildResponse(), this.attributes);
}
 
Example #4
Source File: RamlResourceSnippet.java    From restdocs-raml with MIT License 5 votes vote down vote up
private void documentSnippet(Operation operation) throws IOException {

        WriterResolver writerResolver = new StandardWriterResolver(new RestDocumentationContextPlaceholderResolverFactory(), DEFAULT_SNIPPET_ENCODING, new RamlTemplateFormat());
        try (Writer writer = writerResolver.resolve(operation.getName(), SNIPPET_NAME,
                (RestDocumentationContext) operation.getAttributes().get(RestDocumentationContext.class.getName()))) {
            Map<String, Object> model = createModel(operation);
            TemplateEngine templateEngine = new MustacheTemplateEngine(new StandardTemplateResourceResolver(new RamlTemplateFormat()));
            writer.append(templateEngine.compileTemplate(SNIPPET_NAME).render(model));
        }
    }
 
Example #5
Source File: OperationBuilder.java    From restdocs-raml with MIT License 5 votes vote down vote up
private RestDocumentationContext createContext() {
	ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
			this.outputDirectory.getAbsolutePath());
	manualRestDocumentation.beforeTest(null, null);
	RestDocumentationContext context = manualRestDocumentation.beforeOperation();
	return context;
}
 
Example #6
Source File: ContractDslSnippet.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private void storeDslContract(Operation operation, String content)
		throws IOException {
	RestDocumentationContext context = (RestDocumentationContext) operation
			.getAttributes().get(RestDocumentationContext.class.getName());
	RestDocumentationContextPlaceholderResolver resolver = new RestDocumentationContextPlaceholderResolver(
			context);
	String resolvedName = replacePlaceholders(resolver, operation.getName());
	File output = new File(context.getOutputDirectory(),
			CONTRACTS_FOLDER + "/" + resolvedName + ".groovy");
	output.getParentFile().mkdirs();
	try (Writer writer = new OutputStreamWriter(
			Files.newOutputStream(output.toPath()))) {
		writer.append(content);
	}
}
 
Example #7
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private Operation operation(String name, OperationRequest request,
		OperationResponse response, RestDocumentationContext context) {
	return new Operation() {

		Map<String, Object> map = new HashMap<>();

		@Override
		public Map<String, Object> getAttributes() {
			this.map.put(RestDocumentationContext.class.getName(), context);
			return this.map;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public OperationRequest getRequest() {
			return request;
		}

		@Override
		public OperationResponse getResponse() {
			return response;
		}
	};
}
 
Example #8
Source File: WireMockJsonSnippet.java    From restdocs-wiremock with Apache License 2.0 5 votes vote down vote up
@Override
public void document(Operation operation) throws IOException {
	RestDocumentationContext context = (RestDocumentationContext) operation.getAttributes()
			.get(RestDocumentationContext.class.getName());
	WriterResolver writerResolver = new StandardWriterResolver(
			new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8", TEMPLATE_FORMAT);
	try (Writer writer = writerResolver.resolve(operation.getName(), SNIPPET_NAME, context)) {
		writer.append(toJsonString(operation));
	}
}
 
Example #9
Source File: OperationBuilder.java    From restdocs-wiremock with Apache License 2.0 5 votes vote down vote up
private RestDocumentationContext createContext() {
	ManualRestDocumentation manualRestDocumentation = new ManualRestDocumentation(
			this.outputDirectory.getAbsolutePath());
	manualRestDocumentation.beforeTest(null, null);
	RestDocumentationContext context = manualRestDocumentation.beforeOperation();
	return context;
}
 
Example #10
Source File: ResponseFieldSnippet.java    From initializr with Apache License 2.0 5 votes vote down vote up
@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() + "/" + getSnippetName(), this.file, context)) {
		Map<String, Object> model = createModel(operation);
		model.putAll(getAttributes());
		TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes()
				.get(TemplateEngine.class.getName());
		writer.append(templateEngine.compileTemplate(getSnippetName()).render(model));
	}
}
 
Example #11
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private Operation operation(OperationRequest request, OperationResponse response,
		RestDocumentationContext context) {
	return operation("foo", request, response, context);
}
 
Example #12
Source File: OperationAttributeHelper.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
public static RestDocumentationContext getDocumentationContext(Operation operation) {
    return (RestDocumentationContext) operation
            .getAttributes().get(RestDocumentationContext.class.getName());
}