|
| 1 | +package com.marklogic.javaclient; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import javax.swing.text.html.FormSubmitEvent; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +import org.json.JSONException; |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 32 | + |
| 33 | +import com.marklogic.client.DatabaseClient; |
| 34 | +import com.marklogic.client.DatabaseClientFactory; |
| 35 | +import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 36 | +import com.marklogic.client.admin.ExtensionMetadata; |
| 37 | +import com.marklogic.client.admin.ExtensionMetadata.ScriptLanguage; |
| 38 | +import com.marklogic.client.admin.MethodType; |
| 39 | +import com.marklogic.client.admin.ResourceExtensionsManager; |
| 40 | +import com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters; |
| 41 | +import com.marklogic.client.document.JSONDocumentManager; |
| 42 | +import com.marklogic.client.document.XMLDocumentManager; |
| 43 | +import com.marklogic.client.extensions.ResourceManager; |
| 44 | +import com.marklogic.client.extensions.ResourceServices.ServiceResult; |
| 45 | +import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator; |
| 46 | +import com.marklogic.client.io.Format; |
| 47 | +import com.marklogic.client.io.InputStreamHandle; |
| 48 | +import com.marklogic.client.io.JacksonHandle; |
| 49 | +import com.marklogic.client.io.StringHandle; |
| 50 | +import com.marklogic.client.util.RequestParameters; |
| 51 | + |
| 52 | +public class TestJSResourceExtensions extends BasicJavaClientREST { |
| 53 | + private static final int BATCH_SIZE=100; |
| 54 | + private static final String DIRECTORY ="/bulkTransform/"; |
| 55 | + private static String dbName = "TestJSResourceExtensionDB"; |
| 56 | + private static String [] fNames = {"TestResourceExtensionDB-1"}; |
| 57 | + private static String restServerName = "REST-Java-Client-API-Server"; |
| 58 | + private static int restPort = 8011; |
| 59 | + private DatabaseClient client ; |
| 60 | + ResourceExtensionsManager resourceMgr; |
| 61 | + |
| 62 | + static public class TestJSExtension extends ResourceManager { |
| 63 | + static final public String NAME = "simpleJSResourceModule"; |
| 64 | + static final public ExtensionMetadata.ScriptLanguage scriptLanguage = ExtensionMetadata.JAVASCRIPT; |
| 65 | + private JSONDocumentManager docMgr; |
| 66 | + |
| 67 | + |
| 68 | + public TestJSExtension(DatabaseClient client) { |
| 69 | + super(); |
| 70 | + // a Resource Manager must be initialized by a Database Client |
| 71 | + client.init(NAME, this, ExtensionMetadata.JAVASCRIPT); |
| 72 | + |
| 73 | + // delegates some services to a document manager |
| 74 | + docMgr = client.newJSONDocumentManager(); |
| 75 | + } |
| 76 | + |
| 77 | + public String getJSON(String docUri) { |
| 78 | + RequestParameters params = new RequestParameters(); |
| 79 | + params.add("arg1", docUri); |
| 80 | + params.add("arg2", "Earth"); |
| 81 | + |
| 82 | + // specify the mime type for each expected document returned |
| 83 | + String[] mimetypes = new String[] {"text/plain"}; |
| 84 | + |
| 85 | + // call the service |
| 86 | + ServiceResultIterator resultItr = getServices().get(params, mimetypes); |
| 87 | + |
| 88 | + // iterate over the results |
| 89 | + List<String> responses = new ArrayList<String>(); |
| 90 | + StringHandle readHandle = new StringHandle(); |
| 91 | + while (resultItr.hasNext()) { |
| 92 | + ServiceResult result = resultItr.next(); |
| 93 | + |
| 94 | + // get the result content |
| 95 | + result.getContent(readHandle); |
| 96 | + responses.add(readHandle.get()); |
| 97 | + } |
| 98 | + |
| 99 | + // release the iterator resources |
| 100 | + resultItr.close(); |
| 101 | + |
| 102 | + return responses.get(0); |
| 103 | + } |
| 104 | + public String postJSON(String docUri) { |
| 105 | + RequestParameters params = new RequestParameters(); |
| 106 | + params.add("uri", docUri); |
| 107 | + // specify the mime type for each expected document returned |
| 108 | + String[] mimetypes = new String[] {"text/plain"}; |
| 109 | + StringHandle output = new StringHandle(); |
| 110 | + String input = "{\"array\" : [1,2,3]}"; |
| 111 | + // call the service |
| 112 | + ServiceResultIterator resultItr= getServices().post(params, new StringHandle(input).withFormat(Format.JSON), mimetypes); |
| 113 | + // iterate over the results |
| 114 | + List<String> responses = new ArrayList<String>(); |
| 115 | + StringHandle readHandle = new StringHandle(); |
| 116 | + while (resultItr.hasNext()) { |
| 117 | + ServiceResult result = resultItr.next(); |
| 118 | + // get the result content |
| 119 | + result.getContent(readHandle); |
| 120 | + responses.add(readHandle.get()); |
| 121 | + } |
| 122 | + |
| 123 | + // release the iterator resources |
| 124 | + resultItr.close(); |
| 125 | + return responses.get(0); |
| 126 | + } |
| 127 | + public String putJSON(String docUri) { |
| 128 | + RequestParameters params = new RequestParameters(); |
| 129 | + params.add("uri", docUri); |
| 130 | + // specify the mime type for each expected document returned |
| 131 | + String[] mimetypes = new String[] {"text/plain"}; |
| 132 | + StringHandle output = new StringHandle(); |
| 133 | + String input = "{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}"; |
| 134 | + StringHandle readHandle = new StringHandle(); |
| 135 | + // call the service |
| 136 | + getServices().put(params, new StringHandle(input).withFormat(Format.JSON), readHandle); |
| 137 | + // iterate over the results |
| 138 | + |
| 139 | + return readHandle.get(); |
| 140 | + } |
| 141 | + public String deleteJSON(String docUri) { |
| 142 | + RequestParameters params = new RequestParameters(); |
| 143 | + params.add("uri", docUri); |
| 144 | + // specify the mime type for each expected document returned |
| 145 | + String[] mimetypes = new String[] {"text/plain"}; |
| 146 | + StringHandle output = new StringHandle(); |
| 147 | + // call the service |
| 148 | + getServices().delete(params, output); |
| 149 | + // iterate over the results |
| 150 | + |
| 151 | + return output.get(); |
| 152 | + } |
| 153 | + } |
| 154 | + @BeforeClass |
| 155 | + public static void setUpBeforeClass() throws Exception { |
| 156 | + System.out.println("In setup"); |
| 157 | + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); |
| 158 | + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + @AfterClass |
| 163 | + public static void tearDownAfterClass() throws Exception { |
| 164 | + System.out.println("In tear down" ); |
| 165 | + tearDownJavaRESTServer(dbName, fNames, restServerName); |
| 166 | + } |
| 167 | + |
| 168 | + @Before |
| 169 | + public void setUp() throws Exception { |
| 170 | + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); |
| 171 | + resourceMgr = client.newServerConfigManager().newResourceExtensionsManager(); |
| 172 | + ExtensionMetadata resextMetadata = new ExtensionMetadata(); |
| 173 | + resextMetadata.setTitle("BasicJSTest"); |
| 174 | + resextMetadata.setDescription("Testing resource extension for java script"); |
| 175 | + System.out.println(resextMetadata.getScriptLanguage()); |
| 176 | + resextMetadata.setScriptLanguage(ScriptLanguage.JAVASCRIPT); |
| 177 | + System.out.println(resextMetadata.getScriptLanguage()); |
| 178 | + resextMetadata.setVersion("1.0"); |
| 179 | + MethodParameters getParams = new MethodParameters(MethodType.GET); |
| 180 | + getParams.add("my-uri", "xs:string?"); |
| 181 | + FileInputStream myStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/JSResource.js"); |
| 182 | + InputStreamHandle handle = new InputStreamHandle(myStream); |
| 183 | + handle.set (myStream); |
| 184 | + resourceMgr.writeServices("simpleJSResourceModule", handle, resextMetadata,getParams); |
| 185 | + |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + @After |
| 190 | + public void tearDown() throws Exception { |
| 191 | + resourceMgr.deleteServices("simpleJSResourceModule"); |
| 192 | + client.release(); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void test1GetAllResourceServices() throws Exception { |
| 198 | + |
| 199 | + JacksonHandle jh = new JacksonHandle(); |
| 200 | + resourceMgr.listServices(jh); |
| 201 | + 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\"}]}}"; |
| 202 | + JSONAssert.assertEquals(expectedList,jh.get().toString(),false); |
| 203 | + TestJSExtension tjs= new TestJSExtension(client); |
| 204 | + String expectedResponse="{\"response\":[200, \"OK\"]}"; |
| 205 | + JSONAssert.assertEquals(expectedResponse, tjs.putJSON("helloJS.json"), false); |
| 206 | + String expAftrPut ="{\"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\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}, \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}"; |
| 207 | + JSONAssert.assertEquals(expAftrPut, tjs.getJSON("helloJS.json"), false); |
| 208 | + JSONAssert.assertEquals(expectedResponse, tjs.postJSON("helloJS.json"), false); |
| 209 | + 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\"}"; |
| 210 | + JSONAssert.assertEquals(expAftrPost, tjs.getJSON("helloJS.json"), false); |
| 211 | + 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\"}"; |
| 212 | +// JSONAssert.assertEquals(expected, tjs.getJSON(), false); |
| 213 | + |
| 214 | + JSONAssert.assertEquals(expectedResponse, tjs.deleteJSON("helloJS.json"), false); |
| 215 | +// System.out.println(tjs.getJSON()); |
| 216 | + JSONAssert.assertEquals(expected, tjs.getJSON("helloJS.json"), false); |
| 217 | + |
| 218 | + } |
| 219 | + @Test |
| 220 | + public void test2GetAllResourceServicesMultipleTimes() throws Exception { |
| 221 | + |
| 222 | + JacksonHandle jh = new JacksonHandle(); |
| 223 | + |
| 224 | + TestJSExtension tjs= new TestJSExtension(client); |
| 225 | + String expectedResponse="{\"response\":[200, \"OK\"]}"; |
| 226 | + //load multiple documents using extension |
| 227 | + for(int i=0;i<150;i++){ |
| 228 | + JSONAssert.assertEquals(expectedResponse, tjs.putJSON("helloJS"+i+".json"), false); |
| 229 | + JSONAssert.assertEquals(expectedResponse, tjs.postJSON("helloJS"+i+".json"), false); |
| 230 | + } |
| 231 | + |
| 232 | + JacksonHandle jh2 = new JacksonHandle(); |
| 233 | + jh.set(jh2.getMapper().readTree(tjs.getJSON("helloJS0.json"))); |
| 234 | +// System.out.println(jh.get().toString()); |
| 235 | + assertEquals("Total documents loaded are",150,jh.get().get("document-count").intValue()); |
| 236 | + |
| 237 | + String expAftrPut ="{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"array\":[1, 2, 3], \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}"; |
| 238 | + 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\"}"; |
| 239 | +// verify by reading all the documents to see put and post services correctly inserted documents and delete them |
| 240 | + for(int j=0;j<150;j++){ |
| 241 | + jh.set(jh2.getMapper().readTree(tjs.getJSON("helloJS"+j+".json"))); |
| 242 | + JSONAssert.assertEquals(expAftrPut,jh.get().get("document-content").findParent("array").toString(), false); |
| 243 | + JSONAssert.assertEquals(expectedResponse, tjs.deleteJSON("helloJS"+j+".json"), false); |
| 244 | + } |
| 245 | +// System.out.println(tjs.getJSON()); |
| 246 | + JSONAssert.assertEquals(expected, tjs.getJSON("helloJS.json"), false); |
| 247 | + |
| 248 | + } |
| 249 | + |
| 250 | +} |
0 commit comments