Skip to content

Commit

Permalink
feat(Meta): Fixed meta for properly loading Simplified Options Lookup…
Browse files Browse the repository at this point in the history
… fields from the Desktop App (#394)
  • Loading branch information
ndickerson authored Nov 2, 2022
1 parent e52c98e commit 5d5f9d5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.bullhorn.dataloader.util.PrintUtil;
import com.bullhorn.dataloader.util.StringConsts;
import com.bullhorn.dataloader.util.ValidationUtil;
import com.bullhornsdk.data.model.entity.core.paybill.optionslookup.SimplifiedOptionsLookup;
import com.bullhornsdk.data.model.entity.meta.Field;
import com.bullhornsdk.data.model.entity.meta.MetaData;
import com.bullhornsdk.data.model.enums.MetaParameter;
Expand Down Expand Up @@ -82,14 +83,20 @@ private void enrichMetaForEntity(MetaData<?> metaData) {
List<Field> fields = metaData.getFields();
Map<String, Method> setterMethodMap = MethodUtil.getSetterMethodMap(entityInfo.getEntityClass());

// Throw out fields in meta that are not in the SDK
List<Field> fieldsToRemove = new ArrayList<>();
for (Field field : fields) {
// Throw out fields in meta that are not in the SDK
if (!setterMethodMap.containsKey(field.getName().toLowerCase())) {
fieldsToRemove.add(field);
printUtil.log("Removed " + entityInfo.getEntityName() + " field: "
+ field.getName() + " that does not exist in SDK-REST.");
}

// SimplifiedOptionsLookup should be treated as a direct field on the entity, since it is sent over as an id within an object
if (SimplifiedOptionsLookup.class.getSimpleName().equals(field.getDataType())) {
field.setAssociatedEntity(null);
field.setType(StringConsts.SCALAR);
}
}
fields.removeAll(fieldsToRemove);

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/bullhorn/dataloader/util/AssociationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,23 @@ public static AssociationField getToManyField(Field field) {
* @return either the current entity or the associated entity
*/
public static EntityInfo getFieldEntity(EntityInfo entityInfo, Cell cell) {
EntityInfo fieldEntityInfo = entityInfo;

if (cell.isAssociation()) {
if (isToMany(entityInfo, cell.getAssociationBaseName())) {
AssociationField associationField = getToManyField(entityInfo, cell.getAssociationBaseName());
return EntityInfo.fromString(associationField.getAssociationType().getSimpleName());
fieldEntityInfo = EntityInfo.fromString(associationField.getAssociationType().getSimpleName());
} else {
Method setMethod = MethodUtil.getSetterMethod(entityInfo, cell.getAssociationBaseName());
return EntityInfo.fromString(setMethod.getParameterTypes()[0].getSimpleName());
fieldEntityInfo = EntityInfo.fromString(setMethod.getParameterTypes()[0].getSimpleName());
}
}
return entityInfo;

if (fieldEntityInfo == null) {
throw new RestApiException("Error getting the associated entity for field: '" + cell.getName() + "'. Check that the field is valid.");
}

return fieldEntityInfo;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bullhorn/dataloader/util/StringConsts.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class StringConsts {
public static final String PROPERTY_FILE_ARG = "propertyfile";
public static final String RELATIVE_FILE_PATH = "relativeFilePath";
public static final String TIMESTAMP = DateUtil.getTimestamp();
public static final String TO_MANY = "TO_MANY";
public static final String SCALAR = "SCALAR";
public static final String TO_ONE = "TO_ONE";
public static final String TO_MANY = "TO_MANY";
}
44 changes: 42 additions & 2 deletions src/test/java/com/bullhorn/dataloader/service/MetaServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import com.bullhorn.dataloader.util.PrintUtil;
import com.bullhorn.dataloader.util.StringConsts;
import com.bullhornsdk.data.exception.RestApiException;
import com.bullhornsdk.data.model.entity.core.paybill.optionslookup.SimplifiedOptionsLookup;
import com.bullhornsdk.data.model.entity.core.standard.Candidate;
import com.bullhornsdk.data.model.entity.core.standard.CorporateUser;
import com.bullhornsdk.data.model.entity.core.standard.Placement;
import com.bullhornsdk.data.model.entity.meta.Field;
import com.bullhornsdk.data.model.entity.meta.StandardMetaData;
import com.bullhornsdk.data.model.enums.MetaParameter;
Expand All @@ -48,7 +50,7 @@ public void setup() {

metaService = new MetaService(restSessionMock, printUtilMock);

// Mock out meta fields
// Mock out Candidate meta fields
Field idField = TestUtils.createField("id", null, null, null, "SCALAR", "Integer");
Field nameField = TestUtils.createField("name", "Name", "", "", "SCALAR", "String");
Field emailField = TestUtils.createField("email", "Email", "", "", "SCALAR", "String");
Expand All @@ -74,9 +76,25 @@ public void setup() {
candidateMeta.setLabel("Employee");
candidateMeta.setFields(new ArrayList<>(Arrays.asList(idField, emailField, commentsField, customTextField, customIntField, ownerField, addressField)));

// Mock out Placement meta fields
Field bteSyncStatusField = TestUtils.createField("bteSyncStatus", "Bte Sync Status", "A lookup field", "", "TO_ONE", "SimplifiedOptionsLookup");
StandardMetaData<SimplifiedOptionsLookup> bteSyncStatusMeta = new StandardMetaData<>();
bteSyncStatusMeta.setEntity("BteSyncStatusLookup");
bteSyncStatusMeta.setLabel("Bte Sync Status Lookup");
bteSyncStatusMeta.setFields(new ArrayList<>(Arrays.asList(idField, nameField)));
bteSyncStatusField.setAssociatedEntity(bteSyncStatusMeta);

// Mock out Placement meta data
StandardMetaData<Placement> placementMeta = new StandardMetaData<>();
placementMeta.setEntity("Placement");
placementMeta.setLabel("Placement");
placementMeta.setFields(new ArrayList<>(Arrays.asList(idField, bteSyncStatusField)));

when(restSessionMock.getRestApi()).thenReturn(restApiMock);
when(restApiMock.getMetaData(eq(Candidate.class), eq(MetaParameter.FULL), eq(Sets.newHashSet(StringConsts.ALL_FIELDS))))
.thenReturn(candidateMeta);
when(restApiMock.getMetaData(eq(Placement.class), eq(MetaParameter.FULL), eq(Sets.newHashSet(StringConsts.ALL_FIELDS))))
.thenReturn(placementMeta);
}

@Test
Expand All @@ -90,7 +108,6 @@ public void testRunCandidate() {
verify(printUtilMock, times(1)).log("Removed Candidate field: customInt100 that does not exist in SDK-REST.");
verify(printUtilMock, times(1)).log("Added Candidate field: externalID that was not in Meta.");
verify(printUtilMock, times(1)).log("Done generating meta for Candidate");
verify(printUtilMock, times(1)).log("Done generating meta for Candidate");
verify(printUtilMock, times(1)).print(stringCaptor.capture());

String jsonPrinted = stringCaptor.getValue();
Expand Down Expand Up @@ -122,6 +139,29 @@ public void testRunCandidate() {
TestUtils.checkJsonObject(fields.getJSONObject(6), "name", "externalID");
}

@Test
public void testRunPlacement() {
String[] testArgs = {Command.META.getMethodName(), EntityInfo.PLACEMENT.getEntityName()};
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);

metaService.run(testArgs);

verify(printUtilMock, times(1)).log("Getting meta for Placement...");
verify(printUtilMock, times(1)).log("Done generating meta for Placement");
verify(printUtilMock, times(1)).print(stringCaptor.capture());

String jsonPrinted = stringCaptor.getValue();
JSONObject meta = new JSONObject(jsonPrinted);
Assert.assertEquals(meta.get("entity"), "Placement");
Assert.assertEquals(meta.get("label"), "Placement");
JSONArray fields = meta.getJSONArray("fields");
TestUtils.checkJsonObject(fields.getJSONObject(0), "name", "id");

JSONObject bteSyncStatusField = fields.getJSONObject(1);
TestUtils.checkJsonObject(bteSyncStatusField, "name,label,description,type", "bteSyncStatus,Bte Sync Status,A lookup field,SCALAR");
Assert.assertFalse(bteSyncStatusField.has("associatedEntity"));
}

@Test(expected = RestApiException.class)
public void testRunBadConnection() {
when(restSessionMock.getRestApi()).thenThrow(new RestApiException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ public void testGetFieldEntityEmpty() {
EntityInfo entityInfo = AssociationUtil.getFieldEntity(EntityInfo.CANDIDATE, cell);
Assert.assertEquals(EntityInfo.CANDIDATE, entityInfo);
}

@Test(expected = RestApiException.class)
public void testGetFieldEntityInvalidAssociation() {
Cell cell = new Cell("name.first", "bill");
AssociationUtil.getFieldEntity(EntityInfo.CANDIDATE, cell);
}
}

0 comments on commit 5d5f9d5

Please sign in to comment.