diff --git a/sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java b/sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java index 040802b5..bc1612dc 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java +++ b/sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java @@ -1,5 +1,6 @@ package com.sap.cds.sdm.caching; +import com.sap.cds.sdm.model.RepoValue; import java.util.List; import java.util.concurrent.TimeUnit; import org.ehcache.Cache; @@ -18,7 +19,7 @@ public class CacheConfig { private static Cache userTokenCache; private static Cache clientCredentialsTokenCache; private static Cache userAuthoritiesTokenCache; - private static Cache versionedRepoCache; + private static Cache repoCache; private static Cache> secondaryTypesCache; private static Cache maxAllowedAttachmentsCache; private static Cache> secondaryPropertiesCache; @@ -52,11 +53,11 @@ public static void initializeCache() { .withExpiry( Expirations.timeToLiveExpiration( new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES)))); - versionedRepoCache = + repoCache = cacheManager.createCache( "versionedRepo", CacheConfigurationBuilder.newCacheConfigurationBuilder( - RepoKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE)) + RepoKey.class, RepoValue.class, ResourcePoolsBuilder.heap(HEAP_SIZE)) .withExpiry( Expirations.timeToLiveExpiration( new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES)))); @@ -107,8 +108,8 @@ public static Cache getClientCredentialsTokenCache() { return clientCredentialsTokenCache; } - public static Cache getVersionedRepoCache() { - return versionedRepoCache; + public static Cache getRepoCache() { + return repoCache; } public static Cache getMaxAllowedAttachmentsCache() { diff --git a/sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java b/sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java index 4a9a8469..b5915cc0 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java +++ b/sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java @@ -27,6 +27,8 @@ private SDMConstants() { public static final String GENERIC_ERROR = "Could not %s the document."; public static final String VERSIONED_REPO_ERROR = "Upload not supported for versioned repositories."; + public static final String VIRUS_REPO_ERROR_MORE_THAN_400MB = + "You cannot upload files that are larger than 400 MB"; public static final String VIRUS_ERROR = "%s contains potential malware and cannot be uploaded."; public static final String REPOSITORY_ERROR = "Failed to get repository info."; public static final String NOT_FOUND_ERROR = "Failed to read document."; diff --git a/sdm/src/main/java/com/sap/cds/sdm/model/RepoValue.java b/sdm/src/main/java/com/sap/cds/sdm/model/RepoValue.java new file mode 100644 index 00000000..005df082 --- /dev/null +++ b/sdm/src/main/java/com/sap/cds/sdm/model/RepoValue.java @@ -0,0 +1,14 @@ +package com.sap.cds.sdm.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class RepoValue { + private Boolean virusScanEnabled; + private Boolean versionEnabled; + private Boolean disableVirusScannerForLargeFile; +} diff --git a/sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java b/sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java index 1133af36..64ed19b4 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java +++ b/sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java @@ -130,6 +130,7 @@ public void addAttachmentToDraft( updatedFields.put("folderId", cmisDocument.getFolderId()); updatedFields.put("status", "Clean"); updatedFields.put("type", "sap-icon://document"); + updatedFields.put("mimeType", cmisDocument.getMimeType()); CqnUpdate updateQuery = Update.entity(attachmentEntity) .data(updatedFields) diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java b/sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java index 0e21b7ce..a4fa8def 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java @@ -298,7 +298,7 @@ private void formResponse( String status = "success"; String name = cmisDocument.getFileName(); String id = cmisDocument.getAttachmentId(); - String objectId = ""; + String objectId = "", mimeType = ""; String error = ""; try { String responseString = EntityUtils.toString(response.getEntity()); @@ -308,6 +308,7 @@ private void formResponse( JSONObject succinctProperties = jsonResponse.getJSONObject("succinctProperties"); status = "success"; objectId = succinctProperties.getString("cmis:objectId"); + mimeType = succinctProperties.getString("cmis:contentStreamMimeType"); } else { if (responseCode == 409) { JSONObject jsonResponse = new JSONObject(responseString); @@ -339,6 +340,7 @@ private void formResponse( finalResponse.put("message", error); if (!objectId.isEmpty()) { finalResponse.put("objectId", objectId); + finalResponse.put("mimeType", mimeType); } } catch (IOException e) { throw new ServiceException(SDMConstants.getGenericError("upload")); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java index 2d152e37..605309dc 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java @@ -3,6 +3,7 @@ import com.sap.cds.Result; import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext; import com.sap.cds.sdm.model.CmisDocument; +import com.sap.cds.sdm.model.RepoValue; import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.services.ServiceException; import com.sap.cds.services.persistence.PersistenceService; @@ -27,12 +28,10 @@ public String getFolderIdByPath( String parentId, String repositoryId, SDMCredentials sdmCredentials, boolean isSystemUser) throws IOException; - public String checkRepositoryType(String repositoryId, String tenant) throws IOException; + public RepoValue checkRepositoryType(String repositoryId, String tenant) throws IOException; public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) throws IOException; - public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) throws IOException; - public int deleteDocument(String cmisaction, String objectId, String user) throws IOException; public void readDocument( diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java index e1a63aec..ac02336a 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java @@ -5,20 +5,17 @@ import com.sap.cds.Result; import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext; -import com.sap.cds.sdm.caching.CacheConfig; -import com.sap.cds.sdm.caching.RepoKey; -import com.sap.cds.sdm.caching.SecondaryPropertiesKey; -import com.sap.cds.sdm.caching.SecondaryTypesKey; +import com.sap.cds.sdm.caching.*; import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; import com.sap.cds.sdm.model.CmisDocument; +import com.sap.cds.sdm.model.RepoValue; import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.utilities.SDMUtils; import com.sap.cds.services.ServiceException; import com.sap.cds.services.environment.CdsProperties; import com.sap.cds.services.persistence.PersistenceService; import com.sap.cloud.environment.servicebinding.api.ServiceBinding; -import com.sap.cloud.sdk.cloudplatform.connectivity.*; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -480,33 +477,23 @@ else if (responseCode == 403) { } @Override - public String checkRepositoryType(String repositoryId, String tenant) { + public RepoValue checkRepositoryType(String repositoryId, String tenant) { RepoKey repoKey = new RepoKey(); repoKey.setSubdomain(tenant); repoKey.setRepoId(repositoryId); - String type = CacheConfig.getVersionedRepoCache().get(repoKey); - Boolean isVersioned; - if (type == null) { + RepoValue repoValue = CacheConfig.getRepoCache().get(repoKey); + if (repoValue == null) { SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials(); JSONObject repoInfo = getRepositoryInfo(sdmCredentials); - isVersioned = isRepositoryVersioned(repoInfo, repositoryId); - } else { - isVersioned = "Versioned".equals(type); - } - - if (Boolean.TRUE.equals(isVersioned)) { - repoKey = new RepoKey(); - repoKey.setSubdomain(tenant); - repoKey.setRepoId(repositoryId); - CacheConfig.getVersionedRepoCache().put(repoKey, "Versioned"); - return "Versioned"; - } else { + Map repoValueMap = fetchRepositoryData(repoInfo, repositoryId); repoKey = new RepoKey(); repoKey.setSubdomain(tenant); repoKey.setRepoId(repositoryId); - CacheConfig.getVersionedRepoCache().put(repoKey, "Non Versioned"); - return "Non Versioned"; + RepoValue value = repoValueMap.get(repositoryId); + CacheConfig.getRepoCache().put(repoKey, value); + return repoValueMap.get(repositoryId); } + return repoValue; } public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) { @@ -526,17 +513,28 @@ public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) { } } - public Boolean isRepositoryVersioned(JSONObject repoInfo, String repositoryId) { + public Map fetchRepositoryData(JSONObject repoInfo, String repositoryId) { + Map repoValueMap = new HashMap<>(); repoInfo = repoInfo.getJSONObject(repositoryId); JSONObject capabilities = repoInfo.getJSONObject("capabilities"); String type = capabilities.getString("capabilityContentStreamUpdatability"); - if ("pwconly".equals(type)) { - type = "Versioned"; - } else { - type = "Non Versioned"; + RepoValue repoValue = new RepoValue(); + repoValue.setVersionEnabled("pwconly".equals(type) ? true : false); + JSONArray extendedFeaturesArray = repoInfo.getJSONArray("extendedFeatures"); + // Iterate over the array and find the object with featureData + for (int i = 0; i < extendedFeaturesArray.length(); i++) { + JSONObject feature = extendedFeaturesArray.getJSONObject(i); + if (feature.has("featureData")) { + JSONObject featureData = feature.getJSONObject("featureData"); + // Fetch the 'virusScanner' value + repoValue.setVirusScanEnabled(featureData.getBoolean("virusScanner")); + // Fetch the disableVirusScannerForLargeFile + repoValue.setDisableVirusScannerForLargeFile( + featureData.getBoolean("disableVirusScannerForLargeFile")); + } } - - return "Versioned".equals(type); + repoValueMap.put(repositoryId, repoValue); + return repoValueMap; } @Override diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java index bcc3ee6a..c5c95242 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java @@ -1,7 +1,5 @@ package com.sap.cds.sdm.service.handler; -import static com.sap.cds.sdm.persistence.DBQuery.*; - import com.sap.cds.Result; import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.MediaData; import com.sap.cds.feature.attachments.service.AttachmentService; @@ -13,6 +11,7 @@ import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; import com.sap.cds.sdm.model.CmisDocument; +import com.sap.cds.sdm.model.RepoValue; import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.DocumentUploadService; @@ -143,15 +142,24 @@ private boolean isObjectIdPresent(List documents, String objectId) private void validateRepository(AttachmentCreateEventContext eventContext) throws ServiceException, IOException { String repositoryId = SDMConstants.REPOSITORY_ID; - String repocheck = + RepoValue repoValue = sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant()); - if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) { + if (repoValue.getVersionEnabled()) { throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR); } + String len = eventContext.getParameterInfo().getHeaders().get("content-length"); + long contentLen = !StringUtils.isEmpty(len) ? Long.parseLong(len) : -1; + // Check if repository is virus scanned + if (repoValue.getVirusScanEnabled() + && contentLen > 400 * 1024 * 1024 + && !repoValue.getDisableVirusScannerForLargeFile()) { + throw new ServiceException(SDMConstants.VIRUS_REPO_ERROR_MORE_THAN_400MB); + } } private void processEntities(AttachmentCreateEventContext eventContext) throws ServiceException, IOException { + Map attachmentIds = eventContext.getAttachmentIds(); CdsEntity attachmentDraftEntity = getAttachmentDraftEntity(eventContext); String upIdKey = getUpIdKey(attachmentDraftEntity); @@ -297,6 +305,7 @@ private void handleCreateDocumentResult( throw new ServiceException(SDMConstants.MIMETYPE_INVALID_ERROR); default: cmisDocument.setObjectId(createResult.get("objectId").toString()); + cmisDocument.setMimeType(createResult.get("mimeType").toString()); dbQuery.addAttachmentToDraft( getAttachmentDraftEntity(eventContext), persistenceService, cmisDocument); finalizeContext(eventContext, cmisDocument); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java index 3452c11d..cdd7ca2c 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java @@ -12,10 +12,7 @@ import com.sap.cds.reflect.CdsModel; import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; -import com.sap.cds.sdm.model.AttachmentReadContext; -import com.sap.cds.sdm.model.CmisDocument; -import com.sap.cds.sdm.model.CopyAttachmentInput; -import com.sap.cds.sdm.model.SDMCredentials; +import com.sap.cds.sdm.model.*; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.DocumentUploadService; import com.sap.cds.sdm.service.RegisterService; @@ -118,9 +115,9 @@ public void openAttachment(AttachmentReadContext context) throws Exception { private void validateRepository(EventContext eventContext) throws ServiceException, IOException { String repositoryId = SDMConstants.REPOSITORY_ID; - String repocheck = + RepoValue repoValue = sdmService.checkRepositoryType(repositoryId, eventContext.getUserInfo().getTenant()); - if (SDMConstants.REPOSITORY_VERSIONED.equals(repocheck)) { + if (repoValue.getVersionEnabled()) { throw new ServiceException(SDMConstants.VERSIONED_REPO_ERROR); } } diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/SDMServiceImplTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/SDMServiceImplTest.java index ecb32783..6c0003de 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/SDMServiceImplTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/SDMServiceImplTest.java @@ -19,6 +19,7 @@ import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; import com.sap.cds.sdm.model.CmisDocument; +import com.sap.cds.sdm.model.RepoValue; import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.service.*; import com.sap.cds.services.ServiceException; @@ -42,6 +43,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.ehcache.Cache; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -87,40 +89,6 @@ public void setUp() { repoKey.setSubdomain("tenant"); } - @Test - public void testIsRepositoryVersioned_Versioned() throws IOException { - // Mocked JSON structure for a versioned repository - JSONObject capabilities = new JSONObject(); - capabilities.put("capabilityContentStreamUpdatability", "pwconly"); - - JSONObject repoInfo = new JSONObject(); - repoInfo.put("capabilities", capabilities); - - JSONObject root = new JSONObject(); - root.put(REPO_ID, repoInfo); - - // Call the method and verify the result - boolean isVersioned = SDMService.isRepositoryVersioned(root, REPO_ID); - assertTrue(isVersioned); - } - - @Test - public void testIsRepositoryVersioned_NonVersioned() throws IOException { - // Mocked JSON structure for a non-versioned repository - JSONObject capabilities = new JSONObject(); - capabilities.put("capabilityContentStreamUpdatability", "other"); - - JSONObject repoInfo = new JSONObject(); - repoInfo.put("capabilities", capabilities); - - JSONObject root = new JSONObject(); - root.put(REPO_ID, repoInfo); - - // Call the method and verify the result - boolean isVersioned = SDMService.isRepositoryVersioned(root, REPO_ID); - assertFalse(isVersioned); - } - @Test public void testGetRepositoryInfo() throws IOException { JSONObject capabilities = new JSONObject(); @@ -194,42 +162,6 @@ public void testGetRepositoryInfoThrowsServiceExceptionOnHttpClientError() throw assertEquals(SDMConstants.REPOSITORY_ERROR, exception.getMessage()); } - // @Test - // public void testCheckRepositoryTypeCacheVersioned() throws IOException { - // String repositoryId = "repo"; - // String token = "token"; - // try (MockedStatic cacheConfigMockedStatic = - // Mockito.mockStatic(CacheConfig.class)) { - // Cache mockCache = Mockito.mock(Cache.class); - // // when(tokenHandler.getTokenFields(token)).thenReturn(expected); - // when(tokenHandler.getHttpClient(any(), any(), any(), eq("TECHNICAL_CREDENTIALS_FLOW"))) - // .thenReturn(httpClient); - // when(tokenHandler.getSDMCredentials()) - // .thenReturn(new SDMCredentials("test", "test", "test", "test")); - // Mockito.when(mockCache.get(repoKey)).thenReturn("Versioned"); - // cacheConfigMockedStatic.when(CacheConfig::getVersionedRepoCache).thenReturn(mockCache); - // String result = SDMService.checkRepositoryType(token, repositoryId); - // assertEquals("Versioned", result); - // } - // } - - // @Test - // public void testCheckRepositoryTypeCacheNonVersioned() throws IOException { - // String repositoryId = "repo"; - // String token = "token"; - // try (MockedStatic cacheConfigMockedStatic = - // Mockito.mockStatic(CacheConfig.class)) { - // Cache mockCache = Mockito.mock(Cache.class); - // SDMCredentials mockSdmCredentials = new SDMCredentials(); - // mockSdmCredentials.setUrl("test"); - // // when(tokenHandler.getTokenFields(token)).thenReturn(expected); - // Mockito.when(mockCache.get(repoKey)).thenReturn("Non Versioned"); - // cacheConfigMockedStatic.when(CacheConfig::getVersionedRepoCache).thenReturn(mockCache); - // String result = SDMService.checkRepositoryType(token, repositoryId); - // assertEquals("Non Versioned", result); - // } - // } - @Test public void testCheckRepositoryTypeNoCacheVersioned() throws IOException { String repositoryId = "repo"; @@ -240,7 +172,7 @@ public void testCheckRepositoryTypeNoCacheVersioned() throws IOException { Mockito.mockStatic(CacheConfig.class)) { Cache mockCache = Mockito.mock(Cache.class); Mockito.when(mockCache.get(repoKey)).thenReturn(null); - cacheConfigMockedStatic.when(CacheConfig::getVersionedRepoCache).thenReturn(mockCache); + cacheConfigMockedStatic.when(CacheConfig::getRepoCache).thenReturn(mockCache); SDMCredentials mockSdmCredentials = new SDMCredentials(); mockSdmCredentials.setUrl("test"); when(tokenHandler.getHttpClient(any(), any(), any(), eq("TECHNICAL_CREDENTIALS_FLOW"))) @@ -260,15 +192,30 @@ public void testCheckRepositoryTypeNoCacheVersioned() throws IOException { capabilities.put( "capabilityContentStreamUpdatability", "pwconly"); // To match the expected output "Versioned" + JSONObject featureData = new JSONObject(); + featureData.put("virusScanner", "false"); + featureData.put("disableVirusScannerForLargeFile", "false"); + // Create a JSON object representing an 'extendedFeature' entry with 'featureData' + JSONObject extendedFeatureWithVirusScanner = new JSONObject(); + extendedFeatureWithVirusScanner.put("id", "ecmRepoInfo"); + extendedFeatureWithVirusScanner.put("featureData", featureData); + + // Create the array of 'extendedFeatures' + JSONArray extendedFeaturesArray = new JSONArray(); + extendedFeaturesArray.put(extendedFeatureWithVirusScanner); + + // Wrap the 'extendedFeatures' array in the main repoInfo object JSONObject repoInfo = new JSONObject(); + repoInfo.put("extendedFeatures", extendedFeaturesArray); repoInfo.put("capabilities", capabilities); JSONObject mockRepoData = new JSONObject(); mockRepoData.put(repositoryId, repoInfo); InputStream inputStream = new ByteArrayInputStream(mockRepoData.toString().getBytes()); when(entity.getContent()).thenReturn(inputStream); - String result = spySDMService.checkRepositoryType(repositoryId, tenant); - assertEquals("Versioned", result); + RepoValue repoValue = spySDMService.checkRepositoryType(repositoryId, tenant); + assertEquals(true, repoValue.getVersionEnabled()); + assertEquals(false, repoValue.getVirusScanEnabled()); } } @@ -280,9 +227,9 @@ public void testCheckRepositoryTypeNoCacheNonVersioned() throws IOException { Mockito.spy(new SDMServiceImpl(binding, connectionPool, tokenHandler)); try (MockedStatic cacheConfigMockedStatic = Mockito.mockStatic(CacheConfig.class)) { - Cache mockCache = Mockito.mock(Cache.class); + Cache mockCache = Mockito.mock(Cache.class); Mockito.when(mockCache.get(repoKey)).thenReturn(null); - cacheConfigMockedStatic.when(CacheConfig::getVersionedRepoCache).thenReturn(mockCache); + cacheConfigMockedStatic.when(CacheConfig::getRepoCache).thenReturn(mockCache); SDMCredentials mockSdmCredentials = new SDMCredentials(); mockSdmCredentials.setUrl("test"); when(tokenHandler.getHttpClient(any(), any(), any(), eq("TECHNICAL_CREDENTIALS_FLOW"))) @@ -302,7 +249,22 @@ public void testCheckRepositoryTypeNoCacheNonVersioned() throws IOException { capabilities.put( "capabilityContentStreamUpdatability", "notpwconly"); // To match the expected output "Versioned" + JSONObject featureData = new JSONObject(); + featureData.put("virusScanner", "false"); + featureData.put("disableVirusScannerForLargeFile", "false"); + + // Create a JSON object representing an 'extendedFeature' entry with 'featureData' + JSONObject extendedFeatureWithVirusScanner = new JSONObject(); + extendedFeatureWithVirusScanner.put("id", "ecmRepoInfo"); + extendedFeatureWithVirusScanner.put("featureData", featureData); + + // Create the array of 'extendedFeatures' + JSONArray extendedFeaturesArray = new JSONArray(); + extendedFeaturesArray.put(extendedFeatureWithVirusScanner); + + // Wrap the 'extendedFeatures' array in the main repoInfo object JSONObject repoInfo = new JSONObject(); + repoInfo.put("extendedFeatures", extendedFeaturesArray); repoInfo.put("capabilities", capabilities); JSONObject mockRepoData = new JSONObject(); mockRepoData.put(repositoryId, repoInfo); @@ -311,8 +273,34 @@ public void testCheckRepositoryTypeNoCacheNonVersioned() throws IOException { InputStream inputStream = new ByteArrayInputStream(mockRepoData.toString().getBytes()); when(entity.getContent()).thenReturn(inputStream); - String result = spySDMService.checkRepositoryType(repositoryId, tenant); - assertEquals("Non Versioned", result); + RepoValue repoValue = spySDMService.checkRepositoryType(repositoryId, tenant); + assertEquals(false, repoValue.getVersionEnabled()); + assertEquals(false, repoValue.getVirusScanEnabled()); + } + } + + @Test + public void testCheckRepositoryTypeCacheNonVersioned() throws IOException { + String repositoryId = "repo"; + String tenant = "tenant1"; + SDMServiceImpl spySDMService = + Mockito.spy(new SDMServiceImpl(binding, connectionPool, tokenHandler)); + try (MockedStatic cacheConfigMockedStatic = + Mockito.mockStatic(CacheConfig.class)) { + RepoKey repoKey = new RepoKey(); + repoKey.setSubdomain(tenant); + repoKey.setRepoId(repositoryId); + Cache mockCache = Mockito.mock(Cache.class); + RepoValue repoValue = new RepoValue(); + repoValue.setVersionEnabled(false); + repoValue.setVirusScanEnabled(false); + repoValue.setDisableVirusScannerForLargeFile(false); + Mockito.when(mockCache.get(repoKey)).thenReturn(repoValue); + cacheConfigMockedStatic.when(CacheConfig::getRepoCache).thenReturn(mockCache); + repoValue = spySDMService.checkRepositoryType(repositoryId, tenant); + assertEquals(false, repoValue.getVersionEnabled()); + assertEquals(false, repoValue.getVirusScanEnabled()); + assertEquals(false, repoValue.getDisableVirusScannerForLargeFile()); } } diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java index bd0bfc66..00a67ddc 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java @@ -28,6 +28,7 @@ import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; import com.sap.cds.sdm.model.CmisDocument; +import com.sap.cds.sdm.model.RepoValue; import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.DocumentUploadService; @@ -128,7 +129,10 @@ public void testCreateVersioned() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(sdmService.checkRepositoryType(anyString(), any())).thenReturn("Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(true); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); when(mockContext.getMessages()).thenReturn(mockMessages); when(mockMessages.error("Upload not supported for versioned repositories.")) .thenReturn(mockMessage); @@ -156,8 +160,9 @@ public void testCreateVersioned() throws IOException { when(mockContext.getParameterInfo()).thenReturn(mockParameterInfo); // Mock getParameterInfo when(mockParameterInfo.getHeaders()).thenReturn(mockHeaders); // Mock getHeaders - - when(sdmService.checkRepositoryType(anyString(), any())).thenReturn("Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVersionEnabled(true); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); when(mockContext.getMessages()).thenReturn(mockMessages); when(mockMessages.error("Upload not supported for versioned repositories.")) .thenReturn(mockMessage); @@ -178,6 +183,46 @@ public void testCreateVersioned() throws IOException { assertEquals("Upload not supported for versioned repositories.", thrown.getMessage()); } + @Test + public void testCreateVirusEnabled() throws IOException { + // Initialization of mocks and setup + Message mockMessage = mock(Message.class); + Messages mockMessages = mock(Messages.class); + MediaData mockMediaData = mock(MediaData.class); + CdsModel mockModel = mock(CdsModel.class); + try (MockedStatic sdmUtilsMockedStatic = mockStatic(SDMUtils.class); ) { + sdmUtilsMockedStatic + .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) + .thenReturn("0__null"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(true); + repoValue.setDisableVirusScannerForLargeFile(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); + when(mockContext.getMessages()).thenReturn(mockMessages); + when(mockMessages.error(SDMConstants.VIRUS_REPO_ERROR_MORE_THAN_400MB)) + .thenReturn(mockMessage); + when(mockContext.getData()).thenReturn(mockMediaData); + when(mockContext.getModel()).thenReturn(mockModel); + when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); + when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); + when(mockJwtTokenInfo.getToken()).thenReturn("mockedJwtToken"); + when(mockContext.getParameterInfo()).thenReturn(parameterInfo); + headers.put("content-length", "900000089999"); + when(parameterInfo.getHeaders()).thenReturn(headers); + // Use assertThrows to expect a ServiceException and validate the message + ServiceException thrown = + assertThrows( + ServiceException.class, + () -> { + handlerSpy.createAttachment(mockContext); + }); + + // Verify the exception message + assertEquals(SDMConstants.VIRUS_REPO_ERROR_MORE_THAN_400MB, thrown.getMessage()); + } + } + @Test public void testCreateNonVersionedDuplicate() throws IOException { // Initialization of mocks and setup @@ -209,7 +254,12 @@ public void testCreateNonVersionedDuplicate() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -276,7 +326,12 @@ public void testCreateNonVersionedDIDuplicate() throws IOException { when(mockAssocType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockContext.getData()).thenReturn(mockMediaData); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -354,7 +409,12 @@ public void testCreateNonVersionedDIVirus() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -438,7 +498,12 @@ public void testCreateNonVersionedDIOther() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -510,7 +575,12 @@ public void testCreateNonVersionedDIUnauthorized() throws IOException { when(mockAssociationElement.getType()).thenReturn(mockAssociationType); when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(mockContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t1"); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockContext.getData()).thenReturn(mockMediaData); when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); when(mockDraftEntity.getQualifiedName()).thenReturn("some.qualified.name"); @@ -574,7 +644,13 @@ public void testCreateNonVersionedDIBlocked() throws IOException { when(mockAssociationElement.getType()).thenReturn(mockAssociationType); when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + repoValue.setDisableVirusScannerForLargeFile(false); + when(mockContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t1"); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockContext.getData()).thenReturn(mockMediaData); when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); when(mockDraftEntity.getQualifiedName()).thenReturn("some.qualified.name"); @@ -632,7 +708,85 @@ public void testCreateNonVersionedDISuccess() throws IOException { mockCreateResult.put("url", "url"); mockCreateResult.put("name", "sample.pdf"); mockCreateResult.put("objectId", "objectId"); + mockCreateResult.put("mimeType", "application/pdf"); + + when(mockMediaData.getFileName()).thenReturn("sample.pdf"); + when(mockMediaData.getContent()).thenReturn(contentStream); + when(mockContext.getModel()).thenReturn(cdsModel); + when(cdsModel.findEntity(anyString())).thenReturn(Optional.of(mockEntity)); + when(mockEntity.findAssociation("up_")).thenReturn(Optional.of(mockAssociationElement)); + when(mockAssociationElement.getType()).thenReturn(mockAssociationType); + when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); + when(mockCqnElementRef.path()).thenReturn("ID"); + when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); + when(mockResult.list()).thenReturn(nonEmptyRowList); + when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); + when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); + when(mockJwtTokenInfo.getToken()).thenReturn("mockedJwtToken"); + when(mockContext.getData()).thenReturn(mockMediaData); + doReturn(false).when(handlerSpy).duplicateCheck(any(), any(), any()); + when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderid"); + JSONObject mockResponse = new JSONObject(); + mockResponse.put("status", "success"); + mockResponse.put("objectId", "123"); + + // Mock the behavior of createDocumentRx to return the mock response wrapped in a Single + when(documentUploadService.createDocument(any(), any(), anyBoolean())) + .thenReturn(mockCreateResult); + ParameterInfo mockParameterInfo = mock(ParameterInfo.class); + Map mockHeaders = new HashMap<>(); + mockHeaders.put("content-length", "12345"); + + when(mockContext.getParameterInfo()).thenReturn(mockParameterInfo); // Mock getParameterInfo + when(mockParameterInfo.getHeaders()).thenReturn(mockHeaders); // Mock getHeaders + try (MockedStatic sdmUtilsMockedStatic = mockStatic(SDMUtils.class); ) { + sdmUtilsMockedStatic + .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) + .thenReturn("0__null"); + when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) + .thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + .thenReturn(mockResult); + SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); + when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); + when(mockDraftEntity.getQualifiedName()).thenReturn("some.qualified.name"); + + when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); + handlerSpy.createAttachment(mockContext); + verifyNoInteractions(mockMessages); + } + } + @Test + public void testCreateVirusEnabledDisableLargeFileDISuccess() throws IOException { + // Initialization of mocks and setup + Map mockAttachmentIds = new HashMap<>(); + mockAttachmentIds.put("up__ID", "upid"); + mockAttachmentIds.put("ID", "id"); + mockAttachmentIds.put("repositoryId", "repo1"); + MediaData mockMediaData = mock(MediaData.class); + Result mockResult = mock(Result.class); + Row mockRow = mock(Row.class); + List nonEmptyRowList = List.of(mockRow); + CdsEntity mockEntity = mock(CdsEntity.class); + CdsEntity mockDraftEntity = mock(CdsEntity.class); + CdsElement mockAssociationElement = mock(CdsElement.class); + CdsAssociationType mockAssociationType = mock(CdsAssociationType.class); + CqnElementRef mockCqnElementRef = mock(CqnElementRef.class); + byte[] byteArray = "Example content".getBytes(); + InputStream contentStream = new ByteArrayInputStream(byteArray); + JSONObject mockCreateResult = new JSONObject(); + mockCreateResult.put("status", "success"); + mockCreateResult.put("url", "url"); + mockCreateResult.put("name", "sample.pdf"); + mockCreateResult.put("objectId", "objectId"); + mockCreateResult.put("mimeType", "application/pdf"); when(mockMediaData.getFileName()).thenReturn("sample.pdf"); when(mockMediaData.getContent()).thenReturn(contentStream); when(mockContext.getModel()).thenReturn(cdsModel); @@ -642,7 +796,13 @@ public void testCreateNonVersionedDISuccess() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(true); + repoValue.setVersionEnabled(false); + repoValue.setDisableVirusScannerForLargeFile(true); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -653,6 +813,7 @@ public void testCreateNonVersionedDISuccess() throws IOException { JSONObject mockResponse = new JSONObject(); mockResponse.put("status", "success"); mockResponse.put("objectId", "123"); + mockResponse.put("mimeType", "application/pdf"); // Mock the behavior of createDocumentRx to return the mock response wrapped in a Single when(documentUploadService.createDocument(any(), any(), anyBoolean())) .thenReturn(mockCreateResult); @@ -706,7 +867,10 @@ public void testCreateNonVersionedNoUpAssociation() throws IOException { when(cdsModel.findEntity(anyString())).thenReturn(Optional.of(mockDraftEntity)); when(mockDraftEntity.findAssociation("up_")).thenReturn(Optional.empty()); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -761,7 +925,10 @@ public void testCreateNonVersionedEmptyResultList() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(emptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -810,7 +977,12 @@ public void testCreateNonVersionedNameConstraint() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); @@ -1011,9 +1183,10 @@ public void testReadAttachment_NotVersionedRepository() throws IOException { when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); when(mockJwtTokenInfo.getToken()).thenReturn("dummyToken"); when(mockReadContext.getContentId()).thenReturn("objectId:part2"); - - when(sdmService.checkRepositoryType(SDMConstants.REPOSITORY_ID, token)) - .thenReturn("NotVersioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(SDMConstants.REPOSITORY_ID, token)).thenReturn(repoValue); SDMCredentials mockSdmCredentials = new SDMCredentials(); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); @@ -1029,8 +1202,10 @@ public void testReadAttachment_FailureInReadDocument() throws IOException { when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); when(mockJwtTokenInfo.getToken()).thenReturn("dummyToken"); when(mockReadContext.getContentId()).thenReturn("objectId:part2"); - when(sdmService.checkRepositoryType(SDMConstants.REPOSITORY_ID, token)) - .thenReturn("NotVersioned"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(SDMConstants.REPOSITORY_ID, token)).thenReturn(repoValue); SDMCredentials mockSdmCredentials = new SDMCredentials(); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); doThrow(new ServiceException(SDMConstants.FILE_NOT_FOUND_ERROR)) @@ -1080,7 +1255,12 @@ public void testMaxCountErrorMessage() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockResult.rowCount()).thenReturn(3L); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); @@ -1144,7 +1324,12 @@ public void testMaxCountError() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockResult.list()).thenReturn(nonEmptyRowList); when(mockResult.rowCount()).thenReturn(3L); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); @@ -1186,7 +1371,12 @@ public void testMaxCountError() throws IOException { @Test public void throwAttachmetDraftEntityException() throws IOException { - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("Non Versioned"); + when(eventContext.getUserInfo()).thenReturn(userInfo); + when(userInfo.getTenant()).thenReturn("t123"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(mockContext.getModel()).thenReturn(cdsModel); when(mockContext.getAuthenticationInfo()).thenReturn(mockAuthInfo); when(mockAuthInfo.as(JwtTokenAuthenticationInfo.class)).thenReturn(mockJwtTokenInfo); diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java index 73a0f9c9..177e3b6c 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java @@ -18,10 +18,7 @@ import com.sap.cds.reflect.CdsModel; import com.sap.cds.sdm.constants.SDMConstants; import com.sap.cds.sdm.handler.TokenHandler; -import com.sap.cds.sdm.model.AttachmentReadContext; -import com.sap.cds.sdm.model.CmisDocument; -import com.sap.cds.sdm.model.CopyAttachmentInput; -import com.sap.cds.sdm.model.SDMCredentials; +import com.sap.cds.sdm.model.*; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.DocumentUploadService; import com.sap.cds.sdm.service.RegisterService; @@ -186,8 +183,10 @@ void testCreate_shouldCreateLink() throws IOException { .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderId123"); SDMCredentials sdmCredentials = new SDMCredentials(); @@ -220,9 +219,10 @@ void testCreate_ThrowsServiceException_WhenVersionedRepo() throws IOException { when(mockContext.getModel()).thenReturn(cdsModel); when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("tenant1"); - - when(sdmService.checkRepositoryType(anyString(), anyString())) - .thenReturn(SDMConstants.REPOSITORY_VERSIONED); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(true); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); // Act & Assert ServiceException ex = @@ -276,14 +276,14 @@ void testCreate_ShouldThrowSpecifiedExceptionWhenMaxCountReached() throws IOExce .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("2__Maximum two links allowed"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); // Act & Assert ServiceException ex = assertThrows(ServiceException.class, () -> sdmServiceGenericHandler.create(mockContext)); assertEquals("Maximum two links allowed", ex.getMessage()); - - // Assert - verify(sdmService).checkRepositoryType(anyString(), anyString()); } @Test @@ -332,14 +332,14 @@ void testCreate_ShouldThrowDefaultExceptionWhenMaxCountReached() throws IOExcept .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("2__"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); // Act & Assert ServiceException ex = assertThrows(ServiceException.class, () -> sdmServiceGenericHandler.create(mockContext)); assertEquals(String.format(SDMConstants.MAX_COUNT_ERROR_MESSAGE, 2), ex.getMessage()); - - // Assert - verify(sdmService).checkRepositoryType(anyString(), anyString()); } @Test @@ -388,16 +388,16 @@ void testCreate_ShouldThrowExceptionWhenRestrictedCharacterInLinkName() throws I .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(true); - + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), any())).thenReturn(repoValue); // Act & Assert ServiceException ex = assertThrows(ServiceException.class, () -> sdmServiceGenericHandler.create(mockContext)); assertEquals( SDMConstants.linkNameConstraintMessage(Collections.singletonList("test/URL"), "created"), ex.getMessage()); - - // Assert - verify(sdmService).checkRepositoryType(anyString(), anyString()); } @Test @@ -449,8 +449,10 @@ void testCreate_ThrowsServiceExceptionOnDuplicateFile() throws IOException { .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); // Act & Assert ServiceException ex = @@ -503,8 +505,10 @@ void testCreate_ThrowsServiceException_WhenCreateDocumentThrowsException() throw .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderId123"); SDMCredentials sdmCredentials = new SDMCredentials(); @@ -568,8 +572,10 @@ void testCreate_ThrowsServiceExceptionOnDuplicateStatus() throws IOException { .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderId123"); SDMCredentials sdmCredentials = new SDMCredentials(); @@ -634,8 +640,10 @@ void testCreate_ThrowsServiceExceptionOnFailStatus() throws IOException { .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderId123"); SDMCredentials sdmCredentials = new SDMCredentials(); @@ -699,8 +707,10 @@ void testCreate_ThrowsServiceExceptionOnUnauthorizedStatus() throws IOException .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); sdmUtilsMock.when(() -> SDMUtils.isRestrictedCharactersInName(anyString())).thenReturn(false); - - when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn("NON_VERSIONED"); + RepoValue repoValue = new RepoValue(); + repoValue.setVirusScanEnabled(false); + repoValue.setVersionEnabled(false); + when(sdmService.checkRepositoryType(anyString(), anyString())).thenReturn(repoValue); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderId123"); SDMCredentials sdmCredentials = new SDMCredentials();