|
20 | 20 | import static org.junit.Assert.assertFalse; |
21 | 21 | import static org.junit.Assert.fail; |
22 | 22 |
|
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
23 | 25 | import java.io.StringWriter; |
24 | 26 | import java.util.ArrayList; |
25 | 27 | import java.util.Arrays; |
|
42 | 44 |
|
43 | 45 | import com.marklogic.client.io.Format; |
44 | 46 | 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; |
45 | 57 | import org.junit.AfterClass; |
46 | 58 | import org.junit.BeforeClass; |
47 | 59 | import org.junit.Ignore; |
48 | 60 | import org.junit.Test; |
49 | 61 |
|
| 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; |
50 | 66 | import com.marklogic.client.DatabaseClient; |
51 | 67 | import com.marklogic.client.admin.QueryOptionsManager; |
52 | 68 | import com.marklogic.client.document.ServerTransform; |
@@ -114,6 +130,8 @@ public static void afterClass() { |
114 | 130 | } |
115 | 131 |
|
116 | 132 | public static void setup() throws Exception { |
| 133 | + |
| 134 | + changeAssignmentPolicy("bucket"); |
117 | 135 | WriteBatcher writeBatcher = moveMgr.newWriteBatcher(); |
118 | 136 | moveMgr.startJob(writeBatcher); |
119 | 137 | // a collection so we're only looking at docs related to this test |
@@ -880,5 +898,67 @@ class Output { |
880 | 898 | // The number of documents should be more than maxuris but less than (maxuris+ threadcount*batchsize) |
881 | 899 | assertTrue("Output list does not contain expected number of outputs", (outputUris.size() >= 20) && outputUris.size()<50); |
882 | 900 | } |
| 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 | + } |
883 | 963 |
|
884 | 964 | } |
0 commit comments