/** * Creates a LocalizedString for the {@code Locale.ENGLISH}. * @param value the value of the english translation * @return localized string with value */ public static LocalizedString en(final String value) { return LocalizedString.of(Locale.ENGLISH, value); }
@Test public void ofEnglishLocale() { assertThat(LocalizedString.ofEnglish("dog food")) .isEqualTo(LocalizedString.of(Locale.ENGLISH, "dog food")); }
/** * Creates an instance without any value. * * @return instance without any value */ public static LocalizedString empty() { return of(); }
@Test public void mapValue() throws Exception { final LocalizedString upperCased = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food") .mapValue((locale, value) -> value.toUpperCase(locale)); assertThat(upperCased).isEqualTo(LocalizedString.of(Locale.GERMAN, "HUNDEFUTTER", Locale.ENGLISH, "DOG FOOD")); }
@Test public void toJsonNode() throws Exception { final LocalizedString value = LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"); final JsonNode actual = SphereJsonUtils.toJsonNode(value); assertThat(actual.get("de").asText()).isEqualTo("Hundefutter"); assertThat(actual.get("en").asText()).isEqualTo("dog food"); }
@Test public void localizedString() throws Exception { assertThat(variant.findAttribute(localizedStringNamedAttributeAccess).get()). isEqualTo(LocalizedString.of(GERMAN, "val-loc-string-de", ENGLISH, "val-loc-string-en")); }
@Test public void toJsonString() throws Exception { final LocalizedString value = LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"); assertThat(SphereJsonUtils.toJsonString(value)) .isEqualTo("{\"en\":\"dog food\",\"de\":\"Hundefutter\"}"); }
@Test public void getterWithAttributeAccess() throws Exception { assertThat(variant.findAttribute(LOC_STRING_ATTRIBUTE, ofLocalizedString()).get()). isEqualTo(LocalizedString.of(GERMAN, "val-loc-string-de", ENGLISH, "val-loc-string-en")); }
@Test public void getLocales() { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food"); final Set<Locale> locales = ls.getLocales(); assertThat(locales) .isEqualTo(new HashSet<>(asList(Locale.GERMAN, Locale.ENGLISH))); }
@Test public void stream() throws Exception { final LocalizedString upperCased = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food") .stream() .map(e -> LocalizedStringEntry.of(e.getLocale(), e.getValue().toUpperCase())) .collect(LocalizedString.streamCollector()); assertThat(upperCased).isEqualTo(LocalizedString.of(Locale.GERMAN, "HUNDEFUTTER", Locale.ENGLISH, "DOG FOOD")); }
@Test public void createFromTwoValues() throws Exception { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food"); assertThat(ls.getLocales()).containsExactly(Locale.GERMAN, Locale.ENGLISH); assertThat(ls.get(Locale.GERMAN)).isEqualTo("Hundefutter"); assertThat(ls.get(Locale.ENGLISH)).isEqualTo("dog food"); }
@Test public void execution() { final Project project = client().executeBlocking(ProjectGet.of()); final CartClassificationDraft cartClassification = CartClassificationDraftBuilder.of(Collections.singleton(LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")))).build(); final Project updatedProjectCartClassification = client().executeBlocking(ProjectUpdateCommand.of(project, SetShippingRateInputType.of(cartClassification))); assertThat(updatedProjectCartClassification.getShippingRateInputType()).isNotNull(); assertThat(updatedProjectCartClassification.getShippingRateInputType().getType()).isEqualTo("CartClassification"); assertThat(((CartClassification)updatedProjectCartClassification.getShippingRateInputType()).getValues()).containsOnly( LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")) ); } }
@Test public void nameLocale() { checkFilter(builder -> builder.name(LocalizedString.of(Locale.GERMAN, "foo")), m -> m.name().locale(Locale.GERMAN)); }
@Test public void localizedStringAttribute() throws Exception { testSingleAndSet(AttributeAccess.ofLocalizedString(), AttributeAccess.ofLocalizedStringSet(), asSet(LocalizedString.of(ENGLISH, "hello"), LocalizedString.of(ENGLISH, "world")), LocalizedStringAttributeType.of(), AttributeDefinitionBuilder.of("localized-text-attribute", LABEL, LocalizedStringAttributeType.of()).inputHint(TEXT_INPUT_HINT).build()); }
@Test public void setMetaDescription() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString metaDescription = LocalizedString .of(ENGLISH, "SPHERE.IO™ is the first Platform-as-a-Service solution for eCommerce."); final SetMetaDescription action = SetMetaDescription.of(metaDescription); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, action)); assertThat(updatedProduct.getMasterData().getStaged().getMetaDescription()).isEqualTo(metaDescription); return updatedProduct; }); }
@Test public void setMetaKeywords() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString metaKeywords = LocalizedString .of(ENGLISH, "Platform-as-a-Service, e-commerce, http, api, tool"); final SetMetaKeywords action = SetMetaKeywords.of(metaKeywords); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, action)); assertThat(updatedProduct.getMasterData().getStaged().getMetaKeywords()).isEqualTo(metaKeywords); return updatedProduct; }); }
@Test public void setMetaTitle() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString metaTitle = LocalizedString .of(ENGLISH, "commercetools SPHERE.IO™ - Next generation eCommerce"); final SetMetaTitle action = SetMetaTitle.of(metaTitle); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, action)); assertThat(updatedProduct.getMasterData().getStaged().getMetaTitle()).isEqualTo(metaTitle); return updatedProduct; }); }
@Test public void plusFieldDefinitionsTests() { final LocalizedString l = LocalizedString.of(Locale.GERMAN, "l"); final TypeDraftBuilder builder = TypeDraftBuilder.of("type-key", l, ResourceTypeIdsSetBuilder.of().addCategories()); builder.plusFieldDefinitions( FieldDefinition.of( ReferenceFieldType.of(Category.referenceTypeId()), "fieldName", l, false) ).build(); } }
private static Product createEvilTestProduct(final ProductType productType, final String germanName, final String evilValue, final String sku) { final LocalizedString name = LocalizedString.of(GERMAN, germanName); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .attributes(AttributeDraft.of(ATTR_NAME_EVIL, evilValue)) .sku(sku) .build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, name, name.slugifiedUnique(), masterVariant).build(); return client().executeBlocking(ProductCreateCommand.of(productDraft)); } }