Search in sources :

Example 1 with FolderTemplate

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());
}
Also used : HashMap(java.util.HashMap) FolderTemplate(com.epam.pipeline.entity.templates.FolderTemplate) Folder(com.epam.pipeline.entity.pipeline.Folder) EntityVO(com.epam.pipeline.controller.vo.EntityVO) AbstractDataStorage(com.epam.pipeline.entity.datastorage.AbstractDataStorage) PipeConfValue(com.epam.pipeline.entity.metadata.PipeConfValue) AclPermissionEntry(com.epam.pipeline.entity.security.acl.AclPermissionEntry) DataStorageWithMetadataVO(com.epam.pipeline.controller.vo.data.storage.DataStorageWithMetadataVO) PermissionVO(com.epam.pipeline.controller.vo.PermissionVO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with FolderTemplate

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);
    }
}
Also used : FileReader(java.io.FileReader) IOException(java.io.IOException) FolderTemplate(com.epam.pipeline.entity.templates.FolderTemplate) File(java.io.File)

Example 3 with FolderTemplate

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;
}
Also used : TemplatesScanner(com.epam.pipeline.manager.git.TemplatesScanner) FolderTemplate(com.epam.pipeline.entity.templates.FolderTemplate) Template(com.epam.pipeline.entity.template.Template) FolderTemplate(com.epam.pipeline.entity.templates.FolderTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

FolderTemplate (com.epam.pipeline.entity.templates.FolderTemplate)3 Transactional (org.springframework.transaction.annotation.Transactional)2 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)1 EntityVO (com.epam.pipeline.controller.vo.EntityVO)1 PermissionVO (com.epam.pipeline.controller.vo.PermissionVO)1 DataStorageWithMetadataVO (com.epam.pipeline.controller.vo.data.storage.DataStorageWithMetadataVO)1 AbstractDataStorage (com.epam.pipeline.entity.datastorage.AbstractDataStorage)1 PipeConfValue (com.epam.pipeline.entity.metadata.PipeConfValue)1 Folder (com.epam.pipeline.entity.pipeline.Folder)1 AclPermissionEntry (com.epam.pipeline.entity.security.acl.AclPermissionEntry)1 Template (com.epam.pipeline.entity.template.Template)1 TemplatesScanner (com.epam.pipeline.manager.git.TemplatesScanner)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1