Skip to content

Commit 75864b7

Browse files
ehennumehennum
authored andcommitted
#1302 generate classes with mapped handles
1 parent 8b875bf commit 75864b7

File tree

6 files changed

+160
-73
lines changed

6 files changed

+160
-73
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/InputOutputCaller.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ static <I,O> InputOutputCaller<I,O> on(
5050
return new InputOutputEndpointImpl<>(client, apiDecl, new HandleProvider.ContentHandleProvider<>(inputHandle, outputHandle));
5151
}
5252

53-
/* TODO: ADD TO ISSUE -- ALSO FOR InputCaller AND OutputCaller -- AND DELETE
54-
static <IC,IR,OC,OR> InputOutputCaller<BufferableContentHandle<IC,IR>,BufferableContentHandle<OC,OR>> onHandles(
55-
DatabaseClient client, JSONWriteHandle apiDecl,
56-
BufferableContentHandle<IC,IR> inputHandle, BufferableContentHandle<OC,OR> outputHandle
57-
) {
58-
return new InputOutputEndpointImpl<>(client, apiDecl, new HandleProvider.DirectHandleProvider<>(inputHandle, outputHandle));
59-
}
60-
*/
61-
6253
/**
6354
* Makes one call to an endpoint that doesn't take endpoint constants, endpoint state, or a session.
6455
* @param input the request data sent to the endpoint

ml-development-tools/src/main/kotlin/com/marklogic/client/tools/proxy/Generator.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode
2020
import com.fasterxml.jackson.databind.node.ObjectNode
2121
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
2222
import com.fasterxml.jackson.module.kotlin.readValue
23+
import com.marklogic.client.io.marker.BufferableContentHandle
2324
import java.io.File
2425
import com.networknt.schema.*
2526

@@ -233,12 +234,22 @@ class Generator {
233234

234235
val allMappings = getAllMappings()
235236
val typeMappings = allMappings[dataType]
236-
if (typeMappings === null) {
237+
if (typeMappings?.contains(mapping) == true) {
238+
return mapping
239+
} else if (dataType == "anyDocument") {
240+
try {
241+
val mappingClass = Class.forName(mapping)
242+
if (!BufferableContentHandle::class.java.isAssignableFrom(mappingClass)) {
243+
throw IllegalArgumentException("""mapped class ${mapping} for anyDocument data type must extend BufferableContentHandle""")
244+
}
245+
return mapping
246+
} catch(e: ClassNotFoundException) {
247+
throw IllegalArgumentException("""could not load mapped class ${mapping} for anyDocument data type""")
248+
}
249+
} else if (typeMappings === null) {
237250
throw IllegalArgumentException("""no mappings for data type ${dataType}""")
238-
} else if (!typeMappings.contains(mapping)) {
239-
throw IllegalArgumentException("""no mapping to ${mapping} for data type ${dataType}""")
240251
}
241-
return mapping
252+
throw IllegalArgumentException("""no mapping to ${mapping} for data type ${dataType}""")
242253
}
243254
fun getSigDataType(mappedType: String, isMultiple: Boolean): String {
244255
val sigType =

ml-development-tools/src/test/java/com/marklogic/client/test/dbfunction/positive/AnyDocumentBundle.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ final class AnyDocumentBundleImpl implements AnyDocumentBundle {
5252
private BaseProxy.DBFunctionRequest req_sendReceiveRequiredDoc;
5353
private BaseProxy.DBFunctionRequest req_sendReceiveOptionalDoc;
5454
private BaseProxy.DBFunctionRequest req_sendReceiveAnyDocs;
55+
private BaseProxy.DBFunctionRequest req_sendReceiveMappedDoc;
5556

5657
private AnyDocumentBundleImpl(DatabaseClient dbClient, JSONWriteHandle servDecl) {
5758
this.dbClient = dbClient;
@@ -65,6 +66,8 @@ private AnyDocumentBundleImpl(DatabaseClient dbClient, JSONWriteHandle servDecl)
6566
"sendReceiveOptionalDoc.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
6667
this.req_sendReceiveAnyDocs = this.baseProxy.request(
6768
"sendReceiveAnyDocs.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
69+
this.req_sendReceiveMappedDoc = this.baseProxy.request(
70+
"sendReceiveMappedDoc.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
6871
}
6972

7073
@Override
@@ -126,6 +129,21 @@ private Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(Bas
126129
).responseMultiple(true, Format.UNKNOWN)
127130
.asStreamOfHandles(null, new com.marklogic.client.io.InputStreamHandle());
128131
}
132+
133+
@Override
134+
public com.marklogic.client.io.StringHandle sendReceiveMappedDoc(String uri, com.marklogic.client.io.StringHandle doc) {
135+
return sendReceiveMappedDoc(
136+
this.req_sendReceiveMappedDoc.on(this.dbClient), uri, doc
137+
);
138+
}
139+
private com.marklogic.client.io.StringHandle sendReceiveMappedDoc(BaseProxy.DBFunctionRequest request, String uri, com.marklogic.client.io.StringHandle doc) {
140+
return request
141+
.withParams(
142+
BaseProxy.atomicParam("uri", true, BaseProxy.StringType.fromString(uri)),
143+
BaseProxy.documentParam("doc", true, doc)
144+
).responseSingle(true, Format.UNKNOWN)
145+
.asHandle(new com.marklogic.client.io.StringHandle());
146+
}
129147
}
130148

131149
return new AnyDocumentBundleImpl(db, serviceDeclaration);
@@ -167,4 +185,13 @@ private Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(Bas
167185
*/
168186
Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs);
169187

188+
/**
189+
* Invokes the sendReceiveMappedDoc operation on the database server
190+
*
191+
* @param uri provides input
192+
* @param doc provides input
193+
* @return as output
194+
*/
195+
com.marklogic.client.io.StringHandle sendReceiveMappedDoc(String uri, com.marklogic.client.io.StringHandle doc);
196+
170197
}

ml-development-tools/src/test/java/com/marklogic/client/test/dbfunction/positive/AnyDocumentBundleTest.java

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
import com.marklogic.client.FailedRequestException;
1919
import com.marklogic.client.impl.BaseProxy;
20+
import com.marklogic.client.io.BaseHandle;
2021
import com.marklogic.client.io.Format;
2122
import com.marklogic.client.io.InputStreamHandle;
23+
import com.marklogic.client.io.StringHandle;
24+
import com.marklogic.client.io.marker.BufferableContentHandle;
2225
import com.marklogic.client.test.dbfunction.DBFunctionTestUtil;
2326
import org.junit.Test;
2427

25-
import java.io.IOException;
28+
import java.io.ByteArrayInputStream;
2629
import java.util.function.BiFunction;
2730
import 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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"functionName": "sendReceiveMappedDoc",
3+
"params": [ {
4+
"name": "uri",
5+
"datatype": "string",
6+
"multiple": false,
7+
"nullable": true
8+
}, {
9+
"name": "doc",
10+
"datatype": "anyDocument",
11+
"multiple": false,
12+
"nullable": true,
13+
"$javaClass" : "com.marklogic.client.io.StringHandle"
14+
} ],
15+
"return": {
16+
"datatype": "anyDocument",
17+
"multiple": false,
18+
"nullable": true,
19+
"$javaClass" : "com.marklogic.client.io.StringHandle"
20+
}
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
var uri;
3+
var doc;
4+
5+
// could write to the database at this point...
6+
7+
// instead, just return the documents in the same order for inspection on the client
8+
doc;

0 commit comments

Comments
 (0)