Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Improving unit test coverage for Cerberus server (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
RVelp1 authored Apr 12, 2021
1 parent f1f965f commit 165476c
Show file tree
Hide file tree
Showing 16 changed files with 850 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.nike.internal.util.StringUtils;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Stream;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -30,7 +31,7 @@ public class AuditLogsS3TimeBasedRollingPolicy<E> extends TimeBasedRollingPolicy

private final String bucket;
private final String bucketRegion;
private LinkedBlockingQueue<String> logChunkFileS3Queue = new LinkedBlockingQueue<>();
@Setter private LinkedBlockingQueue<String> logChunkFileS3Queue = new LinkedBlockingQueue<>();
private S3LogUploaderService s3LogUploaderService = null;

@Autowired
Expand All @@ -54,7 +55,7 @@ private boolean isS3AuditLogCopyingEnabled() {

@Override
public void rollover() throws RolloverFailure {
super.rollover();
superRollOver();

if (isS3AuditLogCopyingEnabled()) {
String filename = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName() + ".gz";
Expand All @@ -65,4 +66,9 @@ public void rollover() throws RolloverFailure {
}
}
}

void superRollOver() {
super.rollover();
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,20 @@ public void onApplicationEvent(AuditableEvent event) {
.put(
"principal_type",
cerberusPrincipal
.map(p -> cerberusPrincipal.get().getPrincipalType().getName())
.map(p -> p.getPrincipalType().getName())
.orElse(AuditableEventContext.UNKNOWN))
.put(
"principal_token_created",
cerberusPrincipal
.map(
p ->
cerberusPrincipal
.get()
.getCreated()
.format(ATHENA_DATE_FORMATTER))
.map(p -> p.getCreated().format(ATHENA_DATE_FORMATTER))
.orElseGet(
() ->
OffsetDateTime.parse(PARTY_LIKE_ITS_99, ISO_OFFSET_DATE_TIME)
.format(ATHENA_DATE_FORMATTER)))
.put(
"principal_token_expires",
cerberusPrincipal
.map(
p ->
cerberusPrincipal
.get()
.getExpires()
.format(ATHENA_DATE_FORMATTER))
.map(p -> p.getExpires().format(ATHENA_DATE_FORMATTER))
.orElseGet(
() ->
OffsetDateTime.parse(PARTY_LIKE_ITS_99, ISO_OFFSET_DATE_TIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PreDestroy;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -52,7 +53,7 @@ public class S3LogUploaderService {
private static final String ATHENA_LOG_NAME = "athena-audit-logger";
private static final String ATHENA_LOG_APPENDER = "athena-log-appender";

private final ExecutorService executor = Executors.newSingleThreadExecutor();
@Setter private ExecutorService executor = Executors.newSingleThreadExecutor();
private final AmazonS3 amazonS3;
private final String bucket;
private final String bucketRegion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ch.qos.logback.core.rolling;

import com.nike.cerberus.audit.logger.service.S3LogUploaderService;
import java.util.concurrent.LinkedBlockingQueue;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class AuditLogsS3TimeBasedRollingPolicyTest {

private AuditLogsS3TimeBasedRollingPolicy auditLogsS3TimeBasedRollingPolicy;

@Test
public void testLogUploaderServiceIfLogChunkFileS3QueueIsEmpty() {
auditLogsS3TimeBasedRollingPolicy =
new AuditLogsS3TimeBasedRollingPolicy("bucket", "bucketRegion");
S3LogUploaderService s3LogUploaderService = Mockito.mock(S3LogUploaderService.class);
auditLogsS3TimeBasedRollingPolicy.setS3LogUploaderService(s3LogUploaderService);
Mockito.verify(s3LogUploaderService, Mockito.never()).ingestLog(Mockito.anyString());
}

@Test
public void testRollOverIfAuditCopyIsNotEnabled() {
auditLogsS3TimeBasedRollingPolicy = Mockito.spy(new AuditLogsS3TimeBasedRollingPolicy("", ""));
Mockito.doNothing().when(auditLogsS3TimeBasedRollingPolicy).superRollOver();
S3LogUploaderService s3LogUploaderService = Mockito.mock(S3LogUploaderService.class);
auditLogsS3TimeBasedRollingPolicy.setS3LogUploaderService(s3LogUploaderService);
TimeBasedFileNamingAndTriggeringPolicy timeBasedFileNamingAndTriggeringPolicy =
Mockito.spy(TimeBasedFileNamingAndTriggeringPolicy.class);
auditLogsS3TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(
timeBasedFileNamingAndTriggeringPolicy);
auditLogsS3TimeBasedRollingPolicy.rollover();
Mockito.verify(timeBasedFileNamingAndTriggeringPolicy, Mockito.never())
.getElapsedPeriodsFileName();
Mockito.verify(s3LogUploaderService, Mockito.never()).ingestLog(Mockito.anyString());
}

@Test
public void testRollOverIfAuditCopyIsEnabled() {
auditLogsS3TimeBasedRollingPolicy =
Mockito.spy(new AuditLogsS3TimeBasedRollingPolicy("bucket", "region"));
Mockito.doNothing().when(auditLogsS3TimeBasedRollingPolicy).superRollOver();
S3LogUploaderService s3LogUploaderService = Mockito.mock(S3LogUploaderService.class);
auditLogsS3TimeBasedRollingPolicy.setS3LogUploaderService(s3LogUploaderService);
TimeBasedFileNamingAndTriggeringPolicy timeBasedFileNamingAndTriggeringPolicy =
Mockito.spy(TimeBasedFileNamingAndTriggeringPolicy.class);
Mockito.when(timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName())
.thenReturn("elapsedfilename");
auditLogsS3TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(
timeBasedFileNamingAndTriggeringPolicy);
LinkedBlockingQueue<String> logChunkFileS3Queue = new LinkedBlockingQueue<>();
auditLogsS3TimeBasedRollingPolicy.setLogChunkFileS3Queue(logChunkFileS3Queue);
auditLogsS3TimeBasedRollingPolicy.rollover();
Mockito.verify(timeBasedFileNamingAndTriggeringPolicy).getElapsedPeriodsFileName();
Mockito.verify(s3LogUploaderService).ingestLog("elapsedfilename.gz");
Assert.assertTrue(logChunkFileS3Queue.size() == 0);
}

@Test
public void testRollOverIfAuditCopyIsEnabledAndS3UploaderIsNull() {
auditLogsS3TimeBasedRollingPolicy =
Mockito.spy(new AuditLogsS3TimeBasedRollingPolicy("bucket", "region"));
Mockito.doNothing().when(auditLogsS3TimeBasedRollingPolicy).superRollOver();
TimeBasedFileNamingAndTriggeringPolicy timeBasedFileNamingAndTriggeringPolicy =
Mockito.spy(TimeBasedFileNamingAndTriggeringPolicy.class);
Mockito.when(timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName())
.thenReturn("elapsedfilename");
auditLogsS3TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(
timeBasedFileNamingAndTriggeringPolicy);
LinkedBlockingQueue<String> logChunkFileS3Queue = new LinkedBlockingQueue<>();
auditLogsS3TimeBasedRollingPolicy.setLogChunkFileS3Queue(logChunkFileS3Queue);
auditLogsS3TimeBasedRollingPolicy.rollover();
Mockito.verify(timeBasedFileNamingAndTriggeringPolicy).getElapsedPeriodsFileName();
Assert.assertTrue(logChunkFileS3Queue.size() > 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.nike.cerberus.audit.logger;

import com.amazonaws.services.athena.AmazonAthena;
import org.junit.Assert;
import org.junit.Test;

public class AthenaClientFactoryTest {

@Test
public void testGetClientAlwaysReturnsSameAthenaInstance() {
AthenaClientFactory athenaClientFactory = new AthenaClientFactory();
AmazonAthena clientInstance1 = athenaClientFactory.getClient("region-2");
AmazonAthena clientInstance2 = athenaClientFactory.getClient("region-2");
Assert.assertSame(clientInstance1, clientInstance2);
}

@Test
public void testGetClientDoesNotThrowNPEWhenRegionIsEmptyString() {
AthenaClientFactory athenaClientFactory = new AthenaClientFactory();
athenaClientFactory.getClient("");
}

@Test(expected = NullPointerException.class)
public void testGetClientThrowsNPEWhenRegionIsNull() {
AthenaClientFactory athenaClientFactory = new AthenaClientFactory();
athenaClientFactory.getClient(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.nike.cerberus.audit.logger;

import com.amazonaws.services.s3.AmazonS3;
import org.junit.Assert;
import org.junit.Test;

public class S3ClientFactoryTest {

@Test
public void testS3ClientFactoryAlwaysReturnSameInstance() {
S3ClientFactory s3ClientFactory = new S3ClientFactory();
AmazonS3 s3Instance1 = s3ClientFactory.getClient("region-1");
AmazonS3 s3Instance2 = s3ClientFactory.getClient("region-1");
Assert.assertSame(s3Instance1, s3Instance2);
}

@Test
public void testS3ClientFactoryDoesNotThrowsNPEWhenEmptyStringIsPassedAsRegion() {
S3ClientFactory s3ClientFactory = new S3ClientFactory();
s3ClientFactory.getClient("");
}

@Test(expected = NullPointerException.class)
public void testS3ClientFactoryThrowsNPEWhenNullRegionIsPassed() {
S3ClientFactory s3ClientFactory = new S3ClientFactory();
s3ClientFactory.getClient(null);
}
}
Loading

0 comments on commit 165476c

Please sign in to comment.