4242import okhttp3 .Request ;
4343import okhttp3 .Response ;
4444
45+ import com .marklogic .client .io .marker .JSONWriteHandle ;
46+
47+ import com .marklogic .client .impl .BaseProxy ;
48+
4549public class ClientApiFunctionalTest extends BasicJavaClientREST {
4650 private static String dbName = "TestClientAPIOneDB" ;
4751 private static String [] fNames = { "TestClientAPIOneDB-1" };
@@ -80,6 +84,8 @@ public class ClientApiFunctionalTest extends BasicJavaClientREST {
8084 private static String endPointURI_11 = endPointURI_5 ;
8185 // For testing Open API end
8286
87+ private static String endPointURI_12 = "/ext/TestE2EModuleXQY/TestE2EModuleXQY" ;
88+
8389 /* Note : In case there is a need to re-run the tests, please
8490 delete these App Servers manually. The db and forest for these are deleted in aftercalss().
8591 These server are left behind and in case of re-runs DB and forest creations will not happen,
@@ -108,7 +114,7 @@ public static void setUp() throws Exception {
108114 createForest (fNamesMod [0 ], dbNameMod );
109115 createAppServer (serverName , port );
110116
111- createUserRolesWithPrevilages ("apiRole" , "xdbc:eval" , "xdbc:eval-in" , "xdmp:eval-in" , "any-uri" , "xdbc:invoke" , "xdmp:eval" , "xdmp:eval-in" );
117+ createUserRolesWithPrevilages ("apiRole" , "xdbc:eval" , "xdbc:eval-in" , "xdmp:eval-in" , "any-uri" , "xdbc:invoke" , "xdmp:eval" , "xdmp:eval-in" , "xdmp:invoke" , "xdmp:invoke-in" );
112118 createRESTUser ("apiUser" , "ap1U53r" , "apiRole" , "rest-admin" , "rest-writer" , "rest-reader" ,
113119 "rest-extension-user" , "manage-user" );
114120 createRESTUser ("secondApiUser" , "ap1U53r" , "apiRole" , "rest-admin" , "rest-writer" , "rest-reader" ,
@@ -218,35 +224,46 @@ public static void setUp() throws Exception {
218224 file = null ;
219225 handle = null ;
220226 // For Open API tests end
227+
228+ file = new File ("src/test/java/com/marklogic/client/functionaltest/data/api/TestE2EModuleXQY.xqy" );
229+ handle = new FileHandle (file );
230+ docMgr .write (endPointURI_12 +".xqy" , metadataHandle , handle );
231+ file = null ;
232+ handle = null ;
233+ file = new File ("src/test/java/com/marklogic/client/functionaltest/data/api/TestE2EModuleXQY.api" );
234+ handle = new FileHandle (file );
235+ docMgr .write (endPointURI_12 +".api" , metadataHandle , handle );
236+ file = null ;
237+ handle = null ;
221238 }
222239
223240 @ AfterClass
224241 public static void tearDownAfterClass () throws Exception {
225- System .out .println ("In tear down" );
226242
227- deleteUserRole ("apiRole" );
228- deleteRESTUser ("apiUser" );
229- deleteRESTUser ("secondApiUser" );
230- deleteUserRole ("ForbiddenRole" );
231- deleteRESTUser ("ForbiddenUser" );
232-
233- // release client
234- dbclient .release ();
235- associateRESTServerWithDB (modServerName , "Documents" );
236- associateRESTServerWithDB (serverName , "Documents" );
237- associateRESTServerWithModuleDB (modServerName , "Modules" );
238- associateRESTServerWithModuleDB (serverName , "Modules" );
239-
240- associateRESTServerWithDB ("TestRESTServerOnAPI" , "Documents" );
241- associateRESTServerWithModuleDB ("TestRESTServerOnAPI" , "Modules" );
242- deleteDB (dbName );
243- deleteForest (fNames [0 ]);
244-
245- deleteDB (dbNameMod );
246- deleteForest (fNamesMod [0 ]);
247-
248- deleteDB ("TestRESTServerOnAPI-modules" );
249- deleteForest ("TestRESTServerOnAPI-modules-1" );
243+ System .out .println ("In tear down" );
244+ deleteUserRole ("apiRole" );
245+ deleteRESTUser ("apiUser" );
246+ deleteRESTUser ("secondApiUser" );
247+ deleteUserRole ("ForbiddenRole" );
248+ deleteRESTUser ("ForbiddenUser" );
249+
250+ // release client
251+ dbclient .release ();
252+ associateRESTServerWithDB (modServerName , "Documents" );
253+ associateRESTServerWithDB (serverName , "Documents" );
254+ associateRESTServerWithModuleDB (modServerName , "Modules" );
255+ associateRESTServerWithModuleDB (serverName , "Modules" );
256+
257+ associateRESTServerWithDB ("TestRESTServerOnAPI" , "Documents" );
258+ associateRESTServerWithModuleDB ("TestRESTServerOnAPI" , "Modules" );
259+ deleteDB (dbName );
260+ deleteForest (fNames [0 ]);
261+
262+ deleteDB (dbNameMod );
263+ deleteForest (fNamesMod [0 ]);
264+
265+ deleteDB ("TestRESTServerOnAPI-modules" );
266+ deleteForest ("TestRESTServerOnAPI-modules-1" );
250267 }
251268
252269 public static void createAppServer (String appServerName , int restPort ) {
@@ -374,7 +391,20 @@ public void TestE2EItemPrice() throws Exception {
374391 assertEquals (20000.0 , responseBack12 , 0.00 );
375392 }
376393
377- // This test requires TestE2ERequiredParam.api Fn Decl file
394+ @ Test
395+ public void TestE2EXqyFunction () throws Exception {
396+ System .out .println ("Running TestE2EXqyFunction" );
397+ // Invoke the function
398+ String responseBack1 = TestE2EModuleXQY .on (dbclient ).xqyfunction ("MAGLITE" );
399+ System .out .println ("Response from the Client API call is " + responseBack1 );
400+ assertTrue ("Response when valid parameter passed incorrect" , responseBack1 .contains ("QA Module Returns MAGLITE" ));
401+ // Pass null for parameter
402+ String responseBack2 = TestE2EModuleXQY .on (dbclient ).xqyfunction (null );
403+ System .out .println ("Response from the Client API call is " + responseBack2 );
404+ assertTrue ("Response when null parameter passed incorrect" , responseBack2 .contains ("QA Module Returns Passed in null parameter." ));
405+ }
406+
407+ // This test requires TestE2ERequiredParam.api Fn Decl file
378408 @ Test
379409 public void TestE2ERequiredParam () throws Exception {
380410
@@ -1074,4 +1104,77 @@ public void SessionChecks(SessionState api_session, String uri, String content)
10741104 */
10751105 void SessionChecks (SessionState api_session , String uri , String content );
10761106
1107+ }
1108+
1109+ interface TestE2EModuleXQY {
1110+ /**
1111+ * Creates a TestE2EModuleXQY object for executing operations on the database server.
1112+ *
1113+ * The DatabaseClientFactory class can create the DatabaseClient parameter. A single
1114+ * client object can be used for any number of requests and in multiple threads.
1115+ *
1116+ * @param db provides a client for communicating with the database server
1117+ * @return an object for executing database operations
1118+ */
1119+ static TestE2EModuleXQY on (DatabaseClient db ) {
1120+ return on (db , null );
1121+ }
1122+ /**
1123+ * Creates a TestE2EModuleXQY object for executing operations on the database server.
1124+ *
1125+ * The DatabaseClientFactory class can create the DatabaseClient parameter. A single
1126+ * client object can be used for any number of requests and in multiple threads.
1127+ *
1128+ * The service declaration uses a custom implementation of the same service instead
1129+ * of the default implementation of the service by specifying an endpoint directory
1130+ * in the modules database with the implementation. A service.json file with the
1131+ * declaration can be read with FileHandle or a string serialization of the JSON
1132+ * declaration with StringHandle.
1133+ *
1134+ * @param db provides a client for communicating with the database server
1135+ * @param serviceDeclaration substitutes a custom implementation of the service
1136+ * @return an object for executing database operations
1137+ */
1138+ static TestE2EModuleXQY on (DatabaseClient db , JSONWriteHandle serviceDeclaration ) {
1139+ final class TestE2EModuleXQYImpl implements TestE2EModuleXQY {
1140+ private DatabaseClient dbClient ;
1141+ private BaseProxy baseProxy ;
1142+
1143+ private BaseProxy .DBFunctionRequest req_xqyfunction ;
1144+
1145+ private TestE2EModuleXQYImpl (DatabaseClient dbClient , JSONWriteHandle servDecl ) {
1146+ this .dbClient = dbClient ;
1147+ this .baseProxy = new BaseProxy ("/ext/TestE2EModuleXQY/" , servDecl );
1148+
1149+ this .req_xqyfunction = this .baseProxy .request (
1150+ "TestE2EModuleXQY.xqy" , BaseProxy .ParameterValuesKind .SINGLE_ATOMIC );
1151+ }
1152+
1153+ @ Override
1154+ public String xqyfunction (String items ) {
1155+ return xqyfunction (
1156+ this .req_xqyfunction .on (this .dbClient ), items
1157+ );
1158+ }
1159+ private String xqyfunction (BaseProxy .DBFunctionRequest request , String items ) {
1160+ return BaseProxy .StringType .toString (
1161+ request
1162+ .withParams (
1163+ BaseProxy .atomicParam ("items" , true , BaseProxy .StringType .fromString (items ))
1164+ ).responseSingle (false , null )
1165+ );
1166+ }
1167+ }
1168+
1169+ return new TestE2EModuleXQYImpl (db , serviceDeclaration );
1170+ }
1171+
1172+ /**
1173+ * Invokes the xqyfunction operation on the database server
1174+ *
1175+ * @param items provides input
1176+ * @return as output
1177+ */
1178+ String xqyfunction (String items );
1179+
10771180}
0 commit comments