|
2 | 2 |
|
3 | 3 | import static com.commercetools.api.models.common.LocalizedString.ofEnglish; |
4 | 4 | import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat; |
| 5 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_DESCRIPTION_5; |
| 6 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_5; |
| 7 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_NAME_5; |
5 | 8 | import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes; |
6 | 9 | import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes; |
7 | 10 | import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_SOURCE_CLIENT; |
8 | 11 | import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_TARGET_CLIENT; |
9 | 12 | import static org.assertj.core.api.Assertions.assertThat; |
10 | 13 |
|
| 14 | +import com.commercetools.api.models.common.LocalizedString; |
11 | 15 | import com.commercetools.api.models.product_type.AttributeDefinitionDraft; |
12 | 16 | import com.commercetools.api.models.product_type.AttributeDefinitionDraftBuilder; |
13 | 17 | import com.commercetools.api.models.product_type.AttributeNestedType; |
| 18 | +import com.commercetools.api.models.product_type.AttributeNestedTypeBuilder; |
| 19 | +import com.commercetools.api.models.product_type.AttributeSetTypeBuilder; |
14 | 20 | import com.commercetools.api.models.product_type.ProductType; |
15 | 21 | import com.commercetools.api.models.product_type.ProductTypeChangeLabelActionBuilder; |
16 | 22 | import com.commercetools.api.models.product_type.ProductTypeDraft; |
17 | 23 | import com.commercetools.api.models.product_type.ProductTypeDraftBuilder; |
18 | 24 | import com.commercetools.api.models.product_type.ProductTypePagedQueryResponse; |
| 25 | +import com.commercetools.api.models.product_type.ProductTypeReferenceBuilder; |
19 | 26 | import com.commercetools.api.models.product_type.ProductTypeUpdateAction; |
20 | 27 | import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; |
21 | 28 | import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; |
@@ -116,6 +123,75 @@ void sync_WithEmptyTargetProject_ShouldReturnProperStatistics() { |
116 | 123 | + " of NestedType attribute definition(s) referencing a missing product type)."); |
117 | 124 | } |
118 | 125 |
|
| 126 | + @Test |
| 127 | + void sync_WithProductTypeReferencingItselfAsAttribute_ShouldCreateProductType() { |
| 128 | + // preparation |
| 129 | + final AttributeDefinitionDraft nestedTypeAttr = |
| 130 | + AttributeDefinitionDraftBuilder.of() |
| 131 | + .name("selfReferenceAttr") |
| 132 | + .label(LocalizedString.ofEnglish("selfReferenceAttr")) |
| 133 | + .type( |
| 134 | + AttributeSetTypeBuilder.of() |
| 135 | + .elementType( |
| 136 | + AttributeSetTypeBuilder.of() |
| 137 | + .elementType( |
| 138 | + AttributeSetTypeBuilder.of() |
| 139 | + .elementType( |
| 140 | + AttributeNestedTypeBuilder.of() |
| 141 | + .typeReference( |
| 142 | + ProductTypeReferenceBuilder.of() |
| 143 | + .id(PRODUCT_TYPE_KEY_5) |
| 144 | + .build()) |
| 145 | + .build()) |
| 146 | + .build()) |
| 147 | + .build()) |
| 148 | + .build()) |
| 149 | + .isSearchable(false) |
| 150 | + .isRequired(false) |
| 151 | + .build(); |
| 152 | + |
| 153 | + final ProductTypeDraft oldProductTypeDraft = |
| 154 | + ProductTypeDraftBuilder.of() |
| 155 | + .key(PRODUCT_TYPE_KEY_5) |
| 156 | + .name(PRODUCT_TYPE_NAME_5) |
| 157 | + .description(PRODUCT_TYPE_DESCRIPTION_5) |
| 158 | + .attributes(nestedTypeAttr) |
| 159 | + .build(); |
| 160 | + |
| 161 | + // Sync productDraft with attribute referencing itself to source project |
| 162 | + new ProductTypeSync(ProductTypeSyncOptionsBuilder.of(CTP_SOURCE_CLIENT).build()) |
| 163 | + .sync(List.of(oldProductTypeDraft)) |
| 164 | + .toCompletableFuture() |
| 165 | + .join(); |
| 166 | + final ProductType oldProductType = |
| 167 | + CTP_SOURCE_CLIENT |
| 168 | + .productTypes() |
| 169 | + .withKey(PRODUCT_TYPE_KEY_5) |
| 170 | + .get() |
| 171 | + .executeBlocking() |
| 172 | + .getBody(); |
| 173 | + |
| 174 | + // test |
| 175 | + final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions); |
| 176 | + final ProductTypeSyncStatistics productTypeSyncStatistics = |
| 177 | + ProductTypeTransformUtils.toProductTypeDrafts( |
| 178 | + CTP_SOURCE_CLIENT, referenceIdToKeyCache, List.of(oldProductType)) |
| 179 | + .thenCompose(newDrafts -> productTypeSync.sync(newDrafts)) |
| 180 | + .toCompletableFuture() |
| 181 | + .join(); |
| 182 | + |
| 183 | + // assertion |
| 184 | + assertThat(errorMessages).isEmpty(); |
| 185 | + assertThat(exceptions).isEmpty(); |
| 186 | + assertThat(builtUpdateActions).isEmpty(); |
| 187 | + assertThat(productTypeSyncStatistics).hasValues(1, 1, 0, 0, 0); |
| 188 | + assertThat(productTypeSyncStatistics.getReportMessage()) |
| 189 | + .isEqualTo( |
| 190 | + "Summary: 1 product types were processed in total" |
| 191 | + + " (1 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" |
| 192 | + + " of NestedType attribute definition(s) referencing a missing product type)."); |
| 193 | + } |
| 194 | + |
119 | 195 | @Test |
120 | 196 | void sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics() { |
121 | 197 | // preparation |
|
0 commit comments