SObjectNode addNode(final SObjectNode node) {
        final String givenObjectType = node.getObjectType();

        if (objectType != null && !objectType.equals(givenObjectType)) {
            throw new IllegalArgumentException(
                    "SObjectTree can hold only records of the same type, previously given: " + objectType
                                               + ", and now trying to add: "
                                               + givenObjectType);
        }
        objectType = givenObjectType;

        records.add(node);

        return node;
    }
@Test
    public void shouldSerializeToJson() throws JsonProcessingException {
        final ObjectMapper mapper = JsonUtils.createObjectMapper();
        mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);

        final ObjectWriter writer = mapper.writerFor(SObjectTree.class);

        final SObjectTree tree = new SObjectTree();

        final SObjectNode account1 = new SObjectNode(tree, simpleAccount);
        account1.addChild("Contacts", smith);
        account1.addChild("Contacts", evans);
        tree.addNode(account1);

        final SObjectNode account2 = new SObjectNode(tree, simpleAccount2);
        tree.addNode(account2);

        final String json = writer.writeValueAsString(tree);
        final String expected = "{" + "\"records\":[" + "{" + "\"Industry\":\"Banking\"," + "\"Name\":\"SampleAccount\","
                                + "\"NumberOfEmployees\":100,"
                                + "\"Phone\":\"1234567890\"," + "\"Website\":\"www.salesforce.com\"," + "\"attributes\":{"
                                + "\"referenceId\":\"ref1\"," + "\"type\":\"Account\","
                                + "\"url\":null" + "}," + "\"Contacts\":{" + "\"records\":[" + "{"
                                + "\"Email\":\"sample@salesforce.com\"," + "\"LastName\":\"Smith\","
                                + "\"Title\":\"President\"," + "\"attributes\":{" + "\"referenceId\":\"ref2\","
                                + "\"type\":\"Contact\"," + "\"url\":null" + "}" + "}," + "{"
                                + "\"Email\":\"sample@salesforce.com\"," + "\"LastName\":\"Evans\","
                                + "\"Title\":\"Vice President\"," + "\"attributes\":{"
                                + "\"referenceId\":\"ref3\"," + "\"type\":\"Contact\"," + "\"url\":null" + "}" + "}" + "]" + "}"
                                + "}," + "{" + "\"Industry\":\"Banking\","
                                + "\"Name\":\"SampleAccount2\"," + "\"NumberOfEmployees\":100," + "\"Phone\":\"1234567890\","
                                + "\"Website\":\"www.salesforce2.com\","
                                + "\"attributes\":{" + "\"referenceId\":\"ref4\"," + "\"type\":\"Account\"," + "\"url\":null"
                                + "}" + "}" + "]" + "}";
        assertEquals(expected, json, "Should serialize to JSON as in Salesforce example");
    }