aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/vespaxmlparser/MockReader.java
blob: 3248108aa6de9633915672cb03bc9d4f6b52b73a (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaxmlparser;

import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentRemove;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.vespa.http.server.MetaStream;
import com.yahoo.vespa.http.server.util.ByteLimitedInputStream;

import java.io.InputStream;
import java.lang.reflect.Field;

/**
 * Mock for ExternalFeedTestCase which had to override package private methods.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class MockReader implements FeedReader {

    MetaStream stream;
    boolean finished = false;

    public MockReader(InputStream stream) throws Exception {
        this.stream = getMetaStream(stream);
    }

    private static MetaStream getMetaStream(InputStream stream) {
        if (stream instanceof MetaStream) {
            return (MetaStream) stream;
        }
        if (!(stream instanceof ByteLimitedInputStream)) {
            throw new IllegalStateException("Given unknown stream type.");
        }
        //Ooooooo this is so ugly
        try {
            ByteLimitedInputStream byteLimitedInputStream = (ByteLimitedInputStream) stream;
            Field f = byteLimitedInputStream.getClass().getDeclaredField("wrappedStream"); //NoSuchFieldException
            f.setAccessible(true);
            return (MetaStream) f.get(byteLimitedInputStream);
        } catch (Exception e) {
            throw new IllegalStateException("Implementation of ByteLimitedInputStream has changed.", e);
        }
    }

    @Override
    public FeedOperation read() throws Exception {
        if (finished) {
            return FeedOperation.INVALID;
        }

        byte whatToDo = stream.getNextOperation();
        DocumentId id = new DocumentId("id:banana:banana::doc1");
        DocumentType docType = new DocumentType("banana");
        return switch (whatToDo) {
            case 0 -> FeedOperation.INVALID;
            case 1 -> new DocumentFeedOperation(new DocumentPut(new Document(docType, id)));
            case 2 -> new RemoveFeedOperation(new DocumentRemove(id));
            case 3 -> new DocumentUpdateFeedOperation(new DocumentUpdate(docType, id));
            default -> throw new RuntimeException("boom");
        };
    }

}