Skip to content

Commit 7a630da

Browse files
committed
Adding method to change assignment-policy.
1 parent ddab183 commit 7a630da

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

marklogic-client-api/src/test/java/com/marklogic/client/test/datamovement/QueryBatcherTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.fail;
2222

23+
import java.io.IOException;
24+
import java.io.InputStream;
2325
import java.io.StringWriter;
2426
import java.util.ArrayList;
2527
import java.util.Arrays;
@@ -42,11 +44,25 @@
4244

4345
import com.marklogic.client.io.Format;
4446
import com.marklogic.client.query.RawCtsQueryDefinition;
47+
48+
import org.apache.http.HttpEntity;
49+
import org.apache.http.HttpResponse;
50+
import org.apache.http.auth.AuthScope;
51+
import org.apache.http.auth.UsernamePasswordCredentials;
52+
import org.apache.http.client.methods.HttpGet;
53+
import org.apache.http.client.methods.HttpPut;
54+
import org.apache.http.entity.StringEntity;
55+
import org.apache.http.impl.client.DefaultHttpClient;
56+
import org.apache.http.util.EntityUtils;
4557
import org.junit.AfterClass;
4658
import org.junit.BeforeClass;
4759
import org.junit.Ignore;
4860
import org.junit.Test;
4961

62+
import com.fasterxml.jackson.databind.JsonNode;
63+
import com.fasterxml.jackson.databind.ObjectMapper;
64+
import com.fasterxml.jackson.databind.node.ArrayNode;
65+
import com.fasterxml.jackson.databind.node.ObjectNode;
5066
import com.marklogic.client.DatabaseClient;
5167
import com.marklogic.client.admin.QueryOptionsManager;
5268
import com.marklogic.client.document.ServerTransform;
@@ -114,6 +130,8 @@ public static void afterClass() {
114130
}
115131

116132
public static void setup() throws Exception {
133+
134+
changeAssignmentPolicy("bucket");
117135
WriteBatcher writeBatcher = moveMgr.newWriteBatcher();
118136
moveMgr.startJob(writeBatcher);
119137
// a collection so we're only looking at docs related to this test
@@ -880,5 +898,67 @@ class Output {
880898
// The number of documents should be more than maxuris but less than (maxuris+ threadcount*batchsize)
881899
assertTrue("Output list does not contain expected number of outputs", (outputUris.size() >= 20) && outputUris.size()<50);
882900
}
901+
902+
static void changeAssignmentPolicy(String value) throws IOException {
903+
904+
InputStream getResponseStream = null;
905+
DefaultHttpClient defaultClient = null;
906+
907+
String propertyName = "assignment-policy";
908+
ObjectMapper mapper = new ObjectMapper();
909+
ObjectNode mainNode = mapper.createObjectNode();
910+
ArrayNode childArray = mapper.createArrayNode();
911+
ObjectNode childNodeObject = mapper.createObjectNode();
912+
childNodeObject.put("assignment-policy-name", "bucket");
913+
childArray.add(childNodeObject);
914+
mainNode.withArray("assignment-policy").add(childArray);
915+
String dbName = "java-unittest";
916+
917+
try {
918+
defaultClient = new DefaultHttpClient();
919+
defaultClient.getCredentialsProvider().setCredentials(new AuthScope(client.getHost(), 8002),
920+
new UsernamePasswordCredentials("admin", "admin"));
921+
HttpGet getrequest = new HttpGet("http://" + client.getHost() + ":" + 8002 + "/manage/v2/databases/"
922+
+ dbName + "/properties?format=json");
923+
HttpResponse getResponse = defaultClient.execute(getrequest);
924+
getResponseStream = getResponse.getEntity().getContent();
925+
JsonNode jsonNode = mapper.readTree(getResponseStream);
926+
if (!jsonNode.isNull()) {
927+
if (!jsonNode.has(propertyName)) {
928+
((ObjectNode) jsonNode).putArray(propertyName).addAll(mainNode.withArray(propertyName));
929+
} else {
930+
if (!jsonNode.path(propertyName).isArray()) {
931+
((ObjectNode) jsonNode).putAll(mainNode);
932+
} else {
933+
JsonNode member = jsonNode.withArray(propertyName);
934+
if (mainNode.path(propertyName).isArray()) {
935+
((ArrayNode) member).addAll(mainNode.withArray(propertyName));
936+
}
937+
}
938+
}
939+
940+
HttpPut put = new HttpPut("http://" + client.getHost() + ":" + 8002 + "/manage/v2/databases/" + dbName
941+
+ "/properties?format=json");
942+
put.addHeader("Content-type", "application/json");
943+
put.setEntity(new StringEntity(jsonNode.toString()));
944+
945+
HttpResponse putResponse = defaultClient.execute(put);
946+
HttpEntity respEntity = putResponse.getEntity();
947+
if (respEntity != null) {
948+
String content = EntityUtils.toString(respEntity);
949+
System.out.println(content);
950+
}
951+
} else {
952+
System.out.println("REST call for database properties returned NULL "
953+
+ getResponse.getStatusLine().getStatusCode());
954+
}
955+
} catch (Exception e) {
956+
e.printStackTrace();
957+
} finally {
958+
if (getResponseStream != null)
959+
getResponseStream.close();
960+
defaultClient.getConnectionManager().shutdown();
961+
}
962+
}
883963

884964
}

0 commit comments

Comments
 (0)