@Test public void slugifyRespectsAllowedCharacters() { final String actual = LocalizedString.ofEnglish("Pick + Mix 18 Volt").slugified().get(Locale.ENGLISH); final String expected = "pick-mix-18-volt"; assertThat(actual).isEqualTo(expected); }
@Test public void changeAssetName_ByAssetKeyToJsonString_ShouldSerialiseAssetKeyField() { final UpdateAction<Product> action = ChangeAssetName.ofAssetKeyAndVariantId(1, "foo", ofEnglish("bar"), true); final String actionAsJson = SphereJsonUtils.toJsonString(action); assertThat(actionAsJson).contains("\"assetKey\":\"foo\""); } }
@Test public void slugifyRespectsAllowedLength() { final int allowedLength = 256; final String tooLongString = StringUtils.repeat("a", allowedLength - 1) + "bc"; assertThat(tooLongString).hasSize(allowedLength + 1); final String actual = LocalizedString.ofEnglish(tooLongString).slugified().get(Locale.ENGLISH); assertThat(actual).hasSize(allowedLength).endsWith("ab"); }
@Test public void slugifyUniqueRespectsAllowedLength() { final int allowedLength = 256; final String tooLongString = StringUtils.repeat("a", allowedLength + 1); assertThat(tooLongString).hasSize(allowedLength + 1); final String actual = LocalizedString.ofEnglish(tooLongString).slugifiedUnique().get(Locale.ENGLISH); assertThat(actual).hasSize(allowedLength).matches("a{247}-\\d{8}"); }
public void changeNameWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newName = LocalizedString.ofEnglish("newName " + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(newName, staged))); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(newName); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void changeSlugWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newSlug = LocalizedString.ofEnglish("new-slug-" + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeSlug.of(newSlug, staged))); assertThat(updatedProduct.getMasterData().getStaged().getSlug()).isEqualTo(newSlug); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void setDescriptionWithStaged(final Boolean staged) { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newDescription = LocalizedString.ofEnglish("new description " + RANDOM.nextInt()); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetDescription.of(newDescription, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getDescription()).isEqualTo(newDescription); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@Test public void setReference() throws Exception { final AttributeAccess<Set<Reference<Product>>> access = AttributeAccess.ofProductReferenceSet(); final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of("productreference", LocalizedString.ofEnglish("foo"), SetAttributeType.of(ReferenceAttributeType.ofProduct())).build(); assertThat(access.canHandle(attributeDefinition)).isTrue(); } }
@Test public void setAssetDescription() { withCategoryHavingAssets(client(), category -> { final LocalizedString newDescription = LocalizedString.ofEnglish("new description"); final String assetId = category.getAssets().get(0).getId(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetDescription.of(assetId, newDescription))); final Asset updatedAsset = updatedCategory.getAssets().get(0); assertThat(updatedAsset.getDescription()).isEqualTo(newDescription); }); }
@Test public void changeAssetNameById() { withCategoryHavingAssets(client(), category -> { final LocalizedString newName = LocalizedString.ofEnglish("new name"); final String assetId = category.getAssets().get(0).getId(); final CategoryUpdateCommand command = CategoryUpdateCommand.of(category, ChangeAssetName.of(assetId, newName)); final Category updatedCategory = client().executeBlocking(command); final Asset updatedAsset = updatedCategory.getAssets().get(0); assertThat(updatedAsset.getName()).isEqualTo(newName); }); }
@Test public void changeName() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString newName = LocalizedString.ofEnglish("newName " + RANDOM.nextInt()); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(newName))); assertThat(updatedProduct.getMasterData().getStaged().getName()).isEqualTo(newName); return updatedProduct; }); }
private static ProductType createProductType() { final AttributeDefinition colorAttrDef = AttributeDefinitionBuilder.of(ATTR_NAME_COLOR, LocalizedString.ofEnglish(ATTR_NAME_COLOR), StringAttributeType.of()).build(); final AttributeDefinition sizeAttrDef = AttributeDefinitionBuilder.of(ATTR_NAME_SIZE, LocalizedString.ofEnglish(ATTR_NAME_SIZE), NumberAttributeType.of()).build(); final ProductTypeDraft productTypeDraft = ProductTypeDraft.of(randomKey(), PRODUCT_TYPE_NAME, "", asList(colorAttrDef, sizeAttrDef)); final ProductTypeCreateCommand productTypeCreateCommand = ProductTypeCreateCommand.of(productTypeDraft); return client().executeBlocking(productTypeCreateCommand); }
@Test public void setDescription() throws Exception { withUpdateableProduct(client(), product -> { final LocalizedString newDescription = LocalizedString.ofEnglish("new description " + RANDOM.nextInt()); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetDescription.of(newDescription)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getDescription()).isEqualTo(newDescription); return updatedProduct; }); }
private static ProductType createEvilProductType() { final AttributeDefinition evilAttrDef = AttributeDefinitionBuilder.of(ATTR_NAME_EVIL, LocalizedString.ofEnglish(ATTR_NAME_EVIL), StringAttributeType.of()).build(); final ProductTypeDraft evilProductTypeDraft = ProductTypeDraft.of(randomKey(), EVIL_PRODUCT_TYPE_NAME, "", singletonList(evilAttrDef)); final ProductTypeCreateCommand evilProductTypeCreateCommand = ProductTypeCreateCommand.of(evilProductTypeDraft); return client().executeBlocking(evilProductTypeCreateCommand); }
@Test public void concurrentModification() throws Exception { withCategory(client(), cat -> { final CategoryUpdateCommand cmd = CategoryUpdateCommand.of(cat, ChangeName.of(LocalizedString.ofEnglish("new"))); final Category successfulUpdatedCategory = client().executeBlocking(cmd); final Throwable throwable = catchThrowable(() -> client().executeBlocking(cmd));//same command twice assertThat(throwable).isInstanceOf(ConcurrentModificationException.class); final ConcurrentModificationException exception = (ConcurrentModificationException) throwable; assertThat(exception.getCurrentVersion()) .isGreaterThanOrEqualTo(successfulUpdatedCategory.getVersion()); }); }
public static Product createTestProduct(final BlockingSphereClient client, final String name, final ProductVariantDraft masterVariant, final Referenceable<ProductType> productTypeReferenceable) { final LocalizedString locName = LocalizedString.ofEnglish(name); final ProductDraft productDraft = ProductDraftBuilder.of(productTypeReferenceable.toReference(), locName, locName.slugifiedUnique(), masterVariant).build(); return client.executeBlocking(ProductCreateCommand.of(productDraft)); } }
private static AssetDraft getAssetDraft2() { final AssetSource assetSource1 = AssetSourceBuilder.ofUri("http://dev.commercetools.com/assets/img/CT-logo.svg") .key(randomKey()) .contentType("image/svg+xml") .build(); final LocalizedString name = LocalizedString.ofEnglish("commercetools logo"); return AssetDraftBuilder.of(singletonList(assetSource1), name) .key("asset2Key") .tags("desktop-sized", "svg-format", "commercetools", "awesome") .build(); }
private static ShoppingList createShoppingList(final BlockingSphereClient client, final String name) { final ShoppingListDraft draft = newShoppingListDraftBuilder() .name(LocalizedString.ofEnglish(name)) .key(randomKey()) .build(); return client.executeBlocking(ShoppingListCreateCommand.of(draft)); } }
private static AssetDraft getAssetDraft2() { final AssetSource assetSource1 = AssetSourceBuilder.ofUri("http://dev.commercetools.com/assets/img/CT-logo.svg") .key(randomKey()) .contentType("image/svg+xml") .build(); final LocalizedString name = LocalizedString.ofEnglish("commercetools logo"); return AssetDraftBuilder.of(singletonList(assetSource1), name) .tags("desktop-sized", "svg-format", "commercetools", "awesome") .key(randomKey()) .build(); } }
private static CartDiscount getCartDiscount(final BlockingSphereClient client, final String name) { final Query<CartDiscount> query = CartDiscountQuery.of().withPredicates(m -> m.name().lang(ENGLISH).is(name)); return client.executeBlocking(query).head().orElseGet(() -> { final CartDiscountDraft draft = newCartDiscountDraftBuilder() .requiresDiscountCode(true) .name(LocalizedString.ofEnglish(name)) .build(); return client.executeBlocking(CartDiscountCreateCommand.of(draft)); }); }