1717
1818import com .marklogic .client .FailedRequestException ;
1919import com .marklogic .client .impl .BaseProxy ;
20+ import com .marklogic .client .io .BaseHandle ;
2021import com .marklogic .client .io .Format ;
2122import com .marklogic .client .io .InputStreamHandle ;
23+ import com .marklogic .client .io .StringHandle ;
24+ import com .marklogic .client .io .marker .BufferableContentHandle ;
2225import com .marklogic .client .test .dbfunction .DBFunctionTestUtil ;
2326import org .junit .Test ;
2427
25- import java .io .IOException ;
28+ import java .io .ByteArrayInputStream ;
2629import java .util .function .BiFunction ;
2730import java .util .stream .Stream ;
2831
@@ -38,59 +41,53 @@ public class AnyDocumentBundleTest {
3841 };
3942 final static private AnyDocumentBundle testObj = AnyDocumentBundle .on (DBFunctionTestUtil .db );
4043
41- /* TODO:
42- positive test:
43- mapped StringHandle
44- negative test:
45- mapped to a class that's not a handle for single or multiple param or return value
46- */
47-
4844 @ Test
49- public void sendReceiveOptionalJSONDocTest () throws IOException {
50- testDocImpl (uris [0 ], Format .JSON , docs [0 ], testObj ::sendReceiveOptionalDoc );
45+ public void sendReceiveOptionalJSONDocTest () {
46+ testInputStreamImpl (uris [0 ], Format .JSON , docs [0 ], testObj ::sendReceiveOptionalDoc );
5147 }
5248 @ Test
53- public void sendReceiveOptionalXMLDocTest () throws IOException {
54- testDocImpl (uris [1 ], Format .XML , docs [1 ], testObj ::sendReceiveOptionalDoc );
49+ public void sendReceiveOptionalXMLDocTest () {
50+ testInputStreamImpl (uris [1 ], Format .XML , docs [1 ], testObj ::sendReceiveOptionalDoc );
5551 }
5652 @ Test
57- public void sendReceiveRequiredJSONDocTest () throws IOException {
58- testDocImpl (uris [0 ], Format .JSON , docs [0 ], testObj ::sendReceiveRequiredDoc );
53+ public void sendReceiveOptionalNullUriTest () {
54+ // send null uri with non-null handle to optional endpoint
55+ testInputStreamImpl (null , Format .JSON , docs [0 ], testObj ::sendReceiveOptionalDoc );
5956 }
6057 @ Test
61- public void sendReceiveRequiredXMLDocTest () throws IOException {
62- testDocImpl (uris [1 ], Format .XML , docs [1 ], testObj ::sendReceiveRequiredDoc );
58+ public void sendReceiveOptionalNullDocTest () {
59+ // send non-null uri with null handle to optional endpoint
60+ testInputStreamImpl (uris [0 ], null , null , testObj ::sendReceiveOptionalDoc );
6361 }
6462 @ Test
65- public void sendReceiveOptionalNullUriTest () throws IOException {
66- // send null uri with non- null handle to optional endpoint
67- testDocImpl (null , Format . JSON , docs [ 0 ] , testObj ::sendReceiveOptionalDoc );
63+ public void sendReceiveOptionalNullUriDocTest () {
64+ // send null uri and null handle to optional endpoint
65+ testInputStreamImpl (null , null , null , testObj ::sendReceiveOptionalDoc );
6866 }
67+
6968 @ Test
70- public void sendReceiveOptionalNullDocTest () throws IOException {
71- // send non-null uri with null handle to optional endpoint
72- testDocImpl (uris [0 ], null , null , testObj ::sendReceiveOptionalDoc );
69+ public void sendReceiveRequiredJSONDocTest () {
70+ testInputStreamImpl (uris [0 ], Format .JSON , docs [0 ], testObj ::sendReceiveRequiredDoc );
7371 }
7472 @ Test
75- public void sendReceiveOptionalNullUriDocTest () throws IOException {
76- // send null uri and null handle to optional endpoint
77- testDocImpl (null , null , null , testObj ::sendReceiveOptionalDoc );
73+ public void sendReceiveRequiredXMLDocTest () {
74+ testInputStreamImpl (uris [1 ], Format .XML , docs [1 ], testObj ::sendReceiveRequiredDoc );
7875 }
7976 @ Test
80- public void sendReceiveRequiredNullDocTest () throws IOException {
77+ public void sendReceiveRequiredNullDocTest () {
8178 // negative test: send non-null uri with null handle to required endpoint
8279 try {
83- testDocImpl (uris [0 ], null , null , testObj ::sendReceiveRequiredDoc );
80+ testInputStreamImpl (uris [0 ], null , null , testObj ::sendReceiveRequiredDoc );
8481 fail ("no exception for required parameter with null value" );
8582 } catch (BaseProxy .RequiredParamException e ) {
8683 assertEquals ("null value for required parameter: doc" , e .getMessage ());
8784 }
8885 }
8986 @ Test
90- public void sendReceiveOptionalInvalidFormatDocTest () throws IOException {
87+ public void sendReceiveOptionalInvalidFormatDocTest () {
9188 // negative test: send handle with invalid format
9289 try {
93- testDocImpl (uris [0 ], formats [1 ], docs [0 ], testObj ::sendReceiveOptionalDoc );
90+ testInputStreamImpl (uris [0 ], formats [1 ], docs [0 ], testObj ::sendReceiveOptionalDoc );
9491 fail ("no exception for invalid format" );
9592 } catch (FailedRequestException e ) {
9693 assertEquals (400 , e .getServerStatusCode ());
@@ -99,43 +96,68 @@ public void sendReceiveOptionalInvalidFormatDocTest() throws IOException {
9996 }
10097
10198 @ Test
102- public void sendReceiveAnyTwoDocsTest () throws IOException {
103- testDocsImpl (uris , formats , docs , testObj ::sendReceiveAnyDocs );
99+ public void sendReceiveMappedJSONDocTest () {
100+ testStringImpl (uris [ 0 ], Format . JSON , docs [ 0 ] , testObj ::sendReceiveMappedDoc );
104101 }
105102 @ Test
106- public void sendReceiveAnyOneJSONDocTest () throws IOException {
107- testDocsImpl (uris [0 ], formats [ 0 ] , docs [0 ], testObj ::sendReceiveAnyDocs );
103+ public void sendReceiveMappedXMLDocTest () {
104+ testStringImpl (uris [1 ], Format . XML , docs [1 ], testObj ::sendReceiveMappedDoc );
108105 }
109106 @ Test
110- public void sendReceiveAnyOneXMLDocTest () throws IOException {
111- testDocsImpl (uris [1 ], formats [1 ], docs [1 ], testObj ::sendReceiveAnyDocs );
107+ public void sendReceiveMappedNullUriTest () {
108+ // send null uri with non-null handle to optional endpoint
109+ testStringImpl (null , Format .JSON , docs [0 ], testObj ::sendReceiveMappedDoc );
112110 }
113111 @ Test
114- public void sendReceiveManyTwoDocsTest () throws IOException {
115- testDocsImpl (uris , formats , docs , testObj ::sendReceiveManyDocs );
112+ public void sendReceiveMappedNullDocTest () {
113+ // send non-null uri with null handle to optional endpoint
114+ testStringImpl (uris [0 ], null , null , testObj ::sendReceiveMappedDoc );
116115 }
117116 @ Test
118- public void sendReceiveManyOneJSONDocTest () throws IOException {
119- testDocsImpl (uris [0 ], formats [0 ], docs [0 ], testObj ::sendReceiveManyDocs );
117+ public void sendReceiveMappedNullUriDocTest () {
118+ // send null uri and null handle to optional endpoint
119+ testStringImpl (null , null , null , testObj ::sendReceiveMappedDoc );
120120 }
121+
121122 @ Test
122- public void sendReceiveManyOneXMLDocTest () throws IOException {
123- testDocsImpl (uris [1 ], formats [1 ], docs [1 ], testObj ::sendReceiveManyDocs );
123+ public void sendReceiveAnyTwoDocsTest () {
124+ testDocsImpl (uris , formats , docs , testObj ::sendReceiveAnyDocs );
125+ }
126+ @ Test
127+ public void sendReceiveAnyOneJSONDocTest () {
128+ testDocsImpl (uris [0 ], formats [0 ], docs [0 ], testObj ::sendReceiveAnyDocs );
129+ }
130+ @ Test
131+ public void sendReceiveAnyOneXMLDocTest () {
132+ testDocsImpl (uris [1 ], formats [1 ], docs [1 ], testObj ::sendReceiveAnyDocs );
124133 }
125134 @ Test
126- public void sendReceiveAnyNullUrisTest () throws IOException {
135+ public void sendReceiveAnyNullUrisTest () {
127136 testDocsImpl (null , formats , docs , testObj ::sendReceiveAnyDocs );
128137 }
129138 @ Test
130- public void sendReceiveAnyNullDocsTest () throws IOException {
139+ public void sendReceiveAnyNullDocsTest () {
131140 testDocsImpl (uris , null , null , testObj ::sendReceiveAnyDocs );
132141 }
133142 @ Test
134- public void sendReceiveAnyNullUrisDocsTest () throws IOException {
143+ public void sendReceiveAnyNullUrisDocsTest () {
135144 testDocsImpl (null , (Format []) null , null , testObj ::sendReceiveAnyDocs );
136145 }
146+
147+ @ Test
148+ public void sendReceiveManyTwoDocsTest () {
149+ testDocsImpl (uris , formats , docs , testObj ::sendReceiveManyDocs );
150+ }
151+ @ Test
152+ public void sendReceiveManyOneJSONDocTest () {
153+ testDocsImpl (uris [0 ], formats [0 ], docs [0 ], testObj ::sendReceiveManyDocs );
154+ }
155+ @ Test
156+ public void sendReceiveManyOneXMLDocTest () {
157+ testDocsImpl (uris [1 ], formats [1 ], docs [1 ], testObj ::sendReceiveManyDocs );
158+ }
137159 @ Test
138- public void sendReceiveManyNullDocsTest () throws IOException {
160+ public void sendReceiveManyNullDocsTest () {
139161 try {
140162 testDocsImpl (uris , null , null , testObj ::sendReceiveManyDocs );
141163 fail ("no exception for required parameter with null value" );
@@ -146,7 +168,7 @@ public void sendReceiveManyNullDocsTest() throws IOException {
146168 }
147169 }
148170 @ Test
149- public void sendReceiveAnyInvalidFormatDocsTest () throws IOException {
171+ public void sendReceiveAnyInvalidFormatDocsTest () {
150172 // negative test: send handle with invalid format
151173 try {
152174 testDocsImpl (uris [1 ], formats [0 ], docs [1 ], testObj ::sendReceiveAnyDocs );
@@ -157,21 +179,28 @@ public void sendReceiveAnyInvalidFormatDocsTest() throws IOException {
157179 }
158180 }
159181
160- private void testDocImpl (String uri , Format format , String doc , BiFunction <String , InputStreamHandle , InputStreamHandle > caller ) {
161- InputStreamHandle inputHandle = null ;
162- if (doc != null ) {
163- inputHandle = new InputStreamHandle ();
164- if (format != null ) {
165- inputHandle .setFormat (format );
166- }
167- inputHandle .fromBuffer (doc .getBytes ());
182+ private void testStringImpl (String uri , Format format , String doc , BiFunction <String , StringHandle , StringHandle > caller ) {
183+ testDocImpl (uri , format , (doc == null ) ? null : new StringHandle (doc ), caller );
184+ }
185+ private void testInputStreamImpl (String uri , Format format , String doc , BiFunction <String , InputStreamHandle , InputStreamHandle > caller ) {
186+ testDocImpl (uri , format , (doc == null ) ? null : new InputStreamHandle (new ByteArrayInputStream (doc .getBytes ())), caller );
187+ }
188+ private <T extends BufferableContentHandle <?,?>> void testDocImpl (String uri , Format format , T inputHandle , BiFunction <String , T , T > caller ) {
189+ if (inputHandle != null && format != null ) {
190+ BaseHandle <?, ?> inputBase = (BaseHandle <?, ?>) inputHandle ;
191+ inputBase .setFormat (format );
168192 }
193+ testDocImpl (uri , inputHandle , caller );
194+ }
195+ private <T extends BufferableContentHandle <?,?>> void testDocImpl (String uri , T inputHandle , BiFunction <String , T , T > caller ) {
196+ T outputHandle = caller .apply (uri , inputHandle );
169197
170- InputStreamHandle outputHandle = caller .apply (uri , inputHandle );
171-
172- if (doc != null ) {
173- assertEquals (format , outputHandle .getFormat ());
174- assertEquals (doc , new String (outputHandle .toBuffer ()).trim ());
198+ if (inputHandle != null ) {
199+ assertNotNull (outputHandle );
200+ BaseHandle <?,?> inputBase = (BaseHandle <?,?>) inputHandle ;
201+ BaseHandle <?,?> outputBase = (BaseHandle <?,?>) outputHandle ;
202+ assertEquals (inputBase .getFormat (), outputBase .getFormat ());
203+ assertEquals (new String (inputHandle .toBuffer ()).trim (), new String (outputHandle .toBuffer ()).trim ());
175204 } else {
176205 assertNull (outputHandle );
177206 }
0 commit comments