Skip to content

Commit cf3e188

Browse files
Fixing compilation errors and uts after merging develop
1 parent 6f8b13d commit cf3e188

10 files changed

Lines changed: 116 additions & 81 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/constants/SDMErrorKeys.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ private SDMErrorKeys() {
9898
public static final String UNSUPPORTED_PROPERTIES_PREFIX_KEY = "SDM.unsupportedPropertiesPrefix";
9999
public static final String UNSUPPORTED_PROPERTIES_SUFFIX_KEY = "SDM.unsupportedPropertiesSuffix";
100100
public static final String MAX_COUNT_ERROR_MESSAGE_KEY = "SDM.maxCountErrorMessage";
101+
public static final String FETCH_CHANGELOG_ERROR_KEY = "SDM.fetchChangelogError";
102+
public static final String FAILED_TO_MOVE_ATTACHMENT_KEY = "SDM.failedToMoveAttachment";
103+
public static final String INVALID_SECONDARY_PROPERTIES_FOR_MOVE_PREFIX_KEY =
104+
"SDM.invalidSecondaryPropertiesForMovePrefix";
105+
public static final String INVALID_SECONDARY_PROPERTIES_FOR_MOVE_SUFFIX_KEY =
106+
"SDM.invalidSecondaryPropertiesForMoveSuffix";
107+
public static final String SDM_MOVE_OPERATION_FAILED_KEY = "SDM.sdmMoveOperationFailed";
101108

102109
// public static final String FILENAME_EMPTY_ERROR_MSG = "SDM.File.filenameEmptyError";
103110
// public static final String SDM_ROLES_ERROR_MSG = "SDM.Authorization.sdmRolesError";

sdm/src/main/java/com/sap/cds/sdm/constants/SDMErrorMessages.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ private SDMErrorMessages() {
117117
public static final String UNSUPPORTED_PROPERTIES_SUFFIX =
118118
"\nPlease contact your administrator for assistance with any necessary adjustments.";
119119
public static final String MAX_COUNT_ERROR_MESSAGE = "Cannot upload more than %s attachments.";
120+
public static final String FETCH_CHANGELOG_ERROR = "Could not fetch the changelog";
121+
public static final String FAILED_TO_MOVE_ATTACHMENT = "Failed to move attachment";
122+
public static final String INVALID_SECONDARY_PROPERTIES_FOR_MOVE_PREFIX =
123+
"Invalid secondary properties detected: ";
124+
public static final String INVALID_SECONDARY_PROPERTIES_FOR_MOVE_SUFFIX =
125+
". Attachment rolled back to source.";
126+
public static final String SDM_MOVE_OPERATION_FAILED = "SDM move operation failed";
120127

121128
// Helper Methods to create error/warning messages
122129
public static String buildErrorMessage(

sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.sap.cds.sdm.constants.SDMConstants;
1616
import com.sap.cds.sdm.model.CmisDocument;
1717
import com.sap.cds.sdm.service.handler.AttachmentCopyEventContext;
18-
import com.sap.cds.sdm.utilities.SDMUtils;
1918
import com.sap.cds.sdm.service.handler.AttachmentMoveEventContext;
19+
import com.sap.cds.sdm.utilities.SDMUtils;
2020
import com.sap.cds.services.ServiceException;
2121
import com.sap.cds.services.persistence.PersistenceService;
2222
import java.util.*;
@@ -165,15 +165,18 @@ public Map<String, String> getValidSecondaryPropertiesForMove(
165165
Optional<CdsEntity> optionalParentEntity = model.findEntity(parentEntity);
166166
if (optionalParentEntity.isEmpty()) {
167167
throw new ServiceException(
168-
String.format(SDMConstants.PARENT_ENTITY_NOT_FOUND_ERROR, parentEntity));
168+
String.format(SDMUtils.getErrorMessage("PARENT_ENTITY_NOT_FOUND_ERROR"), parentEntity));
169169
}
170170

171171
// Find the composition element in the parent entity
172172
Optional<CdsElement> compositionElement =
173173
optionalParentEntity.get().findElement(compositionName);
174174
if (compositionElement.isEmpty() || !compositionElement.get().getType().isAssociation()) {
175175
throw new ServiceException(
176-
String.format(SDMConstants.COMPOSITION_NOT_FOUND_ERROR, compositionName, parentEntity));
176+
String.format(
177+
SDMUtils.getErrorMessage("COMPOSITION_NOT_FOUND_ERROR"),
178+
compositionName,
179+
parentEntity));
177180
}
178181

179182
// Get the target entity of the composition
@@ -187,7 +190,9 @@ public Map<String, String> getValidSecondaryPropertiesForMove(
187190
attachmentEntity = model.findEntity(targetEntityName + "_drafts");
188191
if (attachmentEntity.isEmpty()) {
189192
throw new ServiceException(
190-
String.format(SDMConstants.TARGET_ATTACHMENT_ENTITY_NOT_FOUND_ERROR, targetEntityName));
193+
String.format(
194+
SDMUtils.getErrorMessage("TARGET_ATTACHMENT_ENTITY_NOT_FOUND_ERROR"),
195+
targetEntityName));
191196
}
192197
}
193198

@@ -248,15 +253,18 @@ public Object[] getValidSecondaryPropertiesWithEntity(AttachmentMoveEventContext
248253
Optional<CdsEntity> optionalParentEntity = model.findEntity(parentEntity);
249254
if (optionalParentEntity.isEmpty()) {
250255
throw new ServiceException(
251-
String.format(SDMConstants.PARENT_ENTITY_NOT_FOUND_ERROR, parentEntity));
256+
String.format(SDMUtils.getErrorMessage("PARENT_ENTITY_NOT_FOUND_ERROR"), parentEntity));
252257
}
253258

254259
// Find the composition element
255260
Optional<CdsElement> compositionElement =
256261
optionalParentEntity.get().findElement(compositionName);
257262
if (compositionElement.isEmpty() || !compositionElement.get().getType().isAssociation()) {
258263
throw new ServiceException(
259-
String.format(SDMConstants.COMPOSITION_NOT_FOUND_ERROR, compositionName, parentEntity));
264+
String.format(
265+
SDMUtils.getErrorMessage("COMPOSITION_NOT_FOUND_ERROR"),
266+
compositionName,
267+
parentEntity));
260268
}
261269

262270
// Get the target entity
@@ -269,7 +277,9 @@ public Object[] getValidSecondaryPropertiesWithEntity(AttachmentMoveEventContext
269277
attachmentEntity = model.findEntity(targetEntityName + "_drafts");
270278
if (attachmentEntity.isEmpty()) {
271279
throw new ServiceException(
272-
String.format(SDMConstants.TARGET_ATTACHMENT_ENTITY_NOT_FOUND_ERROR, targetEntityName));
280+
String.format(
281+
SDMUtils.getErrorMessage("TARGET_ATTACHMENT_ENTITY_NOT_FOUND_ERROR"),
282+
targetEntityName));
273283
}
274284
}
275285

sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.sap.cds.sdm.model.CopyAttachmentInput;
1414
import com.sap.cds.sdm.model.MoveAttachmentInput;
1515
import com.sap.cds.sdm.service.handler.AttachmentCopyEventContext;
16-
import com.sap.cds.sdm.utilities.SDMUtils;
1716
import com.sap.cds.sdm.service.handler.AttachmentMoveEventContext;
17+
import com.sap.cds.sdm.utilities.SDMUtils;
1818
import com.sap.cds.services.ServiceDelegator;
1919
import com.sap.cds.services.request.UserInfo;
2020
import java.io.InputStream;
@@ -80,7 +80,8 @@ public Map<String, Object> moveAttachments(MoveAttachmentInput input, boolean is
8080
String[] targetFacetParts = input.targetFacet().split("\\.");
8181
if (targetFacetParts.length < 2) {
8282
throw new IllegalArgumentException(
83-
String.format(SDMConstants.INVALID_FACET_FORMAT_ERROR, input.targetFacet()));
83+
String.format(
84+
SDMUtils.getErrorMessage("INVALID_FACET_FORMAT_ERROR"), input.targetFacet()));
8485
}
8586

8687
// The last part is the composition name, everything else is the parent entity

sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ private String getRepositoryId(String jsonString) {
787787
}
788788
}
789789
} catch (Exception e) {
790-
throw new ServiceException(SDMConstants.FAILED_TO_PARSE_REPOSITORY_RESPONSE, e);
790+
throw new ServiceException(
791+
SDMUtils.getErrorMessage("FAILED_TO_PARSE_REPOSITORY_RESPONSE"), e);
791792
}
792793
return null;
793794
}
@@ -805,15 +806,16 @@ public JSONObject getChangeLog(
805806
int responseCode = response.getStatusLine().getStatusCode();
806807
String responseString = EntityUtils.toString(response.getEntity());
807808
if (responseCode == 403) {
808-
throw new ServiceException(SDMConstants.USER_NOT_AUTHORISED_ERROR);
809+
throw new ServiceException(SDMUtils.getErrorMessage("USER_NOT_AUTHORISED_ERROR"));
809810
} else if (responseCode != 200) {
810-
logger.info(SDMConstants.REPOSITORY_ERROR + " : " + responseString);
811-
throw new ServiceException(SDMConstants.REPOSITORY_ERROR + " : " + responseString);
811+
logger.info(SDMUtils.getErrorMessage("REPOSITORY_ERROR") + " : " + responseString);
812+
throw new ServiceException(
813+
SDMUtils.getErrorMessage("REPOSITORY_ERROR") + " : " + responseString);
812814
}
813815
repoId = getRepositoryId(responseString);
814816
} catch (IOException e) {
815-
logger.info(SDMConstants.REPOSITORY_ERROR + " : " + e.getMessage());
816-
throw new ServiceException(SDMConstants.REPOSITORY_ERROR, e);
817+
logger.info(SDMUtils.getErrorMessage("REPOSITORY_ERROR") + " : " + e.getMessage());
818+
throw new ServiceException(SDMUtils.getErrorMessage("REPOSITORY_ERROR"), e);
817819
}
818820
sdmUrl =
819821
sdmUrl
@@ -827,15 +829,15 @@ public JSONObject getChangeLog(
827829
int responseCode = response.getStatusLine().getStatusCode();
828830
String responseString = EntityUtils.toString(response.getEntity());
829831
if (responseCode == 403) {
830-
throw new ServiceException(SDMConstants.USER_NOT_AUTHORISED_ERROR);
832+
throw new ServiceException(SDMUtils.getErrorMessage("USER_NOT_AUTHORISED_ERROR"));
831833
} else if (responseCode == 404) {
832-
throw new ServiceException(SDMConstants.FILE_NOT_FOUND_ERROR);
834+
throw new ServiceException(SDMUtils.getErrorMessage("FILE_NOT_FOUND_ERROR"));
833835
} else if (responseCode != 200) {
834-
throw new ServiceException(SDMConstants.FETCH_CHANGELOG_ERROR);
836+
throw new ServiceException(SDMUtils.getErrorMessage("FETCH_CHANGELOG_ERROR"));
835837
}
836838
return new JSONObject(responseString);
837839
} catch (IOException e) {
838-
throw new ServiceException(SDMConstants.FETCH_CHANGELOG_ERROR, e);
840+
throw new ServiceException(SDMUtils.getErrorMessage("FETCH_CHANGELOG_ERROR"), e);
839841
}
840842
}
841843

@@ -928,7 +930,7 @@ public String moveAttachment(
928930
.blockingFirst();
929931
} catch (Exception e) {
930932
logger.error("Failed to move attachment after retries: {}", e.getMessage(), e);
931-
throw new ServiceException(SDMConstants.FAILED_TO_MOVE_ATTACHMENT, e);
933+
throw new ServiceException(SDMUtils.getErrorMessage("FAILED_TO_MOVE_ATTACHMENT"), e);
932934
}
933935
}
934936
}

sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.sap.cds.reflect.CdsEntity;
88
import com.sap.cds.reflect.CdsModel;
99
import com.sap.cds.sdm.constants.SDMConstants;
10+
import com.sap.cds.sdm.constants.SDMErrorMessages;
1011
import com.sap.cds.sdm.handler.TokenHandler;
1112
import com.sap.cds.sdm.model.AttachmentMoveContext;
1213
import com.sap.cds.sdm.model.AttachmentProcessingResults;
@@ -308,12 +309,9 @@ private List<Map<String, String>> checkMaxCountConstraintForMove(
308309

309310
CdsEntity targetAttachmentEntity = targetEntityOptional.get();
310311

311-
// Get maxCount and error message from annotations
312-
String errorMessageCount =
312+
long maxCount =
313313
SDMUtils.getAttachmentCountAndMessage(
314314
context.getModel().entities().toList(), targetAttachmentEntity);
315-
String[] maxCountArr = errorMessageCount.split("__");
316-
long maxCount = Long.parseLong(maxCountArr[0]);
317315

318316
// If maxCount is 0 or negative, no limit is enforced
319317
if (maxCount <= 0) {
@@ -351,20 +349,7 @@ private List<Map<String, String>> checkMaxCountConstraintForMove(
351349

352350
// Check if total would exceed maxCount
353351
if (totalCountAfterMove > maxCount) {
354-
String errorMessage = maxCountArr[1];
355-
String failureReason;
356-
357-
if (errorMessage != null && !"null".equalsIgnoreCase(errorMessage)) {
358-
// Use custom error message from annotation
359-
failureReason = errorMessage;
360-
} else {
361-
// Use default error message
362-
failureReason =
363-
String.format(
364-
"Cannot move %d attachment(s). Target entity allows maximum %d attachments, and"
365-
+ " already has %d. Maximum count would be exceeded.",
366-
objectIds.size(), maxCount, existingCount);
367-
}
352+
String failureReason = SDMUtils.getErrorMessage("MAX_COUNT_ERROR_MESSAGE");
368353

369354
logger.warn(
370355
"Move operation rejected: Total count {} exceeds maxCount {}. Marking all {} attachments"
@@ -382,7 +367,7 @@ private List<Map<String, String>> checkMaxCountConstraintForMove(
382367
}
383368

384369
// Show warning message
385-
context.getMessages().warn(failureReason);
370+
context.getMessages().warn(String.format(failureReason, maxCount));
386371
}
387372
} catch (Exception e) {
388373
logger.error(
@@ -755,9 +740,9 @@ private void handleValidationFailure(
755740
failure.put(OBJECT_ID_KEY, objectId);
756741
failure.put(
757742
FAILURE_REASON_KEY,
758-
SDMConstants.INVALID_SECONDARY_PROPERTIES_PREFIX
743+
SDMUtils.getErrorMessage("INVALID_SECONDARY_PROPERTIES_FOR_MOVE_PREFIX")
759744
+ String.join(", ", invalidProperties)
760-
+ SDMConstants.INVALID_SECONDARY_PROPERTIES_SUFFIX);
745+
+ SDMUtils.getErrorMessage("INVALID_SECONDARY_PROPERTIES_FOR_MOVE_SUFFIX"));
761746
failedAttachments.add(failure);
762747
}
763748

@@ -772,7 +757,7 @@ private String parseSDMErrorMessage(Exception exception) {
772757
String errorMessage = extractErrorMessage(exception);
773758

774759
if (errorMessage == null || errorMessage.isEmpty()) {
775-
return SDMConstants.SDM_MOVE_OPERATION_FAILED;
760+
return SDMUtils.getErrorMessage("SDM_MOVE_OPERATION_FAILED");
776761
}
777762

778763
// Try to match specific error types
@@ -818,7 +803,7 @@ private String extractErrorMessage(Exception exception) {
818803
private boolean isGenericMessage(String message) {
819804
return message == null
820805
|| message.isEmpty()
821-
|| message.equals(SDMConstants.FAILED_TO_MOVE_ATTACHMENT);
806+
|| message.equals(SDMUtils.getErrorMessage("FAILED_TO_MOVE_ATTACHMENT"));
822807
}
823808

824809
/**
@@ -842,15 +827,15 @@ private String matchSpecificErrorType(String errorMessage) {
842827
if (lowerCaseMessage.contains("unauthorized")
843828
|| lowerCaseMessage.contains("not authorized")
844829
|| lowerCaseMessage.contains("permission")) {
845-
return SDMConstants.USER_NOT_AUTHORISED_ERROR;
830+
return SDMUtils.getErrorMessage("USER_NOT_AUTHORISED_ERROR");
846831
}
847832

848833
if (lowerCaseMessage.contains("blocked") || lowerCaseMessage.contains("mimetype")) {
849-
return SDMConstants.MIMETYPE_INVALID_ERROR;
834+
return SDMUtils.getErrorMessage("MIMETYPE_INVALID_ERROR");
850835
}
851836

852837
if (lowerCaseMessage.contains("not found") || lowerCaseMessage.contains("object not found")) {
853-
return SDMConstants.FILE_NOT_FOUND_ERROR;
838+
return SDMUtils.getErrorMessage("FILE_NOT_FOUND_ERROR");
854839
}
855840

856841
return null;
@@ -873,7 +858,7 @@ private String parseDuplicateError(String errorMessage) {
873858
int withIndex = detailedMessage.indexOf(" with Id");
874859
if (withIndex != -1) {
875860
String filename = detailedMessage.substring(6, withIndex).trim();
876-
return SDMConstants.getDuplicateFilesError(filename);
861+
return SDMErrorMessages.getDuplicateFilesError(filename);
877862
}
878863
}
879864
return detailedMessage;
@@ -908,9 +893,9 @@ private String buildValidationFailureMessage(
908893
// Check if we have invalid properties information in the context
909894
if (moveContext.getInvalidProperties() != null
910895
&& !moveContext.getInvalidProperties().isEmpty()) {
911-
return SDMConstants.INVALID_SECONDARY_PROPERTIES_PREFIX
896+
return SDMUtils.getErrorMessage("INVALID_SECONDARY_PROPERTIES_FOR_MOVE_PREFIX")
912897
+ String.join(", ", moveContext.getInvalidProperties())
913-
+ SDMConstants.INVALID_SECONDARY_PROPERTIES_SUFFIX;
898+
+ SDMUtils.getErrorMessage("INVALID_SECONDARY_PROPERTIES_FOR_MOVE_SUFFIX");
914899
}
915900

916901
// Detect specific failure types and provide meaningful messages

sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void testUpdateNameWithDuplicateFilenames() throws IOException {
162162
.when(
163163
() ->
164164
AttachmentsHandlerUtils.validateFileNames(
165-
any(), anyList(), anyString(), anyString()))
165+
any(), anyList(), anyString(), anyString(), any()))
166166
.thenAnswer(
167167
invocation -> {
168168
Messages msgs = invocation.getArgument(0);
@@ -505,7 +505,7 @@ public void testUpdateNameWithEmptyFilename() throws IOException {
505505
.when(
506506
() ->
507507
AttachmentsHandlerUtils.validateFileNames(
508-
any(), anyList(), anyString(), anyString()))
508+
any(), anyList(), anyString(), anyString(), any()))
509509
.thenAnswer(
510510
invocation -> {
511511
Messages msgs = invocation.getArgument(0);
@@ -585,7 +585,10 @@ public void testUpdateNameWithEmptyFilename() throws IOException {
585585
anyList(), anyString(), anyString()))
586586
.thenReturn(new ArrayList<>());
587587
sdmUtilsMockedStatic
588-
.when(() -> SDMUtils.FileNameDuplicateInDrafts(anyList(), anyString(), anyString()))
588+
.when(
589+
() ->
590+
SDMUtils.FileNameDuplicateInDrafts(
591+
anyList(), anyString(), anyString(), anyString()))
589592
.thenReturn(new HashSet<>());
590593

591594
// Act
@@ -675,7 +678,7 @@ public void testUpdateNameWithRestrictedCharacters() throws IOException {
675678
.when(
676679
() ->
677680
AttachmentsHandlerUtils.validateFileNames(
678-
any(), anyList(), anyString(), anyString()))
681+
any(), anyList(), anyString(), anyString(), any()))
679682
.thenAnswer(
680683
invocation -> {
681684
Messages msgs = invocation.getArgument(0);

0 commit comments

Comments
 (0)