mambu-gmbh / Mambu-APIs-Java

Java Library for working with the Mambu APIs
https://www.mambu.com
Apache License 2.0
29 stars 33 forks source link

Can't create client - Nullpointer exception for clientActivateDateField #165

Closed azakordonets closed 6 years ago

azakordonets commented 6 years ago

Hi, i'm using Mambu API version 6 and can't make client creation to work for me. Here's a simple example:

class MambuClientDtoGenerator{
public static ClientExpanded expandedClient() {
        ClientExpanded client = new ClientExpanded();
        client.setClient(randomClient());
        client.setAddresses(Collections.singletonList(randomAddress()));

        return client;
    }

public static Client randomClient() {
        Client client = new Client(faker.name().firstName(), faker.name().lastName());
        client.setCreationDate(new Date());
        client.setBirthDate(faker.date().past(25 * 365, TimeUnit.DAYS));
        client.setApprovedDate(new Date());
        client.setLastModifiedDate(new Date());
        return client;
    }

    public static Address randomAddress() {
        Address address = new Address();
        address.setCity(faker.address().city());
        address.setCountry(faker.address().country());
        address.setPostcode(faker.address().zipCode());
        address.setLine1(faker.address().streetAddress());
        address.setLine2(faker.address().streetAddressNumber());
        return address;
    }
}

ClientExpanded clientDetails = MambuClientDtoGenerator.expandedClient();
mambuAPIServiceFactory.getClientService().createClient(clientDetails);

As a result i get nullpointer exception and from stack i see that closedDate is null. If i set it to some date, then it's gonna be closed, right ? :

java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1770)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
    at java.text.DateFormat.format(DateFormat.java:345)
    at com.google.gson.DefaultDateTypeAdapter.write(DefaultDateTypeAdapter.java:88)
    at com.google.gson.DefaultDateTypeAdapter.write(DefaultDateTypeAdapter.java:40)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
    at com.google.gson.Gson.toJson(Gson.java:669)
    at com.google.gson.Gson.toJson(Gson.java:648)
    at com.google.gson.Gson.toJson(Gson.java:603)
    at com.mambu.apisdk.util.ServiceHelper.makeApiJson(ServiceHelper.java:392)
    at com.mambu.apisdk.util.ServiceExecutor.executeJson(ServiceExecutor.java:305)
    at com.mambu.apisdk.util.ServiceExecutor.executeJson(ServiceExecutor.java:355)
    at com.mambu.apisdk.services.ClientsService.createClient(ClientsService.java:302)

I'm running Java8 with Mambu-APi version 6 + mambumodels 6

azakordonets commented 6 years ago

The only way i could get through this dates nullpointer hell is by building this Client object :

Client client = new Client(faker.name().firstName(), faker.name().lastName());
        client.setCreationDate(new Date());
        client.setBirthDate(faker.date().past(25 * 365, TimeUnit.DAYS));
        client.setApprovedDate(new Date());
        client.setLastModifiedDate(new Date());
        client.setToActive(new Date());
        client.setToExited(new Date());

How can i make with your API Client library this simple post requets :

{ 
    "client" : { 
        "firstName" : "Jan",
        "lastName" : "Trommelaar",
        "clientRole" : { "encodedKey" : {"role"}} }
    } 
}

I thought that it should be easy...

azakordonets commented 6 years ago

Also, i your documentation i can't seem to find which fields are mandatory - https://developer.mambu.com/customer/portal/articles/1617472-clients-api

acostros commented 6 years ago

Hi Andrew,

You may found the mandatory fields for creating a client under "Parameters for Client" section on the page you pointed https://developer.mambu.com/customer/portal/articles/1617472-clients-api Also you may find some client creation samples in DemoTestClientService class inside the project you forked from Github

azakordonets commented 6 years ago

Thanks, but your example in demo package doesn't work or me :

public static Client randomClient(ClientRole clientRole) {
        Client client = new Client(faker.name().firstName(), faker.name().lastName());
        String clientId = faker.internet().uuid();
        client.setId(clientId);
        client.setLoanCycle(null);
        client.setGroupLoanCycle(null);
        client.setToInactive();
        client.setToActive(new Date());
        client.setLastModifiedDate(new Date());

        client.setCreationDate(faker.date().future(1, TimeUnit.DAYS));
        client.setEmailAddress(String.format("testUser-%s@example.com", clientId));
        client.setHomePhone(faker.phoneNumber().phoneNumber());
        client.setMiddleName(" Middle ");
        client.setMobilePhone1(faker.phoneNumber().cellPhone());
        client.setMobilePhone2(faker.phoneNumber().cellPhone());
        client.setPreferredLanguage(Language.ENGLISH); // MBU-9221 in 3.12

        client.setBirthDate(faker.date().past(25 * 365, TimeUnit.DAYS));
        client.setClientRole(clientRole);
        return client;
    }

public static ClientExpanded expandedClient(ClientRole clientRole) throws MambuApiException {
        ClientExpanded client = new ClientExpanded();
        client.setClient(randomClient(clientRole));
        client.setAddresses(Collections.singletonList(randomAddress()));
        client.setIdDocuments(MambuClientDtoGenerator.createDocumentsIds());
        List<CustomFieldValue> clientCustomInformation =
            makeForEntityCustomFieldValues(CustomFieldType.CLIENT_INFO, clientRole.getEncodedKey(), true);
        // Add All custom fields
        client.setCustomFieldValues(clientCustomInformation);
        return client;
    }

At first i got a NullPointer exception on lastModifiedDate field, when i set it to todays date, i get nullpointer now in approvedDate field - in Calendar:1770 class.

Here's stacktrace :

java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1770)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
    at java.text.DateFormat.format(DateFormat.java:345)
    at com.google.gson.DefaultDateTypeAdapter.write(DefaultDateTypeAdapter.java:88)
    at com.google.gson.DefaultDateTypeAdapter.write(DefaultDateTypeAdapter.java:40)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
    at com.google.gson.Gson.toJson(Gson.java:669)
    at com.google.gson.Gson.toJson(Gson.java:648)
    at com.google.gson.Gson.toJson(Gson.java:603)
    at com.mambu.apisdk.util.ServiceHelper.makeApiJson(ServiceHelper.java:392)
    at com.mambu.apisdk.util.ServiceExecutor.executeJson(ServiceExecutor.java:305)
    at com.mambu.apisdk.util.ServiceExecutor.executeJson(ServiceExecutor.java:355)
    at com.mambu.apisdk.services.ClientsService.createClient(ClientsService.java:302)
azakordonets commented 6 years ago

FYI, same problems i'm getting with creating groups..

acostros commented 6 years ago

What is the version you use for the Gson library?

azakordonets commented 6 years ago
compile group: 'javax.jdo', name: 'jdo-api', version: '3.1'
    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'

Same as in your project

azakordonets commented 6 years ago

You're right about dependency - i found that i had a trancendent dependency of a lower version. After fixing that i faced next problem :

Jun 26, 2018 11:48:37 AM com.mambu.apisdk.util.RequestExecutorImpl logJsonInput
WARNING: Input JsonString={"client":{"state":"ACTIVE","id":"d4ec9ff3-ab84-44f4-af8d-11e611402137","creationDate":"2018-06-26T14:26:25+0200","lastModifiedDate":"2018-06-26T11:48:35+0200","activationDate":"2018-06-26T11:48:35+0200","firstName":"Reyna","lastName":"Lebsack","middleName":" Middle ","homePhone":"576.804.5882 x918","emailAddress":"testUser-d4ec9ff3-ab84-44f4-af8d-11e611402137@example.com","mobilePhone1":"357-914-8142","mobilePhone2":"1-718-063-1910","birthDate":"2012-02-21T06:04:06+0100","clientRole":{"encodedKey":"8a8b8fac5a3f1197015a5b4229ab002a","name":"Individual","id":"individual","clientType":"CLIENT","creationDate":"2017-02-20T12:22:27+0100","canOpenAccounts":false,"canGuarantee":true,"requireID":false,"useDefaultAddress":true,"index":-1},"preferredLanguage":"ENGLISH"},"addresses":[],"customInformation":[],"idDocuments":[],"notificationTemplates":[],"groupKeys":[]}

com.mambu.apisdk.exception.MambuApiException: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>    <head>        <meta http-equiv="content-type" content="text/html; charset=utf-8" />        <title>Mambu.com || Error</title>        <link rel="stylesheet" href="/css/reset.css" />        <link rel="stylesheet" href="/css/text.css" />        <link rel="stylesheet" href="/css/960.css" />        <link rel="stylesheet" href="/css/main.css" />                <link rel="shortcut icon" href="/img/favicon.gif" type="image/gif" />     <link rel="icon" href="/img/favicon.gif" type="image/gif" />        <meta name="description" content="Mambu is an online software platform that lets microcredit organizations outsource their technical challenges and focus on their core business." />        <meta name="keywords" content="microfinance, software, microcredit, software as a service, loan management, credit managment, online credit management, portfolio management software" />            <meta name="robots" content="all" />      <meta name="robots" content="index, follow" />        <meta name="revisit-after" content="7 days" />        <meta name="version" content="1.0" />        <meta name="author" content="Frederik Pfisterer" />        <meta name="copyright" content="Mambu.com" />                <style id="antiClickjack">body{display:none !important;}</style>       <script type="text/javascript">        if (self === top) {             var antiClickjack = document.getElementById("antiClickjack");               antiClickjack.parentNode.removeChild(antiClickjack);        } else {            top.location = self.location;           }        </script>     </head><body><div class="container_12">    <div class="grid_4 prefix_4 suffix_4">    <img src="/img/logo_small.png">    <h1 class="shy">Error</h1>        <p>     Sorry, there was an error processing your request. <br /><br />        Most likely our team is aware of the problem and working on it, however if this error persists, please send us an email to <a href="mailto:support@mambu.com">support@mambu.com</a> noting what you were trying to do when the error occured.<br />    </p>        </div></div></body></html>
acostros commented 6 years ago

This it look like a bug in the server side (most probably caused by some server side validation). Now your call passed through the SDK to the server and the last line you sent is the response from the server. Please try creating a client by supplying only the mandatory fields. This will help us understand the issue.

azakordonets commented 6 years ago

Ok, here's what i have found :

public static Client randomClient(ClientRole clientRole) {
        Client client = new Client(faker.name().firstName(), faker.name().lastName());
        client.setClientRole(clientRole);
        return client;

Works.

When i try this to add to client Id with client.setId(faker.internet().uuid()); then i get an error :

Jun 26, 2018 5:51:59 PM com.mambu.apisdk.util.RequestExecutorImpl logJsonInput
WARNING: Input JsonString={"client":{"state":"INACTIVE","id":"7ef287cb-a9a4-4b03-aa55-8449476ccaec","firstName":"Kailey","lastName":"Beier","loanCycle":0,"groupLoanCycle":0,"clientRole":{"encodedKey":"8a8b8fac5a3f1197015a5b4229ab002a","name":"Individual","id":"individual","clientType":"CLIENT","creationDate":"2017-02-20T12:22:27+0100","canOpenAccounts":false,"canGuarantee":true,"requireID":false,"useDefaultAddress":true,"index":-1},"preferredLanguage":"ENGLISH"},"addresses":[],"customInformation":[],"idDocuments":[],"notificationTemplates":[],"groupKeys":[]}

com.mambu.apisdk.exception.MambuApiException: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>    <head>        <meta http-equiv="content-type" content="text/html; charset=utf-8" />        <title>Mambu.com || Error</title>        <link rel="stylesheet" href="/css/reset.css" />        <link rel="stylesheet" href="/css/text.css" />        <link rel="stylesheet" href="/css/960.css" />        <link rel="stylesheet" href="/css/main.css" />                <link rel="shortcut icon" href="/img/favicon.gif" type="image/gif" />     <link rel="icon" href="/img/favicon.gif" type="image/gif" />        <meta name="description" content="Mambu is an online software platform that lets microcredit organizations outsource their technical challenges and focus on their core business." />        <meta name="keywords" content="microfinance, software, microcredit, software as a service, loan management, credit managment, online credit management, portfolio management software" />            <meta name="robots" content="all" />      <meta name="robots" content="index, follow" />        <meta name="revisit-after" content="7 days" />        <meta name="version" content="1.0" />        <meta name="author" content="Frederik Pfisterer" />        <meta name="copyright" content="Mambu.com" />                <style id="antiClickjack">body{display:none !important;}</style>       <script type="text/javascript">        if (self === top) {             var antiClickjack = document.getElementById("antiClickjack");               antiClickjack.parentNode.removeChild(antiClickjack);        } else {            top.location = self.location;           }        </script>     </head><body><div class="container_12">    <div class="grid_4 prefix_4 suffix_4">    <img src="/img/logo_small.png">    <h1 class="shy">Error</h1>        <p>     Sorry, there was an error processing your request. <br /><br />        Most likely our team is aware of the problem and working on it, however if this error persists, please send us an email to <a href="mailto:support@mambu.com">support@mambu.com</a> noting what you were trying to do when the error occured.<br />    </p>        </div></div></body></html>
acostros commented 6 years ago

Hi Andrew,

Thanks for investigating further and discovering the issue. You just found a bug on our create client API. The issue is caused by the length of the id field, maximum length the API server accepts is 32 and in your request the id length is 36. We'll fix the issue in our next release. In the mean time, as workaround, please use IDs that have max 32 characters.

acostros commented 6 years ago

Hi Andrew,

I came back to you because I feel like I need to complete my last comment. The fix we'll come with in the next version will consist in a validation that will make sure the length of the id does not exceed 32 chars, this is the maximum length we support on IDs. We won't increase the size for this field. If you need to set an ID on the client that have a length that is bigger than 32 chars then you may use a custom field.

azakordonets commented 6 years ago

No problem, i can have it as 32 chars. Thanks. Closing this for now.