@Test(groups = "slow") public void testCustomFieldNoId() throws Exception { // Verify that when coming from a plugin, the id isn't required final CustomField customField = Mockito.mock(CustomField.class); Mockito.when(customField.getObjectId()).thenReturn(accountId); Mockito.when(customField.getObjectType()).thenReturn(ObjectType.ACCOUNT); Mockito.when(customField.getFieldName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(customField.getFieldValue()).thenReturn(UUID.randomUUID().toString()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField), callContext); assertListenerStatus(); }
@Override public CustomFieldModelDao apply(final CustomField input) { // Respect user-specified ID // TODO See https://github.com/killbill/killbill/issues/35 if (input.getId() != null) { return new CustomFieldModelDao(input.getId(), context.getCreatedDate(), context.getCreatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } else { return new CustomFieldModelDao(context.getCreatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } } });
@Override public CustomFieldModelDao apply(final CustomField input) { return new CustomFieldModelDao(input.getId(), internalCallContext.getCreatedDate(), internalCallContext.getUpdatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } });
Assert.assertEquals(all.get(0).getObjectType(), ObjectType.ACCOUNT); Assert.assertEquals(all.get(0).getFieldName(), customField2.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), customField2.getFieldValue());
@Test(groups = "slow") public void testCustomFieldUpdate() throws Exception { final CustomField customField1 = new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField1), callContext); assertListenerStatus(); final CustomField update1 = new StringCustomField(customField1.getId(), customField1.getFieldName(), "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate()); customFieldUserApi.updateCustomFields(ImmutableList.of(update1), callContext); List<CustomField> all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext); Assert.assertEquals(all.size(), 1); Assert.assertEquals(all.get(0).getId(), update1.getId()); Assert.assertEquals(all.get(0).getObjectType(), update1.getObjectType()); Assert.assertEquals(all.get(0).getObjectId(), update1.getObjectId()); Assert.assertEquals(all.get(0).getFieldName(), update1.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), "value2"); try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID.getCode()); } try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField(customField1.getId(), "wrongName", "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_INVALID_UPDATE.getCode()); } }
Assert.assertEquals(customFields.size(), 1); Assert.assertEquals(customFields.get(0).getFieldName(), customField.getFieldName()); Assert.assertEquals(customFields.get(0).getFieldValue(), customField.getFieldValue()); Assert.assertEquals(customFields.get(0).getObjectId(), customField.getObjectId()); Assert.assertEquals(customFields.get(0).getObjectType(), customField.getObjectType());
import org.apache.lucene.document.Document; import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField; import org.hibernate.search.bridge.FieldBridge; import org.hibernate.search.bridge.LuceneOptions; import java.io.IOException; public class CustomFieldBridge implements FieldBridge { public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { if (value != null) { CustomField customField = (CustomField) value; if (customField.getFieldValue() != null) { String fieldName = customField.getFieldName(); String fieldValue = customField.getFieldValue(); CustomFacetsConfig config = new CustomFacetsConfig(); config.setIndexFieldName(fieldName, fieldName); Document doc = new Document(); doc.add(new SortedSetDocValuesFacetField(fieldName, fieldValue)); try { config.CustomBuild(doc, document); } catch (IOException e) { e.printStackTrace(); } } } } }
public CustomFieldJson(final CustomField input, @Nullable final List<AuditLog> auditLogs) { this(input.getId(), input.getObjectId(), input.getObjectType(), input.getFieldName(), input.getFieldValue(), toAuditLogJson(auditLogs)); }
@Test(groups = "slow") public void testCustomFieldNoId() throws Exception { // Verify that when coming from a plugin, the id isn't required final CustomField customField = Mockito.mock(CustomField.class); Mockito.when(customField.getObjectId()).thenReturn(accountId); Mockito.when(customField.getObjectType()).thenReturn(ObjectType.ACCOUNT); Mockito.when(customField.getFieldName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(customField.getFieldValue()).thenReturn(UUID.randomUUID().toString()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField), callContext); assertListenerStatus(); }
@Override public CustomFieldModelDao apply(final CustomField input) { // Respect user-specified ID // TODO See https://github.com/killbill/killbill/issues/35 if (input.getId() != null) { return new CustomFieldModelDao(input.getId(), context.getCreatedDate(), context.getCreatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } else { return new CustomFieldModelDao(context.getCreatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } } });
@Override public CustomFieldModelDao apply(final CustomField input) { return new CustomFieldModelDao(input.getId(), internalCallContext.getCreatedDate(), internalCallContext.getUpdatedDate(), input.getFieldName(), input.getFieldValue(), input.getObjectId(), input.getObjectType()); } });
Assert.assertEquals(all.get(0).getObjectType(), ObjectType.ACCOUNT); Assert.assertEquals(all.get(0).getFieldName(), customField2.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), customField2.getFieldValue());
@Test(groups = "slow") public void testCustomFieldUpdate() throws Exception { final CustomField customField1 = new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField1), callContext); assertListenerStatus(); final CustomField update1 = new StringCustomField(customField1.getId(), customField1.getFieldName(), "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate()); customFieldUserApi.updateCustomFields(ImmutableList.of(update1), callContext); List<CustomField> all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext); Assert.assertEquals(all.size(), 1); Assert.assertEquals(all.get(0).getId(), update1.getId()); Assert.assertEquals(all.get(0).getObjectType(), update1.getObjectType()); Assert.assertEquals(all.get(0).getObjectId(), update1.getObjectId()); Assert.assertEquals(all.get(0).getFieldName(), update1.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), "value2"); try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID.getCode()); } try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField(customField1.getId(), "wrongName", "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_INVALID_UPDATE.getCode()); } }
Assert.assertEquals(customFields.size(), 1); Assert.assertEquals(customFields.get(0).getFieldName(), customField.getFieldName()); Assert.assertEquals(customFields.get(0).getFieldValue(), customField.getFieldValue()); Assert.assertEquals(customFields.get(0).getObjectId(), customField.getObjectId()); Assert.assertEquals(customFields.get(0).getObjectType(), customField.getObjectType());