Skip to content

Commit

Permalink
fix(WorkersCompensationRate): Ensuring that privateLabel.id is availa…
Browse files Browse the repository at this point in the history
…ble in meta (#427)

* fix(WorkersCompensationRate): Ensuring that privateLabel.id is available in meta

* Fix a typo

* Removed unnecessary check

* Updated tests to verify fields length

---------

Co-authored-by: Nathan Dickerson <[email protected]>
  • Loading branch information
ndickerson and Nathan Dickerson authored Jan 11, 2024
1 parent 4a0eca2 commit 0a6392a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
34 changes: 29 additions & 5 deletions src/main/java/com/bullhorn/dataloader/service/MetaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -18,8 +19,10 @@
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.core.standard.PrivateLabel;
import com.bullhornsdk.data.model.entity.meta.Field;
import com.bullhornsdk.data.model.entity.meta.MetaData;
import com.bullhornsdk.data.model.entity.meta.StandardMetaData;
import com.bullhornsdk.data.model.enums.MetaParameter;
import com.google.common.collect.Sets;

Expand Down Expand Up @@ -103,14 +106,23 @@ private void enrichMetaForEntity(MetaData<?> metaData) {
// Add additional fields from SDK-REST that are not in meta
if (setterMethodMap.containsKey(StringConsts.EXTERNAL_ID.toLowerCase())
&& fields.stream().noneMatch(field -> field.getName().equals(StringConsts.EXTERNAL_ID))) {
Field externalIdField = new Field();
externalIdField.setName(StringConsts.EXTERNAL_ID);
externalIdField.setType("SCALAR");
externalIdField.setDataType("String");
fields.add(externalIdField);
fields.add(createField(StringConsts.EXTERNAL_ID, StringConsts.EXTERNAL_ID, "SCALAR", "String"));
printUtil.log("Added " + entityInfo.getEntityName() + " field: "
+ StringConsts.EXTERNAL_ID + " that was not in Meta.");
}
if (entityInfo.equals(EntityInfo.WORKERS_COMPENSATION_RATE)
&& fields.stream().noneMatch(field -> field.getName().equals(StringConsts.PRIVATE_LABEL))) {
StandardMetaData<PrivateLabel> associatedEntityMeta = new StandardMetaData<>();
associatedEntityMeta.setEntity("PrivateLabel");
associatedEntityMeta.setLabel("Private Label");
associatedEntityMeta.setFields(new ArrayList<>(Collections.singletonList(
createField(StringConsts.ID, "ID", "ID", "Integer"))));
Field privateLabelField = createField(StringConsts.PRIVATE_LABEL, "Private Label", "TO_ONE", null);
privateLabelField.setAssociatedEntity(associatedEntityMeta);
fields.add(privateLabelField);
printUtil.log("Added " + entityInfo.getEntityName() + " field: "
+ StringConsts.PRIVATE_LABEL + " that was not in Meta.");
}
}
}

Expand Down Expand Up @@ -148,4 +160,16 @@ private JSONArray fieldsToJson(List<Field> fields) {
}
return jsonFields;
}

/**
* Convenience constructor that builds up a small Field object.
*/
private static Field createField(String name, String label, String type, String dataType) {
Field field = new Field();
field.setName(name);
field.setLabel(label);
field.setType(type);
field.setDataType(dataType);
return field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class StringConsts {
public static final String NAME = "name";
public static final String NOTE_ID = "noteID";
public static final String PARENT_ENTITY_ID = "parentEntityID";
public static final String PRIVATE_LABEL = "privateLabel";
public static final String PROPERTY_FILE_ARG = "propertyfile";
public static final String RELATIVE_FILE_PATH = "relativeFilePath";
public static final String TIMESTAMP = DateUtil.getTimestamp();
Expand Down
49 changes: 45 additions & 4 deletions src/test/java/com/bullhorn/dataloader/service/MetaServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.core.standard.WorkersCompensationRate;
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 @@ -51,7 +52,7 @@ public void setup() {
metaService = new MetaService(restSessionMock, printUtilMock);

// Mock out Candidate meta fields
Field idField = TestUtils.createField("id", null, null, null, "SCALAR", "Integer");
Field idField = TestUtils.createField("id", "ID", null, null, "ID", "Integer");
Field nameField = TestUtils.createField("name", "Name", "", "", "SCALAR", "String");
Field emailField = TestUtils.createField("email", "Email", "", "", "SCALAR", "String");
Field commentsField = TestUtils.createField("comments", "Comments", "General Comments",
Expand All @@ -60,6 +61,7 @@ public void setup() {
"Useful sometimes", "SCALAR", "String");
Field customIntField = TestUtils.createField("customInt100", "Brand new field", "", "", "SCALAR", "Integer");
Field ownerField = TestUtils.createField("owner", "Recruiter", "", "", "TO_ONE", "");
Field startDateField = TestUtils.createField("startDate", "Start Date", null, null, "SCALAR", "Timestamp");
StandardMetaData<CorporateUser> corporateUserMeta = new StandardMetaData<>();
corporateUserMeta.setEntity("CorporateUser");
corporateUserMeta.setLabel("Recruiter");
Expand All @@ -70,7 +72,7 @@ public void setup() {
Field cityField = TestUtils.createField("city", "City", "", "", "SCALAR", "String");
addressField.setFields(new ArrayList<>(Arrays.asList(address1Field, cityField)));

// Mock out Candidate meta data
// Mock out Candidate meta
StandardMetaData<Candidate> candidateMeta = new StandardMetaData<>();
candidateMeta.setEntity("Candidate");
candidateMeta.setLabel("Employee");
Expand All @@ -84,17 +86,25 @@ public void setup() {
bteSyncStatusMeta.setFields(new ArrayList<>(Arrays.asList(idField, nameField)));
bteSyncStatusField.setAssociatedEntity(bteSyncStatusMeta);

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

// Mock out WorkersCompensationRate meta
StandardMetaData<WorkersCompensationRate> workersCompensationRateMeta = new StandardMetaData<>();
workersCompensationRateMeta.setEntity("WorkersCompensationRate");
workersCompensationRateMeta.setLabel("Workers Compensation Rate");
workersCompensationRateMeta.setFields(new ArrayList<>(Arrays.asList(idField, startDateField)));

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);
when(restApiMock.getMetaData(eq(WorkersCompensationRate.class), eq(MetaParameter.FULL), eq(Sets.newHashSet(StringConsts.ALL_FIELDS))))
.thenReturn(workersCompensationRateMeta);
}

@Test
Expand All @@ -115,6 +125,7 @@ public void testRunCandidate() {
Assert.assertEquals(meta.get("entity"), "Candidate");
Assert.assertEquals(meta.get("label"), "Employee");
JSONArray fields = meta.getJSONArray("fields");
Assert.assertEquals(7, fields.length());
TestUtils.checkJsonObject(fields.getJSONObject(0), "name", "id");
TestUtils.checkJsonObject(fields.getJSONObject(1), "name,label,description,hint", "email,Email,,");
TestUtils.checkJsonObject(fields.getJSONObject(2), "name,label,description,hint",
Expand Down Expand Up @@ -155,13 +166,43 @@ public void testRunPlacement() {
Assert.assertEquals(meta.get("entity"), "Placement");
Assert.assertEquals(meta.get("label"), "Placement");
JSONArray fields = meta.getJSONArray("fields");
Assert.assertEquals(2, fields.length());
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
public void testRunWorkersCompensationRate() {
String[] testArgs = {Command.META.getMethodName(), EntityInfo.WORKERS_COMPENSATION_RATE.getEntityName()};
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);

metaService.run(testArgs);

verify(printUtilMock, times(1)).log("Getting meta for WorkersCompensationRate...");
verify(printUtilMock, times(1)).log("Added WorkersCompensationRate field: privateLabel that was not in Meta.");
verify(printUtilMock, times(1)).log("Done generating meta for WorkersCompensationRate");
verify(printUtilMock, times(1)).print(stringCaptor.capture());

String jsonPrinted = stringCaptor.getValue();
JSONObject meta = new JSONObject(jsonPrinted);
Assert.assertEquals(meta.get("entity"), "WorkersCompensationRate");
Assert.assertEquals(meta.get("label"), "Workers Compensation Rate");
JSONArray fields = meta.getJSONArray("fields");
Assert.assertEquals(3, fields.length());
TestUtils.checkJsonObject(fields.getJSONObject(0), "name,label,type,dataType", "id,ID,ID,Integer");
TestUtils.checkJsonObject(fields.getJSONObject(1), "name,label,type,dataType", "startDate,Start Date,SCALAR,Timestamp");

JSONObject privateLabelField = fields.getJSONObject(2);
TestUtils.checkJsonObject(privateLabelField, "name,label,type", "privateLabel,Private Label,TO_ONE");
JSONObject ownerAssociation = privateLabelField.getJSONObject("associatedEntity");
Assert.assertEquals(ownerAssociation.getString("entity"), "PrivateLabel");
JSONArray ownerAssociationFields = ownerAssociation.getJSONArray("fields");
TestUtils.checkJsonObject(ownerAssociationFields.getJSONObject(0), "name,label,type,dataType", "id,ID,ID,Integer");
}

@Test(expected = RestApiException.class)
public void testRunBadConnection() {
when(restSessionMock.getRestApi()).thenThrow(new RestApiException());
Expand Down

0 comments on commit 0a6392a

Please sign in to comment.