CommerceUtils { public static ArrayNode getItemVariantOptions(JsonNode item) { JsonNode structuredContent = item.path("structuredContent"); JsonNode variants = structuredContent.path("variants"); if (variants.size() <= 1) { return EMPTY_ARRAY; } ArrayNode userDefinedOptions = JsonUtils.createArrayNode(); JsonNode ordering = structuredContent.path("variantOptionOrdering"); for (int i = 0; i < ordering.size(); i++) { String optionName = ordering.path(i).asText(); ObjectNode option = JsonUtils.createObjectNode(); option.put("name", optionName); option.putArray("values"); userDefinedOptions.add(option); } for (int i = 0; i < variants.size(); i++) { JsonNode variant = variants.path(i); JsonNode attributes = variant.get("attributes"); if (attributes == null) { continue; } Iterator<String> fields = attributes.fieldNames(); while (fields.hasNext()) { String field = fields.next(); String variantOptionValue = attributes.get(field).asText(); ObjectNode userDefinedOption = null; for (int j = 0; j < userDefinedOptions.size(); j++) { ObjectNode current = (ObjectNode)userDefinedOptions.get(j); if (current.get("name").asText().equals(field)) { userDefinedOption = current; } } if (userDefinedOption != null) { boolean hasOptionValue = false; ArrayNode optionValues = (ArrayNode)userDefinedOption.get("values"); for (int k = 0; k < optionValues.size(); k++) { String optionValue = optionValues.get(k).asText(); if (optionValue.equals(variantOptionValue)) { hasOptionValue = true; break; } } if (!hasOptionValue) { optionValues.add(variantOptionValue); } } } } return userDefinedOptions; } 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 testGetItemVariantOptions() { Map<String, JsonNode> jsonMap = loadJson(getClass(), "get-item-variant-options.json"); for (String testKey : extractTests(jsonMap)) { JsonNode item = jsonMap.get(testKey); JsonNode actual = CommerceUtils.getItemVariantOptions(item); JsonNode expected = jsonMap.get(testKey + "-expected"); JsonAssert.assertJsonEquals(actual.toString(), expected.toString()); } }