summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java
blob: c4b5a771725d03b180ddca46a58d161a8c49bd09 (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
// 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.pricing;

import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo;
import com.yahoo.vespa.hosted.controller.restapi.ContainerTester;
import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerCloudTest;
import org.junit.jupiter.api.Test;

import java.net.URLEncoder;

import static com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo.SupportLevel.BASIC;
import static com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo.SupportLevel.COMMERCIAL;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author hmusum
 */
public class PricingApiHandlerTest extends ControllerContainerCloudTest {

    @Test
    void testPricingInfoBasic() {
        var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformation1App(BASIC));
        tester().assertJsonResponse(request, """
                                            {
                                              "applications": [
                                                {
                                                  "priceInfo": [
                                                    {"description": "Basic support unit price", "amount": "4.30"},
                                                    {"description": "Volume discount", "amount": "-0.10"}
                                                  ]
                                                }
                                              ],
                                              "priceInfo": [
                                                {"description": "Committed spend", "amount": "-0.20"}
                                              ],
                                              "totalAmount": "4.00"
                                            }
                                            """,
                                    200);
    }

    @Test
    void testPricingInfoBasicEnclave() {
        var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformation1AppEnclave(BASIC));
        tester().assertJsonResponse(request, """
                                            {
                                              "applications": [
                                                {
                                                  "priceInfo": [
                                                    {"description": "Basic support unit price", "amount": "4.30"},
                                                    {"description": "Enclave", "amount": "-0.15"},
                                                    {"description": "Volume discount", "amount": "-0.10"}
                                                  ]
                                                }
                                              ],
                                              "priceInfo": [
                                                {"description": "Enclave (minimum $10k per month)", "amount": "10.15"},
                                                {"description": "Committed spend", "amount": "-0.20"}
                                              ],
                                              "totalAmount": "3.85"
                                            }
                                            """,
                                    200);
    }

    @Test
    void testPricingInfoCommercialEnclave() {
        var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformation1AppEnclave(COMMERCIAL));
        tester().assertJsonResponse(request, """
                                            {
                                              "applications": [
                                                {
                                                  "priceInfo": [
                                                    {"description": "Commercial support unit price", "amount": "13.30"},
                                                    {"description": "Enclave", "amount": "-0.15"},
                                                    {"description": "Volume discount", "amount": "-0.10"}
                                                  ]
                                                }
                                              ],
                                              "priceInfo": [
                                                {"description": "Enclave (minimum $10k per month)", "amount": "1.15"},
                                                {"description": "Committed spend", "amount": "-0.20"}
                                              ],
                                              "totalAmount": "12.85"
                                            }
                                            """,
                                    200);
    }

    @Test
    void testPricingInfoCommercialEnclave2Apps() {
        var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformation2AppsEnclave(COMMERCIAL));
        tester().assertJsonResponse(request, """
                                            {
                                              "applications": [
                                                {
                                                  "priceInfo": [
                                                    {"description": "Commercial support unit price", "amount": "13.30"},
                                                    {"description": "Enclave", "amount": "-0.15"},
                                                    {"description": "Volume discount", "amount": "-0.10"}
                                                  ]
                                                },
                                                {
                                                  "priceInfo": [
                                                    {"description": "Commercial support unit price", "amount": "13.30"},
                                                    {"description": "Enclave", "amount": "-0.15"},
                                                    {"description": "Volume discount", "amount": "-0.10"}
                                                  ]
                                                }
                                              ],
                                              "priceInfo": [
                                                {"description": "Committed spend", "amount": "-0.20"}
                                              ],
                                              "totalAmount": "25.90"
                                            }
                                            """,
                                    200);
    }

    @Test
    void testInvalidRequests() {
        ContainerTester tester = tester();
        tester.assertJsonResponse(request("/pricing/v1/pricing"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"No price information found in query\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Error in query parameter, expected '=' between key and value: ''\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?supportLevel=basic&committedSpend=0"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"No application resources found in query\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?supportLevel=basic&committedSpend=0&resources"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Error in query parameter, expected '=' between key and value: 'resources'\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?supportLevel=basic&committedSpend=0&resources="),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Error in query parameter, expected '=' between key and value: 'resources='\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?supportLevel=basic&committedSpend=0&key=value"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Unknown query parameter 'key'\"}",
                                  400);
        tester.assertJsonResponse(request("/pricing/v1/pricing?supportLevel=basic&committedSpend=0&application=key%3Dvalue"),
                                  "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Unknown key 'key'\"}",
                                  400);
    }

    private ContainerTester tester() {
        ContainerTester tester = new ContainerTester(container, null);
        assertEquals(SystemName.Public, tester.controller().system());
        return tester;
    }

    /**
     * 1 app, with 2 clusters (with total resources for all clusters with each having
     * 1 node, with 4 vcpu, 8 Gb memory, 100 Gb disk and no GPU,
     * price will be 20000 + 2000 + 200
     */
    String urlEncodedPriceInformation1App(PricingInfo.SupportLevel supportLevel) {
        return "application=" + URLEncoder.encode("vcpu=4,memoryGb=8,diskGb=100,gpuMemoryGb=0", UTF_8) +
                "&supportLevel=" + supportLevel.name().toLowerCase() + "&committedSpend=20";
    }

    /**
     * 1 app, with 2 clusters (with total resources for all clusters with each having
     * 1 node, with 4 vcpu, 8 Gb memory, 100 Gb disk and no GPU,
     * price will be 20000 + 2000 + 200
     */
    String urlEncodedPriceInformation1AppEnclave(PricingInfo.SupportLevel supportLevel) {
        return "application=" + URLEncoder.encode("enclaveVcpu=4,enclaveMemoryGb=8,enclaveDiskGb=100,enclaveGpuMemoryGb=0", UTF_8) +
                "&supportLevel=" + supportLevel.name().toLowerCase() + "&committedSpend=20";
    }

    /**
     * 2 apps, with 1 cluster (with total resources for all clusters with each having
     * 1 node, with 4 vcpu, 8 Gb memory, 100 Gb disk and no GPU,
     */
    String urlEncodedPriceInformation2AppsEnclave(PricingInfo.SupportLevel supportLevel) {
        return "application=" + URLEncoder.encode("enclaveVcpu=4,enclaveMemoryGb=8,enclaveDiskGb=100,enclaveGpuMemoryGb=0", UTF_8) +
                "&application=" + URLEncoder.encode("enclaveVcpu=4,enclaveMemoryGb=8,enclaveDiskGb=100,enclaveGpuMemoryGb=0", UTF_8) +
                "&supportLevel=" + supportLevel.name().toLowerCase() + "&committedSpend=0";
    }

}