如何测试java(Elasticsearch)服务?

时间:2018-03-01 09:29:20

标签: java elasticsearch junit

我使用Java 8,Elastic 6.1,Rest高级客户端来搜索和添加信息(日志和指标)。 知道我需要测试这个服务,添加和保存信息的方法(比如在Junit4的帮助下)。 我找到了此https://www.elastic.co/guide/en/elasticsearch/reference/current/testing-framework.html的文档,但并不完全了解如何操作以及从何处开始。 例如,我有添加日志的代码:

@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);
    }

从我应该开始?

0 个答案:

没有答案