CommerceUtils { public static double getTotalStockRemaining(JsonNode item) { ProductType type = getProductType(item); JsonNode structuredContent = item.path("structuredContent"); if (EnumSet.of(ProductType.DIGITAL, ProductType.GIFT_CARD).contains(type)) { return Double.POSITIVE_INFINITY; } else { int total = 0; JsonNode variants = structuredContent.path("variants"); for (int i = 0; i < variants.size(); i++) { JsonNode variant = variants.get(i); if (isTruthy(variant.path("unlimited"))) { return Double.POSITIVE_INFINITY; } else { total += variant.path("qtyInStock").asLong(); } } return total; } } private  CommerceUtils(); static boolean useCLDRMode(Context ctx); static ProductType getProductType(JsonNode item); static boolean isSubscribable(JsonNode item); static JsonNode getSubscriptionPlanBillingPeriodNode(JsonNode item); static String getUnitFromSubscriptionPlanBillingPeriod(JsonNode billingPeriodNode); static int getValueFromSubscriptionPlanBillingPeriod(JsonNode billingPeriodNode); static int getNumBillingCyclesFromSubscriptionPlanNode(JsonNode item); static JsonNode getLowestPriceAmongVariants(JsonNode item); static JsonNode getHighestPriceAmongVariants(JsonNode item); static JsonNode getSalePriceMoneyNode(JsonNode item); static Decimal getAmountFromMoneyNode(JsonNode moneyNode); static String getCurrencyFromMoneyNode(JsonNode moneyNode); static Decimal getLegacyPriceFromMoneyNode(JsonNode moneyNode); static double getTotalStockRemaining(JsonNode item); static boolean hasVariedPrices(JsonNode item); static boolean hasVariants(JsonNode item); static boolean isOnSale(JsonNode item); static boolean isSoldOut(JsonNode item); static void writeLegacyMoneyString(Decimal value, StringBuilder buf); static void writeVariantFormat(JsonNode variant, StringBuilder buf); static ArrayNode getItemVariantOptions(JsonNode item); static boolean isMultipleQuantityAllowedForServices(JsonNode websiteSettings);  }
@Test public void testGetTotalStockRemaining() { Map<String, JsonNode> jsonMap = loadJson(getClass(), "get-total-stock-remaining.json"); JsonNode item = jsonMap.get("getTotalStock-unlimited-physical"); assertEquals(getTotalStockRemaining(item), Double.POSITIVE_INFINITY); item = jsonMap.get("getTotalStock-digital"); assertEquals(getTotalStockRemaining(item), Double.POSITIVE_INFINITY); item = jsonMap.get("getTotalStock-six-service"); assertEquals(getTotalStockRemaining(item), 6.0); item = jsonMap.get("getTotalStock-0-physical"); assertEquals(getTotalStockRemaining(item), 0.0); item = jsonMap.get("getTotalStock-0-physical-2"); assertEquals(getTotalStockRemaining(item), 0.0); item = jsonMap.get("getTotalStock-unknown"); assertEquals(getTotalStockRemaining(item), 0.0); }