1616
1717package com .marklogic .client .functionaltest ;
1818
19- import static org .junit .Assert .* ;
19+ import static org .junit .Assert .assertEquals ;
2020
2121import java .io .FileInputStream ;
2222import java .util .ArrayList ;
2323import java .util .List ;
2424
25- import javax .swing .text .html .FormSubmitEvent ;
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41- import org .json .JSONException ;
4225import org .junit .After ;
4326import org .junit .AfterClass ;
4427import org .junit .Before ;
5437import com .marklogic .client .admin .MethodType ;
5538import com .marklogic .client .admin .ResourceExtensionsManager ;
5639import com .marklogic .client .admin .ResourceExtensionsManager .MethodParameters ;
57- import com .marklogic .client .document .JSONDocumentManager ;
58- import com .marklogic .client .document .XMLDocumentManager ;
5940import com .marklogic .client .extensions .ResourceManager ;
6041import com .marklogic .client .extensions .ResourceServices .ServiceResult ;
6142import com .marklogic .client .extensions .ResourceServices .ServiceResultIterator ;
6647import com .marklogic .client .util .RequestParameters ;
6748
6849public class TestJSResourceExtensions extends BasicJavaClientREST {
69- private static final int BATCH_SIZE =100 ;
70- private static final String DIRECTORY ="/bulkTransform/" ;
7150 private static String dbName = "TestJSResourceExtensionDB" ;
7251 private static String [] fNames = {"TestResourceExtensionDB-1" };
7352 private static String restServerName = "REST-Java-Client-API-Server" ;
@@ -78,16 +57,10 @@ public class TestJSResourceExtensions extends BasicJavaClientREST {
7857 static public class TestJSExtension extends ResourceManager {
7958 static final public String NAME = "simpleJSResourceModule" ;
8059 static final public ExtensionMetadata .ScriptLanguage scriptLanguage = ExtensionMetadata .JAVASCRIPT ;
81- private JSONDocumentManager docMgr ;
82-
83-
8460 public TestJSExtension (DatabaseClient client ) {
8561 super ();
8662 // a Resource Manager must be initialized by a Database Client
8763 client .init (NAME , this );
88-
89- // delegates some services to a document manager
90- docMgr = client .newJSONDocumentManager ();
9164 }
9265
9366 public String getJSON (String docUri ) {
@@ -111,18 +84,17 @@ public String getJSON(String docUri) {
11184 result .getContent (readHandle );
11285 responses .add (readHandle .get ());
11386 }
114-
11587 // release the iterator resources
11688 resultItr .close ();
117-
11889 return responses .get (0 );
11990 }
91+
12092 public String postJSON (String docUri ) {
12193 RequestParameters params = new RequestParameters ();
12294 params .add ("uri" , docUri );
12395 // specify the mime type for each expected document returned
12496 String [] mimetypes = new String [] {"text/plain" };
125- StringHandle output = new StringHandle ();
97+ new StringHandle ();
12698 String input = "{\" array\" : [1,2,3]}" ;
12799 // call the service
128100 ServiceResultIterator resultItr = getServices ().post (params , new StringHandle (input ).withFormat (Format .JSON ), mimetypes );
@@ -135,46 +107,41 @@ public String postJSON(String docUri) {
135107 result .getContent (readHandle );
136108 responses .add (readHandle .get ());
137109 }
138-
139110 // release the iterator resources
140111 resultItr .close ();
141112 return responses .get (0 );
142113 }
114+
143115 public String putJSON (String docUri ) {
144116 RequestParameters params = new RequestParameters ();
145117 params .add ("uri" , docUri );
146- // specify the mime type for each expected document returned
147- String [] mimetypes = new String [] {"text/plain" };
148- StringHandle output = new StringHandle ();
118+ new StringHandle ();
149119 String input = "{\" argument1\" :\" hello\" , \" argument2\" :\" Earth\" , \" content\" :\" This is a JSON document\" , \" response\" :[200, \" OK\" ], \" outputTypes\" :\" application/json\" }" ;
150120 StringHandle readHandle = new StringHandle ();
151121 // call the service
152122 getServices ().put (params , new StringHandle (input ).withFormat (Format .JSON ), readHandle );
153123 // iterate over the results
154-
155124 return readHandle .get ();
156125 }
126+
157127 public String deleteJSON (String docUri ) {
158128 RequestParameters params = new RequestParameters ();
159129 params .add ("uri" , docUri );
160- // specify the mime type for each expected document returned
161- String [] mimetypes = new String [] {"text/plain" };
162130 StringHandle output = new StringHandle ();
163131 // call the service
164132 getServices ().delete (params , output );
165133 // iterate over the results
166-
167134 return output .get ();
168135 }
169136 }
137+
170138 @ BeforeClass
171139 public static void setUpBeforeClass () throws Exception {
172140 System .out .println ("In setup" );
173141 setupJavaRESTServer (dbName , fNames [0 ], restServerName ,restPort ,false );
174- createUserRolesWithPrevilages ("test-eval" ,"xdbc:eval" , "xdbc:eval-in" ,"xdmp:eval-in" ,"any-uri" ,"xdbc:invoke" );
175- createRESTUser ("eval-user" , "x" , "test-eval" ,"rest-admin" ,"rest-writer" ,"rest-reader" );
176- // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
177-
142+ createUserRolesWithPrevilages ("test-eval" ,"xdbc:eval" , "xdbc:eval-in" ,"xdmp:eval-in" ,"any-uri" , "xdmp-eval" , "xdmp:value" , "xdbc:invoke" );
143+ createRESTUser ("eval-user" , "x" , "test-eval" ,"rest-admin" ,"rest-writer" ,"rest-reader" , "rest-extension-user" );
144+ //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
178145 }
179146
180147 @ AfterClass
@@ -202,20 +169,16 @@ public void setUp() throws Exception {
202169 InputStreamHandle handle = new InputStreamHandle (myStream );
203170 handle .set (myStream );
204171 resourceMgr .writeServices ("simpleJSResourceModule" , handle , resextMetadata ,getParams );
205-
206-
207172 }
208173
209174 @ After
210175 public void tearDown () throws Exception {
211176 resourceMgr .deleteServices ("simpleJSResourceModule" );
212177 client .release ();
213-
214178 }
215179
216180 @ Test
217181 public void test1GetAllResourceServices () throws Exception {
218-
219182 JacksonHandle jh = new JacksonHandle ();
220183 resourceMgr .listServices (jh );
221184 String expectedList ="{\" resources\" :{\" resource\" :[{\" name\" :\" simpleJSResourceModule\" , \" source-format\" :\" javascript\" , \" description\" :\" Testing resource extension for java script\" , \" version\" :\" 1.0\" , \" title\" :\" BasicJSTest\" , \" methods\" :{\" method\" :[{\" method-name\" :\" get\" , \" parameter\" :[{\" parameter-name\" :\" my-uri\" , \" parameter-type\" :\" xs:string?\" }]}, {\" method-name\" :\" post\" }, {\" method-name\" :\" put\" }, {\" method-name\" :\" delete\" }]}, \" resource-source\" :\" /v1/resources/simpleJSResourceModule\" }]}}" ;
@@ -229,42 +192,36 @@ public void test1GetAllResourceServices() throws Exception {
229192 String expAftrPost ="{\" argument1\" :\" helloJS.json\" , \" argument2\" :\" Earth\" , \" document-count\" :1, \" content\" :\" This is a JSON document\" , \" document-content\" :{\" argument1\" :\" hello\" , \" argument2\" :\" Earth\" , \" content\" :\" This is a JSON document\" , \" array\" :[1, 2, 3], \" response\" :[200, \" OK\" ], \" outputTypes\" :\" application/json\" }, \" response\" :[200, \" OK\" ], \" outputTypes\" :[\" application/json\" ]}" ;
230193 JSONAssert .assertEquals (expAftrPost , tjs .getJSON ("helloJS.json" ), false );
231194 String expected ="{\" argument1\" :\" helloJS.json\" , \" argument2\" :\" Earth\" , \" document-count\" :0, \" content\" :\" This is a JSON document\" , \" document-content\" :null, \" response\" :[200, \" OK\" ], \" outputTypes\" :[\" application/json\" ]}" ;
232- // JSONAssert.assertEquals(expected, tjs.getJSON(), false);
233195
234196 JSONAssert .assertEquals (expectedResponse , tjs .deleteJSON ("helloJS.json" ), false );
235- // System.out.println(tjs.getJSON());
236197 JSONAssert .assertEquals (expected , tjs .getJSON ("helloJS.json" ), false );
237-
238198 }
199+
239200 @ Test
240- public void test2GetAllResourceServicesMultipleTimes () throws Exception {
241-
201+ public void test2GetAllResourceServicesMultipleTimes () throws Exception {
242202 JacksonHandle jh = new JacksonHandle ();
243203
244204 TestJSExtension tjs = new TestJSExtension (client );
245205 String expectedResponse ="{\" response\" :[200, \" OK\" ]}" ;
246206 //load multiple documents using extension
247- for (int i =0 ;i <150 ;i ++){
248- JSONAssert .assertEquals (expectedResponse , tjs .putJSON ("helloJS" +i +".json" ), false );
249- JSONAssert .assertEquals (expectedResponse , tjs .postJSON ("helloJS" +i +".json" ), false );
207+ for (int i =0 ;i <150 ;i ++) {
208+ JSONAssert .assertEquals (expectedResponse , tjs .putJSON ("helloJS" +i +".json" ), false );
209+ JSONAssert .assertEquals (expectedResponse , tjs .postJSON ("helloJS" +i +".json" ), false );
250210 }
251211
252212 JacksonHandle jh2 = new JacksonHandle ();
253213 jh .set (jh2 .getMapper ().readTree (tjs .getJSON ("helloJS0.json" )));
254- // System.out.println(jh.get().toString());
255214 assertEquals ("Total documents loaded are" ,150 ,jh .get ().get ("document-count" ).intValue ());
256215
257216 String expAftrPut ="{\" argument1\" :\" hello\" , \" argument2\" :\" Earth\" , \" content\" :\" This is a JSON document\" , \" array\" :[1, 2, 3], \" response\" :[200, \" OK\" ], \" outputTypes\" :\" application/json\" }" ;
258217 String expected ="{\" argument1\" :\" helloJS.json\" , \" argument2\" :\" Earth\" , \" database-name\" :\" TestJSResourceExtensionDB\" , \" document-count\" :0, \" content\" :\" This is a JSON document\" , \" document-content\" :null, \" response\" :[200, \" OK\" ], \" outputTypes\" :[\" application/json\" ]}" ;
259218// verify by reading all the documents to see put and post services correctly inserted documents and delete them
260- for (int j =0 ;j <150 ;j ++){
261- jh .set (jh2 .getMapper ().readTree (tjs .getJSON ("helloJS" +j +".json" )));
262- JSONAssert .assertEquals (expAftrPut ,jh .get ().get ("document-content" ).findParent ("array" ).toString (), false );
263- JSONAssert .assertEquals (expectedResponse , tjs .deleteJSON ("helloJS" +j +".json" ), false );
264- }
219+ for (int j =0 ;j <150 ;j ++) {
220+ jh .set (jh2 .getMapper ().readTree (tjs .getJSON ("helloJS" +j +".json" )));
221+ JSONAssert .assertEquals (expAftrPut ,jh .get ().get ("document-content" ).findParent ("array" ).toString (), false );
222+ JSONAssert .assertEquals (expectedResponse , tjs .deleteJSON ("helloJS" +j +".json" ), false );
223+ }
265224 System .out .println (tjs .getJSON ("helloJS.json" ));
266225 JSONAssert .assertEquals (expected , tjs .getJSON ("helloJS.json" ), false );
267-
268226 }
269-
270227}
0 commit comments