Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map result set to Java Object(POJO) and then compare actual response i.e already converted to Java Object(POJO)

How to map result set to Java Object(POJO) and then compare with actual response i.e already converted to Java Object(POJO)

Main Issue I am facing is SQL column type is different from POJO. Now eg: Marketing info with the below response will create a separate POJO but in DB it is mapped as string.

Is there a way to create a generic function to validate Result set from DB to actual response for each and every endpoint?

API Response as below:

{
    "uuid": "00a26ea4-6be3-47dd-8ee6-2f52a71d98b6",
    "email": "[email protected]",
    "firstName": "Pulkit",
    "lastName": "Agrawal",
    "businessName": "MMIS Test 1709386721967",
    "phoneNumber": "4029559455",
    "marketingInfo": {
        "utm_campaign": "Commerce_WhereToBuy"
    },
    "resellerUuid": "47HMNNZ9BPF8E",
    "order": {
        "uuid": "366ae541-899b-420a-9112-0897406bf7ae",
        "hasSubscriptions": false,
        "planId": "4NKNCTE5RGV5P",
        "planName": "Counter Service Restaurant",
        "keyedInRate": {
            "percentage": 3.5,
            "fixedAmount": {
                "value": 0.20,
                "currencyCode": "USD"
            }
        },
        "inPersonRate": {
            "percentage": 2.3,
            "fixedAmount": {
                "value": 0.10,
                "currencyCode": "USD"
            }
        },
        "shippingAddress": {
            "lineOne": "131 Varick St",
            "lineTwo": "11",
            "city": "New York",
            "state": "NY",
            "zipCode": "10014"
        },
        "items": [
            {
                "uuid": "64e2172d-bd99-4dcc-8f04-b39d24f64614",
                "fdmpId": "777-1000108721",
                "displayName": "Flex",
                "purchaseType": "P",
                "quantity": 1,
                "price": {
                    "value": 599.00,
                    "currencyCode": "USD"
                },
                "digitalCatalogId": "FLEX_GEN_3"
            },
            {
                "uuid": "ff873f43-09d6-4fd6-9613-78703e1b1b19",
                "fdmpId": "93183",
                "displayName": "Station Solo",
                "purchaseType": "P",
                "quantity": 1,
                "price": {
                    "value": 1699.00,
                    "currencyCode": "USD"
                },
                "digitalCatalogId": "STATION_SOLO_CASH_DRAWER"
            }
        ]
    },
    "salesforceId": "00Q8J000001uPewUAE",
    "salesforceOwnerId": "00G7j000001EdZdEAK",
    "chainAgentId": "526975216882",
    "clAnalyticsId": "d356c4dec6af56976d667364b3f41b6e779dde2aaf027edf9cfe74cf288505f0",
    "modifiedTime": "2024-03-02T13:38:43.908+00:00"
}

SQL Results as below: enter image description here enter image description here

Code I am trying:

public class SubmitApp extends BaseTest {

@Test
public void shouldBeAbleToCreateLead() throws SQLException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Lead requestCreateLead = leadBuilder();
    Response response = LeadAPI.put(requestCreateLead);
    assertThat(response.statusCode(), equalTo(StatusCode.CODE_200.getCode()));
    LeadResponse actualResponseLead = response.as(LeadResponse.class);

    ResultSet rs = dbConnection();
    List<LeadResponse> responseList = convertSQLResultSetToObject(rs, LeadResponse.class);
}

public static <T> List<T> convertSQLResultSetToObject(ResultSet resultSet, Class<T> clazz) throws SQLException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
    }
    List<T> list = new ArrayList<>();
    while (resultSet.next()) {

        T dto = clazz.getConstructor().newInstance();

        for (Field field : fields) {
            String name = field.getName();

            try {
                String value = resultSet.getString(name);
                field.set(dto, field.getType().getConstructor(String.class).newInstance(value));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        list.add(dto);
    }
    return list;
}


@Step
public Lead leadBuilder() {
    Lead lead = new Lead();
    return lead;
}

Getting error as below in the last line:

java.lang.NoSuchMethodException: com.spotify.oauth2.pojo.LeadResponse.MarketingInfo.<init>(java.lang.String)

P.S: Github Link for the code: Rest Assured Github Repo

like image 445
Pulkit Agrawal Avatar asked Nov 15 '25 07:11

Pulkit Agrawal


1 Answers

The exception is pretty straightforward - some of your field types in a DTO are missing String constructor, namely "MarketingInfo"

Also, the code you've taken is not meant to populate nested objects by default, you should consider some ORM or own factories.

like image 132
TEH EMPRAH Avatar answered Nov 17 '25 23:11

TEH EMPRAH