@Nonnull @Override public List<Asset> getAssets() { return delegate.getAssets(); } }
@Nonnull @Override public List<Asset> getAssets() { return delegate.getAssets(); } }
@Test public void changeAssetOrder() { withCategoryHavingAssets(client(), category -> { final List<Asset> originalAssets = category.getAssets(); final List<String> newAssetOrder = new LinkedList<>(originalAssets.stream().map(Asset::getId).collect(toList())); Collections.reverse(newAssetOrder); final CategoryUpdateCommand command = CategoryUpdateCommand.of(category, ChangeAssetOrder.of(newAssetOrder)); final Category updatedCategory = client().executeBlocking(command); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).extracting(Asset::getId).isEqualTo(newAssetOrder); }); }
@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 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 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 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 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 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 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 removeAssetKey() { withCategoryHavingAssets(client(), category -> { final String assetid = category.getAssets().get(0).getId(); final Category updatedCategory = client().executeBlocking(CategoryUpdateCommand.of(category, SetAssetKey.of(assetid,null))); final List<Asset> assets = updatedCategory.getAssets(); assertThat(assets).haveAtLeastOne(new Condition<>(asset -> asset.getKey() == null,"Check if key is removed")); }); }
@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 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)); }); }
/** * 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 CategoryDraftBuilder of(final Category template) { return new CategoryDraftBuilder(copyAssets(template.getAssets()), copyCustom(template.getCustom()), template.getDescription(), template.getExternalId(), template.getKey(), template.getMetaDescription(), template.getMetaKeywords(), template.getMetaTitle(), template.getName(), template.getOrderHint(), template.getParent(), template.getSlug()); } }
@Test public void copyFromCategory() { final Category parent = CategoryBuilder.of(randomString(), LocalizedString.ofEnglish(randomString()), LocalizedString.ofEnglish(randomString())) .build(); final Category category = CategoryBuilder.of(randomString(), LocalizedString.ofEnglish(randomString()), LocalizedString.ofEnglish(randomString())) .externalId(randomString()) .key(randomKey()) .parent(parent) .assets(null) .build(); final CategoryDraft categoryDraft = CategoryDraftBuilder.of(category).build(); assertThat(category.getName()).isEqualTo(categoryDraft.getName()); assertThat(category.getSlug()).isEqualTo(categoryDraft.getSlug()); assertThat(category.getExternalId()).isEqualTo(categoryDraft.getExternalId()); assertThat(category.getParent()).isEqualTo(categoryDraft.getParent()); assertThat(category.getAssets()).isEqualTo(categoryDraft.getAssets()); assertThat(category.getKey()).isEqualTo(categoryDraft.getKey()); } }
public static CategoryBuilder of(final Category category) { return new CategoryBuilder(category.getId(), category.getName(), category.getSlug()). version(category.getVersion()).createdAt(category.getCreatedAt()). lastModifiedAt(category.getLastModifiedAt()). name(category.getName()).slug(category.getSlug()).description(category.getDescription()). ancestors(category.getAncestors()).parent(category.getParent()).assets(category.getAssets()). orderHint(category.getOrderHint()).externalId(category.getExternalId()); }