-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15dc42d
commit a08013f
Showing
6 changed files
with
89 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
jaggr/jaggr-utils/src/test/java/com/caffinc/jaggr/utils/JsonFileReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters