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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.billing;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.api.integration.user.User;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* @author olaa
*/
public class MockBillingController implements BillingController {
Map<TenantName, PlanId> plans = new HashMap<>();
Map<TenantName, PaymentInstrument> activeInstruments = new HashMap<>();
Map<TenantName, List<Invoice>> committedInvoices = new HashMap<>();
Map<TenantName, Invoice> uncommittedInvoices = new HashMap<>();
Map<TenantName, List<Invoice.LineItem>> unusedLineItems = new HashMap<>();
@Override
public PlanId getPlan(TenantName tenant) {
return plans.getOrDefault(tenant, PlanId.from("trial"));
}
@Override
public PlanResult setPlan(TenantName tenant, PlanId planId, boolean hasDeployments) {
plans.put(tenant, planId);
return PlanResult.success();
}
@Override
public Invoice.Id createInvoiceForPeriod(TenantName tenant, ZonedDateTime startTime, ZonedDateTime endTime, String agent) {
var invoiceId = Invoice.Id.of("id-123");
committedInvoices.computeIfAbsent(tenant, l -> new ArrayList<>())
.add(new Invoice(
invoiceId,
Invoice.StatusHistory.open(),
List.of(),
startTime,
endTime
));
return invoiceId;
}
@Override
public Invoice createUncommittedInvoice(TenantName tenant, LocalDate until) {
return uncommittedInvoices.getOrDefault(tenant, emptyInvoice());
}
@Override
public Map<TenantName, Invoice> createUncommittedInvoices(LocalDate until) {
return uncommittedInvoices;
}
@Override
public List<Invoice.LineItem> getUnusedLineItems(TenantName tenant) {
return unusedLineItems.getOrDefault(tenant, List.of());
}
@Override
public Optional<PaymentInstrument> getDefaultInstrument(TenantName tenant) {
return Optional.ofNullable(activeInstruments.get(tenant));
}
@Override
public String createClientToken(String tenant, String userId) {
return "some-token";
}
@Override
public boolean deleteInstrument(TenantName tenant, String userId, String instrumentId) {
activeInstruments.remove(tenant);
return true;
}
@Override
public void updateInvoiceStatus(Invoice.Id invoiceId, String agent, String status) {
committedInvoices.values().stream()
.flatMap(List::stream)
.filter(invoice -> invoiceId.equals(invoice.id()))
.forEach(invoice -> invoice.statusHistory().history.put(ZonedDateTime.now(), status));
}
@Override
public void addLineItem(TenantName tenant, String description, BigDecimal amount, String agent) {
unusedLineItems.computeIfAbsent(tenant, l -> new ArrayList<>())
.add(new Invoice.LineItem(
"line-item-id",
description,
amount,
"some-plan",
agent,
ZonedDateTime.now()
));
}
@Override
public void deleteLineItem(String lineItemId) {
unusedLineItems.values()
.forEach(lineItems -> lineItems.
removeIf(lineItem -> lineItem.id().equals(lineItemId))
);
}
@Override
public boolean setActivePaymentInstrument(InstrumentOwner paymentInstrument) {
var instrumentId = paymentInstrument.getPaymentInstrumentId();
activeInstruments.put(paymentInstrument.getTenantName(), createInstrument(instrumentId));
return true;
}
@Override
public InstrumentList listInstruments(TenantName tenant, String userId) {
return null;
}
@Override
public List<Invoice> getInvoices(TenantName tenant) {
return committedInvoices.getOrDefault(tenant, List.of());
}
@Override
public void deleteBillingInfo(TenantName tenant, Set<User> users, boolean isPrivileged) {}
private PaymentInstrument createInstrument(String id) {
return new PaymentInstrument(id,
"name",
"displayText",
"brand",
"type",
"endingWith",
"expiryDate",
"addressLine1",
"addressLine2",
"zip",
"city",
"state",
"country");
}
public void addInvoice(TenantName tenantName, Invoice invoice, boolean committed) {
if (committed)
committedInvoices.computeIfAbsent(tenantName, i -> new ArrayList<>())
.add(invoice);
else
uncommittedInvoices.put(tenantName, invoice);
}
private Invoice emptyInvoice() {
return new Invoice(Invoice.Id.of("empty"), Invoice.StatusHistory.open(), List.of(), ZonedDateTime.now(), ZonedDateTime.now());
}
}
|