Search in sources :

Example 1 with ResourceLogic

use of org.apache.syncope.core.logic.ResourceLogic in project syncope by apache.

the class LoggerITCase method customAuditAppender.

@Test
public void customAuditAppender() throws IOException, InterruptedException {
    try (InputStream propStream = getClass().getResourceAsStream("/core-test.properties")) {
        Properties props = new Properties();
        props.load(propStream);
        String auditFilePath = props.getProperty("test.log.dir") + File.separator + "audit_for_Master_file.log";
        String auditNoRewriteFilePath = props.getProperty("test.log.dir") + File.separator + "audit_for_Master_norewrite_file.log";
        // 1. Enable audit for resource update -> catched by FileRewriteAuditAppender
        AuditLoggerName auditLoggerResUpd = new AuditLoggerName(EventCategoryType.LOGIC, ResourceLogic.class.getSimpleName(), null, "update", AuditElements.Result.SUCCESS);
        LoggerTO loggerTOUpd = new LoggerTO();
        loggerTOUpd.setKey(auditLoggerResUpd.toLoggerName());
        loggerTOUpd.setLevel(LoggerLevel.DEBUG);
        loggerService.update(LoggerType.AUDIT, loggerTOUpd);
        // 2. Enable audit for connector update -> NOT catched by FileRewriteAuditAppender
        AuditLoggerName auditLoggerConnUpd = new AuditLoggerName(EventCategoryType.LOGIC, ConnectorLogic.class.getSimpleName(), null, "update", AuditElements.Result.SUCCESS);
        LoggerTO loggerTOConnUpd = new LoggerTO();
        loggerTOConnUpd.setKey(auditLoggerConnUpd.toLoggerName());
        loggerTOConnUpd.setLevel(LoggerLevel.DEBUG);
        loggerService.update(LoggerType.AUDIT, loggerTOConnUpd);
        // 3. check that resource update is transformed and logged onto an audit file.
        ResourceTO resource = resourceService.read(RESOURCE_NAME_CSV);
        assertNotNull(resource);
        resource.setPropagationPriority(100);
        resourceService.update(resource);
        ConnInstanceTO connector = connectorService.readByResource(RESOURCE_NAME_CSV, null);
        assertNotNull(connector);
        connector.setPoolConf(new ConnPoolConfTO());
        connectorService.update(connector);
        File auditTempFile = new File(auditFilePath);
        // check audit_for_Master_file.log, it should contain only a static message
        String auditLog = FileUtils.readFileToString(auditTempFile, Charset.defaultCharset());
        assertTrue(StringUtils.contains(auditLog, "DEBUG Master.syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]" + " - This is a static test message"));
        File auditNoRewriteTempFile = new File(auditNoRewriteFilePath);
        // check audit_for_Master_file.log, it should contain only a static message
        String auditLogNoRewrite = FileUtils.readFileToString(auditNoRewriteTempFile, Charset.defaultCharset());
        assertFalse(StringUtils.contains(auditLogNoRewrite, "DEBUG Master.syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]" + " - This is a static test message"));
        // clean audit_for_Master_file.log
        FileUtils.writeStringToFile(auditTempFile, StringUtils.EMPTY, Charset.defaultCharset());
        loggerService.delete(LoggerType.AUDIT, "syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]");
        resource = resourceService.read(RESOURCE_NAME_CSV);
        assertNotNull(resource);
        resource.setPropagationPriority(200);
        resourceService.update(resource);
        // check that nothing has been written to audit_for_Master_file.log
        assertTrue(StringUtils.isEmpty(FileUtils.readFileToString(auditTempFile, Charset.defaultCharset())));
    } catch (IOException e) {
        fail("Unable to read/write log files" + e.getMessage());
    }
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) ResourceLogic(org.apache.syncope.core.logic.ResourceLogic) ResourceTO(org.apache.syncope.common.lib.to.ResourceTO) InputStream(java.io.InputStream) ConnInstanceTO(org.apache.syncope.common.lib.to.ConnInstanceTO) ConnPoolConfTO(org.apache.syncope.common.lib.to.ConnPoolConfTO) IOException(java.io.IOException) Properties(java.util.Properties) ConnectorLogic(org.apache.syncope.core.logic.ConnectorLogic) File(java.io.File) AuditLoggerName(org.apache.syncope.common.lib.types.AuditLoggerName) Test(org.junit.jupiter.api.Test)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 LoggerTO (org.apache.syncope.common.lib.log.LoggerTO)1 ConnInstanceTO (org.apache.syncope.common.lib.to.ConnInstanceTO)1 ConnPoolConfTO (org.apache.syncope.common.lib.to.ConnPoolConfTO)1 ResourceTO (org.apache.syncope.common.lib.to.ResourceTO)1 AuditLoggerName (org.apache.syncope.common.lib.types.AuditLoggerName)1 ConnectorLogic (org.apache.syncope.core.logic.ConnectorLogic)1 ResourceLogic (org.apache.syncope.core.logic.ResourceLogic)1 Test (org.junit.jupiter.api.Test)1