Skip to content

Commit

Permalink
Adding tests, updating version
Browse files Browse the repository at this point in the history
  • Loading branch information
SriramKeerthi committed Nov 27, 2016
1 parent 15dc42d commit a08013f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 25 deletions.
2 changes: 1 addition & 1 deletion jaggr/jaggr-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jaggr-parent</artifactId>
<groupId>com.caffinc</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.caffinc.jaggr.utils;

import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
* Tests the JsonFileReader utility
*
* @author Sriram
* @since 11/27/2016
*/
public class JsonFileReaderTest {
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
private static final Random RANDOM = new Random();
private static final Gson GSON = new Gson();

@Test
public void testGetFileLines() throws Exception {
Path tempFilePath = Paths.get(TEMP_DIR, "jsontest" + RANDOM.nextInt() + ".json");
try {
List<String> lines;
try (BufferedWriter br = new BufferedWriter(new FileWriter(tempFilePath.toFile()))
) {
for (int i = 0; i < 10; i++)
br.write(i + "\n");
}
lines = JsonFileReader.getFileLines(tempFilePath.toString());
for (int i = 0; i < 10; i++) {
Assert.assertEquals("Value should match value written to file", String.valueOf(i), lines.get(i));
}
} finally {
Files.delete(tempFilePath);
}
}

@Test
public void testReadJsonFromFile() throws Exception {
Path tempFilePath = Paths.get(TEMP_DIR, "jsontest" + RANDOM.nextInt() + ".json");
try {
List<Map<String, Object>> expectedData = new ArrayList<>();
try (BufferedWriter br = new BufferedWriter(new FileWriter(tempFilePath.toFile()))
) {
for (int i = 0; i < 10; i++) {
Map<String, Object> json = new HashMap<>();
json.put("_id", (double) i);
json.put("val", RANDOM.nextDouble());
expectedData.add(json);
br.write(GSON.toJson(json) + "\n");
}
}
List<Map<String, Object>> jsonLines = JsonFileReader.readJsonFromFile(tempFilePath.toString());
for (int i = 0; i < 10; i++) {
Assert.assertEquals("Value should match value written to file", expectedData.get(i), jsonLines.get(i));
}
} finally {
Files.delete(tempFilePath);
}
}

@Test
public void testReadJsonFromResource() throws Exception {
List<Map<String, Object>> result = JsonFileReader.readJsonFromResource("raw.json");
Assert.assertEquals("There must be ten lines in the file", 10, result.size());
for (Map<String, Object> obj : result) {
Assert.assertEquals("There must be 9 elements in the object", 9, obj.size());
}
}
}
10 changes: 10 additions & 0 deletions jaggr/jaggr-utils/src/test/resources/raw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{"index":1,"index_start_at":56,"integer":25,"float":14.0942,"name":"Mitchell","surname":"Archer","fullname":"Dennis Sherrill","email":"rita@bowman.lc","bool":false}
{"index":2,"index_start_at":57,"integer":33,"float":13.9546,"name":"Miriam","surname":"Stanton","fullname":"Meredith Watson","email":"lisa@livingston.es","bool":false}
{"index":3,"index_start_at":58,"integer":27,"float":18.8563,"name":"Leo","surname":"Robinson","fullname":"Brent Albright","email":"richard@richardson.do","bool":false}
{"index":4,"index_start_at":59,"integer":14,"float":14.914,"name":"Dianne","surname":"Middleton","fullname":"Alex Gay","email":"melinda@glover.az","bool":false}
{"index":5,"index_start_at":60,"integer":36,"float":13.7685,"name":"Dan","surname":"Kirk","fullname":"Anna Bradley","email":"alice@christensen.cr","bool":true}
{"index":6,"index_start_at":61,"integer":8,"float":18.4737,"name":"Natalie","surname":"Lassiter","fullname":"Karl Cassidy","email":"jonathan@gray.vu","bool":true}
{"index":7,"index_start_at":62,"integer":24,"float":15.9886,"name":"Eileen","surname":"Bunn","fullname":"Shannon Hendrix","email":"luis@king.cx","bool":true}
{"index":8,"index_start_at":63,"integer":15,"float":14.8193,"name":"Clifford","surname":"Schwartz","fullname":"Sara Gilbert","email":"judith@hunt.et","bool":false}
{"index":9,"index_start_at":64,"integer":33,"float":14.2817,"name":"Florence","surname":"Washington","fullname":"Thomas Casey","email":"vivian@caldwell.hn","bool":false}
{"index":10,"index_start_at":65,"integer":10,"float":17.4529,"name":"Ellen","surname":"Haynes","fullname":"Lee Denton","email":"scott@wrenn.uy","bool":true}
2 changes: 1 addition & 1 deletion jaggr/jaggr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jaggr-parent</artifactId>
<groupId>com.caffinc</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
22 changes: 0 additions & 22 deletions jaggr/jaggr/src/test/resources/raw2.json

This file was deleted.

2 changes: 1 addition & 1 deletion jaggr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.caffinc</groupId>
<artifactId>jaggr-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.1</version>
<version>0.2.2</version>
<modules>
<module>jaggr-utils</module>
<module>jaggr</module>
Expand Down

0 comments on commit a08013f

Please sign in to comment.