Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AB2D-6144] increase ab2d-aggregator test coverage #390

Merged
merged 4 commits into from
Jun 17, 2024
Merged
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
Expand Up @@ -10,12 +10,15 @@
import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static gov.cms.ab2d.aggregator.Aggregator.ONE_MEGA_BYTE;
import static gov.cms.ab2d.aggregator.FileOutputType.DATA;
import static gov.cms.ab2d.aggregator.FileOutputType.ERROR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class AggregatorCallableTest {
Expand All @@ -30,7 +33,7 @@ class AggregatorCallableTest {
private static final int MIB = 1048576;

@Test
void doItAll(@TempDir File tmpDirFolder) throws IOException, InterruptedException {
void testDoItAllWithDataFile(@TempDir File tmpDirFolder) throws IOException, InterruptedException {
long t1 = System.currentTimeMillis();
AggregatorCallable callable = new AggregatorCallable(tmpDirFolder.getAbsolutePath(), JOB_ID, "contract",
MAX_MEG, STREAM_DIR, FINISH_DIR, MULTIPLIER);
Expand All @@ -57,6 +60,29 @@ void doItAll(@TempDir File tmpDirFolder) throws IOException, InterruptedExceptio
System.out.println("Time is: " + (t2 - t1) / 1000);
}

@Test
void testDoItAllWithErrorFile(@TempDir File tmpDirFolder) throws IOException, InterruptedException, ExecutionException {
AggregatorCallable callable = new AggregatorCallable(
tmpDirFolder.getAbsolutePath(), JOB_ID, "contract", MAX_MEG, STREAM_DIR, FINISH_DIR, MULTIPLIER
);
JobHelper.workerSetUpJobDirectories(JOB_ID, tmpDirFolder.getAbsolutePath(), STREAM_DIR, FINISH_DIR);
Future<Integer> future = executor.submit(callable);
// For each batch
for (int i = 0; i < 100; i++) {
// Create a file for the batch of beneficiaries
ClaimsStream stream = new ClaimsStream(JOB_ID, tmpDirFolder.getAbsolutePath(), ERROR, STREAM_DIR, FINISH_DIR, MIB);
// For each beneficiary
for (int b = 0; b < 250; b++) {
int length = RANDOM.nextInt(1000);
stream.write(AggregatorTest.getAlphaNumericString(length) + "\n");
}
stream.close();
}
JobHelper.workerFinishJob(tmpDirFolder.getAbsolutePath() + File.separator + JOB_ID + File.separator + STREAM_DIR);
// assert number of aggregations
assertEquals(13, future.get());
}

// Disabled because it takes a long time but keeping as it is useful when you are doing performance tests
@Disabled
@Test
Expand Down Expand Up @@ -93,4 +119,4 @@ void combineBigFiles(@TempDir File tmpDirFolder) throws IOException {
long t2 = System.currentTimeMillis();
System.out.println("Combining files to (" + (t2 - t1) + ")");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class ClaimsStreamTest {
private static final String FINISH_DIR = "finished";
private static final int MIB = 1048576;

@Test
void testInit(@TempDir File tmpDirFolder) {
// Tests the constructor that uses the default buffer size
try (ClaimsStream stream = new ClaimsStream(JOB_ID, tmpDirFolder.getAbsolutePath(), DATA, STREAM_DIR, FINISH_DIR)) {
assertTrue(stream.isOpen());
stream.flush();
stream.close();
assertFalse(stream.isOpen());
} catch (Exception ex) {
fail(ex);
}
}

@Test
void testCreateAndWriteToStream(@TempDir File tmpDirFolder) {
ClaimsStream savedStream = null;
Expand Down Expand Up @@ -46,4 +59,4 @@ void testCreateAndWriteToStream(@TempDir File tmpDirFolder) {
assertTrue(theFile.exists());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static gov.cms.ab2d.aggregator.FileOutputType.UNKNOWN;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;

class FileOutputTypeTest {

@Test
Expand All @@ -18,4 +20,12 @@ void isErrorFile() {
assertEquals(UNKNOWN, FileOutputType.getFileType("bogus.txt"));
assertEquals(UNKNOWN, FileOutputType.getFileType((String) null));
}
}

@Test
void testActualFile() {
File file1 = new File("abc." + ERROR.getSuffix());
File file2 = new File("abc." + DATA.getSuffix());
assertEquals(ERROR, FileOutputType.getFileType(file1));
assertEquals(DATA, FileOutputType.getFileType(file2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,21 @@ void testDeleteAllInDir() throws IOException {
assertFalse(fulltmpdir.exists());
}

@Test
void testDeleteAllInEmptyDirs() throws IOException {
File dir = createADir(System.getProperty(JAVA_TMPDIR) + File.separator + TST_DIR);
assertTrue(dir.exists());
assertTrue(deleteAllInDir(dir));
assertFalse(dir.exists());

assertTrue(deleteAllInDir(new File("does-not-exist")));
}

static Path createFile(File dir, String fileName, String data) throws IOException {
Files.createDirectories(Path.of(dir.getAbsolutePath()));
Path p = Path.of(dir.getAbsolutePath(), fileName);
assertTrue(p.toFile().createNewFile());
Files.writeString(p, data);
return p;
}
}
}
Loading