Example usage for java.util StringJoiner StringJoiner

List of usage examples for java.util StringJoiner StringJoiner

Introduction

In this page you can find the example usage for java.util StringJoiner StringJoiner.

Prototype

public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 

Source Link

Document

Constructs a StringJoiner with no characters in it using copies of the supplied prefix , delimiter and suffix .

Usage

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testSearch() throws Exception {
    String[] indices = randomIndicesNames(0, 5);
    SearchRequest searchRequest = new SearchRequest(indices);

    int numTypes = randomIntBetween(0, 5);
    String[] types = new String[numTypes];
    for (int i = 0; i < numTypes; i++) {
        types[i] = "type-" + randomAlphaOfLengthBetween(2, 5);
    }//w w  w.  jav a 2 s . c o m
    searchRequest.types(types);

    Map<String, String> expectedParams = new HashMap<>();
    setRandomSearchParams(searchRequest, expectedParams);
    setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // rarely skip setting the search source completely
    if (frequently()) {
        // frequently set the search source to have some content, otherwise leave it
        // empty but still set it
        if (frequently()) {
            if (randomBoolean()) {
                searchSourceBuilder.size(randomIntBetween(0, Integer.MAX_VALUE));
            }
            if (randomBoolean()) {
                searchSourceBuilder.from(randomIntBetween(0, Integer.MAX_VALUE));
            }
            if (randomBoolean()) {
                searchSourceBuilder.minScore(randomFloat());
            }
            if (randomBoolean()) {
                searchSourceBuilder.explain(randomBoolean());
            }
            if (randomBoolean()) {
                searchSourceBuilder.profile(randomBoolean());
            }
            if (randomBoolean()) {
                searchSourceBuilder
                        .highlighter(new HighlightBuilder().field(randomAlphaOfLengthBetween(3, 10)));
            }
            if (randomBoolean()) {
                searchSourceBuilder.query(new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10),
                        randomAlphaOfLengthBetween(3, 10)));
            }
            if (randomBoolean()) {
                searchSourceBuilder.aggregation(
                        new TermsAggregationBuilder(randomAlphaOfLengthBetween(3, 10), ValueType.STRING)
                                .field(randomAlphaOfLengthBetween(3, 10)));
            }
            if (randomBoolean()) {
                searchSourceBuilder
                        .suggest(new SuggestBuilder().addSuggestion(randomAlphaOfLengthBetween(3, 10),
                                new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(3, 10))));
            }
            if (randomBoolean()) {
                searchSourceBuilder.addRescorer(new QueryRescorerBuilder(new TermQueryBuilder(
                        randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10))));
            }
            if (randomBoolean()) {
                searchSourceBuilder.collapse(new CollapseBuilder(randomAlphaOfLengthBetween(3, 10)));
            }
        }
        searchRequest.source(searchSourceBuilder);
    }

    Request request = RequestConverters.search(searchRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String index = String.join(",", indices);
    if (Strings.hasLength(index)) {
        endpoint.add(index);
    }
    String type = String.join(",", types);
    if (Strings.hasLength(type)) {
        endpoint.add(type);
    }
    endpoint.add("_search");
    assertEquals(HttpPost.METHOD_NAME, request.getMethod());
    assertEquals(endpoint.toString(), request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertToXContentBody(searchSourceBuilder, request.getEntity());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testSearchTemplate() throws Exception {
    // Create a random request.
    String[] indices = randomIndicesNames(0, 5);
    SearchRequest searchRequest = new SearchRequest(indices);

    Map<String, String> expectedParams = new HashMap<>();
    setRandomSearchParams(searchRequest, expectedParams);
    setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);

    SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(searchRequest);

    searchTemplateRequest.setScript("{\"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" }}}");
    searchTemplateRequest.setScriptType(ScriptType.INLINE);
    searchTemplateRequest.setProfile(randomBoolean());

    Map<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("field", "name");
    scriptParams.put("value", "soren");
    searchTemplateRequest.setScriptParams(scriptParams);

    // Verify that the resulting REST request looks as expected.
    Request request = RequestConverters.searchTemplate(searchTemplateRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String index = String.join(",", indices);
    if (Strings.hasLength(index)) {
        endpoint.add(index);/*from  ww  w .java  2  s  . c o  m*/
    }
    endpoint.add("_search/template");

    assertEquals(HttpGet.METHOD_NAME, request.getMethod());
    assertEquals(endpoint.toString(), request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertToXContentBody(searchTemplateRequest, request.getEntity());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testExistsAlias() {
    GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
    String[] indices = randomBoolean() ? null : randomIndicesNames(0, 5);
    getAliasesRequest.indices(indices);/*  w ww.  ja  va 2  s.c o m*/
    // the HEAD endpoint requires at least an alias or an index
    boolean hasIndices = indices != null && indices.length > 0;
    String[] aliases;
    if (hasIndices) {
        aliases = randomBoolean() ? null : randomIndicesNames(0, 5);
    } else {
        aliases = randomIndicesNames(1, 5);
    }
    getAliasesRequest.aliases(aliases);
    Map<String, String> expectedParams = new HashMap<>();
    setRandomLocal(getAliasesRequest, expectedParams);
    setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions,
            expectedParams);

    Request request = RequestConverters.existsAlias(getAliasesRequest);
    StringJoiner expectedEndpoint = new StringJoiner("/", "/", "");
    if (indices != null && indices.length > 0) {
        expectedEndpoint.add(String.join(",", indices));
    }
    expectedEndpoint.add("_alias");
    if (aliases != null && aliases.length > 0) {
        expectedEndpoint.add(String.join(",", aliases));
    }
    assertEquals(HttpHead.METHOD_NAME, request.getMethod());
    assertEquals(expectedEndpoint.toString(), request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertNull(request.getEntity());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testFieldCaps() {
    // Create a random request.
    String[] indices = randomIndicesNames(0, 5);
    String[] fields = generateRandomStringArray(5, 10, false, false);

    FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(indices)
            .fields(fields);//  w w  w  .  j  a va 2 s.c  o  m

    Map<String, String> indicesOptionsParams = new HashMap<>();
    setRandomIndicesOptions(fieldCapabilitiesRequest::indicesOptions, fieldCapabilitiesRequest::indicesOptions,
            indicesOptionsParams);

    Request request = RequestConverters.fieldCaps(fieldCapabilitiesRequest);

    // Verify that the resulting REST request looks as expected.
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String joinedIndices = String.join(",", indices);
    if (!joinedIndices.isEmpty()) {
        endpoint.add(joinedIndices);
    }
    endpoint.add("_field_caps");

    assertEquals(endpoint.toString(), request.getEndpoint());
    assertEquals(4, request.getParameters().size());

    // Note that we don't check the field param value explicitly, as field names are
    // passed through
    // a hash set before being added to the request, and can appear in a
    // non-deterministic order.
    assertThat(request.getParameters(), hasKey("fields"));
    String[] requestFields = Strings.splitStringByCommaToArray(request.getParameters().get("fields"));
    assertEquals(new HashSet<>(Arrays.asList(fields)), new HashSet<>(Arrays.asList(requestFields)));

    for (Map.Entry<String, String> param : indicesOptionsParams.entrySet()) {
        assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue()));
    }

    assertNull(request.getEntity());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testRankEval() throws Exception {
    RankEvalSpec spec = new RankEvalSpec(
            Collections.singletonList(
                    new RatedRequest("queryId", Collections.emptyList(), new SearchSourceBuilder())),
            new PrecisionAtK());
    String[] indices = randomIndicesNames(0, 5);
    RankEvalRequest rankEvalRequest = new RankEvalRequest(spec, indices);
    Map<String, String> expectedParams = new HashMap<>();
    setRandomIndicesOptions(rankEvalRequest::indicesOptions, rankEvalRequest::indicesOptions, expectedParams);

    Request request = RequestConverters.rankEval(rankEvalRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String index = String.join(",", indices);
    if (Strings.hasLength(index)) {
        endpoint.add(index);/* w  w  w  . ja v a2 s. co  m*/
    }
    endpoint.add(RestRankEvalAction.ENDPOINT);
    assertEquals(endpoint.toString(), request.getEndpoint());
    assertEquals(3, request.getParameters().size());
    assertEquals(expectedParams, request.getParameters());
    assertToXContentBody(spec, request.getEntity());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testPutPipeline() throws IOException {
    String pipelineId = "some_pipeline_id";
    PutPipelineRequest request = new PutPipelineRequest("some_pipeline_id",
            new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
    Map<String, String> expectedParams = new HashMap<>();
    setRandomMasterTimeout(request, expectedParams);
    setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);

    Request expectedRequest = RequestConverters.putPipeline(request);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    endpoint.add("_ingest/pipeline");
    endpoint.add(pipelineId);// w  ww . ja v  a 2 s  . co  m
    assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
    assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod());
    assertEquals(expectedParams, expectedRequest.getParameters());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testGetPipeline() {
    String pipelineId = "some_pipeline_id";
    Map<String, String> expectedParams = new HashMap<>();
    GetPipelineRequest request = new GetPipelineRequest("some_pipeline_id");
    setRandomMasterTimeout(request, expectedParams);
    Request expectedRequest = RequestConverters.getPipeline(request);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    endpoint.add("_ingest/pipeline");
    endpoint.add(pipelineId);//  w w w. j av a 2 s  .  c om
    assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
    assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod());
    assertEquals(expectedParams, expectedRequest.getParameters());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testDeletePipeline() {
    String pipelineId = "some_pipeline_id";
    Map<String, String> expectedParams = new HashMap<>();
    DeletePipelineRequest request = new DeletePipelineRequest(pipelineId);
    setRandomMasterTimeout(request, expectedParams);
    setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    Request expectedRequest = RequestConverters.deletePipeline(request);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    endpoint.add("_ingest/pipeline");
    endpoint.add(pipelineId);/*from   ww w  . ja v a  2s. co  m*/
    assertEquals(endpoint.toString(), expectedRequest.getEndpoint());
    assertEquals(HttpDelete.METHOD_NAME, expectedRequest.getMethod());
    assertEquals(expectedParams, expectedRequest.getParameters());
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testIndexPutSettings() throws IOException {
    String[] indices = randomBoolean() ? null : randomIndicesNames(0, 2);
    UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indices);
    Map<String, String> expectedParams = new HashMap<>();
    setRandomMasterTimeout(updateSettingsRequest, expectedParams);
    setRandomTimeout(updateSettingsRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    setRandomIndicesOptions(updateSettingsRequest::indicesOptions, updateSettingsRequest::indicesOptions,
            expectedParams);/*from   w w  w.  j  a  va2s .  co  m*/
    if (randomBoolean()) {
        updateSettingsRequest.setPreserveExisting(randomBoolean());
        if (updateSettingsRequest.isPreserveExisting()) {
            expectedParams.put("preserve_existing", "true");
        }
    }

    Request request = RequestConverters.indexPutSettings(updateSettingsRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    if (indices != null && indices.length > 0) {
        endpoint.add(String.join(",", indices));
    }
    endpoint.add("_settings");
    assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
    assertEquals(HttpPut.METHOD_NAME, request.getMethod());
    assertToXContentBody(updateSettingsRequest, request.getEntity());
    assertEquals(expectedParams, request.getParameters());
}

From source file:org.openecomp.sdc.be.model.operations.impl.PropertyOperation.java

private boolean isPropertyOmitted(List<PropertyDefinition> newProperties,
        List<PropertyDefinition> oldProperties, String dataTypeName) {

    boolean isValid = validateChangeInCaseOfEmptyProperties(newProperties, oldProperties, dataTypeName);
    if (false == isValid) {
        log.debug("At least one property is missing in the new data type {}", dataTypeName);
        return false;
    }//from w  ww .  j av  a  2 s. c o  m

    if (newProperties != null && oldProperties != null) {

        List<String> newProps = newProperties.stream().map(p -> p.getName()).collect(Collectors.toList());
        List<String> oldProps = oldProperties.stream().map(p -> p.getName()).collect(Collectors.toList());

        if (false == newProps.containsAll(oldProps)) {
            StringJoiner joiner = new StringJoiner(",", "[", "]");
            newProps.forEach(p -> joiner.add(p));
            log.debug(
                    "Properties {} in data type {} are missing, but they already defined in the existing data type",
                    joiner.toString(), dataTypeName);
            return true;
        }

    }
    return false;
}