use of com.epam.pipeline.entity.templates.FolderTemplate in project cloud-pipeline by epam.
the class FolderTemplateManagerTest method createFolderFromTemplateTest.
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
@WithMockUser(username = TEST_USER)
public void createFolderFromTemplateTest() throws IOException {
Map<String, PipeConfValue> metadata = new HashMap<>();
metadata.put(DATA_KEY_1, new PipeConfValue(DATA_TYPE_1, DATA_VALUE_1));
DataStorageWithMetadataVO dataStorageVO = new DataStorageWithMetadataVO();
dataStorageVO.setName(DATASTORAGE_NAME_1);
dataStorageVO.setType(DataStorageType.S3);
dataStorageVO.setPath(TEST_PATH);
dataStorageVO.setMetadata(metadata);
PermissionVO permissionVO = new PermissionVO();
permissionVO.setMask(AclPermission.READ.getMask());
permissionVO.setUserName(TEST_ROLE);
permissionVO.setPrincipal(false);
FolderTemplate childFolderTemplate1 = FolderTemplate.builder().name(CHILD_TEMPLATE_FOLDER_NAME_1).build();
FolderTemplate folderTemplate = FolderTemplate.builder().name(TEMPLATE_FOLDER_NAME).datastorages(Stream.of(dataStorageVO).collect(Collectors.toList())).children(Stream.of(childFolderTemplate1).collect(Collectors.toList())).metadata(metadata).permissions(Stream.of(permissionVO).collect(Collectors.toList())).build();
Folder folder = new Folder();
folder.setName(TEMPLATE_FOLDER_NAME);
folderTemplateManager.createFolderFromTemplate(folder, folderTemplate);
Folder savedRootFolder = folderManager.loadByNameOrId(TEMPLATE_FOLDER_NAME);
savedRootFolder = folderManager.load(savedRootFolder.getId());
Assert.assertNotNull(savedRootFolder);
Long rootFolderId = savedRootFolder.getId();
List<EntityVO> metadataEntries = Collections.singletonList(new EntityVO(rootFolderId, AclClass.FOLDER));
Assert.assertEquals(metadata, metadataManager.listMetadataItems(metadataEntries).get(0).getData());
AbstractDataStorage clonedDataStorage = savedRootFolder.getStorages().get(0);
clonedDataStorage = dataStorageManager.load(clonedDataStorage.getId());
Assert.assertTrue(clonedDataStorage.getName().startsWith(DATASTORAGE_NAME_1));
Assert.assertTrue(clonedDataStorage.getPath().startsWith(TEST_PATH));
metadataEntries = Collections.singletonList(new EntityVO(clonedDataStorage.getId(), AclClass.DATA_STORAGE));
Assert.assertEquals(metadata, metadataManager.listMetadataItems(metadataEntries).get(0).getData());
List<AclPermissionEntry> rootFolderPermissions = permissionManager.getPermissions(rootFolderId, AclClass.FOLDER).getPermissions();
Assert.assertEquals(1, rootFolderPermissions.size());
AclPermissionEntry actualPermission = rootFolderPermissions.get(0);
Assert.assertEquals(permissionVO.getMask(), actualPermission.getMask());
Assert.assertEquals(permissionVO.getPrincipal(), actualPermission.getSid().isPrincipal());
Assert.assertEquals(permissionVO.getUserName(), actualPermission.getSid().getName());
Folder savedChildFolder = folderManager.loadByNameOrId(TEMPLATE_FOLDER_NAME + "/" + CHILD_TEMPLATE_FOLDER_NAME_1);
Assert.assertNotNull(savedChildFolder);
Assert.assertEquals(rootFolderId, savedChildFolder.getParentId());
}
use of com.epam.pipeline.entity.templates.FolderTemplate in project cloud-pipeline by epam.
the class FolderTemplateManager method parseTemplateJson.
private FolderTemplate parseTemplateJson(String templateDirPath, String name) {
File templateJson = new File(templateDirPath, TEMPLATE_FILE_NAME);
try (FileReader fileReader = new FileReader(templateJson)) {
String json = IOUtils.toString(fileReader);
json = json.replaceAll(REPLACE_MARK, name);
FolderTemplate template = JsonMapper.parseData(json, new TypeReference<FolderTemplate>() {
});
Assert.notNull(template, messageHelper.getMessage(MessageConstants.ERROR_FOLDER_INVALID_TEMPLATE, TEMPLATE_FILE_NAME));
return template;
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
use of com.epam.pipeline.entity.templates.FolderTemplate in project cloud-pipeline by epam.
the class FolderTemplateManager method create.
@Transactional(propagation = Propagation.REQUIRED)
public Folder create(final Folder folder, final String templateName) {
TemplatesScanner templatesScanner = new TemplatesScanner(folderTemplatesDirectoryPath);
Template template = templatesScanner.listTemplates().get(templateName);
Assert.notNull(template, messageHelper.getMessage(MessageConstants.ERROR_FOLDER_TEMPLATE_NOT_FOUND, templateName));
Assert.isTrue(StringUtils.hasText(folder.getName()), messageHelper.getMessage(MessageConstants.ERROR_FOLDER_NAME_IS_EMPTY));
FolderTemplate folderTemplate = parseTemplateJson(template.getDirPath(), folder.getName());
Assert.isTrue(StringUtils.hasText(folderTemplate.getName()), messageHelper.getMessage(MessageConstants.ERROR_TEMPLATE_FOLDER_NAME_IS_EMPTY, templateName));
Assert.isNull(folderDao.loadFolderByNameAndParentId(folderTemplate.getName(), folder.getParentId()), messageHelper.getMessage(MessageConstants.ERROR_FOLDER_NAME_EXISTS, folderTemplate.getName(), folder.getParentId()));
prepareTemplate(folderTemplate, folder.getName());
createFolderFromTemplate(folder, folderTemplate);
return folder;
}
Aggregations