|
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; |
| 8 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.assertAttributesAreEqual; |
| 9 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.getProductTypeByKey; |
5 | 10 | import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes; |
6 | 11 | import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes; |
7 | 12 | import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_SOURCE_CLIENT; |
8 | 13 | import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_TARGET_CLIENT; |
9 | 14 | import static org.assertj.core.api.Assertions.assertThat; |
10 | 15 |
|
| 16 | +import com.commercetools.api.models.common.LocalizedString; |
| 17 | +import com.commercetools.api.models.product_type.AttributeDefinition; |
11 | 18 | import com.commercetools.api.models.product_type.AttributeDefinitionDraft; |
12 | 19 | import com.commercetools.api.models.product_type.AttributeDefinitionDraftBuilder; |
13 | 20 | import com.commercetools.api.models.product_type.AttributeNestedType; |
| 21 | +import com.commercetools.api.models.product_type.AttributeNestedTypeBuilder; |
| 22 | +import com.commercetools.api.models.product_type.AttributeSetType; |
| 23 | +import com.commercetools.api.models.product_type.AttributeSetTypeBuilder; |
14 | 24 | import com.commercetools.api.models.product_type.ProductType; |
15 | 25 | import com.commercetools.api.models.product_type.ProductTypeChangeLabelActionBuilder; |
16 | 26 | import com.commercetools.api.models.product_type.ProductTypeDraft; |
17 | 27 | import com.commercetools.api.models.product_type.ProductTypeDraftBuilder; |
18 | 28 | import com.commercetools.api.models.product_type.ProductTypePagedQueryResponse; |
| 29 | +import com.commercetools.api.models.product_type.ProductTypeReference; |
| 30 | +import com.commercetools.api.models.product_type.ProductTypeReferenceBuilder; |
19 | 31 | import com.commercetools.api.models.product_type.ProductTypeUpdateAction; |
20 | 32 | import com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl; |
21 | 33 | import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; |
|
27 | 39 | import io.vrap.rmf.base.client.ApiHttpResponse; |
28 | 40 | import java.util.ArrayList; |
29 | 41 | import java.util.List; |
| 42 | +import java.util.Optional; |
30 | 43 | import java.util.stream.Collectors; |
31 | 44 | import org.junit.jupiter.api.AfterAll; |
32 | 45 | import org.junit.jupiter.api.BeforeEach; |
@@ -116,6 +129,87 @@ void sync_WithEmptyTargetProject_ShouldReturnProperStatistics() { |
116 | 129 | + " of NestedType attribute definition(s) referencing a missing product type)."); |
117 | 130 | } |
118 | 131 |
|
| 132 | + @Test |
| 133 | + void sync_WithProductTypeReferencingItselfAsAttribute_ShouldCreateProductType() { |
| 134 | + // preparation |
| 135 | + final AttributeDefinitionDraft nestedTypeAttr = |
| 136 | + AttributeDefinitionDraftBuilder.of() |
| 137 | + .name("selfReferenceAttr") |
| 138 | + .label(LocalizedString.ofEnglish("selfReferenceAttr")) |
| 139 | + .type( |
| 140 | + AttributeSetTypeBuilder.of() |
| 141 | + .elementType( |
| 142 | + AttributeNestedTypeBuilder.of() |
| 143 | + .typeReference( |
| 144 | + ProductTypeReferenceBuilder.of().id(PRODUCT_TYPE_KEY_5).build()) |
| 145 | + .build()) |
| 146 | + .build()) |
| 147 | + .isSearchable(true) |
| 148 | + .isRequired(false) |
| 149 | + .build(); |
| 150 | + |
| 151 | + final ProductTypeDraft oldProductTypeDraft = |
| 152 | + ProductTypeDraftBuilder.of() |
| 153 | + .key(PRODUCT_TYPE_KEY_5) |
| 154 | + .name(PRODUCT_TYPE_NAME_5) |
| 155 | + .description(PRODUCT_TYPE_DESCRIPTION_5) |
| 156 | + .attributes(nestedTypeAttr) |
| 157 | + .build(); |
| 158 | + |
| 159 | + // Sync productDraft with attribute referencing itself to source project |
| 160 | + new ProductTypeSync(ProductTypeSyncOptionsBuilder.of(CTP_SOURCE_CLIENT).build()) |
| 161 | + .sync(List.of(oldProductTypeDraft)) |
| 162 | + .toCompletableFuture() |
| 163 | + .join(); |
| 164 | + final ProductType oldProductType = |
| 165 | + CTP_SOURCE_CLIENT |
| 166 | + .productTypes() |
| 167 | + .withKey(PRODUCT_TYPE_KEY_5) |
| 168 | + .get() |
| 169 | + .executeBlocking() |
| 170 | + .getBody(); |
| 171 | + |
| 172 | + // test |
| 173 | + final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions); |
| 174 | + final ProductTypeSyncStatistics productTypeSyncStatistics = |
| 175 | + ProductTypeTransformUtils.toProductTypeDrafts( |
| 176 | + CTP_SOURCE_CLIENT, referenceIdToKeyCache, List.of(oldProductType)) |
| 177 | + .thenCompose(newDrafts -> productTypeSync.sync(newDrafts)) |
| 178 | + .toCompletableFuture() |
| 179 | + .join(); |
| 180 | + |
| 181 | + // assertion |
| 182 | + assertThat(errorMessages).isEmpty(); |
| 183 | + assertThat(exceptions).isEmpty(); |
| 184 | + assertThat(builtUpdateActions).isEmpty(); |
| 185 | + assertThat(productTypeSyncStatistics).hasValues(1, 1, 0, 0, 0); |
| 186 | + assertThat(productTypeSyncStatistics.getReportMessage()) |
| 187 | + .isEqualTo( |
| 188 | + "Summary: 1 product types were processed in total" |
| 189 | + + " (1 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" |
| 190 | + + " of NestedType attribute definition(s) referencing a missing product type)."); |
| 191 | + |
| 192 | + final Optional<ProductType> newProductType = |
| 193 | + getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5); |
| 194 | + assertThat(newProductType).isPresent(); |
| 195 | + assertThat(newProductType) |
| 196 | + .hasValueSatisfying( |
| 197 | + productType -> { |
| 198 | + assertAttributesAreEqual(productType.getAttributes(), List.of(nestedTypeAttr)); |
| 199 | + final AttributeDefinition attributeDefinition1 = productType.getAttributes().get(0); |
| 200 | + assertThat(attributeDefinition1.getType()).isInstanceOf(AttributeSetType.class); |
| 201 | + final AttributeSetType attributeSetType = |
| 202 | + (AttributeSetType) attributeDefinition1.getType(); |
| 203 | + assertThat(attributeSetType.getElementType()).isInstanceOf(AttributeNestedType.class); |
| 204 | + final AttributeNestedType attributeNestedType = |
| 205 | + (AttributeNestedType) attributeSetType.getElementType(); |
| 206 | + assertThat(attributeNestedType.getTypeReference()) |
| 207 | + .isInstanceOf(ProductTypeReference.class); |
| 208 | + assertThat(attributeNestedType.getTypeReference().getId()) |
| 209 | + .isEqualTo(productType.getId()); |
| 210 | + }); |
| 211 | + } |
| 212 | + |
119 | 213 | @Test |
120 | 214 | void sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics() { |
121 | 215 | // preparation |
|
0 commit comments