aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/organization/MockIssueHandler.java
blob: fb4986d00614297ee79350af32c4732c78ad187b (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.organization;

import com.yahoo.component.annotation.Inject;
import com.yahoo.vespa.hosted.controller.api.integration.organization.IssueInfo.Status;

import java.io.InputStream;
import java.net.URI;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author jvenstad
 */
public class MockIssueHandler implements IssueHandler {

    private final Clock clock;
    private final AtomicLong counter = new AtomicLong();
    private final Map<IssueId, MockIssue> issues = new HashMap<>();
    private final Map<IssueId, Map<String, InputStream>> attachments = new HashMap<>();
    private final Map<String, ProjectInfo> projects = new HashMap<>();

    @Inject
    @SuppressWarnings("unused")
    public MockIssueHandler() {
        this(Clock.systemUTC());
    }

    public MockIssueHandler(Clock clock) {
        this.clock = clock;
    }

    @Override
    public IssueId file(Issue issue) {
        if (issue.assignee().isEmpty() && issue.assigneeId().isEmpty()) throw new RuntimeException();
        IssueId issueId = IssueId.from("" + counter.incrementAndGet());
        issues.put(issueId, new MockIssue(issue));
        return issueId;
    }

    @Override
    public List<IssueInfo> findAllBySimilarity(Issue issue) {
        return issues.entrySet().stream()
                     .filter(entry -> entry.getValue().issue.summary().equals(issue.summary()))
                     .map(entry -> new IssueInfo(entry.getKey(),
                                                 entry.getValue().updated,
                                                 entry.getValue().isOpen() ? Status.toDo : Status.done,
                                                 entry.getValue().assigneeId))
                     .toList();
    }

    @Override
    public void update(IssueId issueId, String description) {
        touch(issueId);
    }

    @Override
    public void commentOn(IssueId issueId, String comment) {
        touch(issueId);
    }

    @Override
    public boolean isOpen(IssueId issueId) {
        return issues.get(issueId).open;
    }

    @Override
    public boolean isActive(IssueId issueId, Duration maxInactivity) {
        return issues.get(issueId).updated.isAfter(clock.instant().minus(maxInactivity));
    }

    @Override
    public Optional<User> assigneeOf(IssueId issueId) {
        return Optional.ofNullable(issues.get(issueId).assignee);
    }

    @Override
    public Optional<AccountId> assigneeIdOf(IssueId issueId) {
        return Optional.ofNullable(issues.get(issueId).assigneeId);
    }

    @Override
    public boolean reassign(IssueId issueId, User assignee) {
        issues.get(issueId).assignee = assignee;
        touch(issueId);
        return true;
    }

    @Override
    public boolean addWatcher(IssueId issueId, String watcher) {
        issues.get(issueId).addWatcher(watcher);
        return true;
    }

    @Override
    public Optional<User> escalate(IssueId issueId, Contact contact) {
        List<List<User>> contacts = getContactUsers(contact);
        Optional<User> assignee = assigneeOf(issueId);
        int assigneeLevel = -1;
        if (assignee.isPresent())
            for (int level = contacts.size(); --level > assigneeLevel; )
                if (contacts.get(level).contains(assignee.get()))
                    assigneeLevel = level;

        for (int level = assigneeLevel + 1; level < contacts.size(); level++)
            for (User target : contacts.get(level))
                if (reassign(issueId, target))
                    return Optional.of(target);

        return Optional.empty();
    }

    @Override
    public boolean issueExists(Issue issue) {
        return issues.values().stream().anyMatch(i -> i.issue.summary().equals(issue.summary()));
    }

    @Override
    public ProjectInfo projectInfo(String projectKey) {
        return projects.get(projectKey);
    }

    @Override
    public void addAttachment(IssueId id, String filename, Supplier<InputStream> contentAsStream) {
        attachments.computeIfAbsent(id, __ -> new HashMap<>()).put(filename, contentAsStream.get());
    }

    public MockIssueHandler close(IssueId issueId) {
        issues.get(issueId).open = false;
        touch(issueId);
        return this;
    }

    public Map<IssueId, MockIssue> issues() {
        return issues;
    }

    private List<List<User>> getContactUsers(Contact contact) {
        return contact.persons().stream()
                .map(userList ->
                        userList.stream().map(user ->
                                user.split(" ")[0])
                                .map(User::from)
                                .toList()
                ).toList();
    }


    private void touch(IssueId issueId) {
        issues.get(issueId).updated = clock.instant();
    }

    public void addProject(String projectKey, ProjectInfo projectInfo) {
        projects.put(projectKey, projectInfo);
    }

    public class MockIssue {

        private Issue issue;
        private Instant updated;
        private boolean open;
        private User assignee;
        private AccountId assigneeId;
        private List<String> watchers;

        private MockIssue(Issue issue) {
            this.issue = issue;
            this.updated = clock.instant();
            this.open = true;
            this.assignee = issue.assignee().orElse(null);
            this.assigneeId = issue.assigneeId().orElse(null);
            this.watchers = new ArrayList<>();
        }

        public Issue issue() { return issue; }
        public User assignee() { return assignee; }
        public AccountId assigneeId() { return assigneeId; }
        public boolean isOpen() { return open; }
        public List<String> watchers() { return List.copyOf(watchers); }
        public void addWatcher(String watcher) { watchers.add(watcher); }

    }

}