Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.audit.destination;

import org.apache.ranger.audit.model.AuditEventBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @generated by copilot
* @description Unit Test cases for AuditDestination
* */
class AuditDestinationTest {
private TestAuditDestination destination;
private Properties props;

@BeforeEach
void setUp() {
destination = new TestAuditDestination();
props = new Properties();
props.setProperty("test.enabled", "true");
props.setProperty("test.prop1", "value1");
}

@Test
void testConstructor() {
// Simply verify constructor doesn't throw exception
assertNotNull(new TestAuditDestination());
}

@Test
void testEmptyMethods() {
// These methods have empty implementations, so just verify they don't throw exceptions
assertDoesNotThrow(() -> destination.start());
assertDoesNotThrow(() -> destination.stop());
assertDoesNotThrow(() -> destination.waitToComplete());
assertDoesNotThrow(() -> destination.waitToComplete(1000));
assertDoesNotThrow(() -> destination.flush());
}

// Concrete implementation for testing
private static class TestAuditDestination extends AuditDestination {
private boolean logCalled;
private int eventsLogged;

@Override
public boolean logJSON(String event) {
logCalled = true;
return true;
}

@Override
public boolean log(Collection<AuditEventBase> events) {
logCalled = true;
eventsLogged = events.size();
return true;
}

public boolean isLogCalled() {
return logCalled;
}

public int getEventsLogged() {
return eventsLogged;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.audit.destination;

import org.apache.ranger.audit.model.AuditEventBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @generated by copilot
* @description Unit Test cases for FileAuditDestination
* */
class FileAuditDestinationTest {
@TempDir
File tempDir;

@Mock
private AuditEventBase mockEvent1;

@Mock
private AuditEventBase mockEvent2;

private FileAuditDestination destination;
private Properties properties;
private final String propPrefix = "test";

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
destination = new FileAuditDestination();
properties = new Properties();

// Configure properties
properties.setProperty(propPrefix + "." + FileAuditDestination.PROP_FILE_LOCAL_DIR, tempDir.getAbsolutePath());
properties.setProperty(propPrefix + "." + FileAuditDestination.PROP_FILE_LOCAL_FILE_NAME_FORMAT, "test_audit.log");
properties.setProperty(propPrefix + "." + FileAuditDestination.PROP_FILE_FILE_ROLLOVER, "3600"); // 1 hour
}

@Test
void testInit() throws Exception {
destination.init(properties, propPrefix);

assertTrue(destination.initDone);

// Use reflection to access private fields
Field logFileNameFormatField = FileAuditDestination.class.getDeclaredField("logFileNameFormat");
logFileNameFormatField.setAccessible(true);
assertEquals("test_audit.log", logFileNameFormatField.get(destination));

assertEquals(3600, destination.fileRolloverSec);

Field logFolderField = FileAuditDestination.class.getDeclaredField("logFolder");
logFolderField.setAccessible(true);
File logFolder = (File) logFolderField.get(destination);
assertEquals(tempDir.getAbsolutePath(), logFolder.getAbsolutePath());
}

@Test
void testInitWithMissingDir() throws Exception {
Properties emptyProps = new Properties();
destination.init(emptyProps, propPrefix);

assertFalse(destination.initDone);

Field logFolderField = FileAuditDestination.class.getDeclaredField("logFolder");
logFolderField.setAccessible(true);
assertNull(logFolderField.get(destination));
}

@Test
void testInitWithDefaultFileFormat() throws Exception {
properties.remove(propPrefix + "." + FileAuditDestination.PROP_FILE_LOCAL_FILE_NAME_FORMAT);
destination.init(properties, propPrefix);

assertTrue(destination.initDone);

Field logFileNameFormatField = FileAuditDestination.class.getDeclaredField("logFileNameFormat");
logFileNameFormatField.setAccessible(true);
assertEquals("%app-type%_ranger_audit.log", logFileNameFormatField.get(destination));
}

@Test
void testCloseFileIfNeeded() throws Exception {
// Setup
destination.init(properties, propPrefix);

// First write to create the file
List<String> jsonEvents = Arrays.asList("{\"event\":\"test\"}");
destination.logJSON(jsonEvents);

// Get private fileCreateTime field
Field fileCreateTimeField = FileAuditDestination.class.getDeclaredField("fileCreateTime");
fileCreateTimeField.setAccessible(true);

// Set fileCreateTime to a time in the past that exceeds rollover period
Date oldTime = new Date(System.currentTimeMillis() - 4000 * 1000L); // 4000 seconds ago
fileCreateTimeField.set(destination, oldTime);

// Write again which should trigger file rollover
destination.logJSON(jsonEvents);

// Verify new file was created (should be 2 files now)
File[] files = tempDir.listFiles((dir, name) -> name.startsWith("test_audit"));
assertEquals(2, files.length);
}

@Test
void testStop() throws Exception {
// Setup
destination.init(properties, propPrefix);
List<String> jsonEvents = Arrays.asList("{\"event\":\"test\"}");
destination.logJSON(jsonEvents);

// Get private logWriter field to verify it exists before stop
Field logWriterField = FileAuditDestination.class.getDeclaredField("logWriter");
logWriterField.setAccessible(true);
assertNotNull(logWriterField.get(destination));

// Execute stop
destination.stop();

// Verify
Field isStoppedField = FileAuditDestination.class.getDeclaredField("isStopped");
isStoppedField.setAccessible(true);
assertTrue((boolean) isStoppedField.get(destination));

// logWriter should be null after stop
assertNull(logWriterField.get(destination));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.audit.model;

import org.junit.jupiter.api.Test;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @generated by copilot
* @description Unit Test cases for AuditIndexRecord
* */
class AuditIndexRecordTest {
@Test
void testDefaultValues() {
AuditIndexRecord record = new AuditIndexRecord();
assertNull(record.getId());
assertNull(record.getFilePath());
assertEquals(0, record.getLinePosition());
assertEquals(SPOOL_FILE_STATUS.write_inprogress, record.getStatus());
assertNull(record.getFileCreateTime());
assertNull(record.getWriteCompleteTime());
assertNull(record.getDoneCompleteTime());
assertNull(record.getLastSuccessTime());
assertNull(record.getLastFailedTime());
assertEquals(0, record.getFailedAttemptCount());
assertFalse(record.getLastAttempt());
}

@Test
void testSettersAndGetters() {
AuditIndexRecord record = new AuditIndexRecord();
String id = "abc123";
String filePath = "/tmp/file";
int linePosition = 42;
SPOOL_FILE_STATUS status = SPOOL_FILE_STATUS.done;
Date now = new Date();
Date later = new Date(now.getTime() + 1000);
Date muchLater = new Date(now.getTime() + 2000);

record.setId(id);
record.setFilePath(filePath);
record.setLinePosition(linePosition);
record.setStatus(status);
record.setFileCreateTime(now);
record.setWriteCompleteTime(later);
record.setDoneCompleteTime(muchLater);
record.setLastSuccessTime(later);
record.setLastFailedTime(muchLater);
record.setFailedAttemptCount(3);
record.setLastAttempt(true);

assertEquals(id, record.getId());
assertEquals(filePath, record.getFilePath());
assertEquals(linePosition, record.getLinePosition());
assertEquals(status, record.getStatus());
assertEquals(now, record.getFileCreateTime());
assertEquals(later, record.getWriteCompleteTime());
assertEquals(muchLater, record.getDoneCompleteTime());
assertEquals(later, record.getLastSuccessTime());
assertEquals(muchLater, record.getLastFailedTime());
assertEquals(3, record.getFailedAttemptCount());
assertTrue(record.getLastAttempt());
}

@Test
void testToString() {
AuditIndexRecord record = new AuditIndexRecord();
record.setId("id1");
record.setFilePath("/path/to/file");
record.setLinePosition(10);
record.setStatus(SPOOL_FILE_STATUS.done);
record.setFailedAttemptCount(2);
record.setLastAttempt(true);

String str = record.toString();
assertTrue(str.contains("AuditIndexRecord [id=id1"));
assertTrue(str.contains("filePath=/path/to/file"));
assertTrue(str.contains("linePosition=10"));
assertTrue(str.contains("status=done"));
assertTrue(str.contains("failedAttemptCount=2"));
assertTrue(str.contains("lastAttempt=true"));
}
}
Loading