Skip to content

Commit 032eb52

Browse files
author
salander85
committed
Create empty StateDraft if required fields are missing
1 parent f2147bd commit 032eb52

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,37 @@ public static List<StateDraft> mapToStateDrafts(
6565
state -> {
6666
final List<StateResourceIdentifier> newTransitions =
6767
replaceTransitionIdsWithKeys(state, referenceIdToKeyCache);
68-
return StateDraftBuilder.of()
69-
.key(state.getKey())
70-
.type(state.getType())
71-
.name(state.getName())
72-
.description(state.getDescription())
73-
.initial(state.getInitial())
74-
.roles(state.getRoles())
75-
.transitions(newTransitions)
76-
.build();
68+
return getStateDraft(state, newTransitions);
7769
})
7870
.collect(Collectors.toList());
7971
}
8072

73+
/**
74+
* Creates a new {@link StateDraft} from given {@link State} and transitions as {@link
75+
* java.util.List}&lt; {@link StateResourceIdentifier}&gt;
76+
*
77+
* @param state - template state to build the draft from
78+
* @param newTransitions - transformed list of state resource identifiers
79+
* @return a new {@link StateDraft} with all fields copied from the {@param state} and transitions
80+
* set {@param newTransitions} - it will return empty StateDraft if key or type are missing.
81+
*/
82+
private static StateDraft getStateDraft(
83+
State state, List<StateResourceIdentifier> newTransitions) {
84+
if (state.getKey() != null && state.getType() != null) {
85+
return StateDraftBuilder.of()
86+
.key(state.getKey())
87+
.type(state.getType())
88+
.name(state.getName())
89+
.description(state.getDescription())
90+
.initial(state.getInitial())
91+
.roles(state.getRoles())
92+
.transitions(newTransitions)
93+
.build();
94+
} else {
95+
return StateDraft.of();
96+
}
97+
}
98+
8199
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
82100
private static List<StateResourceIdentifier> replaceTransitionIdsWithKeys(
83101
@Nonnull final State state, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ void mapToStateDrafts_WithNullReferences_ShouldNotFail() {
120120
assertThat(referenceReplacedDrafts.get(0).getTransitions()).isEqualTo(null);
121121
}
122122

123+
@Test
124+
void mapToStateDrafts_WithMissingRequiredFields_ShouldNotFailAndReturnEmptyDraft() {
125+
// preparation
126+
final State mockState = mock(State.class);
127+
when(mockState.getTransitions()).thenReturn(null);
128+
129+
// test
130+
final List<StateDraft> referenceReplacedDrafts =
131+
StateReferenceResolutionUtils.mapToStateDrafts(List.of(mockState), referenceIdToKeyCache);
132+
133+
assertThat(referenceReplacedDrafts.get(0)).isEqualTo(StateDraft.of());
134+
}
135+
123136
@Nonnull
124137
private static State getStateMock(@Nonnull final String key) {
125138
final State state = mock(State.class);

0 commit comments

Comments
 (0)