Skip to content

Commit

Permalink
Fixed #17 #6 managed case of creating the output directories
Browse files Browse the repository at this point in the history
  • Loading branch information
baubakg committed May 1, 2021
1 parent b42404e commit b30fca5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ac_test_output/
target/
test-output/
.DS_Store
log_parser_output/
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,28 @@ public static File exportParseDefinitionToJSON(ParseDefinition in_parseDefinitio
public static File exportParseDefinitionToJSON(ParseDefinition in_parseDefinition, File in_jsonFile)
throws ParseDefinitionImportExportException {
ObjectMapper mapper = new ObjectMapper();

createParents(in_jsonFile);

return exportParseDefinitionToJSON(in_parseDefinition, in_jsonFile, mapper);

}

/**
* A util method for ensuring the the parent directories
*
* Author : gandomi
*
* @param in_file A file that may or may not exist
*
*/
protected static void createParents(File in_file) {
//Make sure that the parent directories exist
if (in_file.getParentFile()!=null && !in_file.getParentFile().exists()) {
in_file.getParentFile().mkdirs();
}
}

/**
* Exports a ParseDefinition to a JSON file
*
Expand All @@ -150,7 +167,7 @@ protected static File exportParseDefinitionToJSON(ParseDefinition in_parseDefini
mapper.writeValue(in_jsonFile, in_parseDefinition);
} catch (IOException e) {
throw new ParseDefinitionImportExportException(
"Errror while exporting parse definition to file " + in_jsonFile.getPath(), e);
"Error while exporting parse definition to file " + in_jsonFile.getPath(), e);
}

return in_jsonFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.adobe.campaign.tests.logparser;

public abstract class LogParser {

public static final String OUTPUT_ROOT = "log_parser_output";
public static final String OUTPUT_DIR = OUTPUT_ROOT + "/" + "data";

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@

import java.io.File;
import java.io.IOException;

import org.mockito.ArgumentMatchers;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.adobe.campaign.tests.logparser.exceptions.ParseDefinitionImportExportException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ParseDefinitionTests {

@BeforeMethod
public void cleanUp() {
File l_stdOutPutFile = new File(LogParser.OUTPUT_DIR);

if (l_stdOutPutFile.exists()) {
l_stdOutPutFile.delete();
}

}

@Test
public void testcopyConstructorParseDefinitionEntry() {

Expand Down Expand Up @@ -181,93 +189,6 @@ public void testParseDefinitionEquals2() {

}

@Test
public void testExportToJSONParrseDefinitionEntry()
throws JsonParseException, JsonMappingException, IOException {

//Create a parse definition
ParseDefinitionEntry l_verbDefinition = new ParseDefinitionEntry();

l_verbDefinition.setTitle("verb");
l_verbDefinition.setStart("\"");
l_verbDefinition.setEnd(" /");

ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("path");
l_apiDefinition.setStart(" /rest/head/");
l_apiDefinition.setEnd(" ");

ParseDefinition l_parseDefinition = new ParseDefinition("rest calls");
l_parseDefinition.addEntry(l_apiDefinition);
l_parseDefinition.addEntry(l_verbDefinition);

ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper2 = new ObjectMapper();

try {

// Java objects to JSON file
mapper.writeValue(new File("ac_test_output/firstDefinitionEntry.json"), l_apiDefinition);

// Java objects to JSON string - compact-print
mapper.writeValueAsString(l_apiDefinition);

// Java objects to JSON string - pretty-print
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(l_parseDefinition);

} catch (IOException e) {
e.printStackTrace();
}

//Read JSON
ParseDefinitionEntry fetchedJSON = mapper2
.readValue(new File("ac_test_output/firstDefinitionEntry.json"), ParseDefinitionEntry.class);

assertThat("Both `parseDefinitions should be the same", fetchedJSON, equalTo(l_apiDefinition));

}

@Test
public void testExportToJSON() throws JsonParseException, JsonMappingException, IOException {

//Create a parse definition
ParseDefinitionEntry l_verbDefinition = new ParseDefinitionEntry();

l_verbDefinition.setTitle("verb");
l_verbDefinition.setStart("\"");
l_verbDefinition.setEnd(" /");

ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("path");
l_apiDefinition.setStart(" /rest/head/");
l_apiDefinition.setEnd(" ");

ParseDefinition l_parseDefinition = new ParseDefinition("rest calls");
l_parseDefinition.addEntry(l_apiDefinition);
l_parseDefinition.addEntry(l_verbDefinition);

ObjectMapper mapper = new ObjectMapper();

try {

// Java objects to JSON file
mapper.writeValue(new File("ac_test_output/firstDefinition.json"), l_parseDefinition);

} catch (IOException e) {
e.printStackTrace();
}
ObjectMapper mapper2 = new ObjectMapper();

//Read JSON
ParseDefinition fetchedJSON = mapper2.readValue(new File("ac_test_output/firstDefinition.json"),
ParseDefinition.class);

assertThat("Both `parseDefinitions should be the same", fetchedJSON, equalTo(l_parseDefinition));

}

@Test
public void testImportExportToJSON() throws ParseDefinitionImportExportException {

Expand All @@ -288,7 +209,7 @@ public void testImportExportToJSON() throws ParseDefinitionImportExportException
l_parseDefinition.addEntry(l_apiDefinition);
l_parseDefinition.addEntry(l_verbDefinition);

final String l_jsonPath = "ac_test_output/firstDefinition.json";
final String l_jsonPath = LogParser.OUTPUT_DIR + "/firstDefinition.json";

File l_storedJSON = ParseDefinitionFactory.exportParseDefinitionToJSON(l_parseDefinition, l_jsonPath);

Expand All @@ -306,7 +227,7 @@ public void testImportExportToJSON_negative() throws ParseDefinitionImportExport
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
Mockito.doThrow(IOException.class).when(mapper).writeValue(ArgumentMatchers.any(File.class),
ArgumentMatchers.any(ParseDefinition.class));

Mockito.doThrow(IOException.class).when(mapper).readValue(ArgumentMatchers.any(File.class),
ArgumentMatchers.any(Class.class));

Expand All @@ -320,13 +241,41 @@ public void testImportExportToJSON_negative() throws ParseDefinitionImportExport
ParseDefinition l_parseDefinition = new ParseDefinition("rest calls");
l_parseDefinition.addEntry(l_verbDefinition);

final String l_jsonPath = "ac_test_output/firstDefinition.json";
final String l_jsonPath = LogParser.OUTPUT_DIR + "/firstDefinition.json";

Assert.assertThrows(ParseDefinitionImportExportException.class, () -> ParseDefinitionFactory
.exportParseDefinitionToJSON(l_parseDefinition, new File(l_jsonPath), mapper));

Assert.assertThrows(ParseDefinitionImportExportException.class, () -> ParseDefinitionFactory
.importParseDefinition(new File(l_jsonPath), mapper));
Assert.assertThrows(ParseDefinitionImportExportException.class,
() -> ParseDefinitionFactory.importParseDefinition(new File(l_jsonPath), mapper));

}

//// Utils

@Test
public void testCreateParents() {
File parentDir = new File(LogParser.OUTPUT_DIR);

parentDir.delete();
File l_jsonPath = new File(LogParser.OUTPUT_DIR + "/random.json");

assertThat("The parent dir should not exist", !l_jsonPath.getParentFile().exists());

ParseDefinitionFactory.createParents(l_jsonPath);

assertThat("The parent dir should now exist", l_jsonPath.getParentFile().exists());

//The second execution should happen with no problems.
ParseDefinitionFactory.createParents(l_jsonPath);

assertThat("The parent dir should now exist", l_jsonPath.getParentFile().exists());

//No Parent Dir
File l_jsonPath2 = new File("random2.json");
ParseDefinitionFactory.createParents(l_jsonPath2);


}

}
2 changes: 1 addition & 1 deletion src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://logging.apache.org/log4j/2.0/config">
<Appenders>
<File name="FILE"
fileName="ac_test_output/logs/logfile.log">
fileName="log_parser_output/logs/logfile.log">
<PatternLayout
pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</File>
Expand Down

0 comments on commit b30fca5

Please sign in to comment.