Skip to content

Commit 3a47c99

Browse files
State Transitions to sync correctly from source to target. (#763)
* State Transitions to sync correctly from source to target.
1 parent f818e06 commit 3a47c99

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

src/integration-test/java/com/commercetools/sync/integration/externalsource/states/StateSyncIT.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,42 @@ void sync_WithEmptyNewTransition_ShouldRemoveTransitions() {
912912
.join();
913913
}
914914

915+
@Test
916+
void sync_WithNullTransitions_TargetStateTransitionsShouldBeNull() {
917+
918+
final StateDraft stateDraft = createStateDraft(keyA);
919+
final State state = createStateInSource(stateDraft);
920+
921+
final StateSyncOptions stateSyncOptions =
922+
StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
923+
924+
final StateSync stateSync = new StateSync(stateSyncOptions);
925+
final List<StateDraft> stateDrafts =
926+
StateTransformUtils.toStateDrafts(
927+
CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(state))
928+
.join();
929+
// test
930+
final StateSyncStatistics stateSyncStatistics =
931+
stateSync.sync(stateDrafts).toCompletableFuture().join();
932+
933+
assertThat(stateSyncStatistics).hasValues(1, 1, 0, 0, 0);
934+
935+
CtpQueryUtils.queryAll(
936+
CTP_TARGET_CLIENT,
937+
StateQueryBuilder.of().plusPredicates(q -> q.key().is(keyA)).build(),
938+
Function.identity())
939+
.thenApply(
940+
fetchedCategories ->
941+
fetchedCategories.stream().flatMap(List::stream).collect(Collectors.toList()))
942+
.thenAccept(
943+
resultStates -> {
944+
Assertions.assertThat(resultStates.size()).isEqualTo(1);
945+
Assertions.assertThat(resultStates.get(0).getTransitions()).isNull();
946+
})
947+
.toCompletableFuture()
948+
.join();
949+
}
950+
915951
@Test
916952
void sync_WithStateWithoutKey_ShouldAddErrorMessage() {
917953
String nameA = "state-A";
@@ -1165,8 +1201,9 @@ private StateDraft createStateDraftReferencingStateDrafts(
11651201
}
11661202

11671203
private StateDraft createStateDraft(final String key, final State... transitionStates) {
1168-
List<Reference<State>> references = new ArrayList<>();
1204+
List<Reference<State>> references = null;
11691205
if (transitionStates.length > 0) {
1206+
references = new ArrayList<>();
11701207
for (State transitionState : transitionStates) {
11711208
references.add(referenceOfId(transitionState.getId()));
11721209
}
@@ -1178,7 +1215,7 @@ private StateDraft createStateDraftWithReference(
11781215
final String key, final List<Reference<State>> references) {
11791216
return StateDraftBuilder.of(key, StateType.REVIEW_STATE)
11801217
.roles(Collections.singleton(StateRole.REVIEW_INCLUDED_IN_STATISTICS))
1181-
.transitions(new HashSet<>(references))
1218+
.transitions(null == references ? null : new HashSet<>(references))
11821219
.initial(true)
11831220
.build();
11841221
}

src/main/java/com/commercetools/sync/states/utils/StateReferenceResolutionUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public static List<StateDraft> mapToStateDrafts(
7777
private static Set<Reference<State>> replaceTransitionIdsWithKeys(
7878
@Nonnull final State state, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) {
7979
final Set<Reference<State>> transitions = state.getTransitions();
80+
81+
if (transitions == null) return null;
82+
8083
final Set<Reference<State>> newTransitions = new HashSet<>();
81-
if (transitions != null && !transitions.isEmpty()) {
84+
if (!transitions.isEmpty()) {
8285
transitions.forEach(
8386
transition -> {
8487
newTransitions.add(

src/test/java/com/commercetools/sync/states/utils/StateReferenceResolutionUtilsTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,29 @@ void mapToStateDrafts_WithAllNonExpandedReferences_ShouldReturnReferencesWithout
9292
}
9393

9494
@Test
95-
void mapToStateDrafts_WithNullOrEmptyReferences_ShouldNotFail() {
95+
void mapToStateDrafts_WithEmptyReferences_ShouldNotFail() {
9696
// preparation
97-
final State mockState1 = mock(State.class);
98-
when(mockState1.getTransitions()).thenReturn(null);
99-
100-
final State mockState2 = mock(State.class);
101-
when(mockState2.getTransitions()).thenReturn(Collections.emptySet());
97+
final State mockState = mock(State.class);
98+
when(mockState.getTransitions()).thenReturn(Collections.emptySet());
10299

103100
// test
104101
final List<StateDraft> referenceReplacedDrafts =
105-
mapToStateDrafts(Arrays.asList(mockState1, mockState2), referenceIdToKeyCache);
102+
mapToStateDrafts(Arrays.asList(mockState), referenceIdToKeyCache);
106103

107104
assertThat(referenceReplacedDrafts.get(0).getTransitions()).isEqualTo(Collections.emptySet());
108-
assertThat(referenceReplacedDrafts.get(1).getTransitions()).isEqualTo(Collections.emptySet());
105+
}
106+
107+
@Test
108+
void mapToStateDrafts_WithNullReferences_ShouldNotFail() {
109+
// preparation
110+
final State mockState = mock(State.class);
111+
when(mockState.getTransitions()).thenReturn(null);
112+
113+
// test
114+
final List<StateDraft> referenceReplacedDrafts =
115+
mapToStateDrafts(Arrays.asList(mockState), referenceIdToKeyCache);
116+
117+
assertThat(referenceReplacedDrafts.get(0).getTransitions()).isEqualTo(null);
109118
}
110119

111120
@Nonnull

0 commit comments

Comments
 (0)