Search in sources :

Example 11 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBlobHandler method postData.

public static void postData(CloudSolrClient cloudClient, String baseUrl, String blobName, ByteBuffer bytarr) throws IOException {
    HttpPost httpPost = null;
    HttpEntity entity;
    String response = null;
    try {
        httpPost = new HttpPost(baseUrl + "/.system/blob/" + blobName);
        httpPost.setHeader("Content-Type", "application/octet-stream");
        httpPost.setEntity(new ByteArrayEntity(bytarr.array(), bytarr.arrayOffset(), bytarr.limit()));
        entity = cloudClient.getLbClient().getHttpClient().execute(httpPost).getEntity();
        try {
            response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            Map m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
            assertFalse("Error in posting blob " + getAsString(m), m.containsKey("error"));
        } catch (JSONParser.ParseException e) {
            log.error("$ERROR$", response, e);
            fail();
        }
    } finally {
        httpPost.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 12 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestUseDocValuesAsStored2 method testSchemaAPI.

public void testSchemaAPI() throws Exception {
    RestTestHarness harness = restTestHarness;
    String payload = "{\n" + "          'add-field' : {\n" + "                       'name':'a1',\n" + "                       'type': 'string',\n" + "                       'stored':false,\n" + "                       'docValues':true,\n" + "                       'indexed':false\n" + "                       },\n" + "          'add-field' : {\n" + "                       'name':'a2',\n" + "                       'type': 'string',\n" + "                       'stored':false,\n" + "                       'useDocValuesAsStored':true,\n" + "                       'docValues':true,\n" + "                       'indexed':true\n" + "                       },\n" + "          'add-field' : {\n" + "                       'name':'a3',\n" + "                       'type': 'string',\n" + "                       'stored':false,\n" + "                       'useDocValuesAsStored':false,\n" + "                       'docValues':true,\n" + "                       'indexed':true\n" + "                       }\n" + "          }\n";
    String response = harness.post("/schema?wt=json", json(payload));
    Map m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, m.get("errors"));
    // default value of useDocValuesAsStored
    m = TestBulkSchemaAPI.getObj(harness, "a1", "fields");
    assertNotNull("field a1 not created", m);
    assertNull(m.get("useDocValuesAsStored"));
    // useDocValuesAsStored=true
    m = TestBulkSchemaAPI.getObj(harness, "a2", "fields");
    assertNotNull("field a2 not created", m);
    assertEquals(Boolean.TRUE, m.get("useDocValuesAsStored"));
    // useDocValuesAsStored=false
    m = TestBulkSchemaAPI.getObj(harness, "a3", "fields");
    assertNotNull("field a3 not created", m);
    assertEquals(Boolean.FALSE, m.get("useDocValuesAsStored"));
    // Index documents to check the effect
    assertU(adoc("id", "myid1", "a1", "1", "a2", "2", "a3", "3"));
    assertU(commit());
    RestTestBase.assertJQ("/select?q=id:myid*&fl=*", "/response/docs==[{'id':'myid1', 'a1':'1', 'a2':'2'}]");
    RestTestBase.assertJQ("/select?q=id:myid*&fl=id,a1,a2,a3", "/response/docs==[{'id':'myid1', 'a1':'1', 'a2':'2', 'a3':'3'}]");
    RestTestBase.assertJQ("/select?q=id:myid*&fl=a3", "/response/docs==[{'a3':'3'}]");
    // this will return a3 because it is explicitly requested even if '*' is specified
    RestTestBase.assertJQ("/select?q=id:myid*&fl=*,a3", "/response/docs==[{'id':'myid1', 'a1':'1', 'a2':'2', 'a3':'3'}]");
    // this will not return a3 because the glob 'a*' will match only stored + useDocValuesAsStored=true fields
    RestTestBase.assertJQ("/select?q=id:myid*&fl=id,a*", "/response/docs==[{'id':'myid1', 'a1':'1', 'a2':'2'}]");
    // Test replace-field
    // Explicitly set useDocValuesAsStored to false
    payload = "{\n" + "          'replace-field' : {\n" + "                       'name':'a1',\n" + "                       'type': 'string',\n" + "                       'stored':false,\n" + "                       'useDocValuesAsStored':false,\n" + "                       'docValues':true,\n" + "                       'indexed':false\n" + "                       }}";
    response = harness.post("/schema?wt=json", json(payload));
    m = TestBulkSchemaAPI.getObj(harness, "a1", "fields");
    assertNotNull("field a1 doesn't exist any more", m);
    assertEquals(Boolean.FALSE, m.get("useDocValuesAsStored"));
    // Explicitly set useDocValuesAsStored to true
    payload = "{\n" + "          'replace-field' : {\n" + "                       'name':'a1',\n" + "                       'type': 'string',\n" + "                       'stored':false,\n" + "                       'useDocValuesAsStored':true,\n" + "                       'docValues':true,\n" + "                       'indexed':false\n" + "                       }}";
    response = harness.post("/schema?wt=json", json(payload));
    m = TestBulkSchemaAPI.getObj(harness, "a1", "fields");
    assertNotNull("field a1 doesn't exist any more", m);
    assertEquals(Boolean.TRUE, m.get("useDocValuesAsStored"));
    // add a field which is stored as well as docvalues
    payload = "{          'add-field' : {\n" + "                       'name':'a4',\n" + "                       'type': 'string',\n" + "                       'stored':true,\n" + "                       'useDocValuesAsStored':true,\n" + "                       'docValues':true,\n" + "                       'indexed':true\n" + "                       }}";
    response = harness.post("/schema?wt=json", json(payload));
    m = TestBulkSchemaAPI.getObj(harness, "a4", "fields");
    assertNotNull("field a4 not found", m);
    assertEquals(Boolean.TRUE, m.get("useDocValuesAsStored"));
    assertU(adoc("id", "myid1", "a1", "1", "a2", "2", "a3", "3", "a4", "4"));
    assertU(commit());
    RestTestBase.assertJQ("/select?q=id:myid*&fl=*", "/response/docs==[{'id':'myid1', 'a1':'1', 'a2':'2', 'a4':'4'}]");
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 13 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestConfigSetsAPI method postDataAndGetResponse.

public static Map postDataAndGetResponse(CloudSolrClient cloudClient, String uri, ByteBuffer bytarr, String username, String password) throws IOException {
    HttpPost httpPost = null;
    HttpEntity entity;
    String response = null;
    Map m = null;
    try {
        httpPost = new HttpPost(uri);
        if (username != null) {
            String userPass = username + ":" + password;
            String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8));
            BasicHeader header = new BasicHeader("Authorization", "Basic " + encoded);
            httpPost.setHeader(header);
        }
        httpPost.setHeader("Content-Type", "application/octet-stream");
        httpPost.setEntity(new ByteArrayEntity(bytarr.array(), bytarr.arrayOffset(), bytarr.limit()));
        entity = cloudClient.getLbClient().getHttpClient().execute(httpPost).getEntity();
        try {
            response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
        } catch (JSONParser.ParseException e) {
            fail(e.getMessage());
        }
    } finally {
        httpPost.releaseConnection();
    }
    return m;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BasicHeader(org.apache.http.message.BasicHeader)

Example 14 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestConfigSetImmutable method testSolrConfigHandlerImmutable.

@Test
public void testSolrConfigHandlerImmutable() throws Exception {
    String payload = "{\n" + "'create-requesthandler' : { 'name' : '/x', 'class': 'org.apache.solr.handler.DumpRequestHandler' , 'startup' : 'lazy'}\n" + "}";
    String uri = "/config?wt=json";
    String response = restTestHarness.post(uri, SolrTestCaseJ4.json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNotNull(map.get("error"));
    assertTrue(map.get("error").toString().contains("immutable"));
}
Also used : StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map) Test(org.junit.Test)

Example 15 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestSolrConfigHandlerConcurrent method getAsMap.

public static Map getAsMap(String uri, CloudSolrClient cloudClient) throws Exception {
    HttpGet get = new HttpGet(uri);
    HttpEntity entity = null;
    try {
        entity = cloudClient.getLbClient().getHttpClient().execute(get).getEntity();
        String response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        try {
            return (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
        } catch (JSONParser.ParseException e) {
            log.error(response, e);
            throw e;
        }
    } finally {
        EntityUtils.consumeQuietly(entity);
        get.releaseConnection();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Aggregations

JSONParser (org.noggit.JSONParser)37 Map (java.util.Map)30 StringReader (java.io.StringReader)25 RestTestHarness (org.apache.solr.util.RestTestHarness)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6 HttpEntity (org.apache.http.HttpEntity)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 SolrException (org.apache.solr.common.SolrException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 NamedList (org.apache.solr.common.util.NamedList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2