LocaleImportHandler extends RepositoryFileImportFileHandler implements IPlatformImportHandler { @VisibleForTesting Properties loadPropertiesByXml( RepositoryFileImportBundle localeBundle ) { final Properties properties = new Properties(); RepositoryFile file = localeBundle.getFile(); if ( file == null ) { return properties; } String fileTitle = localeBundle.getFile().getName(); if ( ( LOCALE_FOLDER + XML_LOCALE_EXT ).equals( fileTitle ) ) { try { InputStream is = localeBundle.getInputStream(); is.reset(); Document document = getLocalBundleDocument( is ); XPath xPath = XPathFactory.newInstance().newXPath(); String name = xPath.compile( "/index/name" ).evaluate( document ); String desc = xPath.compile( "/index/description" ).evaluate( document ); if ( StringUtils.isNotBlank( name ) && !name.equals( VARIABLE_SYMBOL_FROM_INDEX + TITLE_PROPERTY_NAME ) ) { properties.put( TITLE_PROPERTY_NAME, name ); } if ( StringUtils.isNotBlank( desc ) && !desc.equals( VARIABLE_SYMBOL_FROM_INDEX + DESC_PROPERTY_NAME ) ) { properties.put( DESC_PROPERTY_NAME, desc ); } } catch ( Exception e ) { getLogger().error( e.getMessage() ); } } return properties; } LocaleImportHandler( List<IMimeType> mimeTypes, List<String> artifacts ); void importFile( IPlatformImportBundle bundle );  }
@Test public void loadPropertiesByXmlTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<index>" + "<name>the name</name>" + "<description>the description</description>" + "</index>"; when( repFileBundleMock.getInputStream() ).thenReturn( new ByteArrayInputStream( xml.getBytes() ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 2 ); }
@Test public void loadPropertiesByXmlReferenceToVariableInNameTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<index>" + "<name>%name</name>" + "<description>the description</description>" + "</index>"; when( repFileBundleMock.getInputStream() ).thenReturn( new ByteArrayInputStream( xml.getBytes() ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 1 ); }
@Test public void loadPropertiesByXmlReferenceToVariableInDescriptionTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<index>" + "<name>the name</name>" + "<description>%description</description>" + "</index>"; when( repFileBundleMock.getInputStream() ).thenReturn( new ByteArrayInputStream( xml.getBytes() ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 1 ); }
@Test public void loadPropertiesByXmlReferenceToVariablesTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<index>" + "<name>%name</name>" + "<description>%description</description>" + "</index>"; when( repFileBundleMock.getInputStream() ).thenReturn( new ByteArrayInputStream( xml.getBytes() ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 0 ); }
@Test public void loadPropertiesByXmlWrongFormatTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); IPlatformImporter platformImporterMock = mock( IPlatformImporter.class ); IRepositoryImportLogger logMock = mock( IRepositoryImportLogger.class ); mockStatic( PentahoSystem.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); when( PentahoSystem.get( IPlatformImporter.class ) ).thenReturn( platformImporterMock ); when( platformImporterMock.getRepositoryImportLogger() ).thenReturn( logMock ); when( logMock.hasLogger() ).thenReturn( true ); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<name>the name</name>" + "<description>the description</description>"; when( repFileBundleMock.getInputStream() ).thenReturn( new ByteArrayInputStream( xml.getBytes() ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 0 ); }
@Test public void loadPropertiesByXmlInputStreamExceptionTest() throws Exception { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); IPlatformImporter platformImporterMock = mock( IPlatformImporter.class ); IRepositoryImportLogger logMock = mock( IRepositoryImportLogger.class ); mockStatic( PentahoSystem.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "index.xml" ); when( PentahoSystem.get( IPlatformImporter.class ) ).thenReturn( platformImporterMock ); when( platformImporterMock.getRepositoryImportLogger() ).thenReturn( logMock ); when( logMock.hasLogger() ).thenReturn( true ); when( repFileBundleMock.getInputStream() ).thenThrow( new IOException( "" ) ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 0 ); }
@Test public void loadPropertiesByXmlWrongTypeTest() { RepositoryFileImportBundle repFileBundleMock = mock( RepositoryFileImportBundle.class ); RepositoryFile repFileMock = mock( RepositoryFile.class ); when( repFileBundleMock.getFile() ).thenReturn( repFileMock ); when( repFileMock.getName() ).thenReturn( "fileName" ); assertEquals( localeImportHandler.loadPropertiesByXml( repFileBundleMock ).size(), 0 ); }