summaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/MockedOperationHandler.java
blob: 895c34436ceb7dab1b198346f44ab752c70502f4 (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
64
65
66
67
68
69
70
71
72
73
// 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.FeedOperation;

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("")
                + options.bucketSpace.map(s -> String.format(", bucket space: '%s'", s)).orElse("")
                + options.cluster.map(s -> String.format(", cluster: '%s'", s)).orElse(""));
    }

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

    @Override
    public void update(RestUri restUri, FeedOperation data, Optional<String> route) throws RestApiException {
        log.append("UPDATE: " + data.getDocumentUpdate().getId());
        log.append(data.getDocumentUpdate().fieldUpdates().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, Optional<String> fieldSet) throws RestApiException {
        log.append("GET: " + restUri.generateFullId());
        // This is _not_ an elegant way to return data back to the test.
        // An alternative is removing this entire class in favor of explicit mock expectations.
        return fieldSet.map(fs -> String.format("{\"fields\": {\"fieldset\": \"%s\"}}", fs));
    }

    @Override
    public Optional<String> get(RestUri restUri) throws RestApiException {
        return get(restUri, Optional.empty());
    }

}