Code

Mock Utility Class

Doing some exploration, I created the following Mock class with a mix of methods for filtering using different methods. The client simply invokes the method in a static way.


public final class Mock {
private static String mockFileBaseLocation = System.getProperty("user.dir");
private static String mockFileResourcesLocation = "src\\test\\resources";

public Mock() {
}

public static Map<String, Object> getEachXmlFileAsString(Map<String, Object> data) {
return iterateAndConvert(data).get();
}

public static String getXmlAsString(final String whichFileFromTheMockDatabase) {
return readContentsOfFileIntoString(
Mock.buildFullPathToXmlTestFile(whichFileFromTheMockDatabase));
}

public static Observable<Map<String, Object>> map(Map<String, Object> data) {
return Observable.from(data).map(Mock::transformMap);
}

public static Observable<Map<String, Object>> filter(Map<String, Object> data, String removeKey) {
return Observable.from(data).filter((entry) -> isItemInDataThenRemove(entry, removeKey));
}

public static Observable<Map<String, Object>> preserve(Map<String, Object> data, String preserveKey) {
return Observable.from(data).filter((entry) -> isItemInDataThenPreserveAndRemoveEverythingElse(data, preserveKey));
}

private static Map<String, Object> iterateAndConvertValueXmlToString(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
for (Map.Entry<String,Object> entry : data.entrySet()) {
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue()));
}
return returnMap;
}

private static boolean isItemInDataThenRemove(Map<String, Object> data, String removeKey) {
data.entrySet().stream().filter(entry ->
entry.getKey().equalsIgnoreCase(removeKey)).forEach(entry -> {
data.remove(entry.getKey());
});
return true;
}

private static boolean isItemInDataThenPreserveAndRemoveEverythingElse(Map<String, Object> data, String preserveKey) {
data.entrySet().stream().filter(entry -> !entry.getKey().equalsIgnoreCase(preserveKey)).forEach(entry -> {
System.out.printf("Found element that is not to be preserved %s, removing. %n", entry.getKey());
data.remove(entry.getKey());
});

return data.get(preserveKey) != null;
}

private static Supplier<Map<String, Object>> iterateAndConvert(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
data.entrySet().forEach((entry) ->
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue())));
return () -> returnMap;
}

private static Map<String, Object> transformMap(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
data.entrySet().forEach((entry) ->
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue())));
return returnMap;
}

private static String buildFullPathToXmlTestFile(final String xmlFileName) {
return String.format("%s\\%s\\%s",
Mock.getMockFileBaseLocation(), Mock.getMockFileResourcesLocation(),
xmlFileName);
}

private static String getMockFileBaseLocation() {
return mockFileBaseLocation;
}

private static String getMockFileResourcesLocation() {
return mockFileResourcesLocation;
}

private static String readContentsOfFileIntoString(final String whichFileFromTheMockDatabase) {
String contentToReturn = "";

try (Scanner scanner = new Scanner(new File(whichFileFromTheMockDatabase))) {
scanner.useDelimiter("\\A");
contentToReturn = scanner.next();
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());
ioe.printStackTrace();
}

return contentToReturn;
}

private static InputStream openXmlFile(String fileClassPath) {
File temp = new File(fileClassPath);

try (InputStream inputStream = new FileInputStream(temp)) {
System.out.printf("Returning openXmlFile: %s%n", temp.getAbsolutePath());
return inputStream;
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());
ioe.printStackTrace();
}

return null;
}

private static File createFileFromInputStream(String fileNameAndPath, InputStream inputStream) {
File outFile = new File(fileNameAndPath);

try (OutputStream outputStream = new FileOutputStream(outFile)) {
int readBytes = 0;
byte[] byteBuffer = new byte[2048];
while ((readBytes = inputStream.read(byteBuffer)) != -1) {
outputStream.write(byteBuffer, 0, readBytes);
}
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());
ioe.printStackTrace();
} finally {
if (inputStream != null) {
try { inputStream.close(); } catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());
ioe.printStackTrace();
}
}
}

return outFile;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s