/** * 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()); } }
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 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 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; }); }
@Test public void setAssetCustomTypeByVariantSkuAndAssetKey() { 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.ofSkuAndAssetKey(masterVariant.getSku(), 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; }); } @Test
.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);
.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);