Skip to content

Commit 83c7198

Browse files
authored
Merge pull request #522 from com-pas/feat/521-add-setstringprivate-in-privateutils
feat(#521): Add setStringPrivate in PrivateUtils
2 parents 82aace9 + 13fb65a commit 83c7198

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

sct-commons/src/main/java/org/lfenergy/compas/sct/commons/util/PrivateUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ public static void copyCompasICDHeaderFromLNodePrivateIntoSTDPrivate(TPrivate st
320320
stdPrivate.getContent().add(objectFactory.createICDHeader(compasICDHeader));
321321
}
322322

323-
324323
public static Optional<String> extractStringPrivate(TBaseElement tBaseElement, String privateType) {
325324
return tBaseElement.getPrivate().stream()
326325
.filter(tPrivate -> privateType.equals(tPrivate.getType()))
@@ -330,4 +329,9 @@ public static Optional<String> extractStringPrivate(TBaseElement tBaseElement, S
330329
.filter(StringUtils::isNotBlank)
331330
.findFirst();
332331
}
332+
333+
public static void setStringPrivate(TBaseElement tBaseElement, String privateType, String privateValue) {
334+
tBaseElement.getPrivate().removeIf(tPrivate -> privateType.equals(tPrivate.getType()));
335+
tBaseElement.getPrivate().add(createStringPrivate(privateType, privateValue));
336+
}
333337
}

sct-commons/src/test/java/org/lfenergy/compas/sct/commons/util/PrivateUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,34 @@ void extractStringPrivate_should_succeed() {
644644
// Then
645645
assertThat(result).hasValue("hello World");
646646
}
647+
648+
@Test
649+
void setStringPrivate_when_private_missing_should_create_new_private() {
650+
// Given
651+
TIED tied = new TIED();
652+
// When
653+
PrivateUtils.setStringPrivate(tied, "privateType", "privateValue");
654+
// Then
655+
assertThat(tied.getPrivate())
656+
.singleElement()
657+
.extracting(TPrivate::getType, TPrivate::getContent)
658+
.containsExactly("privateType", List.of("privateValue"));
659+
}
660+
661+
@Test
662+
void setStringPrivate_when_private_is_present_should_update_private() {
663+
// Given
664+
TIED tied = new TIED();
665+
TPrivate tPrivate = new TPrivate();
666+
tPrivate.setType("privateType");
667+
tPrivate.getContent().add("oldValue");
668+
tied.getPrivate().add(tPrivate);
669+
// When
670+
PrivateUtils.setStringPrivate(tied, "privateType", "newValue");
671+
// Then
672+
assertThat(tied.getPrivate())
673+
.singleElement()
674+
.extracting(TPrivate::getType, TPrivate::getContent)
675+
.containsExactly("privateType", List.of("newValue"));
676+
}
647677
}

0 commit comments

Comments
 (0)