Skip to content

Commit

Permalink
feat: test utils to read event queue
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Sep 21, 2023
1 parent 8729c74 commit 88f1798
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class SDKStorage {

private Log L;
private CtxCore ctx;
private static final String FILE_NAME_PREFIX = "[CLY]";
private static final String FILE_NAME_SEPARATOR = "_";
private static final String EVENT_QUEUE_FILE_NAME = "event_queue";
protected static final String FILE_NAME_PREFIX = "[CLY]";
protected static final String FILE_NAME_SEPARATOR = "_";
protected static final String EVENT_QUEUE_FILE_NAME = "event_queue";

protected SDKStorage() {

Expand Down
65 changes: 60 additions & 5 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Stream;

import static ly.count.sdk.java.internal.SDKStorage.EVENT_QUEUE_FILE_NAME;
import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_PREFIX;
import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_SEPARATOR;

public class TestUtils {

public TestUtils() {
private static String DELIMETER = ":::";

private TestUtils() {
}

/**
Expand All @@ -20,7 +29,7 @@ public TestUtils() {
* @param logger logger
* @return array of request params
*/
public Map<String, String>[] getCurrentRequestQueue(File targetFolder, Log logger) {
protected static Map<String, String>[] getCurrentRequestQueue(File targetFolder, Log logger) {

//check whether target folder is a directory or not
if (!targetFolder.isDirectory()) {
Expand Down Expand Up @@ -48,13 +57,59 @@ public Map<String, String>[] getCurrentRequestQueue(File targetFolder, Log logge
return resultMapArray;
}

/**
* Get current event queue from target folder
*
* @param targetFolder where events are stored
* @param logger logger
* @return array of json events
*/
protected static List<EventImpl> getCurrentEventQueue(File targetFolder, Log logger) {
List<EventImpl> events = new ArrayList<>();

if (!targetFolder.isDirectory()) {
logger.e("[TestUtils] " + targetFolder.getAbsolutePath() + " is not a directory");
return events;
}

File file = new File(targetFolder, FILE_NAME_PREFIX + FILE_NAME_SEPARATOR + EVENT_QUEUE_FILE_NAME);
String fileContent = "";
try {
fileContent = Utils.readFileContent(file);
} catch (IOException e) {
//do nothing
}

//EventImplQueue.DELIMITER
Arrays.stream(fileContent.split(DELIMETER)).forEach(s -> {
final EventImpl event = EventImpl.fromJSON(s, (ev) -> {
}, logger);
if (event != null) {
events.add(event);
}
});

return events;
}

/**
* Get last item from list
*
* @param list
* @param <T> type of list
* @return last item from list
*/
public static <T> T getLastItem(List<T> list) {
return list.isEmpty() ? null : list.get(list.size() - 1);
}

/**
* Get request files from target folder, sorted by last modified
*
* @param targetFolder folder where requests are stored
* @return array of request files sorted by last modified
*/
private File[] getRequestFiles(File targetFolder) {
private static File[] getRequestFiles(File targetFolder) {

File[] files = targetFolder.listFiles();
if (files == null) {
Expand All @@ -76,7 +131,7 @@ private File[] getRequestFiles(File targetFolder) {
* @return map of request params
* @throws IOException if file cannot be read
*/
private Map<String, String> parseRequestParams(File file) throws IOException {
private static Map<String, String> parseRequestParams(File file) throws IOException {
try (Scanner scanner = new Scanner(file)) {
String firstLine = scanner.nextLine();
String urlDecodedStr = Utils.urldecode(firstLine);
Expand All @@ -96,4 +151,4 @@ private Map<String, String> parseRequestParams(File file) throws IOException {
return paramMap;
}
}
}
}

0 comments on commit 88f1798

Please sign in to comment.