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

ADOC generation from contracts #68

Closed
wants to merge 6 commits into from
Closed

Conversation

marcingrzejszczak
Copy link
Contributor

DO NOT MERGE YET

Reason for the change

It can be useful to present in the form of documentation the contracts. The description field in the contract can provide an input to the scenario, whereas the contract speaks for itself. Obviously it's parsed so we can do something more descriptive. Also we can give the user an option to provide his own customizations.

Sample implementation

Running this test will result in creation of the following adoc

= Application Contracts

In the following document you'll be able to see all the contracts that are present for this application.

== Contracts

### shouldMarkClientAsNotFraud.groovy


            The following contract describes a situation where the user should be accepted in our system.
            It satisfies the ticket number ABC-123. For further information please contact the Product Owners
            John Doe and Jane Doe.

            Given:
                a user who wants to borrow an acceptable amount of money
            When:
                he is checked in the fraud system
            Then:
                the fraud check status should be "OK"
            And:
                the rejection reason should not contain any data


#### Contract structure

[source,java,indent=0]
----
package contracts

org.springframework.cloud.contract.spec.Contract.make {
            description('''
            The following contract describes a situation where the user should be accepted in our system.
            It satisfies the ticket number ABC-123. For further information please contact the Product Owners
            John Doe and Jane Doe.

            Given:
                a user who wants to borrow an acceptable amount of money
            When:
                he is checked in the fraud system
            Then:
                the fraud check status should be "OK"
            And:
                the rejection reason should not contain any data
            ''')
                request {
                    method 'PUT'
                    url '/fraudcheck'
                    body("""
                        {
                        "clientId":"${value(consumer(regex('[0-9]{10}')), producer('1234567890'))}",
                        "loanAmount":123.123
                        }
                    """
                    )
                    headers {
                        header('Content-Type', 'application/vnd.fraud.v1+json')
                    }

                }
            response {
                status 200
                body(
                        fraudCheckStatus: "OK",
                        rejectionReason: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)')))
                )
                headers {
                    header('Content-Type': value(
                            producer(regex('application/vnd.fraud.v1.json.*')),
                            consumer('application/vnd.fraud.v1+json'))
                    )
                }
            }

}

----

### shouldMarkClientAsFraud.groovy


            The following contract describes a situation where the user should NOT be accepted in our system.
            It satisfies the ticket number ABC-1234. For further information please contact the Product Owners
            John Doe and Jane Doe.

            Given:
                a user who wants to borrow a lot of money
            When:
                he applies for a loan
            Then:
                the loan should not be granted
            And:
                the user should marked as fraud


#### Contract structure

[source,java,indent=0]
----
package contracts

org.springframework.cloud.contract.spec.Contract.make {
            description('''
            The following contract describes a situation where the user should NOT be accepted in our system.
            It satisfies the ticket number ABC-1234. For further information please contact the Product Owners
            John Doe and Jane Doe.

            Given:
                a user who wants to borrow a lot of money
            When:
                he applies for a loan
            Then:
                the loan should not be granted
            And:
                the user should marked as fraud
            ''')
            request {
                method 'PUT'
                url '/fraudcheck'
                body([
                    clientId: value(consumer(regex('[0-9]{10}'))),
                    loanAmount: 99999
                    ])
                headers {
                    header('Content-Type', 'application/vnd.fraud.v1+json')
                }
            }
            response {
                status 200
                body([
                    fraudCheckStatus: "FRAUD",
                    rejectionReason: "Amount too high"
                ])
                headers {
                     header('Content-Type': value(
                             producer(regex('application/vnd.fraud.v1.json.*')),
                             consumer('application/vnd.fraud.v1+json'))
                     )
                }
            }
}
----

In target/generated-snippets.

CC @mminella @dussab @dsyer @spencergibb

@marcingrzejszczak
Copy link
Contributor Author

A rendered example could look like this

image

@jedrzej-andrykowski
Copy link

Great idea! Very useful for non-technical persons such as analysts and testers. But one thing bothering me, that is this documentation doesn't doubles with documentation which is generated by Swagger? (it's known that many companies use it). Maybe it could be cool to add some integration with Swagger f.e. some annotations that add contracts documentation to Swagger output?

@marcingrzejszczak
Copy link
Contributor Author

The difference between Swagger and this is that Swagger produces a technical description of the data model + response codes. Here you have scenarios. Each contract is a request / reply situation. E.g. we don't present all possible options of the request body. We present what kind of request / response is needed to achieve the following type of a response.

@jedrzej-andrykowski
Copy link

I think that contracts are so technical as the Swagger but additionally give you more real situation cases , not only dry request/response structured, nevertheless it looks quite good :)

@marcingrzejszczak marcingrzejszczak modified the milestones: 1.1.0.M1, 1.0.0.RC Sep 6, 2016
@marcingrzejszczak marcingrzejszczak removed this from the 1.1.0.M1 milestone Jan 13, 2017
Spring Operator and others added 3 commits March 18, 2019 15:09
This commit updates URLs to prefer the https protocol. Redirects are not followed to avoid accidentally expanding intentionally shortened URLs (i.e. if using a URL shortener).

# Fixed URLs

## Fixed But Review Recommended
These URLs were fixed, but the https status was not OK. However, the https status was the same as the http request or http redirected to an https URL, so they were migrated. Your review is recommended.

* http://repo.spring.io/plugins-staging-local/ (404) with 1 occurrences migrated to:
  https://repo.spring.io/plugins-staging-local/ ([https](https://repo.spring.io/plugins-staging-local/) result 404).

## Fixed Success
These URLs were switched to an https URL with a 2xx status. While the status was successful, your review is still recommended.

* http://maven.apache.org/xsd/maven-4.0.0.xsd with 49 occurrences migrated to:
  https://maven.apache.org/xsd/maven-4.0.0.xsd ([https](https://maven.apache.org/xsd/maven-4.0.0.xsd) result 200).
* http://repo.spring.io/libs-milestone-local/ with 1 occurrences migrated to:
  https://repo.spring.io/libs-milestone-local/ ([https](https://repo.spring.io/libs-milestone-local/) result 200).
* http://repo.spring.io/libs-snapshot-local/ with 1 occurrences migrated to:
  https://repo.spring.io/libs-snapshot-local/ ([https](https://repo.spring.io/libs-snapshot-local/) result 200).
* http://repo.spring.io/libs-staging-local/ with 3 occurrences migrated to:
  https://repo.spring.io/libs-staging-local/ ([https](https://repo.spring.io/libs-staging-local/) result 200).
* http://solidsoft.wordpress.com/2014/11/13/gradle-tricks-display-dependencies-for-all-subprojects-in-multi-project-build/ with 1 occurrences migrated to:
  https://solidsoft.wordpress.com/2014/11/13/gradle-tricks-display-dependencies-for-all-subprojects-in-multi-project-build/ ([https](https://solidsoft.wordpress.com/2014/11/13/gradle-tricks-display-dependencies-for-all-subprojects-in-multi-project-build/) result 200).
* http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch with 1 occurrences migrated to:
  https://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch ([https](https://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch) result 200).
* http://stackoverflow.com/questions/20618857/gradle-task-groovydoc-failing-with-noclassdeffounderror with 1 occurrences migrated to:
  https://stackoverflow.com/questions/20618857/gradle-task-groovydoc-failing-with-noclassdeffounderror ([https](https://stackoverflow.com/questions/20618857/gradle-task-groovydoc-failing-with-noclassdeffounderror) result 200).
* http://stackoverflow.com/questions/29300806/a-bash-script-to-check-if-a-string-is-present-in-a-comma-separated-list-of-strin with 1 occurrences migrated to:
  https://stackoverflow.com/questions/29300806/a-bash-script-to-check-if-a-string-is-present-in-a-comma-separated-list-of-strin ([https](https://stackoverflow.com/questions/29300806/a-bash-script-to-check-if-a-string-is-present-in-a-comma-separated-list-of-strin) result 200).
* http://www.apache.org/licenses/LICENSE-2.0 with 40 occurrences migrated to:
  https://www.apache.org/licenses/LICENSE-2.0 ([https](https://www.apache.org/licenses/LICENSE-2.0) result 200).
* http://www.apache.org/licenses/LICENSE-2.0.txt with 1 occurrences migrated to:
  https://www.apache.org/licenses/LICENSE-2.0.txt ([https](https://www.apache.org/licenses/LICENSE-2.0.txt) result 200).
* http://repo.spring.io/libs-release-local with 5 occurrences migrated to:
  https://repo.spring.io/libs-release-local ([https](https://repo.spring.io/libs-release-local) result 302).
* http://repo.spring.io/libs-snapshot-local with 2 occurrences migrated to:
  https://repo.spring.io/libs-snapshot-local ([https](https://repo.spring.io/libs-snapshot-local) result 302).
* http://repo.spring.io/libs-staging-local with 1 occurrences migrated to:
  https://repo.spring.io/libs-staging-local ([https](https://repo.spring.io/libs-staging-local) result 302).
* http://repo.spring.io/milestone with 10 occurrences migrated to:
  https://repo.spring.io/milestone ([https](https://repo.spring.io/milestone) result 302).
* http://repo.spring.io/plugins-release-local with 1 occurrences migrated to:
  https://repo.spring.io/plugins-release-local ([https](https://repo.spring.io/plugins-release-local) result 302).
* http://repo.spring.io/plugins-snapshot with 1 occurrences migrated to:
  https://repo.spring.io/plugins-snapshot ([https](https://repo.spring.io/plugins-snapshot) result 302).
* http://repo.spring.io/release with 8 occurrences migrated to:
  https://repo.spring.io/release ([https](https://repo.spring.io/release) result 302).
* http://repo.spring.io/snapshot with 10 occurrences migrated to:
  https://repo.spring.io/snapshot ([https](https://repo.spring.io/snapshot) result 302).

# Ignored
These URLs were intentionally ignored.

* http://maven.apache.org/POM/4.0.0 with 98 occurrences
* http://www.w3.org/2001/XMLSchema-instance with 49 occurrences
This commit updates URLs to prefer the https protocol. Redirects are not followed to avoid accidentally expanding intentionally shortened URLs (i.e. if using a URL shortener).

# Fixed URLs

## Fixed Success
These URLs were switched to an https URL with a 2xx status. While the status was successful, your review is still recommended.

* [ ] http://www.apache.org/licenses/ with 1 occurrences migrated to:
  https://www.apache.org/licenses/ ([https](https://www.apache.org/licenses/) result 200).
* [ ] http://www.apache.org/licenses/LICENSE-2.0 with 379 occurrences migrated to:
  https://www.apache.org/licenses/LICENSE-2.0 ([https](https://www.apache.org/licenses/LICENSE-2.0) result 200).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants