Skip to content

Commit 4de6994

Browse files
authored
Merge pull request #1770 from marklogic/feature/more-connected-restqa-cleanup
Finished switching over to ManageClient
2 parents 9d70444 + ec60558 commit 4de6994

File tree

5 files changed

+27
-311
lines changed

5 files changed

+27
-311
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/functionaltest/ConnectedRESTQA.java

Lines changed: 27 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
import com.marklogic.client.DatabaseClientBuilder;
1515
import com.marklogic.client.DatabaseClientFactory;
1616
import com.marklogic.client.admin.ServerConfigurationManager;
17-
import com.marklogic.client.impl.OkHttpServices;
18-
import com.marklogic.client.impl.RESTServices;
1917
import com.marklogic.client.io.DocumentMetadataHandle;
2018
import com.marklogic.client.io.DocumentMetadataHandle.Capability;
19+
import com.marklogic.client.query.QueryManager;
2120
import com.marklogic.mgmt.ManageClient;
2221
import com.marklogic.mgmt.ManageConfig;
2322
import com.marklogic.mgmt.resource.appservers.ServerManager;
@@ -30,8 +29,6 @@
3029
import com.marklogic.mgmt.resource.temporal.TemporalAxesManager;
3130
import com.marklogic.mgmt.resource.temporal.TemporalCollectionLSQTManager;
3231
import com.marklogic.mgmt.resource.temporal.TemporalCollectionManager;
33-
import okhttp3.*;
34-
import org.json.JSONObject;
3532

3633
import javax.net.ssl.*;
3734
import java.io.IOException;
@@ -43,9 +40,9 @@
4340

4441
public abstract class ConnectedRESTQA {
4542

46-
protected static Properties testProperties = null;
43+
private static Properties testProperties = null;
4744

48-
protected static String authType;
45+
private static String authType;
4946
protected static String restServerName = null;
5047
private static String restSslServerName = null;
5148
private static String ssl_enabled = null;
@@ -68,25 +65,8 @@ public abstract class ConnectedRESTQA {
6865
private static Boolean isLBHost = false;
6966

7067
private static int PROPERTY_WAIT = 0;
71-
private static final int ML_RES_OK = 200;
72-
private static final int ML_RES_CREATED = 201;
73-
private static final int ML_RES_CHANGED = 204;
74-
private static final int ML_RES_BADREQT = 400;
75-
private static final int ML_RES_NOTFND = 404;
76-
private static final String ML_MANAGE_DB = "App-Services";
77-
7868
private static final ObjectMapper objectMapper = new ObjectMapper();
7969

80-
private static OkHttpClient createHttpClient() {
81-
// build client with authentication information.
82-
RESTServices services = new OkHttpServices();
83-
// Manage API is assumed to require digest auth; if that assumption falls apart, we should add a new property
84-
// for this and not assume that the authentication for the REST server will work
85-
services.connect(host_name, Integer.parseInt(admin_port), null, ML_MANAGE_DB,
86-
new DatabaseClientFactory.DigestAuthContext("admin", "admin"));
87-
return (OkHttpClient) services.getClientImplementation();
88-
}
89-
9070
public static void createDB(String dbName) {
9171
new DatabaseManager(newManageClient())
9272
.save(objectMapper.createObjectNode()
@@ -101,7 +81,6 @@ public static void createForest(String fName, String dbName) {
10181
.toString());
10282
}
10383

104-
// creating forests on different hosts
10584
public static void createForestonHost(String fName, String dbName, String hName) {
10685
new ForestManager(newManageClient()).save(objectMapper.createObjectNode()
10786
.put("database", dbName)
@@ -111,82 +90,25 @@ public static void createForestonHost(String fName, String dbName, String hName)
11190
}
11291

11392
public static void postRequest(Map<String, String> params, String endpoint) {
114-
OkHttpClient client;
115-
try {
116-
client = createHttpClient();
117-
String postUrl = new String("http://" + host_name + ":" + admin_port + endpoint);
118-
StringBuilder resp = new StringBuilder();
119-
// Initialize Builder (not RequestBody)
120-
FormBody.Builder builder = new FormBody.Builder();
121-
122-
if (params != null) {
123-
for(Map.Entry<String, String> entry: params.entrySet()) {
124-
builder.add(entry.getKey(), entry.getValue());
125-
}
126-
}
127-
RequestBody formBody = builder.build();
128-
129-
Request request = new Request.Builder()
130-
.header("Content-type", "application/json")
131-
.url(postUrl)
132-
.post(formBody)
133-
.build();
134-
try (Response response = client.newCall(request).execute()) {
135-
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
136-
else if (response.code() == ML_RES_OK) {
137-
resp.append(response.body().string());
138-
if (!resp.toString().isEmpty()) {
139-
System.out.println("Posted params ");
140-
System.out.println(resp);
141-
}
142-
} else {
143-
System.out.println("No proper reponse from post request");
144-
System.out.println(response);
145-
}
146-
}
147-
} catch (Exception e) {
148-
e.printStackTrace();
149-
}
93+
List<String> paramList = new ArrayList<>();
94+
params.entrySet().forEach(entry -> {
95+
paramList.add(entry.getKey());
96+
paramList.add(entry.getValue());
97+
});
98+
newManageClient().postForm(endpoint, paramList.toArray(new String[0]));
15099
}
151100

152101
public static void assocRESTServer(String restServerName, String dbName, int restPort) {
153-
OkHttpClient client;
154-
try {
155-
client = createHttpClient();
156-
String urlStr = new String("http://" + host_name + ":" + admin_port + "/v1/rest-apis?format=json");
157-
String JSONString = "{ \"rest-api\": {\"name\":\"" + restServerName + "\",\"database\":\"" + dbName
158-
+ "\",\"port\":\"" + restPort + "\"}}";
159-
160-
StringBuilder resp = new StringBuilder();
161-
Request request = new Request.Builder()
162-
.header("Content-type", "application/json")
163-
.url(urlStr)
164-
.post(RequestBody.create(JSONString, MediaType.parse("application/json")))
165-
.build();
166-
try (Response response = client.newCall(request).execute()) {
167-
resp.append(response.body().string());
168-
if (!resp.toString().isEmpty()) {
169-
System.out.println("Will try to associate RESTServer with DB");
170-
//System.out.println(resp);
171-
}
172-
}
173-
JsonNode returnResp = new ObjectMapper().readTree(resp.toString());
174-
if (returnResp.get("errorResponse").get("statusCode").asInt() == ML_RES_BADREQT) {
175-
System.out.println("AppServer already exist");
176-
if (dbName.equals("Documents")) {
177-
System.out.println("and Context database is Documents DB");
178-
} else {
179-
System.out.println("and changing context database to " + dbName);
180-
associateRESTServerWithDB(restServerName, dbName);
181-
}
182-
} else if (returnResp.get("errorResponse").get("statusCode").asInt() == ML_RES_CREATED) {
183-
// Enable security on new REST Http Server if SSL is turned on.
184-
if (IsSecurityEnabled()) {
185-
enableSecurityOnRESTServer(restServerName);
186-
}
187-
}
188-
} catch (Exception e) {
189-
e.printStackTrace();
102+
ObjectNode request = objectMapper.createObjectNode();
103+
request.putObject("rest-api")
104+
.put("name", restServerName)
105+
.put("database", dbName)
106+
.put("port", restPort);
107+
108+
ManageClient client = newManageClient();
109+
new RestApiManager(client).createRestApi(restServerName, request.toString());
110+
if (IsSecurityEnabled()) {
111+
enableSecurityOnRESTServer(restServerName);
190112
}
191113
}
192114

@@ -372,33 +294,10 @@ public static void deleteUserRole(String roleName) {
372294
}
373295

374296
public static void detachForest(String dbName, String fName) {
375-
OkHttpClient client;
376-
try {
377-
client = createHttpClient();
378-
379-
String postUrl = new String("http://" + host_name + ":" + admin_port + "/manage/v2/forests/" + fName);
380-
RequestBody formBody = new FormBody.Builder()
381-
.add("state", "detach")
382-
.add("database", dbName)
383-
.build();
384-
Request request = new Request.Builder()
385-
.header("Content-type", "application/json")
386-
.url(postUrl)
387-
.post(formBody)
388-
.build();
389-
Response response = client.newCall(request).execute();
390-
391-
if (response.code() == ML_RES_OK) {
392-
System.out.println("Forest " + fName + " has been detached from database " + dbName);
393-
} else {
394-
System.out.println("Forest " + fName + " detaching from database " + dbName + " ran into problems");
395-
System.out.println(response);
396-
}
397-
} catch (Exception e) {
398-
e.printStackTrace();
399-
} finally {
400-
client = null;
401-
}
297+
newManageClient().postForm("/manage/v2/forests/" + fName,
298+
"state", "detach",
299+
"database", dbName
300+
);
402301
}
403302

404303
public static void deleteForest(String fName) {
@@ -409,71 +308,10 @@ public static void deleteDB(String dbName) {
409308
new DatabaseManager(newManageClient()).deleteByIdField(dbName);
410309
}
411310

412-
//Clear the Database
413311
public static void clearDB(int port) {
414-
OkHttpClient client = createHttpClient();
415-
try {
416-
InputStream jsonstream = null;
417-
String uri = null;
418-
String resGet = null;
419-
JsonNode jnode = null;
420-
if (/*IsSecurityEnabled()*/false) {
421-
// In case of SSL use 8002 port to clear DB contents.
422-
String getrequest = new String("http://" + host_name + ":" + admin_port + "/manage/v2/servers/"
423-
+ getRestAppServerName() + "/properties?group-id=Default&format=json");
424-
Request request = new Request.Builder()
425-
.header("Content-type", "application/json")
426-
.url(getrequest)
427-
.build();
428-
Response response = client.newCall(request).execute();
429-
430-
if (response.code() == ML_RES_OK) {
431-
resGet = response.body().string();
432-
System.out.println("Response from Get is " + resGet);
433-
}
434-
if (resGet != null && !resGet.isEmpty())
435-
jnode = new ObjectMapper().readTree(resGet);
436-
else throw new Exception("Unexpected error " + response);
437-
438-
String dbName = jnode.get("content-database").asText();
439-
System.out.println("App Server's content database properties value from ClearDB is :" + dbName);
440-
441-
ObjectMapper mapper = new ObjectMapper();
442-
ObjectNode mainNode = mapper.createObjectNode();
443-
444-
mainNode.put("operation", "clear-database");
445-
446-
String postUrl = new String("http://" + host_name + ":" + admin_port + "/manage/v2/databases/" + dbName);
447-
Request requestSSLClear = new Request.Builder()
448-
.header("Content-type", "application/json")
449-
.url(postUrl)
450-
.post(RequestBody.create(mainNode.toString(), MediaType.parse("application/json")))
451-
.build();
452-
Response responseSSLClear = client.newCall(requestSSLClear).execute();
453-
if (responseSSLClear.code() == ML_RES_OK) {
454-
System.out.println(dbName + " database contents cleared");
455-
} else {
456-
System.out.println("Database contents did not clear");
457-
}
458-
} else {
459-
uri = "http://" + host_name + ":" + port + "/v1/search/";
460-
Request requestNormClear = new Request.Builder()
461-
.header("Content-type", "application/json")
462-
.url(uri)
463-
.delete()
464-
.build();
465-
Response responseNormClear = client.newCall(requestNormClear).execute();
466-
if (responseNormClear.code() == ML_RES_CHANGED)
467-
System.out.println("Content database cleared for App Server on port " + port);
468-
else {
469-
System.out.println("Content database not cleared");
470-
throw new Exception("Unexpected error " + responseNormClear);
471-
}
472-
}
473-
} catch (Exception e) {
474-
e.printStackTrace();
475-
} finally {
476-
client = null;
312+
try (DatabaseClient client = newDatabaseClientBuilder().withPort(port).build()) {
313+
QueryManager mgr = client.newQueryManager();
314+
mgr.delete(mgr.newDeleteDefinition());
477315
}
478316
}
479317

@@ -871,35 +709,6 @@ public static void deleteElementRangeIndexTemporalCollection(String dbName, Stri
871709
new TemporalCollectionManager(newManageClient(), dbName).deleteByIdField(collectionName);
872710
}
873711

874-
public static void loadBug18993() {
875-
OkHttpClient client = null;
876-
try {
877-
client = createHttpClient();
878-
String document = "<foo>a space b</foo>";
879-
String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute";
880-
String putStr = new String(
881-
"http://" + host_name + ":" + getRestAppServerPort() + "/v1/documents?uri=/a%20b&" + perm);
882-
Request request = new Request.Builder()
883-
.header("Content-type", "application/json")
884-
.url(putStr)
885-
.put(RequestBody.create(document.toLowerCase(), MediaType.parse("application/xml")))
886-
.build();
887-
Response response = client.newCall(request).execute();
888-
if (response.code() == ML_RES_BADREQT) {
889-
System.out.println(response);
890-
}
891-
else {
892-
System.out.println("Loading documents for test 189933 has issues");
893-
System.out.println(response);
894-
}
895-
} catch (Exception e) {
896-
// writing error to Log
897-
e.printStackTrace();
898-
} finally {
899-
client= null;
900-
}
901-
}
902-
903712
public static ObjectNode newServerPayload(String serverName) {
904713
ObjectNode payload = new ObjectMapper().createObjectNode();
905714
payload.put("server-name", serverName);
@@ -1087,8 +896,7 @@ public static DatabaseClient newAdminModulesClient() {
1087896
.build();
1088897
}
1089898

1090-
public static DatabaseClient getDatabaseClient(String user, String password, ConnectionType connType)
1091-
throws KeyManagementException, NoSuchAlgorithmException, IOException {
899+
public static DatabaseClient getDatabaseClient(String user, String password, ConnectionType connType) {
1092900
return newDatabaseClientBuilder()
1093901
.withUsername(user)
1094902
.withPassword(password)

0 commit comments

Comments
 (0)