16
16
17
17
package com .marklogic .client .functionaltest ;
18
18
19
- import static org .junit .Assert .* ;
19
+ import static org .junit .Assert .assertEquals ;
20
20
21
21
import java .io .FileInputStream ;
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
24
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 ;
42
25
import org .junit .After ;
43
26
import org .junit .AfterClass ;
44
27
import org .junit .Before ;
54
37
import com .marklogic .client .admin .MethodType ;
55
38
import com .marklogic .client .admin .ResourceExtensionsManager ;
56
39
import com .marklogic .client .admin .ResourceExtensionsManager .MethodParameters ;
57
- import com .marklogic .client .document .JSONDocumentManager ;
58
- import com .marklogic .client .document .XMLDocumentManager ;
59
40
import com .marklogic .client .extensions .ResourceManager ;
60
41
import com .marklogic .client .extensions .ResourceServices .ServiceResult ;
61
42
import com .marklogic .client .extensions .ResourceServices .ServiceResultIterator ;
66
47
import com .marklogic .client .util .RequestParameters ;
67
48
68
49
public class TestJSResourceExtensions extends BasicJavaClientREST {
69
- private static final int BATCH_SIZE =100 ;
70
- private static final String DIRECTORY ="/bulkTransform/" ;
71
50
private static String dbName = "TestJSResourceExtensionDB" ;
72
51
private static String [] fNames = {"TestResourceExtensionDB-1" };
73
52
private static String restServerName = "REST-Java-Client-API-Server" ;
@@ -78,16 +57,10 @@ public class TestJSResourceExtensions extends BasicJavaClientREST {
78
57
static public class TestJSExtension extends ResourceManager {
79
58
static final public String NAME = "simpleJSResourceModule" ;
80
59
static final public ExtensionMetadata .ScriptLanguage scriptLanguage = ExtensionMetadata .JAVASCRIPT ;
81
- private JSONDocumentManager docMgr ;
82
-
83
-
84
60
public TestJSExtension (DatabaseClient client ) {
85
61
super ();
86
62
// a Resource Manager must be initialized by a Database Client
87
63
client .init (NAME , this );
88
-
89
- // delegates some services to a document manager
90
- docMgr = client .newJSONDocumentManager ();
91
64
}
92
65
93
66
public String getJSON (String docUri ) {
@@ -111,18 +84,17 @@ public String getJSON(String docUri) {
111
84
result .getContent (readHandle );
112
85
responses .add (readHandle .get ());
113
86
}
114
-
115
87
// release the iterator resources
116
88
resultItr .close ();
117
-
118
89
return responses .get (0 );
119
90
}
91
+
120
92
public String postJSON (String docUri ) {
121
93
RequestParameters params = new RequestParameters ();
122
94
params .add ("uri" , docUri );
123
95
// specify the mime type for each expected document returned
124
96
String [] mimetypes = new String [] {"text/plain" };
125
- StringHandle output = new StringHandle ();
97
+ new StringHandle ();
126
98
String input = "{\" array\" : [1,2,3]}" ;
127
99
// call the service
128
100
ServiceResultIterator resultItr = getServices ().post (params , new StringHandle (input ).withFormat (Format .JSON ), mimetypes );
@@ -135,46 +107,41 @@ public String postJSON(String docUri) {
135
107
result .getContent (readHandle );
136
108
responses .add (readHandle .get ());
137
109
}
138
-
139
110
// release the iterator resources
140
111
resultItr .close ();
141
112
return responses .get (0 );
142
113
}
114
+
143
115
public String putJSON (String docUri ) {
144
116
RequestParameters params = new RequestParameters ();
145
117
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 ();
149
119
String input = "{\" argument1\" :\" hello\" , \" argument2\" :\" Earth\" , \" content\" :\" This is a JSON document\" , \" response\" :[200, \" OK\" ], \" outputTypes\" :\" application/json\" }" ;
150
120
StringHandle readHandle = new StringHandle ();
151
121
// call the service
152
122
getServices ().put (params , new StringHandle (input ).withFormat (Format .JSON ), readHandle );
153
123
// iterate over the results
154
-
155
124
return readHandle .get ();
156
125
}
126
+
157
127
public String deleteJSON (String docUri ) {
158
128
RequestParameters params = new RequestParameters ();
159
129
params .add ("uri" , docUri );
160
- // specify the mime type for each expected document returned
161
- String [] mimetypes = new String [] {"text/plain" };
162
130
StringHandle output = new StringHandle ();
163
131
// call the service
164
132
getServices ().delete (params , output );
165
133
// iterate over the results
166
-
167
134
return output .get ();
168
135
}
169
136
}
137
+
170
138
@ BeforeClass
171
139
public static void setUpBeforeClass () throws Exception {
172
140
System .out .println ("In setup" );
173
141
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");
178
145
}
179
146
180
147
@ AfterClass
@@ -202,20 +169,16 @@ public void setUp() throws Exception {
202
169
InputStreamHandle handle = new InputStreamHandle (myStream );
203
170
handle .set (myStream );
204
171
resourceMgr .writeServices ("simpleJSResourceModule" , handle , resextMetadata ,getParams );
205
-
206
-
207
172
}
208
173
209
174
@ After
210
175
public void tearDown () throws Exception {
211
176
resourceMgr .deleteServices ("simpleJSResourceModule" );
212
177
client .release ();
213
-
214
178
}
215
179
216
180
@ Test
217
181
public void test1GetAllResourceServices () throws Exception {
218
-
219
182
JacksonHandle jh = new JacksonHandle ();
220
183
resourceMgr .listServices (jh );
221
184
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 {
229
192
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\" ]}" ;
230
193
JSONAssert .assertEquals (expAftrPost , tjs .getJSON ("helloJS.json" ), false );
231
194
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);
233
195
234
196
JSONAssert .assertEquals (expectedResponse , tjs .deleteJSON ("helloJS.json" ), false );
235
- // System.out.println(tjs.getJSON());
236
197
JSONAssert .assertEquals (expected , tjs .getJSON ("helloJS.json" ), false );
237
-
238
198
}
199
+
239
200
@ Test
240
- public void test2GetAllResourceServicesMultipleTimes () throws Exception {
241
-
201
+ public void test2GetAllResourceServicesMultipleTimes () throws Exception {
242
202
JacksonHandle jh = new JacksonHandle ();
243
203
244
204
TestJSExtension tjs = new TestJSExtension (client );
245
205
String expectedResponse ="{\" response\" :[200, \" OK\" ]}" ;
246
206
//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 );
250
210
}
251
211
252
212
JacksonHandle jh2 = new JacksonHandle ();
253
213
jh .set (jh2 .getMapper ().readTree (tjs .getJSON ("helloJS0.json" )));
254
- // System.out.println(jh.get().toString());
255
214
assertEquals ("Total documents loaded are" ,150 ,jh .get ().get ("document-count" ).intValue ());
256
215
257
216
String expAftrPut ="{\" argument1\" :\" hello\" , \" argument2\" :\" Earth\" , \" content\" :\" This is a JSON document\" , \" array\" :[1, 2, 3], \" response\" :[200, \" OK\" ], \" outputTypes\" :\" application/json\" }" ;
258
217
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\" ]}" ;
259
218
// 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
+ }
265
224
System .out .println (tjs .getJSON ("helloJS.json" ));
266
225
JSONAssert .assertEquals (expected , tjs .getJSON ("helloJS.json" ), false );
267
-
268
226
}
269
-
270
227
}
0 commit comments