org.elasticsearch.client.ResponseException Java Examples

The following examples show how to use org.elasticsearch.client.ResponseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricStoreImpl.java    From griffin with Apache License 2.0 6 votes vote down vote up
@Override
public List<MetricValue> getMetricValues(String metricName, int from,
                                         int size, long tmst)
    throws IOException {
    HttpEntity entity = getHttpEntityForSearch(metricName, from, size,
        tmst);
    try {
        Response response = client.performRequest("GET", urlGet,
            Collections.emptyMap(), entity);
        return getMetricValuesFromResponse(response);
    } catch (ResponseException e) {
        if (e.getResponse().getStatusLine().getStatusCode() == 404) {
            return Collections.emptyList();
        }
        throw e;
    }
}
 
Example #2
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testStartAdJobWithNonexistingDetectorIndex() throws Exception {
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "no such index [.opendistro-anomaly-detectors]",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + randomAlphaOfLength(10) + "/_start",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #3
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testStartAdJobWithNonexistingDetector() throws Exception {
    createRandomAnomalyDetector(true, false);
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "AnomalyDetector is not found with id",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + randomAlphaOfLength(10) + "/_start",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #4
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testStopNonExistingAdJobIndex() throws Exception {
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "no such index [.opendistro-anomaly-detector-jobs]",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + randomAlphaOfLength(10) + "/_stop",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #5
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testStartAdjobWithNullFeatures() throws Exception {
    AnomalyDetector detectorWithoutFeature = TestHelpers.randomAnomalyDetector(null, null, Instant.now());
    String indexName = detectorWithoutFeature.getIndices().get(0);
    TestHelpers.createIndex(client(), indexName, toHttpEntity("{\"name\": \"test\"}"));
    AnomalyDetector detector = createAnomalyDetector(detectorWithoutFeature, true);
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Can't start detector job as no features configured",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId() + "/_start",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #6
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testStartAdjobWithEmptyFeatures() throws Exception {
    AnomalyDetector detectorWithoutFeature = TestHelpers.randomAnomalyDetector(ImmutableList.of(), null, Instant.now());
    String indexName = detectorWithoutFeature.getIndices().get(0);
    TestHelpers.createIndex(client(), indexName, toHttpEntity("{\"name\": \"test\"}"));
    AnomalyDetector detector = createAnomalyDetector(detectorWithoutFeature, true);
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Can't start detector job as no features configured",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId() + "/_start",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #7
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testPreviewAnomalyDetectorWithDetectorAndNoFeatures() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, true);
    AnomalyDetectorExecutionInput input = new AnomalyDetectorExecutionInput(
        detector.getDetectorId(),
        Instant.now().minusSeconds(60 * 10),
        Instant.now(),
        randomAnomalyDetectorWithEmptyFeature()
    );
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Can't preview detector without feature",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    String.format(TestHelpers.AD_BASE_PREVIEW_URI, input.getDetectorId()),
                    ImmutableMap.of(),
                    toHttpEntity(input),
                    null
                )
        );
}
 
Example #8
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testExecuteAnomalyDetectorWithNullDetectorId() throws Exception {
    AnomalyDetectorExecutionInput input = new AnomalyDetectorExecutionInput(
        null,
        Instant.now().minusSeconds(60 * 10),
        Instant.now(),
        null
    );
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    String.format(TestHelpers.AD_BASE_PREVIEW_URI, input.getDetectorId()),
                    ImmutableMap.of(),
                    toHttpEntity(input),
                    null
                )
        );
}
 
Example #9
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testPreviewAnomalyDetectorWhichNotExist() throws Exception {
    createRandomAnomalyDetector(true, false);
    AnomalyDetectorExecutionInput input = new AnomalyDetectorExecutionInput(
        randomAlphaOfLength(5),
        Instant.now().minusSeconds(60 * 10),
        Instant.now(),
        null
    );
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    String.format(TestHelpers.AD_BASE_PREVIEW_URI, input.getDetectorId()),
                    ImmutableMap.of(),
                    toHttpEntity(input),
                    null
                )
        );
}
 
Example #10
Source File: DefaultMigrationClient.java    From elasticsearch-migration with Apache License 2.0 6 votes vote down vote up
public void performRequestIgnoreExistingExceptions(final Migration migration) {
    try {
        performRequest(migration);
    } catch (MigrationFailedException e) {
        if (e.getCause() instanceof ResponseException) {
            final ResponseException responseException = (ResponseException) e.getCause();
            if (responseException.getResponse().getStatusLine().getStatusCode() == 400 &&
                    (responseException.getMessage().contains("index_already_exists_exception") || // ES 5.x
                            responseException.getMessage().contains("resource_already_exists_exception") || // ES 6.x
                            responseException.getMessage().contains("IndexAlreadyExistsException"))) { // ES 1.x and 2.x
                return;
            }
        }

        throw e;
    }
}
 
Example #11
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void elasticsearchOssImage() throws IOException {
    try (
        // oosContainer {
        ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_VERSION)
        // }
    ) {
        container.start();
        Response response = getClient(container).performRequest(new Request("GET", "/"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        // The OSS image does not have any feature under Elastic License
        assertThrows("We should not have /_xpack endpoint with an OSS License",
            ResponseException.class,
            () -> getClient(container).performRequest(new Request("GET", "/_xpack/")));
    }
}
 
Example #12
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testCreateAnomalyDetector() throws Exception {
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(TestHelpers.randomUiMetadata(), null);
    String indexName = detector.getIndices().get(0);
    TestHelpers.createIndex(client(), indexName, toHttpEntity("{\"name\": \"test\"}"));

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, false);

    Exception ex = expectThrows(
        ResponseException.class,
        () -> TestHelpers
            .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null)
    );
    assertThat(ex.getMessage(), containsString(CommonErrorMessages.DISABLED_ERR_MSG));

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, true);
    Response response = TestHelpers
        .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null);
    assertEquals("Create anomaly detector failed", RestStatus.CREATED, restStatus(response));
    Map<String, Object> responseMap = entityAsMap(response);
    String id = (String) responseMap.get("_id");
    int version = (int) responseMap.get("_version");
    assertNotEquals("response is missing Id", AnomalyDetector.NO_ID, id);
    assertTrue("incorrect version", version > 0);
}
 
Example #13
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testCreateAnomalyDetectorWithEmptyIndices() throws Exception {
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(TestHelpers.randomUiMetadata(), null);
    TestHelpers
        .makeRequest(
            client(),
            "PUT",
            "/" + detector.getIndices().get(0),
            ImmutableMap.of(),
            toHttpEntity("{\"settings\":{\"number_of_shards\":1},\"mappings\":{\"properties\":" + "{\"field1\":{\"type\":\"text\"}}}}"),
            null
        );

    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Can't create anomaly detector as no document found in indices",
            () -> TestHelpers
                .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null)
        );
}
 
Example #14
Source File: ElasticsearchClient.java    From presto with Apache License 2.0 6 votes vote down vote up
private static PrestoException propagate(ResponseException exception)
{
    HttpEntity entity = exception.getResponse().getEntity();

    if (entity != null && entity.getContentType() != null) {
        try {
            JsonNode reason = OBJECT_MAPPER.readTree(entity.getContent()).path("error")
                    .path("root_cause")
                    .path(0)
                    .path("reason");

            if (!reason.isMissingNode()) {
                throw new PrestoException(ELASTICSEARCH_QUERY_FAILURE, reason.asText(), exception);
            }
        }
        catch (IOException e) {
            PrestoException result = new PrestoException(ELASTICSEARCH_QUERY_FAILURE, exception);
            result.addSuppressed(e);
            throw result;
        }
    }

    throw new PrestoException(ELASTICSEARCH_QUERY_FAILURE, exception);
}
 
Example #15
Source File: DocumentService.java    From elastic-rabbitmq with MIT License 5 votes vote down vote up
public ESSaveResponse update(String index, String type, Long sourceId, Map<String, String> params, HttpEntity requestBody) {
    params = addTenantId2Param(params);

    // for real-time fetch
    //params.put("refresh", "true");
    try {
        Response response = client.performRequest(
                "POST",
                index + "/" + type + "/" + sourceId + "/_update",
                params,
                requestBody);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode > 299) {
            logger.warn("Problem while indexing a document: {}" +
                    response.getStatusLine().getReasonPhrase());
            throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
        }

        ESSaveResponse esResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                ESSaveResponse.class);
        return esResponse;
    } catch (ResponseException rex) {
        logger.warn("Got elasticsearch exception " + rex);
        Response res = rex.getResponse();
        if (res.getStatusLine().getStatusCode() == 409) {
            logger.warn("Conflict on store object");
            throw new ElasticVersionConflictException("type:" + type + " params:" + params.toString()
                    + " exception:" + rex);
        }
    }catch (IOException e) {
        logger.error("Failed to update document with type [" + type + "] id ["+sourceId+"]");
    }

    return null;
}
 
Example #16
Source File: NodeMappingFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static boolean mappingAlreadyExist(ReactorElasticSearchClient client, IndexName indexName) throws IOException {
    try {
        client.getLowLevelClient().performRequest("GET", "/" + indexName.getValue() + "/_mapping/" + NodeMappingFactory.DEFAULT_MAPPING_NAME);
        return true;
    } catch (ResponseException e) {
        if (e.getResponse().getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
            throw e;
        }
    }
    return false;
}
 
Example #17
Source File: DocumentService.java    From elastic-rabbitmq with MIT License 5 votes vote down vote up
public ESSaveResponse Store(String index, String type, Long sourceId, Map<String, String> params, HttpEntity requestBody) {
    params = addTenantId2Param(params);
    // for real-time fetch
    //params.put("refresh", "true");
    try {
        Response response = client.performRequest(
                "POST",
                index + "/" + type + "/" + sourceId,
                params,
                requestBody);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode > 299) {
            logger.warn("Problem while indexing a document: {}" + response.getStatusLine().getReasonPhrase());
            throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
        }

        ESSaveResponse esQueryResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                ESSaveResponse.class);
        return esQueryResponse;
    } catch (ResponseException rex) {
        logger.warn("Got elasticsearch exception " + rex);
        Response res = rex.getResponse();
        if (res.getStatusLine().getStatusCode() == 409) {
            logger.warn("Conflict on store object");
            throw new ElasticVersionConflictException(index+type);
        }
    } catch (IOException e) {
        logger.error("Failed to store document with type [" + type + "] id [" + sourceId + "]: ",e);
    }
    return null;
}
 
Example #18
Source File: DocumentService.java    From elastic-rest-spring-wrapper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * By specifying the unique identification of an object we can return only that object. If we cannot find the object
 * we throw an {@link QueryByIdNotFoundException}.
 *
 * @param clusterName Name of the cluster to connect to
 * @param request     Object containing the required parameters
 * @param <T>         Type of the object to be mapped to
 * @return Found object of type T
 */
public <T> T queryById(String clusterName, QueryByIdRequest request) {
    if (request.getTypeReference() == null) {
        throw new QueryExecutionException("The TypeReference in the request cannot be null");
    }
    try {
        String endpoint = createEndpointString(request.getIndex(), request.getType(), request.getId());

        Response response = clusterManagementService.getRestClientForCluster(clusterName).performRequest(GET, endpoint, getRequestParams(request));

        GetByIdResponse<T> queryResponse = jacksonObjectMapper.readValue(response.getEntity().getContent(), request.getTypeReference());

        if (!queryResponse.getFound()) {
            throw new QueryByIdNotFoundException(request.getIndex(), request.getType(), request.getId());
        }

        T entity = queryResponse.getSource();

        if (request.getAddId()) {
            addIdToEntity(request.getId(), entity);
        }

        return entity;
    } catch (ResponseException re) {
        if (re.getResponse().getStatusLine().getStatusCode() == 404) {
            throw new QueryByIdNotFoundException(request.getIndex(), request.getType(), request.getId());
        } else {
            logger.warn("Problem while executing request.", re);
            throw new QueryExecutionException("Error when executing a document");
        }
    } catch (IOException e) {
        logger.warn("Problem while executing request.", e);
        throw new QueryExecutionException("Error when executing a document");
    }
}
 
Example #19
Source File: CustomRealmIT.java    From shield-custom-realm-example with Apache License 2.0 5 votes vote down vote up
public void testHttpConnectionWithNoAuthentication() throws Exception {
    try {
        Response bad = getRestClient().performRequest("GET", "/", Collections.emptyMap());
        fail("an exception should be thrown but got: " + bad.getEntity().toString());
    } catch (ResponseException e) {
        Response response = e.getResponse();
        assertThat(response.getStatusLine().getStatusCode(), is(401));
        String value = response.getHeader("WWW-Authenticate");
        assertThat(value, is("custom-challenge"));
    }
}
 
Example #20
Source File: RestElasticClientTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGetMappingNotFound() throws IOException {
    ResponseException mockException = mock(ResponseException.class);
    when(mockException.getResponse()).thenReturn(mockResponse);
    when(mockStatusLine.getStatusCode()).thenReturn(404);

    when(mockRestClient.performRequest(any(Request.class))).thenThrow(mockException);
    assertNull(client.getMapping("status_s", "active"));
}
 
Example #21
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testStopNonExistingAdJob() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, false);
    Response startAdJobResponse = TestHelpers
        .makeRequest(
            client(),
            "POST",
            TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId() + "/_start",
            ImmutableMap.of(),
            "",
            null
        );
    assertEquals("Fail to start AD job", RestStatus.OK, restStatus(startAdJobResponse));

    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Anomaly detector job not exist",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + randomAlphaOfLength(10) + "/_stop",
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #22
Source File: RestElasticClientTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected=ResponseException.class)
public void testGetMappingWithError() throws IOException {
    ResponseException mockException = mock(ResponseException.class);
    when(mockException.getResponse()).thenReturn(mockResponse);
    when(mockStatusLine.getStatusCode()).thenReturn(400);

    when(mockRestClient.performRequest(any(Request.class))).thenThrow(mockException);
    client.getMapping("status_s", "active");
}
 
Example #23
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testCreateAnomalyDetectorWithNotExistingIndices() throws Exception {
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(TestHelpers.randomUiMetadata(), null);
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "index_not_found_exception",
            () -> TestHelpers
                .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null)
        );
}
 
Example #24
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, true);

    AnomalyDetector detectorDuplicateName = new AnomalyDetector(
        AnomalyDetector.NO_ID,
        randomLong(),
        detector.getName(),
        randomAlphaOfLength(5),
        randomAlphaOfLength(5),
        detector.getIndices(),
        ImmutableList.of(randomFeature()),
        randomQuery(),
        randomIntervalTimeConfiguration(),
        randomIntervalTimeConfiguration(),
        randomUiMetadata(),
        randomInt(),
        null
    );

    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Cannot create anomaly detector with name",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI,
                    ImmutableMap.of(),
                    toHttpEntity(detectorDuplicateName),
                    null
                )
        );
}
 
Example #25
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testDeleteAnomalyDetectorWithRunningAdJob() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, false);

    Response startAdJobResponse = TestHelpers
        .makeRequest(
            client(),
            "POST",
            TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId() + "/_start",
            ImmutableMap.of(),
            "",
            null
        );

    assertEquals("Fail to start AD job", RestStatus.OK, restStatus(startAdJobResponse));

    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Detector job is running",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "DELETE",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId(),
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #26
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testDeleteAnomalyDetectorWhichNotExist() throws Exception {
    TestHelpers
        .assertFailWith(
            ResponseException.class,
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "DELETE",
                    TestHelpers.AD_BASE_DETECTORS_URI + "/" + randomAlphaOfLength(5),
                    ImmutableMap.of(),
                    "",
                    null
                )
        );
}
 
Example #27
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testDeleteAnomalyDetector() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, false);

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, false);

    Exception ex = expectThrows(
        ResponseException.class,
        () -> TestHelpers
            .makeRequest(
                client(),
                "DELETE",
                TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId(),
                ImmutableMap.of(),
                "",
                null
            )
    );
    assertThat(ex.getMessage(), containsString(CommonErrorMessages.DISABLED_ERR_MSG));

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, true);

    Response response = TestHelpers
        .makeRequest(
            client(),
            "DELETE",
            TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId(),
            ImmutableMap.of(),
            "",
            null
        );
    assertEquals("Delete anomaly detector failed", RestStatus.OK, restStatus(response));
}
 
Example #28
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testPreviewAnomalyDetector() throws Exception {
    AnomalyDetector detector = createRandomAnomalyDetector(true, false);
    AnomalyDetectorExecutionInput input = new AnomalyDetectorExecutionInput(
        detector.getDetectorId(),
        Instant.now().minusSeconds(60 * 10),
        Instant.now(),
        null
    );

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, false);

    Exception ex = expectThrows(
        ResponseException.class,
        () -> TestHelpers
            .makeRequest(
                client(),
                "POST",
                String.format(AD_BASE_PREVIEW_URI, input.getDetectorId()),
                ImmutableMap.of(),
                toHttpEntity(input),
                null
            )
    );
    assertThat(ex.getMessage(), containsString(CommonErrorMessages.DISABLED_ERR_MSG));

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, true);

    Response response = TestHelpers
        .makeRequest(
            client(),
            "POST",
            String.format(AD_BASE_PREVIEW_URI, input.getDetectorId()),
            ImmutableMap.of(),
            toHttpEntity(input),
            null
        );
    assertEquals("Execute anomaly detector failed", RestStatus.OK, restStatus(response));
}
 
Example #29
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testStatsAnomalyDetector() throws Exception {
    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, false);
    Exception ex = expectThrows(
        ResponseException.class,
        () -> TestHelpers.makeRequest(client(), "GET", AnomalyDetectorPlugin.AD_BASE_URI + "/stats", ImmutableMap.of(), "", null)
    );
    assertThat(ex.getMessage(), containsString(CommonErrorMessages.DISABLED_ERR_MSG));

    updateClusterSettings(EnabledSetting.AD_PLUGIN_ENABLED, true);

    Response statsResponse = TestHelpers
        .makeRequest(client(), "GET", AnomalyDetectorPlugin.AD_BASE_URI + "/stats", ImmutableMap.of(), "", null);

    assertEquals("Get stats failed", RestStatus.OK, restStatus(statsResponse));
}
 
Example #30
Source File: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testUpdateAnomalyDetectorNameToExisting() throws Exception {
    AnomalyDetector detector1 = createRandomAnomalyDetector(true, true);

    AnomalyDetector detector2 = createRandomAnomalyDetector(true, true);

    AnomalyDetector newDetector1WithDetector2Name = new AnomalyDetector(
        detector1.getDetectorId(),
        detector1.getVersion(),
        detector2.getName(),
        detector1.getDescription(),
        detector1.getTimeField(),
        detector1.getIndices(),
        detector1.getFeatureAttributes(),
        detector1.getFilterQuery(),
        detector1.getDetectionInterval(),
        detector1.getWindowDelay(),
        detector1.getUiMetadata(),
        detector1.getSchemaVersion(),
        detector1.getLastUpdateTime()
    );

    TestHelpers
        .assertFailWith(
            ResponseException.class,
            "Cannot create anomaly detector with name",
            () -> TestHelpers
                .makeRequest(
                    client(),
                    "POST",
                    TestHelpers.AD_BASE_DETECTORS_URI,
                    ImmutableMap.of(),
                    toHttpEntity(newDetector1WithDetector2Name),
                    null
                )
        );
}