Search in sources :

Example 1 with TemplateMetadataWrapper

use of com.thinkbiganalytics.repository.api.TemplateMetadataWrapper in project kylo by Teradata.

the class FilesystemRepositoryService method jsonToMetadata.

private TemplateMetadataWrapper jsonToMetadata(Path path, TemplateRepository repository, Optional<Set<String>> registeredTemplates) {
    try {
        TemplateMetadata templateMetaData = mapper.readValue(path.toFile(), TemplateMetadata.class);
        TemplateMetadataWrapper wrapper = new TemplateMetadataWrapper(templateMetaData);
        Path templatePath = Paths.get(repository.getLocation() + "/" + templateMetaData.getFileName());
        if (registeredTemplates.isPresent() && registeredTemplates.get().contains(templateMetaData.getTemplateName())) {
            byte[] content = ImportUtil.streamToByteArray(new FileInputStream(templatePath.toFile()));
            InputStream inputStream = new ByteArrayInputStream(content);
            ImportTemplate importTemplate = ImportUtil.openZip(templatePath.getFileName().toString(), inputStream);
            RegisteredTemplate template = importTemplate.getTemplateToImport();
            templateUpdateInfoCache.get(templateMetaData.getTemplateName(), () -> templateMetaData.isUpdateAvailable());
            // set updated flag if file is modified.
            if (templateMetaData.getLastModified() < template.getUpdateDate().getTime()) {
                String checksum = DigestUtils.md5DigestAsHex(content);
                // capturing checksum first time or it has actually changed?
                boolean templateModified = !StringUtils.equals(templateMetaData.getChecksum(), checksum);
                templateMetaData.setUpdateAvailable(templateModified);
                templateUpdateInfoCache.put(templateMetaData.getTemplateName(), templateModified);
                mapper.writerWithDefaultPrettyPrinter().writeValue(path.toFile(), templateMetaData);
                wrapper = new TemplateMetadataWrapper(templateMetaData);
            }
            if (wrapper.isUpdateAvailable()) {
                wrapper.getUpdates().addAll(template.getChangeComments());
            }
            wrapper.setInstalled(true);
        }
        wrapper.setRepository(repository);
        return wrapper;
    } catch (Exception e) {
        log.error("Error reading metadata", e);
    }
    return null;
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) FileInputStream(java.io.FileInputStream)

Example 2 with TemplateMetadataWrapper

use of com.thinkbiganalytics.repository.api.TemplateMetadataWrapper in project kylo by Teradata.

the class FilesystemRepositoryService method publishTemplate.

@Override
public TemplateMetadataWrapper publishTemplate(String repositoryName, String repositoryType, String templateId, boolean overwrite) throws Exception {
    // export template
    ExportTemplate zipFile = templateExporter.exportTemplate(templateId);
    // get repository
    TemplateRepository repository = getRepositoryByNameAndType(repositoryName, repositoryType);
    // Check if template already exists
    Optional<TemplateMetadataWrapper> foundMetadataOpt = Optional.empty();
    for (TemplateMetadataWrapper item : listTemplatesByRepository(repository)) {
        if (item.getTemplateName().equals(zipFile.getTemplateName())) {
            foundMetadataOpt = Optional.of(item);
            break;
        }
    }
    if (foundMetadataOpt.isPresent()) {
        if (!overwrite) {
            throw new UnsupportedOperationException("Template with same name already exists.");
        }
        log.info("Overwriting template with same name.");
    }
    String digest = DigestUtils.md5DigestAsHex(zipFile.getFile());
    Path templatePath = Paths.get(repository.getLocation() + "/" + zipFile.getFileName());
    TemplateMetadata metadata = new TemplateMetadata(zipFile.getTemplateName(), zipFile.getDescription(), zipFile.getFileName().toString(), digest, zipFile.isStream(), false, templatePath.toFile().lastModified());
    // create repositoryItem
    TemplateMetadataWrapper templateMetadata = foundMetadataOpt.isPresent() ? foundMetadataOpt.get() : new TemplateMetadataWrapper(metadata);
    String baseName = FilenameUtils.getBaseName(templateMetadata.getFileName());
    // write file in first of templateLocations
    Files.write(templatePath, zipFile.getFile());
    log.info("Finished publishing template {} to repository {}.", templateMetadata.getTemplateName(), repository.getName());
    return new TemplateMetadataWrapper(metadata);
}
Also used : Path(java.nio.file.Path) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata)

Example 3 with TemplateMetadataWrapper

use of com.thinkbiganalytics.repository.api.TemplateMetadataWrapper in project kylo by Teradata.

the class FilesystemRepositoryServiceTest method testPublishTemplate_NewTemplateInRepo_NoOverwriteRequired.

@Test
public void testPublishTemplate_NewTemplateInRepo_NoOverwriteRequired() throws Exception {
    ExportTemplate template = new ExportTemplate("testFile", "testTemplate", "testDesc", false, new byte[0]);
    when(templateExporter.exportTemplate(anyString())).thenReturn(template);
    FilesystemRepositoryService spy = PowerMockito.spy(filesystemRepositoryService);
    doReturn(repository).when(spy, "getRepositoryByNameAndType", anyString(), anyString());
    doReturn(new ArrayList<TemplateMetadata>()).when(spy, "listTemplatesByRepository", repository);
    PowerMockito.when(FilenameUtils.getBaseName(anyString())).thenReturn("testName");
    Path pathMock = mock(Path.class);
    PowerMockito.when(Paths.get(anyString())).thenReturn(pathMock);
    Mockito.when(pathMock.toFile()).thenReturn(mock(File.class));
    Mockito.doNothing().when(mapper).writeValue(any(File.class), any());
    TemplateMetadataWrapper newTemplate = spy.publishTemplate("test", "test", "test", false);
    assertNotNull(newTemplate);
    assertTrue(newTemplate.getTemplateName().equals("testTemplate"));
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) File(java.io.File) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with TemplateMetadataWrapper

use of com.thinkbiganalytics.repository.api.TemplateMetadataWrapper in project kylo by Teradata.

the class FilesystemRepositoryServiceTest method testPublishTemplate_ExistingTemplate_OverwriteFalse.

@Test(expected = UnsupportedOperationException.class)
public void testPublishTemplate_ExistingTemplate_OverwriteFalse() throws Exception {
    ExportTemplate template = new ExportTemplate("testFile", "testTemplate", "testDesc", false, new byte[0]);
    TemplateMetadata existingTemplate = new TemplateMetadata("testTemplate", "testDesc", "xyz.zip", false);
    List<TemplateMetadataWrapper> templates = new ArrayList<>();
    templates.add(new TemplateMetadataWrapper(existingTemplate));
    when(templateExporter.exportTemplate(anyString())).thenReturn(template);
    FilesystemRepositoryService spy = PowerMockito.spy(filesystemRepositoryService);
    doReturn(repository).when(spy, "getRepositoryByNameAndType", anyString(), anyString());
    doReturn(templates).when(spy, "listTemplatesByRepository", repository);
    spy.publishTemplate("test", "test", "test", false);
    PowerMockito.verifyStatic(never());
    FilenameUtils.getBaseName(anyString());
}
Also used : TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ArrayList(java.util.ArrayList) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

TemplateMetadataWrapper (com.thinkbiganalytics.repository.api.TemplateMetadataWrapper)4 ExportTemplate (com.thinkbiganalytics.metadata.api.template.export.ExportTemplate)3 TemplateMetadata (com.thinkbiganalytics.repository.api.TemplateMetadata)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)1 ImportTemplate (com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate)1 TemplateRepository (com.thinkbiganalytics.repository.api.TemplateRepository)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1