Skip to content

Commit a1d9bd6

Browse files
Ajit GeorgeAjit George
authored andcommitted
Changed test methods to handle Uber Port and fixes, added read binary method BasicJavaClientREST
1 parent f34ff69 commit a1d9bd6

File tree

2 files changed

+76
-17
lines changed

2 files changed

+76
-17
lines changed

test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,49 @@ public void updateDocumentReaderHandle(DatabaseClient client, String filename, S
675675

676676
docStream.close();
677677
}
678+
679+
/* Read a binary file, and return its contents as an array of bytes.
680+
*
681+
*/
682+
683+
byte[] readBinaryFile(String binaryFile)
684+
{
685+
System.out.println("Read binary file : " + binaryFile);
686+
687+
File file = new File(binaryFile);
688+
System.out.println("File size is : " + file.length());
689+
690+
byte[] byteArray = new byte[(int)file.length()];
691+
692+
try {
693+
InputStream input = null;
694+
try {
695+
int totalBytesRead = 0;
696+
input = new BufferedInputStream(new FileInputStream(file));
697+
while(totalBytesRead < byteArray.length){
698+
int bytesRemaining = byteArray.length - totalBytesRead;
699+
//input.read() returns -1, 0, or more :
700+
int bytesRead = input.read(byteArray, totalBytesRead, bytesRemaining);
701+
if (bytesRead > 0){
702+
totalBytesRead = totalBytesRead + bytesRead;
703+
}
704+
}
705+
706+
System.out.println("Number of bytes read: " + totalBytesRead);
707+
}
708+
finally {
709+
System.out.println("Closing input stream.");
710+
input.close();
711+
}
712+
}
713+
catch (FileNotFoundException fnfex) {
714+
System.out.println("File not found Exception.");
715+
}
716+
catch (IOException ioex) {
717+
System.out.println(ioex);
718+
}
719+
return byteArray;
720+
}
678721

679722
/**
680723
* Read document using ReaderHandle
@@ -1070,19 +1113,30 @@ public void updateDocumentUsingByteHandle(DatabaseClient client, String filename
10701113
{
10711114
// create doc manager
10721115
DocumentManager docMgr = null;
1116+
byte[] contentInByte = null;
1117+
// variable to hold QA data directory path
1118+
String dirpath = new String("src/test/java/com/marklogic/javaclient/data/");
1119+
10731120
docMgr = documentManagerSelector(client, docMgr, type);
1074-
1075-
// acquire the content
1076-
FileReader content = new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename);
1077-
//String contentInString = new String(content);
1078-
BufferedReader br = new BufferedReader(content);
1079-
String readContent = "";
1080-
String line = null;
1081-
while ((line = br.readLine()) != null)
1121+
1122+
if(type.equalsIgnoreCase("BINARY"))
1123+
{
1124+
contentInByte = readBinaryFile(dirpath + filename);
1125+
}
1126+
else
1127+
{
1128+
// acquire the content
1129+
FileReader content = new FileReader(dirpath + filename);
1130+
//String contentInString = new String(content);
1131+
BufferedReader br = new BufferedReader(content);
1132+
String readContent = "";
1133+
String line = null;
1134+
while ((line = br.readLine()) != null)
10821135
readContent = readContent + line;
1083-
br.close();
1136+
br.close();
10841137

1085-
byte[] contentInByte = (byte[])readContent.getBytes();
1138+
contentInByte = (byte[])readContent.getBytes();
1139+
}
10861140
// create an identifier for the document
10871141
String docId = uri;
10881142
// create a handle on the content

test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ public class TestBytesHandle extends BasicJavaClientREST{
2323
private static String dbName = "BytesHandleDB";
2424
private static String [] fNames = {"BytesHandleDB-1"};
2525
private static String restServerName = "REST-Java-Client-API-Server";
26+
//Additional port to test for Uber port
27+
private static int uberPort = 8000;
28+
2629
@BeforeClass
2730
public static void setUp() throws Exception{
2831
System.out.println("In setup");
2932
setupJavaRESTServer(dbName, fNames[0], restServerName,8011);
33+
createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
34+
createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
3035
}
3136

3237
@Test
@@ -39,10 +44,10 @@ public void testXmlCRUD() throws IOException , SAXException, ParserConfiguration
3944
XMLUnit.setNormalizeWhitespace(true);
4045

4146
// connect the client
42-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x",Authentication.DIGEST);
47+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x",Authentication.DIGEST);
4348

4449
// write docs
45-
writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");//***********
50+
writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");
4651

4752
//read docs
4853
BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"XML");
@@ -101,7 +106,7 @@ public void testTextCRUD() throws IOException, ParserConfigurationException, SAX
101106
System.out.println("Runing test TextCRUD");
102107

103108
// connect the client
104-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
109+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
105110

106111
// write docs
107112
writeDocumentUsingBytesHandle(client, filename, uri, "Text");
@@ -161,7 +166,7 @@ public void testJsonCRUD() throws IOException, ParserConfigurationException, SAX
161166
ObjectMapper mapper = new ObjectMapper();
162167

163168
// connect the client
164-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
169+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
165170

166171
// write docs
167172
writeDocumentUsingBytesHandle(client, filename, uri, "JSON");
@@ -220,7 +225,7 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
220225
System.out.println("Running testBinaryCRUD");
221226

222227
// connect the client
223-
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
228+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
224229

225230
// write docs
226231
writeDocumentUsingBytesHandle(client, filename, uri, "Binary");
@@ -250,8 +255,8 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
250255

251256
// get the binary size
252257
long sizeUpdate = getBinarySizeFromByte(fileReadUpdate);
253-
long expectedSizeUpdate = 3290;
254-
//long expectedSizeUpdate = 3322;
258+
//long expectedSizeUpdate = 3290;
259+
long expectedSizeUpdate = 3322;
255260
assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate);
256261

257262
// delete the document

0 commit comments

Comments
 (0)