-
Notifications
You must be signed in to change notification settings - Fork 151
Add tests for CDC Result processing in callback interceptor (#60) #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 203 additions & 9 deletions
212
...a-devkit/src/test/java/com/intuit/ipp/interceptors/CallbackHandlerInterceptorCDCTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,227 @@ | ||
| package com.intuit.ipp.interceptors; | ||
|
|
||
| import com.intuit.ipp.data.CDCResponse; | ||
| import com.intuit.ipp.data.Error; | ||
| import com.intuit.ipp.data.Fault; | ||
| import com.intuit.ipp.data.IntuitEntity; | ||
| import com.intuit.ipp.data.QueryResponse; | ||
| import com.intuit.ipp.exception.FMSException; | ||
| import com.intuit.ipp.services.CDCQueryResult; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import javax.xml.bind.JAXBElement; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertNotNull; | ||
| import static org.testng.Assert.assertNull; | ||
|
|
||
| public class CallbackHandlerInterceptorCDCTest extends CallbackHandlerBase { | ||
|
|
||
|
|
||
| @Test | ||
| public void emptyListIsOk() throws FMSException { | ||
| final List<CDCQueryResult> results = invokeCDC(Collections.<CDCResponse>emptyList()); | ||
| assertNull(results); | ||
| assertNull(invokeCDC(Collections.<CDCResponse>emptyList())); | ||
| } | ||
|
|
||
| @Test | ||
| public void singleWithResponse() throws FMSException { | ||
| assertEmptyResult(invokeCDC(Collections.singletonList(new CDCResponse()))); | ||
| } | ||
|
|
||
| @Test | ||
| public void singleResponseWithEmptyQuery() throws FMSException { | ||
| final CDCResponse o = new CDCResponse(); | ||
| o.setQueryResponse(Collections.singletonList(new QueryResponse())); | ||
|
|
||
| assertEmptyResult(invokeCDC(Collections.singletonList(o))); | ||
| } | ||
|
|
||
| @Test | ||
| public void singleEntityIsOk() throws FMSException { | ||
| final CDCResponse response = new CDCResponse(); | ||
| final QueryResponse queryResponse = new QueryResponse(); | ||
|
|
||
| queryResponse.setIntuitObject(Collections.<JAXBElement<? extends IntuitEntity>>singletonList(getDummyTestEntity())); | ||
|
|
||
| response.setQueryResponse(Collections.singletonList(queryResponse)); | ||
|
|
||
| new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(response)))) | ||
| .assertQueryKeys("IntuitTestEntity"); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Illustrates https://github.com/intuit/QuickBooks-V3-Java-SDK/issues/75 | ||
| * Error is expected in result, but it gives empty result. | ||
| * | ||
| * @throws FMSException | ||
| */ | ||
| @Test | ||
| public void singleItemIsOk() throws FMSException { | ||
| final List<CDCQueryResult> results = invokeCDC(Collections.singletonList(new CDCResponse())); | ||
| public void errorNoResponse() throws FMSException { | ||
| final CDCResponse o = new CDCResponse(); | ||
| final Fault fault = new Fault(); | ||
| final Error error = new Error(); | ||
| fault.setError(Collections.singletonList(error)); | ||
| error.setDetail("My custom error"); | ||
| o.setFault(fault); | ||
| o.setQueryResponse(null); | ||
|
|
||
| assertNotNull(results); | ||
| assertFalse(results.isEmpty()); | ||
| assertEquals(1, results.size()); | ||
| assertEmptyResult(invokeCDC(Collections.singletonList(o))); | ||
| } | ||
|
|
||
| @Test | ||
| public void error() throws FMSException { | ||
| final CDCResponse o = new CDCResponse(); | ||
| final QueryResponse queryResponse = new QueryResponse(); | ||
| final Fault fault = new Fault(); | ||
| final Error error = new Error(); | ||
| fault.setError(Collections.singletonList(error)); | ||
| error.setDetail("My custom error"); | ||
| queryResponse.setFault(fault); | ||
|
|
||
| o.setQueryResponse(Collections.singletonList(queryResponse)); | ||
| new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(o)))) | ||
| .assertErrorsDetails("My custom error"); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void partialError() throws FMSException { | ||
| final CDCResponse o = new CDCResponse(); | ||
| final QueryResponse queryResponse = new QueryResponse(); | ||
| final Fault fault = new Fault(); | ||
| final Error error = new Error(); | ||
| fault.setError(Collections.singletonList(error)); | ||
| error.setDetail("My custom error"); | ||
| queryResponse.setFault(fault); | ||
| queryResponse.setIntuitObject(Collections.<JAXBElement<? extends IntuitEntity>>singletonList(getDummyTestEntity())); | ||
|
|
||
| o.setQueryResponse(Collections.singletonList(queryResponse)); | ||
| new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(o)))) | ||
| .assertErrorsDetails("My custom error") | ||
| .assertQueryKeys("IntuitTestEntity"); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Asserts that query result contains exactly one response without any fields set | ||
| * @param results | ||
| */ | ||
| private void assertEmptyResult(List<CDCQueryResult> results) { | ||
| new ResultChecker(assertAndGetFirst(results)).assertEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts exactly single response and returns first {@link CDCQueryResult} | ||
| * @param results | ||
| * @return | ||
| */ | ||
| private CDCQueryResult assertAndGetFirst(List<CDCQueryResult> results) { | ||
| return new ListChecker<>(results) | ||
| .exactlyOne() | ||
| .first(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Declaratively verifies an arbitrary list | ||
| * @param <T> | ||
| */ | ||
| static class ListChecker<T> { | ||
|
|
||
| private List<T> it; | ||
|
|
||
| public ListChecker(List<T> it) { | ||
| this.it = it; | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that list has exactly one element | ||
| * @return | ||
| */ | ||
| ListChecker<T> exactlyOne() { | ||
| assertNotNull(it); | ||
| assertFalse(it.isEmpty()); | ||
| assertEquals(1, it.size()); | ||
| return this; | ||
| } | ||
|
|
||
| /** Returns first element from list | ||
| * | ||
| * @return | ||
| */ | ||
| T first() { | ||
| return it.get(0); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds verified object | ||
| * @param <T> | ||
| */ | ||
| static class Checker<T> { | ||
|
|
||
| private T it; | ||
|
|
||
| public Checker(T it) { | ||
| this.it = it; | ||
| } | ||
|
|
||
| public T it() { | ||
| return it; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Declaratively verifies result | ||
| */ | ||
| private static class ResultChecker extends Checker<CDCQueryResult> { | ||
| ResultChecker(CDCQueryResult it) { | ||
| super(it); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts query result has no errors nor responses | ||
| * @return | ||
| */ | ||
| ResultChecker assertEmpty() { | ||
| assertNull( it().getFalut()); | ||
| assertNull( it().getQueryResults()); | ||
| assertNull( it().getSize()); | ||
| return this; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Asserts CDC Query Result to have exact number of {@link com.intuit.ipp.services.QueryResult} | ||
| * using its key | ||
| * | ||
| * @param keys | ||
| * @return | ||
| */ | ||
| ResultChecker assertQueryKeys(String... keys) { | ||
| assertEquals(Arrays.asList(keys), it().getQueryResults().keySet()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Asserts all error details in query result | ||
| * @param details | ||
| * @return | ||
| */ | ||
| ResultChecker assertErrorsDetails(String... details) { | ||
| List<String> actualDetails = new ArrayList<>(); | ||
| for(Error error : it().getFalut().getError()) { | ||
| actualDetails.add(error.getDetail()); | ||
| } | ||
| assertEquals(Arrays.asList(details), actualDetails); | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this uncovers either dead code in callback interceptor or potential defect. My expectation here is to be able to get error back.