Skip to content

Commit b5c39c0

Browse files
authored
Merge pull request #1515 from marklogic/feature/test-cleanup
Refactoring tests to simply calls to newClient
2 parents 4ec1323 + 648abe4 commit b5c39c0

14 files changed

+84
-398
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/AbstractFunctionalTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,11 @@ public static void initializeClients() throws Exception {
5555
MarkLogicVersion version = MarkLogicVersion.getMarkLogicVersion(connectAsAdmin());
5656
System.out.println("ML version: " + version.getVersionString());
5757
isML11OrHigher = version.getMajor() >= 11;
58-
final String schemasDbName = "java-functest-schemas";
59-
final String modulesDbName = "java-unittest-modules";
60-
if (IsSecurityEnabled()) {
61-
schemasClient = getDatabaseClientOnDatabase(getRestServerHostName(), getRestServerPort(), schemasDbName, OPTIC_USER, OPTIC_USER_PASSWORD, getConnType());
62-
client = getDatabaseClient(OPTIC_USER, OPTIC_USER_PASSWORD, getConnType());
63-
adminModulesClient = getDatabaseClientOnDatabase(getRestServerHostName(), getRestServerPort(), modulesDbName, getAdminUser(), getAdminPassword(), getConnType());
64-
} else {
65-
schemasClient = newClient(getRestServerHostName(), getRestServerPort(), schemasDbName,
66-
newSecurityContext(OPTIC_USER, OPTIC_USER_PASSWORD));
67-
client = newClient(getRestServerHostName(), getRestServerPort(), null,
68-
newSecurityContext(OPTIC_USER, OPTIC_USER_PASSWORD));
69-
adminModulesClient = newClient(getRestServerHostName(), getRestServerPort(), modulesDbName,
70-
newSecurityContext(getAdminUser(), getAdminPassword()));
71-
}
58+
59+
client = newClientAsUser(OPTIC_USER, OPTIC_USER_PASSWORD);
60+
schemasClient = newClient(getRestServerHostName(), getRestServerPort(), "java-functest-schemas",
61+
newSecurityContext(OPTIC_USER, OPTIC_USER_PASSWORD), null);
62+
adminModulesClient = newAdminModulesClient();
7263

7364
// Required to ensure that tests using the "/ext/" prefix work reliably. Expand to other directories as needed.
7465
adminModulesClient.newServerEval()
@@ -157,13 +148,11 @@ protected final String toWKT(String latLon) {
157148
}
158149

159150
protected static DatabaseClient connectAsRestWriter() {
160-
return newClient(getRestServerHostName(), getRestServerPort(),
161-
newSecurityContext("rest-writer", "x"), getConnType());
151+
return newClientAsUser("rest-writer", "x");
162152
}
163153

164154
protected static DatabaseClient connectAsAdmin() {
165-
return newClient(getRestServerHostName(), getRestServerPort(),
166-
newSecurityContext(getAdminUser(), getAdminPassword()), getConnType());
155+
return newClientAsUser(getAdminUser(), getAdminPassword());
167156
}
168157

169158
protected static void removeFieldIndices() {

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/ClientApiFunctionalTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.node.ArrayNode;
66
import com.marklogic.client.DatabaseClient;
7-
import com.marklogic.client.DatabaseClientFactory.SecurityContext;
87
import com.marklogic.client.FailedRequestException;
98
import com.marklogic.client.SessionState;
109
import com.marklogic.client.document.JSONDocumentManager;
@@ -36,10 +35,6 @@ public class ClientApiFunctionalTest extends AbstractFunctionalTest {
3635
private final static String serverName = "java-functest";
3736

3837
private static DatabaseClient dbclient = null;
39-
private static String host;
40-
private static int port;
41-
private static int restTestport;
42-
4338

4439
// Create an identifier for modules document - Client API call will be to endpoint
4540
private final static String endPointURI_1 = "/ext/TestE2EIntegerParamReturnDouble/TestE2EIntegerParamReturnDouble";
@@ -80,9 +75,6 @@ public static void setUp() throws Exception {
8075
"manage-user");
8176
dbclient = client;
8277
DatabaseClient modulesClient = adminModulesClient;
83-
host = getServer();
84-
port = getRestServerPort();
85-
restTestport = getRestServerPort();
8678

8779
TextDocumentManager docMgr = modulesClient.newTextDocumentManager();
8880
File file = new File(
@@ -277,7 +269,7 @@ public void TestE2ERequiredParam() {
277269
@Test
278270
public void TestE2EUnAuthorizedUser() {
279271
System.out.println("Running TestE2EUnAuthorizedUser");
280-
DatabaseClient dbForbiddenclient = newClient(host, port, newSecurityContext("ForbiddenUser", "ap1U53r"), getConnType());
272+
DatabaseClient dbForbiddenclient = newClientAsUser("ForbiddenUser", "ap1U53r");
281273
String msg;
282274
try {
283275
TestE2EIntegerParaReturnDouble.on(dbForbiddenclient).TestE2EItemPriceErrorCond(10, 50);
@@ -370,8 +362,7 @@ public void TestE2EuserWithInvalidRole() {
370362
// Used this test to verify ResourceNotFoundException when sjs module is installed with incorrect doc URI
371363

372364
System.out.println("Running TestE2EuserWithInvalidRole");
373-
SecurityContext secContext = newSecurityContext("secondApiUser", "ap1U53r");
374-
DatabaseClient dbSecondClient = newClient(host, port, secContext, getConnType());
365+
DatabaseClient dbSecondClient = newClientAsUser("secondApiUser", "ap1U53r");
375366
String msg;
376367
try {
377368
TestE2EIntegerParaReturnDouble.on(dbSecondClient).TestE2EItemPriceErrorCond(10, 50);
@@ -424,7 +415,7 @@ public void TestE2ESessions() {
424415
SessionState apiSession3 = TestE2ESession.on(dbclient).newSessionState();
425416
TestE2ESession.on(dbclient).SessionChecks(apiSession3, "/session3.json", "{\"value\":\"Checking sessions 3\"}");
426417

427-
DatabaseClient dbclientRest = newClient(host, restTestport, newSecurityContext("apiUser", "ap1U53r"), getConnType());
418+
DatabaseClient dbclientRest = newClientAsUser("apiUser", "ap1U53r");
428419
waitForPropertyPropagate();
429420
JSONDocumentManager docMgr = dbclientRest.newJSONDocumentManager();
430421
JacksonHandle jh = new JacksonHandle();
@@ -481,7 +472,7 @@ public void TestE2ESessions() {
481472
}
482473

483474
private String buildUrl(String path) {
484-
String url = "http://" + host + ":" + restTestport;
475+
String url = "http://" + getServer() + ":" + getRestServerPort();
485476
if (StringUtils.hasText(basePath)) {
486477
url += "/" + basePath;
487478
}

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestBulkWriteWithTransformations.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.marklogic.client.fastfunctest;
1818

19-
import com.marklogic.client.DatabaseClientFactory.SecurityContext;
2019
import com.marklogic.client.Transaction;
2120
import com.marklogic.client.admin.ExtensionMetadata;
2221
import com.marklogic.client.admin.TransformExtensionsManager;
@@ -68,12 +67,7 @@ public void setUp() throws Exception {
6867
// create new connection for each test below
6968
createUserRolesWithPrevilages("test-eval", "xdbc:eval", "xdbc:eval-in", "xdmp:eval-in", "any-uri", "xdbc:invoke");
7069
createRESTUser("eval-user", "x", "test-eval", "rest-admin", "rest-writer", "rest-reader");
71-
if (isLBHost())
72-
client = getDatabaseClient("eval-user", "x", getConnType());
73-
else {
74-
SecurityContext secContext = newSecurityContext("eval-user", "x");
75-
client = newClient(appServerHostname, getRestServerPort(), getRestServerName(), secContext, getConnType());
76-
}
70+
client = newClientAsUser("eval-user", "x");
7771
}
7872

7973
@AfterEach

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestDatabaseAuthentication.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.marklogic.client.DatabaseClient;
2020
import com.marklogic.client.DatabaseClientFactory;
21-
import com.marklogic.client.DatabaseClientFactory.SecurityContext;
2221
import com.marklogic.client.io.InputStreamHandle;
2322
import org.junit.jupiter.api.AfterEach;
2423
import org.junit.jupiter.api.BeforeEach;
@@ -39,27 +38,25 @@
3938

4039
public class TestDatabaseAuthentication extends AbstractFunctionalTest {
4140

42-
private static String restServerName = "java-functest";
43-
private String originalServerAuthentication;
41+
private static String restServerName = "java-functest";
42+
private String originalServerAuthentication;
4443

45-
@BeforeEach
46-
public void before() {
47-
originalServerAuthentication = getServerAuthentication(restServerName);
48-
}
44+
@BeforeEach
45+
public void before() {
46+
originalServerAuthentication = getServerAuthentication(restServerName);
47+
}
4948

50-
@AfterEach
51-
public void teardown() throws Exception {
52-
setAuthentication(originalServerAuthentication, restServerName);
53-
setDefaultUser("nobody", restServerName);
54-
}
49+
@AfterEach
50+
public void teardown() throws Exception {
51+
setAuthenticationAndDefaultUser(restServerName, originalServerAuthentication, "nobody");
52+
}
5553

5654
@Test
5755
public void testAuthenticationNone()
5856
{
5957
System.out.println("Running testAuthenticationNone");
6058
if (!IsSecurityEnabled()) {
61-
setAuthentication("application-level", restServerName);
62-
setDefaultUser("rest-admin", restServerName);
59+
setAuthenticationAndDefaultUser(restServerName, "application-level", "rest-admin");
6360
// connect the client
6461
StringBuilder str = new StringBuilder();
6562
try {
@@ -77,16 +74,14 @@ public void testAuthenticationNone()
7774
public void testAuthenticationBasic() throws IOException
7875
{
7976
if (!IsSecurityEnabled()) {
80-
setAuthentication("basic", restServerName);
81-
setDefaultUser("rest-writer", restServerName);
77+
setAuthenticationAndDefaultUser(restServerName, "basic", "rest-writer");
8278

8379
System.out.println("Running testAuthenticationBasic");
8480

8581
String filename = "text-original.txt";
8682

8783
// connect the client
88-
SecurityContext secContext = new DatabaseClientFactory.BasicAuthContext("rest-writer", "x");
89-
DatabaseClient client = newClient(getRestServerHostName(), getRestServerPort(), secContext, getConnType());
84+
DatabaseClient client = newBasicAuthClient("rest-writer", "x");
9085

9186
// write doc
9287
writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text");

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestEvalJavaScript.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ public static void tearDownAfterClass() throws Exception {
7070
}
7171

7272
@BeforeEach
73-
public void setUp() throws KeyManagementException, NoSuchAlgorithmException, Exception {
74-
int restPort = getRestServerPort();
75-
client = getDatabaseClientOnDatabase(appServerHostname, restPort, dbName, "eval-userJS", "x", getConnType());
73+
public void setUp() throws Exception {
74+
client = newClientAsUser("eval-userJS", "x");
7675
}
7776

7877
/*
@@ -519,9 +518,7 @@ public void testMultipleJSfnOnServerEval() {
519518
public void testJSReturningDifferentTypesOrder3fromModules() throws Exception {
520519
System.out.println("Running testJSReturningDifferentTypesOrder3fromModules");
521520
InputStream inputStream = null;
522-
int restPort = getRestServerPort();
523-
DatabaseClient moduleClient = getDatabaseClientOnDatabase(appServerHostname, restPort, "java-unittest-modules",
524-
getAdminUser(), getAdminPassword(), getConnType());
521+
DatabaseClient moduleClient = newAdminModulesClient();
525522
try {
526523
inputStream = new FileInputStream(
527524
"src/test/java/com/marklogic/client/functionaltest/data/javascriptQueries.sjs");
@@ -645,15 +642,13 @@ public void testStreamClosingWithEvalAs() throws KeyManagementException, NoSuchA
645642

646643
// Making sure that mjs modules can be used in eval.
647644
@Test
648-
public void testJavaScriptModules() throws KeyManagementException, NoSuchAlgorithmException, Exception {
645+
public void testJavaScriptModules() {
649646
System.out.println("Running testJavaScriptModules");
650647
DatabaseClient moduleClient = null;
651648
try {
652649
String docId[] = { "/test/words/wd1.json", "/test/words/wd2.json", "/test/words/wd3.json",
653650
"/test/words/wd4.xml", "/test/words/wd5.xml"};
654-
int restPort = getRestServerPort();
655-
moduleClient = getDatabaseClientOnDatabase(appServerHostname, restPort, ("java-unittest-modules"),
656-
getAdminUser(), getAdminPassword(), getConnType());
651+
moduleClient = newAdminModulesClient();
657652
String mjsString = "import sr from '/MarkLogic/jsearch';" +
658653
"var output =" +
659654
" sr.documents()" +

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestEvalXquery.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,11 @@ public void testXqueryWithExtVarAsNode() throws Exception {
415415
// Issue 156 , have test cases where you can pass, element node, text node,
416416
// binary node, json object, json array as an external variable
417417
@Test
418-
public void testXqueryInvokeModuleRetDiffTypes() throws KeyManagementException, NoSuchAlgorithmException, Exception {
418+
public void testXqueryInvokeModuleRetDiffTypes() throws Exception {
419419

420420
InputStream inputStream = null;
421421

422-
int restPort = getRestServerPort();
423-
String appServerHostname = getRestAppServerHostName();
424-
425-
DatabaseClient moduleClient = getDatabaseClientOnDatabase(appServerHostname, restPort, "java-unittest-modules",
426-
getAdminUser(), getAdminPassword(), getConnType());
422+
DatabaseClient moduleClient = newAdminModulesClient();
427423
try {
428424
inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/xquery-modules-with-diff-variable-types.xqy");
429425
InputStreamHandle ish = new InputStreamHandle();
@@ -446,9 +442,6 @@ public void testXqueryInvokeModuleRetDiffTypes() throws KeyManagementException,
446442
EvalResultIterator evr = evl.eval();
447443
this.validateReturnTypes(evr);
448444
evr.close();
449-
450-
} catch (Exception e) {
451-
throw e;
452445
} finally {
453446
if (inputStream != null) {
454447
inputStream.close();

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestHandles.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@
4545

4646
public class TestHandles extends AbstractFunctionalTest {
4747

48-
private static String dbName = "java-functest";
49-
private static String appServerHostname = null;
50-
5148
@BeforeAll
5249
public static void setUp() throws Exception {
5350
createUserRolesWithPrevilages("test-eval", "xdbc:eval", "xdbc:eval-in", "xdmp:eval-in", "any-uri", "xdbc:invoke");
5451
createRESTUser("eval-user", "x", "test-eval", "rest-admin", "rest-writer", "rest-reader");
55-
appServerHostname = getRestAppServerHostName();
5652
}
5753

5854
@AfterEach
@@ -68,17 +64,15 @@ public static void tearDown() throws Exception {
6864

6965
// Begin TestBytesHandle
7066
@Test
71-
public void testXmlCRUD_BytesHandle() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException {
67+
public void testXmlCRUD_BytesHandle() throws IOException, SAXException, ParserConfigurationException {
7268

7369
String filename = "xml-original-test.xml";
7470
String uri = "/write-xml-domhandle/";
7571
System.out.println("Running testXmlCRUD_BytesHandle");
7672
XMLUnit.setIgnoreWhitespace(true);
7773
XMLUnit.setNormalizeWhitespace(true);
7874

79-
// connect the client
80-
DatabaseClientFactory.SecurityContext secContext = newSecurityContext("eval-user", "x");
81-
DatabaseClient client = newClient(appServerHostname, getRestServerPort(), dbName, secContext, getConnType());
75+
DatabaseClient client = newClientAsUser("eval-user", "x");
8276

8377
// write docs
8478
writeDocumentUsingBytesHandle(client, filename, uri, null, "XML");
@@ -133,14 +127,12 @@ public void testXmlCRUD_BytesHandle() throws KeyManagementException, NoSuchAlgor
133127
}
134128

135129
@Test
136-
public void testTextCRUD_BytesHandle() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException {
130+
public void testTextCRUD_BytesHandle() throws IOException, ParserConfigurationException, SAXException {
137131
String filename = "text-original.txt";
138132
String uri = "/write-text-Byteshandle/";
139133
System.out.println("Runing test TextCRUD_BytesHandle");
140134

141-
// connect the client
142-
DatabaseClientFactory.SecurityContext secContext = newSecurityContext("eval-user", "x");
143-
DatabaseClient client = newClient(appServerHostname, getRestServerPort(), dbName, secContext, getConnType());
135+
DatabaseClient client = newClientAsUser("eval-user", "x");
144136

145137
// write docs
146138
writeDocumentUsingBytesHandle(client, filename, uri, "Text");
@@ -199,9 +191,7 @@ public void testJsonCRUD_BytesHandle() throws KeyManagementException, NoSuchAlgo
199191

200192
ObjectMapper mapper = new ObjectMapper();
201193

202-
// connect the client
203-
DatabaseClientFactory.SecurityContext secContext = newSecurityContext("eval-user", "x");
204-
DatabaseClient client = newClient(appServerHostname, getRestServerPort(), dbName, secContext, getConnType());
194+
DatabaseClient client = newClientAsUser("eval-user", "x");
205195

206196
// write docs
207197
writeDocumentUsingBytesHandle(client, filename, uri, "JSON");
@@ -260,9 +250,7 @@ public void testBinaryCRUD_BytesHandle() throws KeyManagementException, NoSuchAl
260250
String uri = "/write-bin-Bytehandle/";
261251
System.out.println("Running testBinaryCRUD_BytesHandle");
262252

263-
// connect the client
264-
DatabaseClientFactory.SecurityContext secContext = newSecurityContext("eval-user", "x");
265-
DatabaseClient client = newClient(appServerHostname, getRestServerPort(), dbName, secContext, getConnType());
253+
DatabaseClient client = newClientAsUser("eval-user", "x");
266254

267255
// write docs
268256
writeDocumentUsingBytesHandle(client, filename, uri, "Binary");

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestJSResourceExtensions.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ public static void tearDownAfterClass() throws Exception {
136136

137137
@BeforeEach
138138
public void setUp() throws Exception {
139-
140-
int restPort = getRestServerPort();
141-
String appServerHostname = getRestAppServerHostName();
142-
client = getDatabaseClientOnDatabase(appServerHostname, restPort, "java-functest", "eval-user", "x", getConnType());
139+
client = newClientAsUser("eval-user", "x");
143140
resourceMgr = client.newServerConfigManager().newResourceExtensionsManager();
144141
ExtensionMetadata resextMetadata = new ExtensionMetadata();
145142
resextMetadata.setTitle("BasicJSTest");
@@ -176,7 +173,7 @@ public void test1GetAllResourceServices() throws Exception {
176173
TestJSExtension tjs = new TestJSExtension(client);
177174
String expectedResponse = "{\"response\":[200, \"OK\"]}";
178175
JSONAssert.assertEquals(expectedResponse, tjs.putJSON("helloJS.json"), false);
179-
String expAftrPut = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\",\"database-name\":\"" + client.getDatabase() + "\", \"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\"]}";
176+
String expAftrPut = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\",\"database-name\":\"java-functest\", \"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\"]}";
180177
JSONAssert.assertEquals(expAftrPut, tjs.getJSON("helloJS.json"), false);
181178
JSONAssert.assertEquals(expectedResponse, tjs.postJSON("helloJS.json"), false);
182179
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\"]}";
@@ -206,7 +203,7 @@ public void test2GetAllResourceServicesMultipleTimes() throws Exception {
206203
assertEquals( 150, jh.get().get("document-count").intValue());
207204

208205
String expAftrPut = "{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"array\":[1, 2, 3], \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}";
209-
String expected = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"database-name\":\"" + client.getDatabase() + "\", \"document-count\":0, \"content\":\"This is a JSON document\", \"document-content\":null, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}";
206+
String expected = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"database-name\":\"java-functest\", \"document-count\":0, \"content\":\"This is a JSON document\", \"document-content\":null, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}";
210207
// verify by reading all the documents to see put and post services
211208
// correctly inserted documents and delete them
212209
for (int j = 0; j < 150; j++) {

0 commit comments

Comments
 (0)