aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/BillingReportMaintainerTest.java
blob: 5cb46664a75f2a85bb97f72639e80908328bdaaa (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
// 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.maintenance;

import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.api.integration.billing.BillStatus;
import com.yahoo.vespa.hosted.controller.api.integration.billing.InvoiceUpdate;
import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanRegistryMock;
import com.yahoo.vespa.hosted.controller.tenant.BillingReference;
import com.yahoo.vespa.hosted.controller.tenant.CloudTenant;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BillingReportMaintainerTest {
    private final ControllerTester tester = new ControllerTester(SystemName.PublicCd);
    private final BillingReportMaintainer maintainer = new BillingReportMaintainer(tester.controller(), Duration.ofMinutes(10));

    @Test
    void only_billable_tenants_are_maintained() {
        var t1 = tester.createTenant("t1");
        var t2  = tester.createTenant("t2");

        tester.controller().serviceRegistry().billingController().setPlan(t1, PlanRegistryMock.paidPlan.id(), false, true);
        maintainer.maintain();

        var b1 = billingReference(t1);
        var b2 = billingReference(t2);

        assertFalse(b1.isEmpty());
        assertTrue(b2.isEmpty());

        assertEquals(tester.clock().instant(), b1.orElseThrow().updated());
        assertNotNull(b1.orElseThrow().reference());
    }

    @Test
    void only_open_bills_with_exported_id_are_maintained() {
        var t1 = tester.createTenant("t1");
        var billingController = tester.controller().serviceRegistry().billingController();
        var billingDb = tester.controller().serviceRegistry().billingDatabase();

        var start = LocalDate.of(2020, 5, 23).atStartOfDay(ZoneOffset.UTC);
        var end = start.toLocalDate().plusDays(6).atStartOfDay(ZoneOffset.UTC);

        var bill1 = billingDb.createBill(t1, start, end, "non-exported");
        var bill2 = billingDb.createBill(t1, start, end, "exported");
        var bill3 = billingDb.createBill(t1, start, end, "exported-and-frozen");
        billingDb.setStatus(bill3, "foo", BillStatus.FROZEN);

        billingController.setPlan(t1, PlanRegistryMock.paidPlan.id(), false, true);

        tester.controller().serviceRegistry().billingReporter().exportBill(billingDb.readBill(bill2).get(), "FOO", cloudTenant(t1));
        tester.controller().serviceRegistry().billingReporter().exportBill(billingDb.readBill(bill3).get(), "FOO", cloudTenant(t1));
        var updates = maintainer.maintainInvoices();
        assertEquals(new InvoiceUpdate(1, 0, 0), updates);

        assertTrue(billingDb.readBill(bill1).get().getExportedId().isEmpty());

        var exportedBill = billingDb.readBill(bill2).get();
        assertEquals("EXT-ID-123", exportedBill.getExportedId().get());
        var lineItems = exportedBill.lineItems();
        assertEquals(1, lineItems.size());
        assertEquals("maintained", lineItems.get(0).id());

        var frozenBill = billingDb.readBill(bill3).get();
        assertEquals("EXT-ID-123", frozenBill.getExportedId().get());
        assertEquals(0, frozenBill.lineItems().size());

    }

    private CloudTenant cloudTenant(TenantName tenantName) {
        return tester.controller().tenants().require(tenantName, CloudTenant.class);
    }

    private Optional<BillingReference> billingReference(TenantName tenantName) {
        return cloudTenant(tenantName).billingReference();
    }

}