Skip to content

Commit 8b875bf

Browse files
ehennumehennum
ehennum
authored andcommitted
#1296 anyDocument unit tests and fixes
1 parent d183231 commit 8b875bf

File tree

13 files changed

+455
-56
lines changed

13 files changed

+455
-56
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

+25-15
Original file line numberDiff line numberDiff line change
@@ -1908,9 +1908,13 @@ static private void updateFormat(ContentDescriptor descriptor, Format format) {
19081908

19091909
static private Format getHeaderFormat(Headers headers) {
19101910
String format = headers.get(HEADER_VND_MARKLOGIC_DOCUMENT_FORMAT);
1911-
if (format != null) {
1911+
if (format != null && format.length() > 0) {
19121912
return Format.valueOf(format.toUpperCase());
19131913
}
1914+
String contentType = headers.get(HEADER_CONTENT_TYPE);
1915+
if (contentType != null && contentType.length() > 0) {
1916+
return Format.getFromMimetype(contentType);
1917+
}
19141918
return null;
19151919
}
19161920

@@ -6088,7 +6092,7 @@ public <C,R> C asContent(BufferableContentHandle<C,R> outputHandle) {
60886092
return content;
60896093
}
60906094
@Override
6091-
public <C,R> BufferableContentHandle<C,R> asHandle(BufferableContentHandle<C,R> outputHandle) {
6095+
public <T extends BufferableContentHandle<?,?>> T asHandle(T outputHandle) {
60926096
if (responseBody == null) return null;
60936097

60946098
return updateHandle(getResponse().headers(), responseBody, outputHandle);
@@ -6560,12 +6564,14 @@ static protected boolean checkNull(ResponseBody body, Format expectedFormat) {
65606564
"Returned document with unknown mime type instead of "+expectedFormat.getDefaultMimetype()
65616565
);
65626566
}
6563-
Format actualFormat = Format.getFromMimetype(actualType.toString());
6564-
if (expectedFormat != actualFormat) {
6565-
body.close();
6566-
throw new RuntimeException(
6567-
"Mime type "+actualType.toString()+" for returned document not recognized for "+expectedFormat.name()
6568-
);
6567+
if (expectedFormat != Format.UNKNOWN) {
6568+
Format actualFormat = Format.getFromMimetype(actualType.toString());
6569+
if (expectedFormat != actualFormat) {
6570+
body.close();
6571+
throw new RuntimeException(
6572+
"Mime type "+actualType.toString()+" for returned document not recognized for "+expectedFormat.name()
6573+
);
6574+
}
65696575
}
65706576
return false;
65716577
}
@@ -6583,11 +6589,13 @@ static protected boolean checkNull(MimeMultipart multipart, Format expectedForma
65836589
"Returned document with unknown mime type instead of "+expectedFormat.getDefaultMimetype()
65846590
);
65856591
}
6586-
Format actualFormat = Format.getFromMimetype(actualType);
6587-
if (expectedFormat != actualFormat) {
6588-
throw new RuntimeException(
6589-
"Mime type "+actualType+" for returned document not recognized for "+expectedFormat.name()
6590-
);
6592+
if (expectedFormat != Format.UNKNOWN) {
6593+
Format actualFormat = Format.getFromMimetype(actualType);
6594+
if (expectedFormat != actualFormat) {
6595+
throw new RuntimeException(
6596+
"Mime type "+actualType+" for returned document not recognized for "+expectedFormat.name()
6597+
);
6598+
}
65916599
}
65926600
return false;
65936601
}
@@ -6604,8 +6612,10 @@ static private void closeResponse(Response response) {
66046612
}
66056613

66066614
Request.Builder forDocumentResponse(Request.Builder requestBldr, Format format) {
6607-
return requestBldr.addHeader(HEADER_ACCEPT, (format == null || format == Format.BINARY) ?
6608-
"application/x-unknown-content-type" : format.getDefaultMimetype());
6615+
return requestBldr.addHeader(
6616+
HEADER_ACCEPT,
6617+
(format == null || format == Format.BINARY || format == Format.UNKNOWN) ?
6618+
"application/x-unknown-content-type" : format.getDefaultMimetype());
66096619
}
66106620

66116621
Request.Builder forMultipartMixedResponse(Request.Builder requestBldr) {

marklogic-client-api/src/main/java/com/marklogic/client/impl/RESTServices.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ interface CallResponse {
596596
interface SingleCallResponse extends CallResponse {
597597
byte[] asBytes();
598598
<C,R> C asContent(BufferableContentHandle<C,R> outputHandle);
599-
<C,R> BufferableContentHandle<C,R> asHandle(BufferableContentHandle<C,R> outputHandle);
599+
<T extends BufferableContentHandle<?,?>> T asHandle(T outputHandle);
600600
InputStream asInputStream();
601601
InputStreamHandle asInputStreamHandle();
602602
Reader asReader();

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

+32-19
Original file line numberDiff line numberDiff line change
@@ -645,26 +645,36 @@ ${funcDecls}
645645
""")}
646646
)"""
647647

648-
val returnConverter =
649-
if (returnType === null || returnMapped === null)
650-
""
651-
else
648+
val returnFormat =
649+
if (returnType === null || returnKind != "document") "null"
650+
else typeFormat(returnType)
651+
val returnChained =
652+
if (returnKind === null) """.responseNone()"""
653+
else if (returnMultiple == true) """.responseMultiple(${returnNullable}, ${returnFormat})"""
654+
else """.responseSingle(${returnNullable}, ${returnFormat})"""
655+
656+
val callImpl = """request${sessionFluent}${paramsFluent}${returnChained}"""
657+
658+
// TODO: also array support?
659+
val bodyImpl =
660+
if (returnType === null || returnMapped === null) {
661+
"""${callImpl};"""
662+
} else if (returnType == "anyDocument") {
663+
"""return ${callImpl}
664+
${
665+
if (returnMultiple) """.asStreamOfHandles(null, new ${returnMapped}())"""
666+
else """.asHandle(new ${returnMapped}())"""
667+
};"""
668+
} else {
652669
"""return BaseProxy.${typeConverter(returnType)}.to${
653670
if (returnMapped.contains("."))
654671
returnMapped.substringAfterLast(".").capitalize()
655672
else
656673
returnMapped.capitalize()
657674
}(
658-
"""
659-
val returnFormat =
660-
if (returnType === null || returnKind != "document") "null"
661-
else typeFormat(returnType)
662-
val returnChained =
663-
if (returnKind === null) """.responseNone()"""
664-
else if (returnMultiple == true) """.responseMultiple(${returnNullable}, ${returnFormat})
665-
)"""
666-
else """.responseSingle(${returnNullable}, ${returnFormat})
667-
)"""
675+
${callImpl}
676+
);"""
677+
}
668678

669679
val fieldReturn =
670680
if (returnType === null) ""
@@ -710,8 +720,7 @@ ${funcDecls}
710720
);
711721
}
712722
private ${sigImpl} {
713-
${returnConverter
714-
}request${sessionFluent}${paramsFluent}${returnChained};
723+
${bodyImpl}
715724
}"""
716725
return defSource
717726
}
@@ -797,13 +806,16 @@ ${funcDecls}
797806
return methods
798807
}
799808
fun paramConverter(paramName: String, paramKind: String, paramType: String, mappedType: String, isNullable: Boolean) : String {
800-
val converter =
801-
"""BaseProxy.${paramKind}Param("${paramName}", ${isNullable}, BaseProxy.${typeConverter(paramType)}.from${
809+
val convertExpr =
810+
if (paramType == "anyDocument") paramName
811+
else """BaseProxy.${typeConverter(paramType)}.from${
802812
if (mappedType.contains("."))
803813
mappedType.substringAfterLast(".").capitalize()
804814
else
805815
mappedType.capitalize()
806-
}(${paramName}))"""
816+
}(${paramName})"""
817+
val converter =
818+
"""BaseProxy.${paramKind}Param("${paramName}", ${isNullable}, ${convertExpr})"""
807819
return converter
808820
}
809821
fun typeConverter(datatype: String) : String {
@@ -816,6 +828,7 @@ ${funcDecls}
816828
fun typeFormat(documentType: String) : String {
817829
val format =
818830
if (documentType == "array" || documentType == "object") "Format.JSON"
831+
else if (documentType == "anyDocument") "Format.UNKNOWN"
819832
else "Format."+documentType.substringBefore("Document").toUpperCase()
820833
return format
821834
}

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

+96-20
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,123 @@ final class AnyDocumentBundleImpl implements AnyDocumentBundle {
4848
private DatabaseClient dbClient;
4949
private BaseProxy baseProxy;
5050

51-
private BaseProxy.DBFunctionRequest req_sendReceiveDocs;
51+
private BaseProxy.DBFunctionRequest req_sendReceiveManyDocs;
52+
private BaseProxy.DBFunctionRequest req_sendReceiveRequiredDoc;
53+
private BaseProxy.DBFunctionRequest req_sendReceiveOptionalDoc;
54+
private BaseProxy.DBFunctionRequest req_sendReceiveAnyDocs;
5255

5356
private AnyDocumentBundleImpl(DatabaseClient dbClient, JSONWriteHandle servDecl) {
5457
this.dbClient = dbClient;
5558
this.baseProxy = new BaseProxy("/dbf/test/anyDocument/", servDecl);
5659

57-
this.req_sendReceiveDocs = this.baseProxy.request(
58-
"sendReceiveDocs.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
60+
this.req_sendReceiveManyDocs = this.baseProxy.request(
61+
"sendReceiveManyDocs.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
62+
this.req_sendReceiveRequiredDoc = this.baseProxy.request(
63+
"sendReceiveRequiredDoc.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
64+
this.req_sendReceiveOptionalDoc = this.baseProxy.request(
65+
"sendReceiveOptionalDoc.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
66+
this.req_sendReceiveAnyDocs = this.baseProxy.request(
67+
"sendReceiveAnyDocs.sjs", BaseProxy.ParameterValuesKind.MULTIPLE_MIXED);
5968
}
6069

6170
@Override
62-
public Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
63-
return sendReceiveDocs(
64-
this.req_sendReceiveDocs.on(this.dbClient), uris, docs
71+
public Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveManyDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
72+
return sendReceiveManyDocs(
73+
this.req_sendReceiveManyDocs.on(this.dbClient), uris, docs
6574
);
6675
}
67-
private Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveDocs(BaseProxy.DBFunctionRequest request, Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
68-
/* TODO:
69-
generate code that
70-
instead of wrapping with BaseProxy.AnyDocumentType.toInputStreamHandle()
71-
calls .asStreamOfHandles()
72-
*/
73-
return request
74-
.withParams(
75-
BaseProxy.atomicParam("uris", true, BaseProxy.StringType.fromString(uris)),
76-
BaseProxy.documentParam("docs", true, docs)
77-
).responseMultiple(true, Format.UNKNOWN)
78-
.asStreamOfHandles(null, new com.marklogic.client.io.InputStreamHandle());
76+
private Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveManyDocs(BaseProxy.DBFunctionRequest request, Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
77+
return request
78+
.withParams(
79+
BaseProxy.atomicParam("uris", false, BaseProxy.StringType.fromString(uris)),
80+
BaseProxy.documentParam("docs", false, docs)
81+
).responseMultiple(false, Format.UNKNOWN)
82+
.asStreamOfHandles(null, new com.marklogic.client.io.InputStreamHandle());
83+
}
84+
85+
@Override
86+
public com.marklogic.client.io.InputStreamHandle sendReceiveRequiredDoc(String uri, com.marklogic.client.io.InputStreamHandle doc) {
87+
return sendReceiveRequiredDoc(
88+
this.req_sendReceiveRequiredDoc.on(this.dbClient), uri, doc
89+
);
90+
}
91+
private com.marklogic.client.io.InputStreamHandle sendReceiveRequiredDoc(BaseProxy.DBFunctionRequest request, String uri, com.marklogic.client.io.InputStreamHandle doc) {
92+
return request
93+
.withParams(
94+
BaseProxy.atomicParam("uri", false, BaseProxy.StringType.fromString(uri)),
95+
BaseProxy.documentParam("doc", false, doc)
96+
).responseSingle(false, Format.UNKNOWN)
97+
.asHandle(new com.marklogic.client.io.InputStreamHandle());
98+
}
99+
100+
@Override
101+
public com.marklogic.client.io.InputStreamHandle sendReceiveOptionalDoc(String uri, com.marklogic.client.io.InputStreamHandle doc) {
102+
return sendReceiveOptionalDoc(
103+
this.req_sendReceiveOptionalDoc.on(this.dbClient), uri, doc
104+
);
105+
}
106+
private com.marklogic.client.io.InputStreamHandle sendReceiveOptionalDoc(BaseProxy.DBFunctionRequest request, String uri, com.marklogic.client.io.InputStreamHandle doc) {
107+
return request
108+
.withParams(
109+
BaseProxy.atomicParam("uri", true, BaseProxy.StringType.fromString(uri)),
110+
BaseProxy.documentParam("doc", true, doc)
111+
).responseSingle(true, Format.UNKNOWN)
112+
.asHandle(new com.marklogic.client.io.InputStreamHandle());
113+
}
114+
115+
@Override
116+
public Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
117+
return sendReceiveAnyDocs(
118+
this.req_sendReceiveAnyDocs.on(this.dbClient), uris, docs
119+
);
120+
}
121+
private Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(BaseProxy.DBFunctionRequest request, Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs) {
122+
return request
123+
.withParams(
124+
BaseProxy.atomicParam("uris", true, BaseProxy.StringType.fromString(uris)),
125+
BaseProxy.documentParam("docs", true, docs)
126+
).responseMultiple(true, Format.UNKNOWN)
127+
.asStreamOfHandles(null, new com.marklogic.client.io.InputStreamHandle());
79128
}
80129
}
81130

82131
return new AnyDocumentBundleImpl(db, serviceDeclaration);
83132
}
84133

85134
/**
86-
* Invokes the sendReceiveDocs operation on the database server
135+
* Invokes the sendReceiveManyDocs operation on the database server
136+
*
137+
* @param uris provides input
138+
* @param docs provides input
139+
* @return as output
140+
*/
141+
Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveManyDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs);
142+
143+
/**
144+
* Invokes the sendReceiveRequiredDoc operation on the database server
145+
*
146+
* @param uri provides input
147+
* @param doc provides input
148+
* @return as output
149+
*/
150+
com.marklogic.client.io.InputStreamHandle sendReceiveRequiredDoc(String uri, com.marklogic.client.io.InputStreamHandle doc);
151+
152+
/**
153+
* Invokes the sendReceiveOptionalDoc operation on the database server
154+
*
155+
* @param uri provides input
156+
* @param doc provides input
157+
* @return as output
158+
*/
159+
com.marklogic.client.io.InputStreamHandle sendReceiveOptionalDoc(String uri, com.marklogic.client.io.InputStreamHandle doc);
160+
161+
/**
162+
* Invokes the sendReceiveAnyDocs operation on the database server
87163
*
88164
* @param uris provides input
89165
* @param docs provides input
90166
* @return as output
91167
*/
92-
Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs);
168+
Stream<com.marklogic.client.io.InputStreamHandle> sendReceiveAnyDocs(Stream<String> uris, Stream<com.marklogic.client.io.InputStreamHandle> docs);
93169

94170
}

0 commit comments

Comments
 (0)