Skip to content

Conversation

@proski
Copy link

@proski proski commented Jun 28, 2019

The first commit adds WireMock based unit tests for the entire StashApiClient.java. In some cases, the tests indicate issues in the code under test. Those issues are documented.

The second commit changed exception handling in StashApiClient. Only specific checked exceptions are used, RuntimeException and Exception are banned. The callers are changes to handle the exceptions. That code was getting RuntimeException and ignored it. Now it's forces to deal with it properly.

@proski
Copy link
Author

proski commented Jun 28, 2019

Improved tests for loading PRs and PR comments from multiple pages to avoid some repetition.

@jakub-bochenski
Copy link

I don't see a good way to embed JSON into Java code (https://openjdk.java.net/jeps/355 will hopefully change it), so I'm constructing JSON programmatically.

How about we use POJOs and then serialize them into Json with Jackson etc.? For simple objects (I don't expect anything else) the transformation should be obvious.

Another idea would be to go for spring-cloud-contract, too bad Stash doesn't publish it's API contracts (or does it?)

@proski
Copy link
Author

proski commented Jun 28, 2019

How about we use POJOs and then serialize them into Json with Jackson etc.? For simple objects (I don't expect anything else) the transformation should be obvious.

I don't think is would be simpler than what we have now.

Another idea would be to go for spring-cloud-contract, too bad Stash doesn't publish it's API contracts (or does it?)

A quick Google search suggests that they exist, but no indication where they are. Again, I don't expect it to be a simplification.

I'm going to test standalone JSON files first.

@proski
Copy link
Author

proski commented Jun 29, 2019

Added support for all public methods except deleteRequest(), which is public by mistake. Still need to test more exceptional conditions like connection reset. Need to test responses better, especially for POST.

It turns out WireMock has good support for configuring through JSON files, I'm going to try it.

@proski
Copy link
Author

proski commented Jun 29, 2019

Converted multi-page tests to JSON files.

Using dynamic port for HTTP. There default is port 8080, which can conflict with Jenkins.

Using wiremock-jre8-standalone in pom.xml, it's recommended if Java 8+ is required.

@proski
Copy link
Author

proski commented Jun 30, 2019

This PR has achieved 91.9% code coverage for StashApiClient.java, which is pretty impressive. Not only is the code under test executed, the outputs are checked.

One problem is that only "easy HTTPS" is being tested, but not HTTP or real HTTPS. I believe JUnit has a way to run tests with multiple parameters, I'll look into that. In any case, unit testing real HTTPS can be tricky.

The unit tests have discovered multiple issues. I documented them in the code.

  • RuntimeException is thrown, but the callers may not be prepared for it, as it's not a checked exception
  • Many calls ignore HTTP errors, especially "malformed response"
  • deletePullRequestComment() gives no indication whether the call has succeeded
  • mergePullRequest() throws on 409 Conflict instead of returning false as apparently intended
  • There are no checks whether the HTTP code indicates an error for the specific request
  • POST requests throw on 204 No Content
  • Timeouts are not testable without very slow tests, as the timings are not configurable

Applying the first commit from #68 on top of this PR (i.e. upgrading the HTTP client) breaks several tests. On the positive side, some errors are not ignored anymore. On the negative side, RuntimeException is thrown in some cases where StashApiException was thrown.

I think the best course of action would be to completely remove RuntimeException from the code first. That will give us visibility into what exceptions we should handle.

@proski proski changed the title [PREVIEW] StashApiClientTest: Add WireMock based tests Clean up exception handline in StashApiClient Jun 30, 2019
@proski proski changed the title Clean up exception handline in StashApiClient Clean up exception handling in StashApiClient Jun 30, 2019
@proski
Copy link
Author

proski commented Jun 30, 2019

I've added a commit to clean up exception handling. It would be better to update the HTTP client if we have a complete picture which exceptions are thrown and what code is handling them.

I didn't try to use perfect messages in the exceptions, I only wanted then to be better than what it is now. I'm trying to keep this PR short so it can be reviewed and merged quickly.

@proski
Copy link
Author

proski commented Jul 4, 2019

It turns out I removed stack traces in many places, please don't merge this PR until I fix that issue.

@proski
Copy link
Author

proski commented Jul 4, 2019

Should be good to merge now.

@proski
Copy link
Author

proski commented Jul 5, 2019

I added comments everywhere StashApiException is handled. I also added javadoc comments to the functions that throw StashApiException in StashRepository.

@proski
Copy link
Author

proski commented Jul 6, 2019

Added unit tests for StashBuildListener.

@proski
Copy link
Author

proski commented Jul 7, 2019

Added unit tests for the changes to StashRepository.java.

@proski
Copy link
Author

proski commented Jul 9, 2019

Added checks for authentication, we really don't want to break it.

proski added 2 commits July 10, 2019 13:31
Stop using RuntimeException, it's an unchecked exception. The callers may
be unaware that they need to handle it.

Stop using Exception. Always use most specific exception types.

Always rethrow caught exceptions by wrapping them in StashApiClient. Let
the callers handle them, as they are in a better position to chose what
to do and emit meaningful logs.

Don't print stack trace in StashApiClient. Print it when the exception is
handled. Some callers log the stack trace to the build log, not to the
global Jenkins log.

Don't log error messages and throw StashApiException at the same time,
unless the log message contains information that is not added to the
exception.

Adjust "throws" declarations. Catch errors where appropriate and log
them.

Document functions that throw StashApiException. Add comments explaining
how StashApiException is handled.

Adjust unit tests for the new behavior.
@jakub-bochenski jakub-bochenski merged commit c218eef into jenkinsci:master Jul 11, 2019
@proski proski deleted the api-client-tests branch July 11, 2019 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants