How to test java(Elasticsearch) service?


(Nikita Krasnov) #1

Hello to everyone,
I use Elastic 6.1, Rest High level client for searching and adding information(logs and metrics).
Right know I need to test this service, methods for adding and saving information(like with the help of Junit4).
I have found documentation for this https://www.elastic.co/guide/en/elasticsearch/reference/current/testing-framework.html, but don't exactly understand how to do it and from what to start.
For example, I have such code for adding logs:

@Override
    public void addLogsToES(ElasticSearchLogs elasticSearchLogs) throws NoConnectionException {
        String date = new SimpleDateFormat("dd.MM.yyyy").format(new Date()).toLowerCase();
        XContentBuilder builder;
        try {
            builder = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("timeStamp", elasticSearchLogs.getTimeStamp())
                    .field("header", elasticSearchLogs.getHeader())
                    .field("type", elasticSearchLogs.getType())
                    .field("message", elasticSearchLogs.getMessage())
                    .field("level", elasticSearchLogs.getLevel())
                    .field("node", elasticSearchLogs.getNode())
                    .field("system", elasticSearchLogs.getSystem())
                    .field("version", elasticSearchLogs.getVersion())
                    .field("port", elasticSearchLogs.getPort())
                    .field("service", elasticSearchLogs.getService())
                    .field("module", elasticSearchLogs.getModule())
                    .field("submodule", elasticSearchLogs.getSubmodule())
                    .field("thread", elasticSearchLogs.getThread())
                    .field("operation", elasticSearchLogs.getOperation())
                    .field("sessionId", elasticSearchLogs.getSessionId())
                    .field("userLogin", elasticSearchLogs.getUserLogin())
                    .field("userName", elasticSearchLogs.getUserName())
                    .field("office", elasticSearchLogs.getOffice())
                    .field("location", elasticSearchLogs.getLocation())
                    .field("ip", elasticSearchLogs.getIp())
                    .field("device", elasticSearchLogs.getDevice())
                    .field("metricType", elasticSearchLogs.getMetricType())
                    .field("value", elasticSearchLogs.getValue())
                    .endObject();
        } catch (IOException e) {
            throw new NoConnectionException("No connection to Elasticsearch", e);
        }

        if (elasticSearchLogs.getSystem().equals("front-admin")
                           && (elasticSearchLogs.getTimeStamp() == null
                           || elasticSearchLogs.getOperation() == null
                           || elasticSearchLogs.getValue() == null || elasticSearchLogs.getValue() == null)) {
                       throw new IllegalArgumentException("elastic.metricValueEmpty");
                   }
            IndexRequest indexRequest = new IndexRequest("portal-logs-" + date,
                    "logs_info").source(builder);
            bulkProcessor.add(indexRequest);
    }

From what I should start?


(David Pilato) #2

It's unclear to me what is actually your question?

Is it a question about "how to start elasticsearch from my maven build?" or something like this?


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.