Skip to content

Commit 92bc782

Browse files
amatiushkinamatiushkin
authored andcommitted
Add tests for CDC Result processing in callback interceptor (#60)
1 parent 8889c3a commit 92bc782

File tree

3 files changed

+177
-18
lines changed

3 files changed

+177
-18
lines changed

ipp-v3-java-devkit/src/test/java/com/intuit/ipp/interceptors/CallbackHandlerBase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.intuit.ipp.interceptors;
22

33
import com.intuit.ipp.data.CDCResponse;
4+
import com.intuit.ipp.data.IntuitEntity;
45
import com.intuit.ipp.data.IntuitResponse;
56
import com.intuit.ipp.exception.FMSException;
67
import com.intuit.ipp.services.CDCQueryResult;
78
import com.intuit.ipp.services.CallbackHandler;
89
import com.intuit.ipp.services.CallbackMessage;
910

11+
import javax.xml.bind.JAXBElement;
12+
import javax.xml.namespace.QName;
1013
import java.util.List;
1114

1215
public class CallbackHandlerBase {
@@ -38,4 +41,11 @@ public void execute(CallbackMessage callbackMessage) {
3841
public static CallbackHandlerInterceptor callback() {
3942
return interceptor;
4043
}
44+
45+
public class IntuitTestEntity extends IntuitEntity {}
46+
47+
public JAXBElement<? extends IntuitEntity> getDummyTestEntity() {
48+
QName qname = new QName("http://www.example.com", "interceptor-test");
49+
return new JAXBElement<>(qname, IntuitTestEntity.class, new IntuitTestEntity());
50+
}
4151
}
Lines changed: 167 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,191 @@
11
package com.intuit.ipp.interceptors;
22

33
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;
48
import com.intuit.ipp.exception.FMSException;
59
import com.intuit.ipp.services.CDCQueryResult;
610
import org.testng.annotations.Test;
711

12+
import javax.xml.bind.JAXBElement;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
815
import java.util.Collections;
916
import java.util.List;
10-
1117
import static org.testng.Assert.assertEquals;
1218
import static org.testng.Assert.assertFalse;
1319
import static org.testng.Assert.assertNotNull;
1420
import static org.testng.Assert.assertNull;
1521

1622
public class CallbackHandlerInterceptorCDCTest extends CallbackHandlerBase {
1723

18-
1924
@Test
2025
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");
2353
}
2454

55+
2556
@Test
26-
public void singleItemIsOk() throws FMSException {
27-
final List<CDCQueryResult> results = invokeCDC(Collections.singletonList(new CDCResponse()));
57+
public void error() throws FMSException {
58+
final CDCResponse o = new CDCResponse();
59+
final QueryResponse queryResponse = new QueryResponse();
60+
final Fault fault = new Fault();
61+
final Error error = new Error();
62+
fault.setError(Collections.singletonList(error));
63+
error.setDetail("My custom error");
64+
queryResponse.setFault(fault);
65+
66+
o.setQueryResponse(Collections.singletonList(queryResponse));
67+
new ResultChecker( assertAndGetFirst(invokeCDC(Collections.singletonList(o))))
68+
.assertErrorsDetails("My custom error");
69+
70+
}
71+
72+
73+
/**
74+
* Asserts that query result contains exactly one response without any fields set
75+
* @param results
76+
*/
77+
private void assertEmptyResult(List<CDCQueryResult> results) {
78+
new ResultChecker(assertAndGetFirst(results)).assertEmpty();
79+
}
2880

29-
assertNotNull(results);
30-
assertFalse(results.isEmpty());
31-
assertEquals(1, results.size());
81+
/**
82+
* Asserts exactly single response and returns first {@link CDCQueryResult}
83+
* @param results
84+
* @return
85+
*/
86+
private CDCQueryResult assertAndGetFirst(List<CDCQueryResult> results) {
87+
return new ListChecker<>(results)
88+
.exactlyOne()
89+
.first();
3290
}
91+
92+
93+
/**
94+
* Declaratively verifies an arbitrary list
95+
* @param <T>
96+
*/
97+
static class ListChecker<T> {
98+
99+
private List<T> it;
100+
101+
public ListChecker(List<T> it) {
102+
this.it = it;
103+
}
104+
105+
/**
106+
* Asserts that list has exactly one element
107+
* @return
108+
*/
109+
ListChecker<T> exactlyOne() {
110+
assertNotNull(it);
111+
assertFalse(it.isEmpty());
112+
assertEquals(1, it.size());
113+
return this;
114+
}
115+
116+
/** Returns first element from list
117+
*
118+
* @return
119+
*/
120+
T first() {
121+
return it.get(0);
122+
}
123+
}
124+
125+
/**
126+
* Holds verified object
127+
* @param <T>
128+
*/
129+
static class Checker<T> {
130+
131+
private T it;
132+
133+
public Checker(T it) {
134+
this.it = it;
135+
}
136+
137+
public T it() {
138+
return it;
139+
}
140+
}
141+
142+
/**
143+
* Declaratively verifies result
144+
*/
145+
private static class ResultChecker extends Checker<CDCQueryResult> {
146+
ResultChecker(CDCQueryResult it) {
147+
super(it);
148+
}
149+
150+
/**
151+
* Asserts query result has no errors nor responses
152+
* @return
153+
*/
154+
ResultChecker assertEmpty() {
155+
assertNull( it().getFalut());
156+
assertNull( it().getQueryResults());
157+
assertNull( it().getSize());
158+
return this;
159+
160+
}
161+
162+
/**
163+
* Asserts CDC Query Result to have exact number of {@link com.intuit.ipp.services.QueryResult}
164+
* using its key
165+
*
166+
* @param keys
167+
* @return
168+
*/
169+
ResultChecker assertQueryKeys(String... keys) {
170+
assertEquals(Arrays.asList(keys), it().getQueryResults().keySet());
171+
return this;
172+
}
173+
174+
/**
175+
* Asserts all error details in query result
176+
* @param details
177+
* @return
178+
*/
179+
ResultChecker assertErrorsDetails(String... details) {
180+
List<String> actualDetails = new ArrayList<>();
181+
for(Error error : it().getFalut().getError()) {
182+
actualDetails.add(error.getDetail());
183+
}
184+
assertEquals(Arrays.asList(details), actualDetails);
185+
return this;
186+
}
187+
188+
}
189+
190+
33191
}

ipp-v3-java-devkit/src/test/java/com/intuit/ipp/interceptors/CallbackHandlerInterceptorTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33

44
import com.intuit.ipp.core.Response;
5-
import com.intuit.ipp.data.IntuitEntity;
65
import com.intuit.ipp.data.IntuitResponse;
76
import com.intuit.ipp.data.QueryResponse;
87
import com.intuit.ipp.exception.FMSException;
98
import com.intuit.ipp.services.CallbackHandler;
109
import com.intuit.ipp.services.CallbackMessage;
1110
import org.testng.annotations.Test;
1211

13-
import javax.xml.bind.JAXBElement;
14-
import javax.xml.namespace.QName;
15-
1612
import static org.testng.Assert.*;
1713

1814
public class CallbackHandlerInterceptorTest extends CallbackHandlerBase {
@@ -97,10 +93,5 @@ public void queryIsOk() throws FMSException {
9793

9894

9995

100-
public class IntuitTestEntity extends IntuitEntity {}
10196

102-
public JAXBElement<? extends IntuitEntity> getDummyTestEntity() {
103-
QName qname = new QName("http://www.example.com", "interceptor-test");
104-
return new JAXBElement<>(qname, IntuitTestEntity.class, new IntuitTestEntity());
105-
}
10697
}

0 commit comments

Comments
 (0)