aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server
diff options
context:
space:
mode:
authorValerij Fredriksen <valerijf@yahooinc.com>2023-10-11 10:54:16 +0200
committerValerij Fredriksen <valerijf@yahooinc.com>2023-10-11 10:54:16 +0200
commit9909280794849eb8a98dbb18f8f756ae7fff781f (patch)
tree4129c5ddf0266cf95a391873f0428cec957bb13e /controller-server
parentabac62c851c27907c7a77c6ca5c7566fdbe64b75 (diff)
Return amounts with 2 decimals
Diffstat (limited to 'controller-server')
-rw-r--r--controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java15
-rw-r--r--controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java8
2 files changed, 13 insertions, 10 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java
index b6b3c8584fd..955941b11cc 100644
--- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java
+++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java
@@ -22,13 +22,13 @@ import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses;
import com.yahoo.yolean.Exceptions;
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
-import java.util.stream.Collectors;
import static com.yahoo.jdisc.http.HttpRequest.Method.GET;
import static com.yahoo.restapi.ErrorResponse.methodNotAllowed;
@@ -46,7 +46,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
public class PricingApiHandler extends ThreadedHttpRequestHandler {
private static final Logger log = Logger.getLogger(PricingApiHandler.class.getName());
- private static final BigDecimal SCALED_ZERO = BigDecimal.ZERO.setScale(2);
private final Controller controller;
@@ -104,7 +103,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler {
case "resources" -> clusterResources.add(clusterResources(entry.getSecond()));
}
}
- if (clusterResources.size() < 1) throw new IllegalArgumentException("No cluster resources found in query");
+ if (clusterResources.isEmpty()) throw new IllegalArgumentException("No cluster resources found in query");
PricingInfo pricingInfo = new PricingInfo(enclave, supportLevel, committedSpend);
return controller.serviceRegistry().pricingController().price(clusterResources, pricingInfo, plan);
@@ -143,7 +142,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler {
if (index <= 0 ) throw new IllegalArgumentException("Error in query parameter, expected '=' between key and value: " + element);
return new Pair<>(element.substring(0, index), element.substring(index + 1));
})
- .collect(Collectors.toList());
+ .toList();
}
private Optional<Plan> plan(String element) {
@@ -160,7 +159,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler {
addItem(array, "Volume discount", priceInfo.volumeDiscount());
addItem(array, "Committed spend", priceInfo.committedAmountDiscount());
- cursor.setString("totalAmount", priceInfo.totalAmount().toPlainString());
+ setBigDecimal(cursor, "totalAmount", priceInfo.totalAmount());
return new SlimeJsonResponse(slime);
}
@@ -169,8 +168,12 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler {
if (amount.compareTo(BigDecimal.ZERO) != 0) {
var o = array.addObject();
o.setString("description", name);
- o.setString("amount", SCALED_ZERO.add(amount).toPlainString());
+ setBigDecimal(o, "amount", amount);
}
}
+ private static void setBigDecimal(Cursor cursor, String name, BigDecimal value) {
+ cursor.setString(name, value.setScale(2, RoundingMode.HALF_UP).toPlainString());
+ }
+
}
diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java
index 09ff6bbc4b1..21f04acf941 100644
--- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java
+++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java
@@ -28,9 +28,9 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest {
{
"priceInfo": [
{"description": "List price", "amount": "2400.00"},
- {"description": "Volume discount", "amount": "-5.00"}
+ {"description": "Volume discount", "amount": "-5.64"}
],
- "totalAmount": "2395.00"
+ "totalAmount": "2394.36"
}
""",
200);
@@ -41,7 +41,7 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest {
ContainerTester tester = new ContainerTester(container, responseFiles);
assertEquals(SystemName.Public, tester.controller().system());
- var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformationWithMissingValueInResourcs());
+ var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformationWithMissingValueInResources());
tester.assertJsonResponse(request,
"{\"error-code\":\"BAD_REQUEST\",\"message\":\"Error in query parameter, expected '=' between key and value: resources\"}",
400);
@@ -58,7 +58,7 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest {
"&resources=" + resources;
}
- String urlEncodedPriceInformationWithMissingValueInResourcs() {
+ String urlEncodedPriceInformationWithMissingValueInResources() {
return URLEncoder.encode("supportLevel=basic&committedSpend=0&enclave=false&resources", UTF_8);
}