aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandlerV2Test.java
blob: 46c1aa107f088986e72460910d73f11e3c502226 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.billing;

import com.yahoo.application.container.handler.Request;
import com.yahoo.config.provision.TenantName;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Bill;
import com.yahoo.vespa.hosted.controller.api.integration.billing.BillStatus;
import com.yahoo.vespa.hosted.controller.api.integration.billing.MockBillingController;
import com.yahoo.vespa.hosted.controller.api.integration.billing.StatusHistory;
import com.yahoo.vespa.hosted.controller.api.role.Role;
import com.yahoo.vespa.hosted.controller.restapi.ContainerTester;
import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerCloudTest;
import com.yahoo.vespa.hosted.controller.security.Auth0Credentials;
import com.yahoo.vespa.hosted.controller.security.CloudTenantSpec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author ogronnesby
 */
public class BillingApiHandlerV2Test extends ControllerContainerCloudTest {

    private static final String responseFiles = "src/test/java/com/yahoo/vespa/hosted/controller/restapi/billing/responses/";

    private static final TenantName tenant = TenantName.from("tenant1");
    private static final TenantName tenant2 = TenantName.from("tenant2");
    private static final Set<Role> tenantReader = Set.of(Role.reader(tenant));
    private static final Set<Role> tenantAdmin = Set.of(Role.administrator(tenant));
    private static final Set<Role> financeAdmin = Set.of(Role.hostedAccountant());

    private MockBillingController billingController;
    private ContainerTester tester;

    @BeforeEach
    public void before() {
        tester = new ContainerTester(container, responseFiles);
        tester.controller().tenants().create(new CloudTenantSpec(tenant, ""), new Auth0Credentials(() -> "foo", Set.of(Role.hostedOperator())));
        var clock = (ManualClock) tester.controller().serviceRegistry().clock();
        clock.setInstant(Instant.parse("2021-04-13T00:00:00Z"));
        billingController = (MockBillingController) tester.serviceRegistry().billingController();
        billingController.addBill(tenant, createBill(), true);
    }

    @Override
    protected String variablePartXml() {
        return "  <component id='com.yahoo.vespa.hosted.controller.security.CloudAccessControlRequests'/>\n" +
                "  <component id='com.yahoo.vespa.hosted.controller.security.CloudAccessControl'/>\n" +

                "  <handler id='com.yahoo.vespa.hosted.controller.restapi.billing.BillingApiHandlerV2'>\n" +
                "    <binding>http://*/billing/v2/*</binding>\n" +
                "  </handler>\n" +

                "  <http>\n" +
                "    <server id='default' port='8080' />\n" +
                "    <filtering>\n" +
                "      <request-chain id='default'>\n" +
                "        <filter id='com.yahoo.vespa.hosted.controller.restapi.filter.ControllerAuthorizationFilter'/>\n" +
                "        <binding>http://*/*</binding>\n" +
                "      </request-chain>\n" +
                "    </filtering>\n" +
                "  </http>\n";
    }

    @Test
    void require_tenant_info() {
        var request = request("/billing/v2/tenant/" + tenant.value()).roles(tenantReader);
        tester.assertResponse(request, "{\"tenant\":\"tenant1\",\"plan\":{\"id\":\"trial\",\"name\":\"Free Trial - for testing purposes\"},\"collection\":\"AUTO\"}");
    }

    @Test
    void require_accountant_for_update_collection() {
        var request = request("/billing/v2/tenant/" + tenant.value(), Request.Method.PATCH)
                .data("{\"collection\": \"INVOICE\"}");

        var forbidden = request.roles(tenantAdmin);
        tester.assertResponse(forbidden, """
                {
                  "code" : 403,
                  "message" : "Access denied"
                }""", 403);

        var success = request.roles(financeAdmin);
        tester.assertResponse(success, """
                {"tenant":"tenant1","plan":{"id":"trial","name":"Free Trial - for testing purposes"},"collection":"INVOICE"}""");
    }

    @Test
    void require_tenant_usage() {
        var request = request("/billing/v2/tenant/" + tenant + "/usage").roles(tenantReader);
        tester.assertResponse(request, "{\"from\":\"2021-04-13\",\"to\":\"2021-04-13\",\"total\":\"0.00\",\"items\":[]}");
    }

    @Test
    void require_tenant_invoice() {
        var listRequest = request("/billing/v2/tenant/" + tenant + "/bill").roles(tenantReader);
        tester.assertResponse(listRequest, "{\"invoices\":[{\"id\":\"id-1\",\"from\":\"2020-05-23\",\"to\":\"2020-05-28\",\"total\":\"123.00\",\"status\":\"OPEN\"}]}");

        var singleRequest = request("/billing/v2/tenant/" + tenant + "/bill/id-1").roles(tenantReader);
        tester.assertResponse(singleRequest, """
                {"id":"id-1","from":"2020-05-23","to":"2020-05-28","total":"123.00","status":"OPEN","statusHistory":[{"at":"2020-05-23T00:00:00Z","status":"OPEN"}],"items":[{"id":"some-id","description":"description","amount":"123.00","plan":{"id":"paid","name":"Paid Plan - for testing purposes"},"majorVersion":0,"cpu":{},"memory":{},"disk":{},"gpu":{}}]}""");
    }

    @Test
    void require_accountant_summary() {
        var tenantRequest = request("/billing/v2/accountant").roles(tenantReader);
        tester.assertResponse(tenantRequest, "{\n" +
                "  \"code\" : 403,\n" +
                "  \"message\" : \"Access denied\"\n" +
                "}", 403);

        var accountantRequest = request("/billing/v2/accountant").roles(Role.hostedAccountant());
        tester.assertResponse(accountantRequest, """
                {"tenants":[{"tenant":"tenant1","plan":{"id":"trial","name":"Free Trial - for testing purposes"},"quota":{"budget":-1.0},"collection":"AUTO","lastBill":"1970-01-01","unbilled":"0.00"}]}""");
    }

    @Test
    void require_accountant_preview() {
        var accountantRequest = request("/billing/v2/accountant/preview").roles(Role.hostedAccountant());
        billingController.uncommittedBills.put(tenant, createBill());

        tester.assertResponse(accountantRequest, """
                        {"tenants":[{"tenant":"tenant1","plan":{"id":"trial","name":"Free Trial - for testing purposes"},"quota":{"budget":-1.0},"collection":"AUTO","lastBill":"2020-05-23","unbilled":"123.00"}]}""");
    }

    @Test
    void require_accountant_tenant_preview() {
        var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/preview").roles(Role.hostedAccountant());
        tester.assertResponse(accountantRequest, "{\"id\":\"empty\",\"from\":\"2021-04-13\",\"to\":\"2021-04-12\",\"total\":\"0.00\",\"status\":\"OPEN\",\"statusHistory\":[{\"at\":\"2021-04-13T00:00:00Z\",\"status\":\"OPEN\"}],\"items\":[]}");
    }

    @Test
    void require_accountant_tenant_bill() {
        var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/preview", Request.Method.POST)
                .roles(Role.hostedAccountant())
                .data("{\"from\": \"2020-05-01\",\"to\": \"2020-06-01\"}");
        tester.assertResponse(accountantRequest, "{\"message\":\"Created bill id-123\"}");
    }

    @Test
    void require_list_of_all_plans() {
        var accountantRequest = request("/billing/v2/accountant/plans")
                .roles(Role.hostedAccountant());
        tester.assertResponse(accountantRequest, "{\"plans\":[{\"id\":\"trial\",\"name\":\"Free Trial - for testing purposes\"},{\"id\":\"paid\",\"name\":\"Paid Plan - for testing purposes\"},{\"id\":\"none\",\"name\":\"None Plan - for testing purposes\"}]}");
    }

   @Test
    void require_additional_items_empty() {
        var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/items")
                .roles(Role.hostedAccountant());
        tester.assertResponse(accountantRequest, """
                {"items":[]}""");
   }

   @Test
    void require_additional_items_with_content() {
       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/items", Request.Method.POST)
                   .roles(Role.hostedAccountant())
                   .data("""
                        {
                            "description": "Additional support costs",
                            "amount": "123.45"
                        }""");
           tester.assertResponse(accountantRequest, """
                {"message":"Added line item for tenant tenant1"}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/items")
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"items":[{"id":"line-item-id","description":"Additional support costs","amount":"123.45","plan":{"id":"paid","name":"Paid Plan - for testing purposes"},"majorVersion":0,"cpu":{},"memory":{},"disk":{},"gpu":{}}]}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/item/line-item-id", Request.Method.DELETE)
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"message":"Successfully deleted line item line-item-id"}""");
       }
   }

   @Test
    void require_current_plan() {
       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/plan")
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"id":"trial","name":"Free Trial - for testing purposes"}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/plan", Request.Method.POST)
                   .roles(Role.hostedAccountant())
                   .data("""
                           {"id": "paid"}""");
           tester.assertResponse(accountantRequest, """
                   {"message":"Plan: paid"}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/plan")
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"id":"paid","name":"Paid Plan - for testing purposes"}""");
       }
   }

   @Test
    void require_current_collection() {
       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/collection")
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"collection":"AUTO"}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/collection", Request.Method.POST)
                   .roles(Role.hostedAccountant())
                   .data("""
                           {"collection": "INVOICE"}""");
           tester.assertResponse(accountantRequest, """
                   {"message":"Collection: INVOICE"}""");
       }

       {
           var accountantRequest = request("/billing/v2/accountant/tenant/tenant1/collection")
                   .roles(Role.hostedAccountant());
           tester.assertResponse(accountantRequest, """
                   {"collection":"INVOICE"}""");
       }
   }

   @Test
    void require_accountant_tenant() {
        var accountantRequest = request("/billing/v2/accountant/tenant/tenant1")
                .roles(Role.hostedAccountant());
        tester.assertResponse(accountantRequest, """
                {"tenant":"tenant1","plan":{"id":"trial","name":"Free Trial - for testing purposes","billed":false,"supported":false},"billing":{},"collection":"AUTO"}""");
   }

    private static Bill createBill() {
        var start = LocalDate.of(2020, 5, 23).atStartOfDay(ZoneOffset.UTC);
        var end = start.toLocalDate().plusDays(6).atStartOfDay(ZoneOffset.UTC);
        var statusHistory = new StatusHistory(new TreeMap<>(Map.of(start, BillStatus.OPEN)));
        return new Bill(
                Bill.Id.of("id-1"),
                TenantName.defaultName(),
                statusHistory,
                List.of(createLineItem(start)),
                start,
                end
        );
    }

    static Bill.LineItem createLineItem(ZonedDateTime addedAt) {
        return new Bill.LineItem(
                "some-id",
                "description",
                new BigDecimal("123.00"),
                "paid",
                "Smith",
                addedAt
        );
    }
}