summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/MockedOperationHandler.java
blob: 3b16d9a378cb8d9a3bd563dcb30e89228e923e51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.restapi.resource;

import com.yahoo.document.restapi.OperationHandler;
import com.yahoo.document.restapi.Response;
import com.yahoo.document.restapi.RestApiException;
import com.yahoo.document.restapi.RestUri;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader;

import java.util.Optional;

/**
 * Mock that collects info about operation and returns them on second delete.
 */
public class MockedOperationHandler implements OperationHandler {

    StringBuilder log = new StringBuilder();
    int deleteCount = 0;

    @Override
    public VisitResult visit(RestUri restUri, String documentSelection, VisitOptions options) throws RestApiException {
        return new VisitResult(Optional.of("token"), "List of json docs, cont token "
                + options.continuation.orElse("not set") + ", doc selection: '"
                + documentSelection + "'"
                + options.wantedDocumentCount.map(n -> String.format(", min docs returned: %d", n)).orElse("")
                + options.fieldSet.map(s -> String.format(", field set: '%s'", s)).orElse("")
                + options.concurrency.map(n -> String.format(", concurrency: %d", n)).orElse(""));
    }

    @Override
    public void put(RestUri restUri, VespaXMLFeedReader.Operation data, Optional<String> route) throws RestApiException {
        log.append("PUT: " + data.getDocument().getId());
        log.append(data.getDocument().getBody().toString());
    }

    @Override
    public void update(RestUri restUri, VespaXMLFeedReader.Operation data, Optional<String> route) throws RestApiException {
        log.append("UPDATE: " + data.getDocumentUpdate().getId());
        log.append(data.getDocumentUpdate().getFieldUpdates().toString());
        if (data.getDocumentUpdate().getCreateIfNonExistent()) {
            log.append("[CREATE IF NON EXISTENT IS TRUE]");
        }
    }

    @Override
    public void delete(RestUri restUri, String condition, Optional<String> route) throws RestApiException {
        deleteCount++;
        if (deleteCount == 2) {
            String theLog = log.toString();
            log = new StringBuilder();
            deleteCount = 0;
            throw new RestApiException(Response.createErrorResponse(666, theLog, RestUri.apiErrorCodes.ERROR_ID_BASIC_USAGE));
        }
        log.append("DELETE: " + restUri.generateFullId());
    }

    @Override
    public Optional<String> get(RestUri restUri) throws RestApiException {
        log.append("GET: " + restUri.generateFullId());
        return Optional.empty();
    }

}