Skip to content

Commit

Permalink
Merge pull request #40 from eBay/byarger/tmbfix
Browse files Browse the repository at this point in the history
v3.2.1 TMBuilder
  • Loading branch information
yarg0007 authored Oct 30, 2023
2 parents e3762b5 + 8df3b66 commit 89f41a8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 17 deletions.
1 change: 1 addition & 0 deletions TMBuilder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Executable requires Java 8 or later. Download is available under [releases](http

| Version | Notes |
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 3.2.1 | Allow core validation set to be exported as developer mock. |
| 3.2.0 | Adding null check option to each of the node types so it is possible to check for null response values. |
| 3.1.2 | Fix check review bug and formatting. |
| 3.1.1 | Fix export developer mock dialog selection messaging. Eliminating confusion around the file filter referencing Java and Kotlin classes which are not necessary when exporting developer mocks. |
Expand Down
2 changes: 1 addition & 1 deletion TMBuilder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>tmbuilder</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<name>TMBuilder</name>
<description>Thin Model Builder (TMBuilder) for NST thin models.</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class TMGuiConstants {

public static final String APP_NAME = "TMBuilder v3.2.0";
public static final String APP_NAME = "TMBuilder v3.2.1";
public static final int DEFAULT_WINDOW_WIDTH = 1000;
public static final int DEFAULT_WINDOW_HEIGHT = 800;
public static final String DICTIONARY_KEY = "<key>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,29 @@ public class DeveloperMockExport {
public void export(File exportPath, List<ValidationSetModel> validationSetModels) throws IOException, ClassNotFoundException {

ValidationSetModel coreValidationSet = getCoreValidationSet(validationSetModels);

String validationSetName = coreValidationSet.getValidationSetName();
validationSetName = camelCaseValidationSetName(validationSetName);
writeDeveloperMock(exportPath, validationSetName, getJsonFromValidationSet(coreValidationSet, coreValidationSet));

for (ValidationSetModel validationSetModel : validationSetModels) {

String json = getJsonFromValidationSet(coreValidationSet, validationSetModel);

String validationSetName = validationSetModel.getValidationSetName();
validationSetName = validationSetModel.getValidationSetName();
validationSetName = camelCaseValidationSetName(validationSetName);

String fileName = String.format(DEVELOPER_MOCK_FILE_NAME_FORMAT, validationSetName);
File exportFile = new File(exportPath, fileName);
FileWriter fileWriter = new FileWriter(exportFile);
fileWriter.write(json);
fileWriter.close();
writeDeveloperMock(exportPath, validationSetName, json);
}
}

protected void writeDeveloperMock(File exportPath, String validationSetName, String json) throws IOException {
String fileName = String.format(DEVELOPER_MOCK_FILE_NAME_FORMAT, validationSetName);
File exportFile = new File(exportPath, fileName);
FileWriter fileWriter = new FileWriter(exportFile);
fileWriter.write(json);
fileWriter.close();
}

public String getJsonFromValidationSet(ValidationSetModel coreValidationSet, ValidationSetModel customValidationSet) throws IOException, ClassNotFoundException {

TreeMap<String, Integer> coreValidationArrayPathToArraySizeMap = ArrayPathToArraySizeMapProcessor.getArrayPathToArraySizeMap(coreValidationSet);
Expand All @@ -58,16 +64,21 @@ public String getJsonFromValidationSet(ValidationSetModel coreValidationSet, Val
TreeMap<String, Integer> arrayPathToArraySizeMap = new TreeMap<>(new SmallestToLargestArrayPathComparator());
arrayPathToArraySizeMap.putAll(coreValidationArrayPathToArraySizeMap);

TreeMap<String, Integer> customValidationArrayPathToArraySizeMap = ArrayPathToArraySizeMapProcessor.getArrayPathToArraySizeMap(customValidationSet);
arrayPathToArraySizeMap.putAll(customValidationArrayPathToArraySizeMap);
if (customValidationSet != null) {
TreeMap<String, Integer> customValidationArrayPathToArraySizeMap = ArrayPathToArraySizeMapProcessor.getArrayPathToArraySizeMap(customValidationSet);
arrayPathToArraySizeMap.putAll(customValidationArrayPathToArraySizeMap);
}

// Build up the jsonMap structure.
Map<String, Object> jsonMap = new HashMap<>();
jsonMap = JsonMapStructureProcessor.getJsonMapForValidations(arrayPathToArraySizeMap, coreValidationSet, customValidationSet);

// Populate the jsonMap with mock values.
JsonMapPopulateProcessor.populateJsonMapWithMockValues(coreValidationSet, jsonMap);
JsonMapPopulateProcessor.populateJsonMapWithMockValues(customValidationSet, jsonMap);

if (customValidationSet != null) {
JsonMapPopulateProcessor.populateJsonMapWithMockValues(customValidationSet, jsonMap);
}

// Write the JSON to file.
String json = gson.toJson(jsonMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public static Map<String, Object> getJsonMapForValidations(TreeMap<String, Integ
// Iterate over each path from both the coreValidationSetModel and the customValidationSetModel, using the
// arrayPathToSizeMap to initialize array nodes to the right size.
NodeModel[] coreNodes = coreValidationSetModel.getData();
NodeModel[] customNodes = customValidationSetModel.getData();

processNodeModels(coreNodes, arrayPathToSizeMap, jsonMap);
processNodeModels(customNodes, arrayPathToSizeMap, jsonMap);

if (customValidationSetModel != null) {
NodeModel[] customNodes = customValidationSetModel.getData();
processNodeModels(customNodes, arrayPathToSizeMap, jsonMap);
}

return jsonMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class DeveloperMockExportTest {

Expand All @@ -33,6 +33,45 @@ public void resetDeveloperMockExport() {
export = new DeveloperMockExport();
}

@Test
public void getCoreValidationSetByItselfAndCustomValidationSet() throws Exception {

DeveloperMockExport exportMock = Mockito.spy(DeveloperMockExport.class);
doNothing().when(exportMock).writeDeveloperMock(Mockito.any(File.class), Mockito.anyString(), Mockito.anyString());

ValidationSetModel coreValidationSetModel = Mockito.mock(ValidationSetModel.class);
when(coreValidationSetModel.getValidationSetName()).thenReturn(ExportConstants.CORE_VALIDATION_SET);
when(coreValidationSetModel.getData()).thenReturn(new NodeModel[0]);

ValidationSetModel customValidationSetModel = Mockito.mock(ValidationSetModel.class);
when(customValidationSetModel.getValidationSetName()).thenReturn("FOO");
when(customValidationSetModel.getData()).thenReturn(new NodeModel[0]);

List<ValidationSetModel> validations = new ArrayList<>();
validations.add(coreValidationSetModel);
validations.add(customValidationSetModel);

exportMock.export(new File(System.getProperty("user.dir")), validations);
verify(exportMock, times(2)).writeDeveloperMock(Mockito.any(File.class), Mockito.anyString(), Mockito.anyString());
}

@Test
public void getCoreValidationSetByItself() throws Exception {

DeveloperMockExport exportMock = Mockito.spy(DeveloperMockExport.class);
doNothing().when(exportMock).writeDeveloperMock(Mockito.any(File.class), Mockito.anyString(), Mockito.anyString());

ValidationSetModel coreValidationSetModel = Mockito.mock(ValidationSetModel.class);
when(coreValidationSetModel.getValidationSetName()).thenReturn(ExportConstants.CORE_VALIDATION_SET);
when(coreValidationSetModel.getData()).thenReturn(new NodeModel[0]);

List<ValidationSetModel> validations = new ArrayList<>();
validations.add(coreValidationSetModel);

exportMock.export(new File(System.getProperty("user.dir")), validations);
verify(exportMock, times(1)).writeDeveloperMock(Mockito.any(File.class), Mockito.anyString(), Mockito.anyString());
}

@Test
public void getCoreValidationSet() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ public class JsonMapStructureProcessorTest {
private JsonMapStructureProcessor processor = new JsonMapStructureProcessor();
private Gson gson = new GsonBuilder().serializeNulls().create();;

@Test
public void testGetJsonMapForValidationsCoreAndNullCustomValues() throws Exception {

JsonBooleanType jsonBooleanType = new JsonBooleanType("Foo");
jsonBooleanType.updateCheckForPath("$.foo.bar", new TMJPStringCheck());
String booleanSerializedType = JsonBaseTypePersistence.serialize(jsonBooleanType);

PathNode[] booleanPathNodes = new PathNode[4];
booleanPathNodes[0] = new PathNode("$", "class com.ebay.tool.thinmodelgen.jsonschema.type.JsonObjectType", 0);
booleanPathNodes[1] = new PathNode("foo", "class com.ebay.tool.thinmodelgen.jsonschema.type.JsonObjectType", 0);
booleanPathNodes[3] = new PathNode("bar", "class com.ebay.tool.thinmodelgen.jsonschema.type.JsonBooleanType", 0);

NodeModel booleanNodeModel = new NodeModel(booleanPathNodes, booleanSerializedType);
NodeModel[] coreNodeModel = new NodeModel[] {booleanNodeModel};
ValidationSetModel coreValidationSet = new ValidationSetModel("core", coreNodeModel);

TreeMap<String, Integer> treeMap = new TreeMap<>(new SmallestToLargestArrayPathComparator());

Map actualJsonMap = processor.getJsonMapForValidations(treeMap, coreValidationSet, null);

// Define expected
HashMap<String, Object> foo = new HashMap<>();
foo.put("bar", null);
HashMap<String, Object> expectedJsonMap = new HashMap<>();
expectedJsonMap.put("foo", foo);

String actual = gson.toJson(actualJsonMap);
String expected = gson.toJson(expectedJsonMap);

assertThat(actual, is(equalTo(expected)));
}

@Test
public void testGetJsonMapForValidationsCoreAndCustom() throws Exception {

Expand Down

0 comments on commit 89f41a8

Please sign in to comment.