|
1 | 1 | package com.intuit.ipp.interceptors; |
2 | 2 |
|
3 | 3 | import com.intuit.ipp.data.CDCResponse; |
| 4 | +import com.intuit.ipp.data.Error; |
| 5 | +import com.intuit.ipp.data.Fault; |
| 6 | +import com.intuit.ipp.data.IntuitEntity; |
| 7 | +import com.intuit.ipp.data.QueryResponse; |
4 | 8 | import com.intuit.ipp.exception.FMSException; |
5 | 9 | import com.intuit.ipp.services.CDCQueryResult; |
6 | 10 | import org.testng.annotations.Test; |
7 | 11 |
|
| 12 | +import javax.xml.bind.JAXBElement; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Arrays; |
8 | 15 | import java.util.Collections; |
9 | 16 | import java.util.List; |
10 | | - |
11 | 17 | import static org.testng.Assert.assertEquals; |
12 | 18 | import static org.testng.Assert.assertFalse; |
13 | 19 | import static org.testng.Assert.assertNotNull; |
14 | 20 | import static org.testng.Assert.assertNull; |
15 | 21 |
|
16 | 22 | public class CallbackHandlerInterceptorCDCTest extends CallbackHandlerBase { |
17 | 23 |
|
18 | | - |
19 | 24 | @Test |
20 | 25 | public void emptyListIsOk() throws FMSException { |
21 | | - final List<CDCQueryResult> results = invokeCDC(Collections.<CDCResponse>emptyList()); |
22 | | - assertNull(results); |
| 26 | + assertNull(invokeCDC(Collections.<CDCResponse>emptyList())); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void singleWithResponse() throws FMSException { |
| 31 | + assertEmptyResult(invokeCDC(Collections.singletonList(new CDCResponse()))); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void singleResponseWithEmptyQuery() throws FMSException { |
| 36 | + final CDCResponse o = new CDCResponse(); |
| 37 | + o.setQueryResponse(Collections.singletonList(new QueryResponse())); |
| 38 | + |
| 39 | + assertEmptyResult(invokeCDC(Collections.singletonList(o))); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void singleEntityIsOk() throws FMSException { |
| 44 | + final CDCResponse response = new CDCResponse(); |
| 45 | + final QueryResponse queryResponse = new QueryResponse(); |
| 46 | + |
| 47 | + queryResponse.setIntuitObject(Collections.<JAXBElement<? extends IntuitEntity>>singletonList(getDummyTestEntity())); |
| 48 | + |
| 49 | + response.setQueryResponse(Collections.singletonList(queryResponse)); |
| 50 | + |
| 51 | + new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(response)))) |
| 52 | + .assertQueryKeys("IntuitTestEntity"); |
23 | 53 | } |
24 | 54 |
|
| 55 | + |
| 56 | + /** |
| 57 | + * Illustrates https://github.com/intuit/QuickBooks-V3-Java-SDK/issues/75 |
| 58 | + * Error is expected in result, but it gives empty result. |
| 59 | + * |
| 60 | + * @throws FMSException |
| 61 | + */ |
25 | 62 | @Test |
26 | | - public void singleItemIsOk() throws FMSException { |
27 | | - final List<CDCQueryResult> results = invokeCDC(Collections.singletonList(new CDCResponse())); |
| 63 | + public void errorNoResponse() throws FMSException { |
| 64 | + final CDCResponse o = new CDCResponse(); |
| 65 | + final Fault fault = new Fault(); |
| 66 | + final Error error = new Error(); |
| 67 | + fault.setError(Collections.singletonList(error)); |
| 68 | + error.setDetail("My custom error"); |
| 69 | + o.setFault(fault); |
| 70 | + o.setQueryResponse(null); |
28 | 71 |
|
29 | | - assertNotNull(results); |
30 | | - assertFalse(results.isEmpty()); |
31 | | - assertEquals(1, results.size()); |
| 72 | + assertEmptyResult(invokeCDC(Collections.singletonList(o))); |
32 | 73 | } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void error() throws FMSException { |
| 77 | + final CDCResponse o = new CDCResponse(); |
| 78 | + final QueryResponse queryResponse = new QueryResponse(); |
| 79 | + final Fault fault = new Fault(); |
| 80 | + final Error error = new Error(); |
| 81 | + fault.setError(Collections.singletonList(error)); |
| 82 | + error.setDetail("My custom error"); |
| 83 | + queryResponse.setFault(fault); |
| 84 | + |
| 85 | + o.setQueryResponse(Collections.singletonList(queryResponse)); |
| 86 | + new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(o)))) |
| 87 | + .assertErrorsDetails("My custom error"); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void partialError() throws FMSException { |
| 93 | + final CDCResponse o = new CDCResponse(); |
| 94 | + final QueryResponse queryResponse = new QueryResponse(); |
| 95 | + final Fault fault = new Fault(); |
| 96 | + final Error error = new Error(); |
| 97 | + fault.setError(Collections.singletonList(error)); |
| 98 | + error.setDetail("My custom error"); |
| 99 | + queryResponse.setFault(fault); |
| 100 | + queryResponse.setIntuitObject(Collections.<JAXBElement<? extends IntuitEntity>>singletonList(getDummyTestEntity())); |
| 101 | + |
| 102 | + o.setQueryResponse(Collections.singletonList(queryResponse)); |
| 103 | + new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(o)))) |
| 104 | + .assertErrorsDetails("My custom error") |
| 105 | + .assertQueryKeys("IntuitTestEntity"); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * Asserts that query result contains exactly one response without any fields set |
| 111 | + * @param results |
| 112 | + */ |
| 113 | + private void assertEmptyResult(List<CDCQueryResult> results) { |
| 114 | + new ResultChecker(assertAndGetFirst(results)).assertEmpty(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Asserts exactly single response and returns first {@link CDCQueryResult} |
| 119 | + * @param results |
| 120 | + * @return |
| 121 | + */ |
| 122 | + private CDCQueryResult assertAndGetFirst(List<CDCQueryResult> results) { |
| 123 | + return new ListChecker<>(results) |
| 124 | + .exactlyOne() |
| 125 | + .first(); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * Declaratively verifies an arbitrary list |
| 131 | + * @param <T> |
| 132 | + */ |
| 133 | + static class ListChecker<T> { |
| 134 | + |
| 135 | + private List<T> it; |
| 136 | + |
| 137 | + public ListChecker(List<T> it) { |
| 138 | + this.it = it; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Asserts that list has exactly one element |
| 143 | + * @return |
| 144 | + */ |
| 145 | + ListChecker<T> exactlyOne() { |
| 146 | + assertNotNull(it); |
| 147 | + assertFalse(it.isEmpty()); |
| 148 | + assertEquals(1, it.size()); |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + /** Returns first element from list |
| 153 | + * |
| 154 | + * @return |
| 155 | + */ |
| 156 | + T first() { |
| 157 | + return it.get(0); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Holds verified object |
| 163 | + * @param <T> |
| 164 | + */ |
| 165 | + static class Checker<T> { |
| 166 | + |
| 167 | + private T it; |
| 168 | + |
| 169 | + public Checker(T it) { |
| 170 | + this.it = it; |
| 171 | + } |
| 172 | + |
| 173 | + public T it() { |
| 174 | + return it; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Declaratively verifies result |
| 180 | + */ |
| 181 | + private static class ResultChecker extends Checker<CDCQueryResult> { |
| 182 | + ResultChecker(CDCQueryResult it) { |
| 183 | + super(it); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Asserts query result has no errors nor responses |
| 188 | + * @return |
| 189 | + */ |
| 190 | + ResultChecker assertEmpty() { |
| 191 | + assertNull( it().getFalut()); |
| 192 | + assertNull( it().getQueryResults()); |
| 193 | + assertNull( it().getSize()); |
| 194 | + return this; |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Asserts CDC Query Result to have exact number of {@link com.intuit.ipp.services.QueryResult} |
| 200 | + * using its key |
| 201 | + * |
| 202 | + * @param keys |
| 203 | + * @return |
| 204 | + */ |
| 205 | + ResultChecker assertQueryKeys(String... keys) { |
| 206 | + assertEquals(Arrays.asList(keys), it().getQueryResults().keySet()); |
| 207 | + return this; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Asserts all error details in query result |
| 212 | + * @param details |
| 213 | + * @return |
| 214 | + */ |
| 215 | + ResultChecker assertErrorsDetails(String... details) { |
| 216 | + List<String> actualDetails = new ArrayList<>(); |
| 217 | + for(Error error : it().getFalut().getError()) { |
| 218 | + actualDetails.add(error.getDetail()); |
| 219 | + } |
| 220 | + assertEquals(Arrays.asList(details), actualDetails); |
| 221 | + return this; |
| 222 | + } |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | + |
33 | 227 | } |
0 commit comments