Skip to content
Open
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
12 changes: 12 additions & 0 deletions agents-audit/dest-cloudwatch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
/*
* 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 com.amazonaws.services.logs.AWSLogs;
import com.amazonaws.services.logs.model.CreateLogStreamRequest;
import com.amazonaws.services.logs.model.InputLogEvent;
import com.amazonaws.services.logs.model.InvalidSequenceTokenException;
import com.amazonaws.services.logs.model.PutLogEventsRequest;
import com.amazonaws.services.logs.model.PutLogEventsResult;
import com.amazonaws.services.logs.model.ResourceNotFoundException;
import org.apache.ranger.audit.model.AuditEventBase;
import org.apache.ranger.audit.provider.MiscUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @generated by copilot
* @description Unit Test cases for AmazonCloudWatchAuditDestination
* */
class AmazonCloudWatchAuditDestinationTest {
@Mock
private AWSLogs mockLogsClient;

@Mock
private AuditEventBase mockAuditEvent;

private AmazonCloudWatchAuditDestination destination;
private PutLogEventsResult response;

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

// Create the destination object and spy on it
AmazonCloudWatchAuditDestination realDestination = new AmazonCloudWatchAuditDestination();
destination = spy(realDestination);

// Create PutLogEventsResponse directly using the builder
response = new PutLogEventsResult().withNextSequenceToken("nextToken");

// Use the created response object
when(mockLogsClient.putLogEvents(any(PutLogEventsRequest.class))).thenReturn(response);
when(mockAuditEvent.getEventTime()).thenReturn(new Date());

// Inject mock client using reflection
Field logsClientField = AmazonCloudWatchAuditDestination.class.getDeclaredField("logsClient");
logsClientField.setAccessible(true);
logsClientField.set(destination, mockLogsClient);
}

@Test
void testInit() throws Exception {
// Setup
Properties props = new Properties();
props.setProperty("test.log_group", "test-group");
props.setProperty("test.log_stream_prefix", "test-stream-");
props.setProperty("test.region", "us-west-2");

// Execute
destination.init(props, "test");

// Verify through reflection
Field logGroupNameField = AmazonCloudWatchAuditDestination.class.getDeclaredField("logGroupName");
logGroupNameField.setAccessible(true);
assertEquals("test-group", logGroupNameField.get(destination));

Field logStreamNameField = AmazonCloudWatchAuditDestination.class.getDeclaredField("logStreamName");
logStreamNameField.setAccessible(true);
String logStreamName = (String) logStreamNameField.get(destination);
assertTrue(logStreamName.startsWith("test-stream-"));

Field regionNameField = AmazonCloudWatchAuditDestination.class.getDeclaredField("regionName");
regionNameField.setAccessible(true);
assertEquals("us-west-2", regionNameField.get(destination));

verify(mockLogsClient).createLogStream(any(CreateLogStreamRequest.class));
}

@Test
void testLog_Success() throws Exception {
// Setup
List<AuditEventBase> events = new ArrayList<>();
events.add(mockAuditEvent);

Properties props = new Properties();
props.setProperty("test.log_group", "test-group");
props.setProperty("test.log_stream_prefix", "test-stream-");
destination.init(props, "test");

// Execute
boolean result = destination.log(events);

// Verify
assertTrue(result);
verify(mockLogsClient).putLogEvents(any(PutLogEventsRequest.class));
verify(destination).addSuccessCount(1);
verify(destination, never()).addFailedCount(anyInt());

// Verify sequence token was stored
Field sequenceTokenField = AmazonCloudWatchAuditDestination.class.getDeclaredField("sequenceToken");
sequenceTokenField.setAccessible(true);
assertEquals("nextToken", sequenceTokenField.get(destination));
}

@Test
void testLog_ResourceNotFoundException() throws Exception {
// Setup
List<AuditEventBase> events = new ArrayList<>();
events.add(mockAuditEvent);

Properties props = new Properties();
props.setProperty("test.log_group", "test-group");
props.setProperty("test.log_stream_prefix", "test-stream-");
destination.init(props, "test");

ResourceNotFoundException ex = new ResourceNotFoundException("not found");
when(mockLogsClient.putLogEvents(any(PutLogEventsRequest.class)))
.thenThrow(ex)
.thenReturn(response);

// Execute
boolean result = destination.log(events);

// Verify
assertTrue(result);
verify(mockLogsClient, times(2)).putLogEvents(any(PutLogEventsRequest.class));
verify(mockLogsClient, times(2)).createLogStream(any(CreateLogStreamRequest.class));
}

@Test
void testLog_InvalidSequenceTokenException() throws Exception {
// Setup
List<AuditEventBase> events = new ArrayList<>();
events.add(mockAuditEvent);

Properties props = new Properties();
props.setProperty("test.log_group", "test-group");
props.setProperty("test.log_stream_prefix", "test-stream-");
destination.init(props, "test");

InvalidSequenceTokenException ex = new InvalidSequenceTokenException("invalid");
ex.setExpectedSequenceToken("correctToken");
when(mockLogsClient.putLogEvents(any(PutLogEventsRequest.class)))
.thenThrow(ex)
.thenReturn(response);

// Execute
boolean result = destination.log(events);

// Verify
assertTrue(result);
verify(mockLogsClient, times(2)).putLogEvents(any(PutLogEventsRequest.class));

// Verify sequence token was updated
Field sequenceTokenField = AmazonCloudWatchAuditDestination.class.getDeclaredField("sequenceToken");
sequenceTokenField.setAccessible(true);
assertEquals("nextToken", sequenceTokenField.get(destination));
}

@Test
void testToInputLogEvent() throws Exception {
// Setup
List<AuditEventBase> events = new ArrayList<>();
AuditEventBase event1 = mock(AuditEventBase.class);
AuditEventBase event2 = mock(AuditEventBase.class);

Date date1 = new Date(1000);
Date date2 = new Date(2000);

when(event1.getEventTime()).thenReturn(date1);
when(event2.getEventTime()).thenReturn(date2);
when(MiscUtil.stringify(event1)).thenReturn("event1");
when(MiscUtil.stringify(event2)).thenReturn("event2");

events.add(event1);
events.add(event2);

// Use reflection to access and invoke the static method
Method toInputLogEventMethod = AmazonCloudWatchAuditDestination.class.getDeclaredMethod(
"toInputLogEvent", Collection.class);
toInputLogEventMethod.setAccessible(true);

@SuppressWarnings("unchecked")
Collection<InputLogEvent> result = (Collection<InputLogEvent>) toInputLogEventMethod.invoke(
null, events);
List<InputLogEvent> resultList = new ArrayList<>(result);

// Verify
assertEquals(2, resultList.size());
assertEquals(1000, resultList.get(0).getTimestamp());
assertEquals(2000, resultList.get(1).getTimestamp());
assertEquals("event1", resultList.get(0).getMessage());
assertEquals("event2", resultList.get(1).getMessage());
}

@Test
void testLog_Exception() throws Exception {
// Setup
List<AuditEventBase> events = new ArrayList<>();
events.add(mockAuditEvent);

Properties props = new Properties();
props.setProperty("test.log_group", "test-group");
props.setProperty("test.log_stream_prefix", "test-stream-");
destination.init(props, "test");

when(mockLogsClient.putLogEvents(any(PutLogEventsRequest.class)))
.thenThrow(new RuntimeException("Test exception"));

// Execute
boolean result = destination.log(events);

// Verify
assertFalse(result);
verify(destination, never()).addSuccessCount(anyInt());
verify(destination).addFailedCount(1);
}

@Test
void testFlush() {
assertDoesNotThrow(() -> destination.flush());
}

@Test
void testStop() {
assertDoesNotThrow(() -> destination.stop());
}
}
12 changes: 12 additions & 0 deletions agents-audit/dest-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
Expand Down
Loading