/** * Creates a new object initialized with the fields of the template parameter. * * @param template the template * @return a new object initialized from the template */ public static AssetDraftBuilder of(final Asset template) { return new AssetDraftBuilder(copyCustom(template.getCustom()), template.getDescription(), template.getKey(), template.getName(), template.getSources(), template.getTags()); } }
@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); }); }
public ResourceTypeIdsSetBuilder addAssets() { return add(Asset.resourceTypeId()); }
@Test public void setAssetTags() { withCategoryHavingAssets(client(), category -> { final Set<String> newTags = new HashSet<>(asList("tag1", "tag2")); final String assetId = category.getAssets().get(0).getId(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetTags.of(assetId, newTags))); final Asset updatedAsset = updatedCategory.getAssets().get(0); assertThat(updatedAsset.getTags()).isEqualTo(newTags); }); } }
@Test public void addAsset() { withCategory(client(), category -> { final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg") .key("rewe-showcase") .contentType("image/jpg") .dimensionsOfWidthAndHeight(1934, 1115) .build(); final LocalizedString name = LocalizedString.ofEnglish("REWE show case"); final LocalizedString description = LocalizedString.ofEnglish("screenshot of the REWE webshop on a mobile and a notebook"); final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name) .description(description) .tags("desktop-sized", "jpg-format", "REWE", "awesome") .build(); final CategoryUpdateCommand command = CategoryUpdateCommand.of(category, AddAsset.of(assetDraft,0)); final Category updatedCategory = client().executeBlocking(command); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).hasSize(1); final Asset asset = assets.get(0); assertThat(asset.getId()).isNotEmpty(); assertThat(asset.getDescription()).isEqualTo(description); assertThat(asset.getName()).isEqualTo(name); assertThat(asset.getSources()).hasSize(1); final AssetSource source = asset.getSources().get(0); assertThat(source.getUri()).isEqualTo("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg"); assertThat(source.getKey()).isEqualTo("rewe-showcase"); assertThat(source.getContentType()).isEqualTo("image/jpg"); assertThat(source.getDimensions()).isEqualTo(AssetDimensions.ofWidthAndHeight(1934, 1115)); }); }
public void setAssetCustomTypeByVariantIdAndAssetKeyWithStaged(@Nonnull final Boolean staged) { withUpdateableType(client(), (Type type) -> { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final Asset assetWithoutCustomType = masterVariant.getAssets().get(0); final String assetKey = assetWithoutCustomType.getKey(); final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofType(type).build(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetAssetCustomType.ofVariantIdAndAssetKey(masterVariant.getId(), assetKey, customFieldsDraft, staged)); final Product updatedProductWithCustomTypeInAssets = client().executeBlocking(cmd); final Asset updatedAsset = updatedProductWithCustomTypeInAssets.getMasterData().getStaged() .getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getCustom()).isNotNull(); assertThat(updatedAsset.getCustom().getType().getId()).isEqualTo(type.getId()); assertThat(updatedProductWithCustomTypeInAssets.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProductWithCustomTypeInAssets; }); return type; }); }
@Test public void changeAssetNameByKey() { withCategoryHavingAssets(client(), category -> { final LocalizedString newName = LocalizedString.ofEnglish("new name"); final String assetKey = category.getAssets().get(0).getKey(); final CategoryUpdateCommand command = CategoryUpdateCommand.of(category, ChangeAssetName.ofKey(assetKey, newName)); final Category updatedCategory = client().executeBlocking(command); final Asset updatedAsset = updatedCategory.getAssets().get(0); assertThat(updatedAsset.getName()).isEqualTo(newName); }); }
@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 assetCustomType() { withUpdateableType(client(), (Type type) -> { withCategoryHavingAssets(client(), category -> { final Asset assetWithoutCustomType = category.getAssets().get(0); final String assetId = assetWithoutCustomType.getId(); final String firstFieldValue = "commercetools"; final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofType(type) .addObject(STRING_FIELD_NAME, firstFieldValue) .build(); final Category updatedCategoryWithCustomTypeInAssets = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetCustomType.of(assetId, customFieldsDraft))); final String actualFieldValue = updatedCategoryWithCustomTypeInAssets.getAssets().get(0) .getCustom().getFieldAsString(STRING_FIELD_NAME); assertThat(firstFieldValue).isEqualTo(actualFieldValue); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(updatedCategoryWithCustomTypeInAssets, SetAssetCustomField.of(assetId, STRING_FIELD_NAME, "new"))); assertThat(updatedCategory.getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME)).isEqualTo("new"); }); return type; }); }
@Test public void setAssetSources() { withCategoryHavingAssets(client(), category -> { final AssetSource assetSource = AssetSourceBuilder.ofUri("http://dev.commercetools.com/assets/img/CT-logo.svg") .key("commercetools-logo") .contentType("image/svg+xml") .build(); final String assetId = category.getAssets().get(0).getId(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetSources.of(assetId, Collections.singletonList(assetSource)))); final Asset updatedAsset = updatedCategory.getAssets().get(0); assertThat(updatedAsset.getSources()).hasSize(1); final AssetSource source = updatedAsset.getSources().get(0); assertThat(source.getUri()).isEqualTo("http://dev.commercetools.com/assets/img/CT-logo.svg"); assertThat(source.getKey()).isEqualTo("commercetools-logo"); assertThat(source.getContentType()).isEqualTo("image/svg+xml"); }); }
@Test public void setAssetKey() { withCategoryHavingAssets(client(), category -> { final String assetid = category.getAssets().get(0).getId(); final String newAssetKey = randomKey(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetKey.of(assetid,newAssetKey))); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).haveAtLeastOne(new Condition<>(asset -> asset.getKey().equals(newAssetKey),"Check if key is updated")); }); }
@Test public void addAssetBySku() { withProduct(client(), (Product product) -> { final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg") .build(); final LocalizedString name = LocalizedString.ofEnglish("REWE show case"); final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name) .build(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, AddAsset.ofSku(sku, assetDraft).withPosition(0).withStaged(false)); final Product updatedProduct = client().executeBlocking(cmd); final List<Asset> assets = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets(); assertThat(assets).hasSize(1); final Asset asset = assets.get(0); assertThat(asset.getName()).isEqualTo(name); assertThat(asset.getSources()).hasSize(1); final AssetSource source = asset.getSources().get(0); assertThat(source.getUri()).isEqualTo("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg"); }); }
@Test public void removeAssetByKey() { withCategoryHavingAssets(client(), category -> { final String assetKey = category.getAssets().get(0).getKey(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, RemoveAsset.ofKey(assetKey))); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).allMatch(asset -> !asset.getKey().equals(assetKey)); }); }
@Test public void removeAssetById() { withCategoryHavingAssets(client(), category -> { final String assetId = category.getAssets().get(0).getId(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, RemoveAsset.of(assetId))); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).allMatch(asset -> !asset.getId().equals(assetId)); }); }
@Test public void addAssetByVariantId() { withProduct(client(), (Product product) -> { final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg") .key("rewe-showcase") .contentType("image/jpg") .dimensionsOfWidthAndHeight(1934, 1115) .build(); final LocalizedString name = LocalizedString.ofEnglish("REWE show case"); final LocalizedString description = LocalizedString.ofEnglish("screenshot of the REWE webshop on a mobile and a notebook"); final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name) .description(description) .tags("desktop-sized", "jpg-format", "REWE", "awesome") .build(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, AddAsset.ofVariantId(MASTER_VARIANT_ID, assetDraft))); final List<Asset> assets = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets(); assertThat(assets).hasSize(1); final Asset asset = assets.get(0); assertThat(asset.getId()).isNotEmpty(); assertThat(asset.getDescription()).isEqualTo(description); assertThat(asset.getName()).isEqualTo(name); assertThat(asset.getSources()).hasSize(1); final AssetSource source = asset.getSources().get(0); assertThat(source.getUri()).isEqualTo("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg"); assertThat(source.getKey()).isEqualTo("rewe-showcase"); assertThat(source.getContentType()).isEqualTo("image/jpg"); assertThat(source.getDimensions()).isEqualTo(AssetDimensions.ofWidthAndHeight(1934, 1115)); }); }
@Test public void setAssetCustomTypeByVariantIdAndAssetKey() { withUpdateableType(client(), (Type type) -> { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final Asset assetWithoutCustomType = masterVariant.getAssets().get(0); final String assetKey = assetWithoutCustomType.getKey(); final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofType(type).build(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetAssetCustomType.ofVariantIdAndAssetKey(masterVariant.getId(), assetKey, customFieldsDraft)); final Product updatedProductWithCustomTypeInAssets = client().executeBlocking(cmd); final Asset updatedAsset = updatedProductWithCustomTypeInAssets.getMasterData().getStaged() .getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getCustom()).isNotNull(); assertThat(updatedAsset.getCustom().getType().getId()).isEqualTo(type.getId()); return updatedProductWithCustomTypeInAssets; }); return type; }); }
public void changeAssetNameByVariantIdAndAssetKeyWithStaged(@Nonnull final Boolean staged) { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newName = LocalizedString.ofEnglish("new name"); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final String assetKey = masterVariant.getAssets().get(0).getKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, ChangeAssetName.ofAssetKeyAndVariantId(masterVariant.getId(), assetKey, newName, staged)); final Product updatedProduct = client().executeBlocking(cmd); final Asset updatedAsset = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getName()).isEqualTo(newName); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void setAssetDescriptionByVariantIdWithStaged(final Boolean staged) { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final LocalizedString newDescription = LocalizedString.ofEnglish("new description"); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final String assetId = masterVariant.getAssets().get(0).getId(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetAssetDescription.ofVariantId(masterVariant.getId(), assetId, newDescription, staged)); final Product updatedProduct = client().executeBlocking(cmd); final Asset updatedAsset = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getDescription()).isEqualTo(newDescription); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final Asset assetWithoutCustomType = masterVariant.getAssets().get(0); final String assetId = assetWithoutCustomType.getId(); .getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME); assertThat(actualFieldValue).isEqualTo(firstFieldValue); assertThat(updatedProductWithCustomTypeInAssets.getMasterData().hasStagedChanges()).isEqualTo(staged); .getAssets().get(0).getCustom().getFieldAsString(STRING_FIELD_NAME)) .isEqualTo("new"); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged);
public void setAssetTagsByVariantIdWithStaged(final Boolean staged) { withProductHavingAssets(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final Set<String> newTags = new HashSet<>(asList("tag1", "tag2")); final ProductVariant masterVariant = product.getMasterData().getStaged().getMasterVariant(); final String assetId = masterVariant.getAssets().get(0).getId(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetAssetTags.ofVariantId(masterVariant.getId(), assetId, newTags, staged)); final Product updatedProduct = client().executeBlocking(cmd); final Asset updatedAsset = updatedProduct.getMasterData().getStaged().getMasterVariant().getAssets().get(0); assertThat(updatedAsset.getTags()).isEqualTo(newTags); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }