Search in sources :

Example 11 with CustomField

use of org.killbill.billing.util.customfield.CustomField in project killbill by killbill.

the class TestDefaultCustomFieldUserApi method testCustomFieldNoId.

@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();
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) Test(org.testng.annotations.Test)

Example 12 with CustomField

use of org.killbill.billing.util.customfield.CustomField in project killbill by killbill.

the class TestDefaultCustomFieldUserApi method testCustomFieldUpdate.

@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());
    }
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomFieldApiException(org.killbill.billing.util.api.CustomFieldApiException) Test(org.testng.annotations.Test)

Example 13 with CustomField

use of org.killbill.billing.util.customfield.CustomField in project killbill by killbill.

the class TestDefaultCustomFieldUserApi method testCustomFieldBasic.

@Test(groups = "slow")
public void testCustomFieldBasic() throws Exception {
    final CustomField customField1 = new StringCustomField("some123", "some 456", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    final CustomField customField2 = new StringCustomField("other123", "other 456", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD, NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField1, customField2), callContext);
    assertListenerStatus();
    // Verify operation is indeed transactional, and nothing was inserted
    final CustomField customField3 = new StringCustomField("qrqrq123", "qrqrq 456", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    try {
        customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField3, customField1), callContext);
    } catch (CustomFieldApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_ALREADY_EXISTS.getCode());
    }
    List<CustomField> all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext);
    Assert.assertEquals(all.size(), 2);
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField3), callContext);
    assertListenerStatus();
    all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext);
    Assert.assertEquals(all.size(), 3);
    eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD, NextEvent.CUSTOM_FIELD);
    customFieldUserApi.removeCustomFields(ImmutableList.of(customField1, customField3), callContext);
    assertListenerStatus();
    all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext);
    Assert.assertEquals(all.size(), 1);
    Assert.assertEquals(all.get(0).getId(), customField2.getId());
    Assert.assertEquals(all.get(0).getObjectId(), accountId);
    Assert.assertEquals(all.get(0).getObjectType(), ObjectType.ACCOUNT);
    Assert.assertEquals(all.get(0).getFieldName(), customField2.getFieldName());
    Assert.assertEquals(all.get(0).getFieldValue(), customField2.getFieldValue());
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomFieldApiException(org.killbill.billing.util.api.CustomFieldApiException) Test(org.testng.annotations.Test)

Example 14 with CustomField

use of org.killbill.billing.util.customfield.CustomField in project killbill by killbill.

the class TestDefaultCustomFieldUserApi method testSaveCustomFieldWithAccountRecordId.

@Test(groups = "slow")
public void testSaveCustomFieldWithAccountRecordId() throws Exception {
    checkPagination(0);
    final String cfName = UUID.randomUUID().toString().substring(1, 4);
    final String cfValue = UUID.randomUUID().toString().substring(1, 4);
    final CustomField customField = new StringCustomField(cfName, cfValue, ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField), callContext);
    assertListenerStatus();
    checkPagination(1);
    // Verify the field was saved
    final List<CustomField> customFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    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());
    // Verify the account_record_id was populated
    dbi.withHandle(new HandleCallback<Void>() {

        @Override
        public Void withHandle(final Handle handle) throws Exception {
            final List<Map<String, Object>> values = handle.select("select account_record_id from custom_fields where object_id = ?", accountId.toString());
            Assert.assertEquals(values.size(), 1);
            Assert.assertEquals(values.get(0).keySet().size(), 1);
            Assert.assertEquals(Long.valueOf(values.get(0).get("account_record_id").toString()), accountRecordId);
            return null;
        }
    });
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.removeCustomFields(customFields, callContext);
    assertListenerStatus();
    List<CustomField> remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 0);
    checkPagination(0);
    // Add again the custom field
    final CustomField newCustomField = new StringCustomField(cfName, cfValue, ObjectType.ACCOUNT, accountId, callContext.getCreatedDate());
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(newCustomField), callContext);
    assertListenerStatus();
    remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 1);
    checkPagination(1);
    // Delete again
    eventsListener.pushExpectedEvent(NextEvent.CUSTOM_FIELD);
    customFieldUserApi.removeCustomFields(remainingCustomFields, callContext);
    assertListenerStatus();
    remainingCustomFields = customFieldUserApi.getCustomFieldsForObject(accountId, ObjectType.ACCOUNT, callContext);
    Assert.assertEquals(remainingCustomFields.size(), 0);
    checkPagination(0);
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CustomFieldApiException(org.killbill.billing.util.api.CustomFieldApiException) Handle(org.skife.jdbi.v2.Handle) Test(org.testng.annotations.Test)

Example 15 with CustomField

use of org.killbill.billing.util.customfield.CustomField in project killbill by killbill.

the class JaxRsResourceBase method createCustomFields.

protected Response createCustomFields(final UUID id, final List<CustomFieldJson> customFields, final CallContext context, final UriInfo uriInfo, final HttpServletRequest request) throws CustomFieldApiException {
    final LinkedList<CustomField> input = new LinkedList<CustomField>();
    for (final CustomFieldJson cur : customFields) {
        verifyNonNullOrEmpty(cur.getName(), "CustomFieldJson name needs to be set");
        verifyNonNullOrEmpty(cur.getValue(), "CustomFieldJson value needs to be set");
        input.add(new StringCustomField(cur.getName(), cur.getValue(), getObjectType(), id, context.getCreatedDate()));
    }
    customFieldUserApi.addCustomFields(input, context);
    return uriBuilder.buildResponse(uriInfo, this.getClass(), "getCustomFields", id, request);
}
Also used : StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomField(org.killbill.billing.util.customfield.CustomField) StringCustomField(org.killbill.billing.util.customfield.StringCustomField) CustomFieldJson(org.killbill.billing.jaxrs.json.CustomFieldJson) LinkedList(java.util.LinkedList)

Aggregations

CustomField (org.killbill.billing.util.customfield.CustomField)17 StringCustomField (org.killbill.billing.util.customfield.StringCustomField)13 CustomFieldJson (org.killbill.billing.jaxrs.json.CustomFieldJson)7 Test (org.testng.annotations.Test)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 TenantContext (org.killbill.billing.util.callcontext.TenantContext)4 TimedResource (org.killbill.commons.metrics.TimedResource)4 URI (java.net.URI)3 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)3 CustomFieldApiException (org.killbill.billing.util.api.CustomFieldApiException)3 AuditLog (org.killbill.billing.util.audit.AuditLog)2 CustomFieldModelDao (org.killbill.billing.util.customfield.dao.CustomFieldModelDao)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1