/** * Handles the given {@code request} and {@code response}, producing documentation * snippets for them using the given {@code configuration}. * @param request the request * @param response the request * @param configuration the configuration * @throws RestDocumentationGenerationException if a failure occurs during handling */ public void handle(REQ request, RESP response, Map<String, Object> configuration) { Map<String, Object> attributes = new HashMap<>(configuration); OperationRequest operationRequest = preprocessRequest( this.requestConverter.convert(request), attributes); OperationResponse operationResponse = preprocessResponse( this.responseConverter.convert(response), attributes); Operation operation = new StandardOperation(this.identifier, operationRequest, operationResponse, attributes); try { for (Snippet snippet : getSnippets(attributes)) { snippet.document(operation); } } catch (IOException ex) { throw new RestDocumentationGenerationException(ex); } }
@Test public void defaultSnippetsAreCalled() throws IOException { given(this.requestConverter.convert(this.request)) .willReturn(this.operationRequest); given(this.responseConverter.convert(this.response)) .willReturn(this.operationResponse); HashMap<String, Object> configuration = new HashMap<>(); Snippet defaultSnippet1 = mock(Snippet.class); Snippet defaultSnippet2 = mock(Snippet.class); configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS, Arrays.asList(defaultSnippet1, defaultSnippet2)); new RestDocumentationGenerator<>("id", this.requestConverter, this.responseConverter, this.snippet).handle(this.request, this.response, configuration); InOrder inOrder = Mockito.inOrder(defaultSnippet1, defaultSnippet2, this.snippet); verifySnippetInvocation(inOrder, defaultSnippet1, configuration); verifySnippetInvocation(inOrder, defaultSnippet2, configuration); verifySnippetInvocation(inOrder, this.snippet, configuration); }
@Test public void basicHandling() throws IOException { given(this.requestConverter.convert(this.request)) .willReturn(this.operationRequest); given(this.responseConverter.convert(this.response)) .willReturn(this.operationResponse); HashMap<String, Object> configuration = new HashMap<>(); new RestDocumentationGenerator<>("id", this.requestConverter, this.responseConverter, this.snippet).handle(this.request, this.response, configuration); verifySnippetInvocation(this.snippet, configuration); }
@Test public void newGeneratorOnlyCallsItsSnippets() throws IOException { OperationRequestPreprocessor requestPreprocessor = mock( OperationRequestPreprocessor.class); OperationResponsePreprocessor responsePreprocessor = mock( OperationResponsePreprocessor.class); given(this.requestConverter.convert(this.request)) .willReturn(this.operationRequest); given(this.responseConverter.convert(this.response)) .willReturn(this.operationResponse); given(requestPreprocessor.preprocess(this.operationRequest)) .willReturn(this.operationRequest); given(responsePreprocessor.preprocess(this.operationResponse)) .willReturn(this.operationResponse); Snippet additionalSnippet1 = mock(Snippet.class); Snippet additionalSnippet2 = mock(Snippet.class); RestDocumentationGenerator<Object, Object> generator = new RestDocumentationGenerator<>( "id", this.requestConverter, this.responseConverter, requestPreprocessor, responsePreprocessor, this.snippet); HashMap<String, Object> configuration = new HashMap<>(); generator.withSnippets(additionalSnippet1, additionalSnippet2) .handle(this.request, this.response, configuration); verifyNoMoreInteractions(this.snippet); verifySnippetInvocation(additionalSnippet1, configuration); verifySnippetInvocation(additionalSnippet2, configuration); }
@Test public void defaultOperationResponsePreprocessorsAreCalled() throws IOException { given(this.requestConverter.convert(this.request)) .willReturn(this.operationRequest); given(this.responseConverter.convert(this.response)) .willReturn(this.operationResponse); HashMap<String, Object> configuration = new HashMap<>(); OperationPreprocessor defaultPreprocessor1 = mock(OperationPreprocessor.class); OperationPreprocessor defaultPreprocessor2 = mock(OperationPreprocessor.class); configuration.put( RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR, Preprocessors.preprocessResponse(defaultPreprocessor1, defaultPreprocessor2)); OperationResponse first = createResponse(); OperationResponse second = createResponse(); OperationResponse third = new OperationResponseFactory() .createFrom(this.operationResponse, new HttpHeaders()); given(this.responsePreprocessor.preprocess(this.operationResponse)) .willReturn(first); given(defaultPreprocessor1.preprocess(first)).willReturn(second); given(defaultPreprocessor2.preprocess(second)).willReturn(third); new RestDocumentationGenerator<>("id", this.requestConverter, this.responseConverter, Preprocessors.preprocessResponse(this.responsePreprocessor), this.snippet) .handle(this.request, this.response, configuration); verifySnippetInvocation(this.snippet, this.operationRequest, third, configuration, 1); }
@Test public void defaultOperationRequestPreprocessorsAreCalled() throws IOException { given(this.requestConverter.convert(this.request)) .willReturn(this.operationRequest); given(this.responseConverter.convert(this.response)) .willReturn(this.operationResponse); HashMap<String, Object> configuration = new HashMap<>(); OperationPreprocessor defaultPreprocessor1 = mock(OperationPreprocessor.class); OperationPreprocessor defaultPreprocessor2 = mock(OperationPreprocessor.class); configuration.put( RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR, Preprocessors.preprocessRequest(defaultPreprocessor1, defaultPreprocessor2)); OperationRequest first = createRequest(); OperationRequest second = createRequest(); OperationRequest third = createRequest(); given(this.requestPreprocessor.preprocess(this.operationRequest)) .willReturn(first); given(defaultPreprocessor1.preprocess(first)).willReturn(second); given(defaultPreprocessor2.preprocess(second)).willReturn(third); new RestDocumentationGenerator<>("id", this.requestConverter, this.responseConverter, Preprocessors.preprocessRequest(this.requestPreprocessor), this.snippet) .handle(this.request, this.response, configuration); verifySnippetInvocation(this.snippet, third, this.operationResponse, configuration, 1); }